layer_mapping_example.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Tue Apr 7 08:41:07 2020
  5. @author: joshs
  6. """
  7. import os
  8. import numpy as np
  9. import xarray as xr
  10. import pandas as pd
  11. import matplotlib.pyplot as plt
  12. from allensdk.brain_observatory.ecephys.ecephys_project_cache import EcephysProjectCache
  13. # %%
  14. cache_dir = '/mnt/nvme0/ecephys_cache_dir'
  15. manifest_path = os.path.join(cache_dir, "manifest.json")
  16. cache = EcephysProjectCache.from_warehouse(manifest=manifest_path)
  17. import warnings
  18. warnings.filterwarnings("ignore")
  19. sessions = cache.get_session_table()
  20. # %%
  21. session = cache.get_session_data(sessions.index.values[0])
  22. # %%
  23. import nrrd
  24. streamlines, header = nrrd.read('/home/joshs/Downloads/laplacian_10.nrrd')
  25. annotations = np.load('/mnt/md0/data/opt/annotation_volume_10um_by_index.npy')
  26. structure_tree = pd.read_csv('/mnt/md0/data/opt/ccf_structure_tree.csv', index_col=0)
  27. # %%
  28. import re
  29. def get_layer_name(acronym):
  30. try:
  31. layer = int(re.findall(r'\d+', acronym)[0])
  32. if layer == 3:
  33. layer = 0
  34. return layer
  35. except IndexError:
  36. return 0
  37. def get_structure_ids(df, annotations):
  38. x = (df.anterior_posterior_ccf_coordinate.values / 10).astype('int')
  39. y = (df.dorsal_ventral_ccf_coordinate.values / 10).astype('int')
  40. z = (df.left_right_ccf_coordinate.values / 10).astype('int')
  41. x[x < 0] = 0
  42. y[y < 0] = 0
  43. z[z < 0] = 0
  44. structure_ids = annotations[x, y, z] - 1 # annotation volume is Matlab-indexed
  45. return structure_ids
  46. # %%
  47. df = session.channels[session.channels.anterior_posterior_ccf_coordinate > 0]
  48. x = (df.anterior_posterior_ccf_coordinate.values / 10).astype('int')
  49. y = (df.dorsal_ventral_ccf_coordinate.values / 10).astype('int')
  50. z = (df.left_right_ccf_coordinate.values / 10).astype('int')
  51. cortical_depth = streamlines[x, y, z]
  52. df['cortical_depth'] = 0
  53. df.loc[df.anterior_posterior_ccf_coordinate > 0, 'cortical_depth'] = cortical_depth
  54. # %%
  55. structure_ids = get_structure_ids(df, annotations)
  56. structure_acronyms = structure_tree.loc[structure_ids].acronym
  57. layers = [get_layer_name(acronym) for acronym in structure_acronyms]
  58. df['cortical_layer'] = layers