download_ARA.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. """
  2. Created on 17/03/2020
  3. @author: Niklas Pallast
  4. Neuroimaging & Neuroengineering
  5. Department of Neurology
  6. University Hospital Cologne
  7. More information can be found here:
  8. http://alleninstitute.github.io/AllenSDK/_modules/allensdk/api/queries/reference_space_api.html
  9. """
  10. import os
  11. import nrrd # pip install pynrrd, if pynrrd is not already installed
  12. import nibabel as nib # pip install nibabel, if nibabel is not already installed
  13. import numpy as np
  14. from allensdk.api.queries.reference_space_api import ReferenceSpaceApi
  15. from allensdk.config.manifest import Manifest
  16. # the annotation download writes a file, so we will need somwhere to put it
  17. annotation_dir = 'annotation'
  18. Manifest.safe_mkdir(annotation_dir)
  19. annotation_path = os.path.join(annotation_dir, 'annotation.nrrd')
  20. # this is a string which contains the name of the latest ccf version
  21. annotation_version = ReferenceSpaceApi.CCF_VERSION_DEFAULT
  22. # download annotations
  23. mcapi = ReferenceSpaceApi()
  24. mcapi.download_annotation_volume(annotation_version, 50, annotation_path)
  25. annotation = nrrd.read(annotation_path)
  26. # read nrrd data and header
  27. _nrrd = nrrd.read(annotation_path)
  28. data = _nrrd[0]
  29. header = _nrrd[1]
  30. # create header and for RAS orientation
  31. space_value = header['space directions']
  32. affine = np.eye(4) * 0.001 * space_value[0, 0]
  33. affine[3][3] = 1
  34. # ensure RAS orientation
  35. data = np.swapaxes(data, 2, 0)
  36. data = np.flip(data, 2)
  37. img = nib.Nifti1Image(data, affine) #
  38. img = nib.as_closest_canonical(img)
  39. hdrIn = img.header
  40. hdrIn.set_xyzt_units('mm')
  41. # img = nib.Nifti1Image(data, img.affine,hdrIn)
  42. scaledNiiData = nib.as_closest_canonical(img)
  43. nib.save(scaledNiiData, os.path.join(annotation_dir, os.path.dirname(annotation_path) + '.nii.gz'))
  44. # the template download writes a file, so we will need somwhere to put it
  45. template_dir = 'template'
  46. Manifest.safe_mkdir(template_dir)
  47. template_path = os.path.join(template_dir, 'template.nrrd')
  48. # download templates
  49. mcapi.download_template_volume(50, template_path)
  50. template = nrrd.read(template_path)
  51. # read nrrd data and header
  52. _nrrd = nrrd.read(template_path)
  53. data = _nrrd[0]
  54. header = _nrrd[1]
  55. # create header and for RAS orientation
  56. space_value = header['space directions']
  57. affine = np.eye(4) * 0.001 * space_value[0, 0]
  58. affine[3][3] = 1
  59. # ensure RAS orientation
  60. data = np.swapaxes(data, 2, 0)
  61. data = np.flip(data, 2)
  62. img = nib.Nifti1Image(data, affine) #
  63. img = nib.as_closest_canonical(img)
  64. hdrIn = img.header
  65. hdrIn.set_xyzt_units('mm')
  66. # img = nib.Nifti1Image(data, img.affine,hdrIn)
  67. scaledNiiData = nib.as_closest_canonical(img)
  68. nib.save(scaledNiiData, os.path.join(template_dir, os.path.dirname(template_path) + '.nii.gz'))