main.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from pre_processing import pre_process
  2. from co_registration import co_register
  3. from template_creation import create_template_from_images
  4. import helper_functions as hf
  5. import sys
  6. import os
  7. from shutil import copyfile
  8. def pre_process_brain_tiffs(tif_path, result_path, mouse_id):
  9. """
  10. Utility function pre-processing all tiffs in folder, outputting pre-processed nifties in result_path.
  11. :param tif_path: The tiffs directory.
  12. :param result_path: The output directory.
  13. :param mouse_id: The mouse id.
  14. :return: Returns nothing.
  15. """
  16. # Get all images and the enclosing tiff folders.
  17. images, folders = hf.images_in_folders(tif_path)
  18. # Ensure that the number of images matches the naming of the folders
  19. if len(images) == int(folders[-1][-2:]):
  20. print('Number of images matches the naming on the folders')
  21. else:
  22. sys.exit('Mismatch between number of images and naming of last TIF folder.')
  23. # Make output directory.
  24. hf.make_dirs([result_path])
  25. # Loop through each image and pre-process.
  26. for image_nr, image_path in enumerate(images, 1):
  27. image_nr_beauty = hf.pretty(image_nr)
  28. output = result_path + mouse_id + '_S' + image_nr_beauty + '.nii'
  29. if not os.path.exists(output):
  30. pre_process(image_path, output)
  31. def main(preprocess=False, coreg=False, make_template=False):
  32. """
  33. Main function performing pre-processing of tiffs, 3-dimensional reconstruction to reference and population-based
  34. template creation.
  35. :param preprocess: Boolean [Default: False]. Will perform pre-processing of tiffs if set to true.
  36. :param coreg: Boolean [Default: False]. Will perform brain reconstruction based on the sequential slices if set to
  37. true.
  38. :param make_template: Boolean [Default: False]. Will perform population-based template creation based on the
  39. reconstructed brain volumes if set to true.
  40. :return: Returns nothing.
  41. """
  42. # Working directory
  43. sandbox = '/run/media/frederik/S_T1/frederik_filip_mqr375/auto-seg/' # Should be removed for final submission.
  44. # Get list of mice ids.
  45. sandbox_mice = sandbox + 'mice/'
  46. mice = os.listdir(sandbox_mice)
  47. if coreg or preprocess:
  48. # Loop through mice ids.
  49. for mouse in sorted(mice):
  50. # Current mouse directory and subdirectories.
  51. work_dir = sandbox_mice + mouse + '/'
  52. raw_dir = work_dir + 'TIF/'
  53. pre_processed_dir = work_dir + 'preprocessed/'
  54. out_dir = work_dir + 'volumes/'
  55. # Make volumes directory.
  56. hf.make_dirs(out_dir)
  57. # Define raw volume and initial transform path (if present).
  58. initial_vol_trans = out_dir + 'initial_transform_' + mouse + '.txt'
  59. raw_vol_path = out_dir + '00_hv_raw_' + mouse + '.nii'
  60. # Pre process tiff images.
  61. if preprocess:
  62. pre_process_brain_tiffs(raw_dir, pre_processed_dir, mouse)
  63. # Create raw volume if not already present.
  64. if not os.path.exists(raw_vol_path):
  65. hf.make_volume(hf.files_in_dir(pre_processed_dir, '.nii'), raw_vol_path)
  66. # Check that an initial transform is present.
  67. if not os.path.exists(initial_vol_trans):
  68. sys.exit('An initial moving transform was not found for at least brain with id : ' + mouse +
  69. '\nMake sure it is named and placed exactly: ' + initial_vol_trans)
  70. # -- This call should be manually modified depending on first or second iteration -- #
  71. # Perform co-registration to reference (Allen in first iteration. DAPI template in second iteration).
  72. # Manually created initial transform is only used in the first iteration.
  73. # First iteration had 0 non-linear steps (parameter nl=0).
  74. if coreg:
  75. mouse_coreg = co_register(sandbox + 'second-iteration_average_allen_0_15.nii.gz',
  76. pre_processed_dir, raw_vol_path, '', out_dir, mouse,
  77. print_log=True, nl=2)
  78. copyfile(mouse_coreg, sandbox + 'volumes/' + mouse + '.nii')
  79. # Population based template creation using symmetric modelling.
  80. if make_template:
  81. create_template_from_images(
  82. sandbox + 'volumes/',
  83. sandbox + 'template/',
  84. symmetric=True
  85. )
  86. if __name__ == '__main__':
  87. main(preprocess=True, coreg=True, make_template=True)