def get_slice_images(self, xyz_channels): # First see if the histology file exists before attempting to connect with FlatIron and # download hist_dir = Path(self.sess_path.parent.parent, 'histology') hist_path_rd = None hist_path_gr = None if hist_dir.exists(): path_to_rd_image = glob.glob(str(hist_dir) + '/*RD.tif') if path_to_rd_image: hist_path_rd = tif2nrrd(Path(path_to_rd_image[0])) else: files = download_histology_data(self.subj, self.lab) if files is not None: hist_path_rd = files[1] path_to_gr_image = glob.glob(str(hist_dir) + '/*GR.tif') if path_to_gr_image: hist_path_gr = tif2nrrd(Path(path_to_gr_image[0])) else: files = download_histology_data(self.subj, self.lab) if files is not None: hist_path_gr = files[0] else: files = download_histology_data(self.subj, self.lab) if files is not None: hist_path_gr = files[0] hist_path_rd = files[1] index = self.brain_atlas.bc.xyz2i( xyz_channels)[:, self.brain_atlas.xyz2dims] ccf_slice = self.brain_atlas.image[index[:, 0], :, index[:, 2]] ccf_slice = np.swapaxes(ccf_slice, 0, 1) label_slice = self.brain_atlas._label2rgb( self.brain_atlas.label[index[:, 0], :, index[:, 2]]) label_slice = np.swapaxes(label_slice, 0, 1) width = [self.brain_atlas.bc.i2x(0), self.brain_atlas.bc.i2x(456)] height = [ self.brain_atlas.bc.i2z(index[0, 2]), self.brain_atlas.bc.i2z(index[-1, 2]) ] if hist_path_rd: hist_atlas_rd = atlas.AllenAtlas(hist_path=hist_path_rd) hist_slice_rd = hist_atlas_rd.image[index[:, 0], :, index[:, 2]] hist_slice_rd = np.swapaxes(hist_slice_rd, 0, 1) else: print('Could not find red histology image for this subject') hist_slice_rd = np.copy(ccf_slice) if hist_path_gr: hist_atlas_gr = atlas.AllenAtlas(hist_path=hist_path_gr) hist_slice_gr = hist_atlas_gr.image[index[:, 0], :, index[:, 2]] hist_slice_gr = np.swapaxes(hist_slice_gr, 0, 1) else: print('Could not find green histology image for this subject') hist_slice_gr = np.copy(ccf_slice) slice_data = { 'hist_rd': hist_slice_rd, 'hist_gr': hist_slice_gr, 'ccf': ccf_slice, 'label': label_slice, 'scale': np.array([(width[-1] - width[0]) / ccf_slice.shape[0], (height[-1] - height[0]) / ccf_slice.shape[1]]), 'offset': np.array([width[0], height[0]]) } return slice_data
from atlaselectrophysiology.load_histology import tif2nrrd from pathlib import Path import sys """ Creates nrrd from image path string """ if __name__ == '__main__': tif2nrrd(Path(*sys.argv[1:]))