def format_annotation_filename(filename, orientation=None, slicing=None, postfix=None, directory=None): """Formats the annotation filename given oriantion and slicing.""" if postfix is None: orientation = res.format_orientation(orientation, default=(1, 2, 3)) postfix = ('_%d_%d_%d_%r' % (orientation + (slicing, ))).replace( ' ', '').replace('(', '_').replace(')', '_').replace(',', '_') fn = filename.split('.') if postfix != '': fn = '.'.join(fn[:-1]) + '_' + postfix + '.' + fn[-1] if directory is not None: fn = os.path.split(fn) fn = os.path.join(directory, fn[-1]) return fn
def _test(): """Tests""" import ClearMap.Settings as settings import ClearMap.IO.IO as io import ClearMap.Visualization.Plot3d as p3d; import ClearMap.Alignment.Resampling as res from importlib import reload reload(res) r = res.resample_shape(source_shape=(100,200,300), sink_shape=(50,50,30)); print('resampled source_shape=%r, sink_shape=%r, source_resolution=%r, sink_resolution=%r' % r) r = res.resample_shape(source_shape=(100,200,300), source_resolution=(2,2,2), sink_resolution=(10,2,1)) print('resampled source_shape=%r, sink_shape=%r, source_resolution=%r, sink_resolution=%r' % r) source = io.join(settings.test_data_path, 'Resampling/test.tif') sink = io.join(settings.test_data_path, "Resampling/resampled.npy") source = io.join(settings.test_data_path, 'Tif/sequence/sequence<Z,4>.tif') sink = io.join(settings.test_data_path, "Resampling/resampled_sequence.tif") source_shape, sink_shape, source_res, sink_res = res.resample_shape(source_shape=io.shape(source), source_resolution=(1.,1.,1.), sink_resolution=(1.6,1.6,2)) axes_order = res._axes_order(None, source_shape, sink_shape); print(axes_order) resampled = res.resample(source, sink, source_resolution=(1.,1.,1.), sink_resolution = (1.6,1.6,2), orientation=None, processes=None); p3d.plot(resampled) p3d.plot(source) inverse = res.resample_inverse(resampled, sink=None, resample_source=source, source_resolution=(1,1,1), sink_resolution = (10,10,2), orientation=None, processes='serial') p3d.plot([source, inverse]) resampled = res.resample(source, sink, source_resolution=(1,1,1), sink_resolution = (10,10,2), orientation=(2,-1,3), processes=None); p3d.plot(resampled) inverse = res.resample_inverse(resampled, sink=None, resample_source=source, source_resolution=(1,1,1), sink_resolution = (10,10,2), orientation=(2,-1,3), processes=None) p3d.plot([source, inverse]) resampled = res.resample(source, sink=None, source_resolution=(1.6,1.6,2), sink_shape=(10,20,30), orientation=None, processes=None) p3d.plot(resampled) # ponints points = res.np.array([[0,0,0], [1,1,1], [1,2,3]], dtype=float); resampled_points = res.resample_points(points, resample_source=source , resample_sink=sink, orientation=None) print(resampled_points) inverse_points = res.resample_points_inverse(resampled_points, resample_source=source , resample_sink=sink, orientation=None) print(inverse_points) print(res.np.allclose(points, inverse_points))
def prepare_annotation_files(slicing=None, orientation=None, directory=None, postfix=None, annotation_file=None, reference_file=None, distance_to_surface_file=None, overwrite=False, verbose=False): """Crop the annotation, reference and distance files to match the data. Arguments --------- slicing : tuple or None The slice specification after reorienting. orientation : tuple, str or None. The orientation specification. Strings can be 'left' or 'right', for the two hemispheres. directory : str or None The target directory. If None, use ClearMap resources folder. postfix : str or None Use this postfix for the cropped annotation file. If None and automatic label is choosen. annotation_file : str or None The annotation file to use. reference_file : str or None The reference file to use. distance_to_surface_file : str or None The distance file to use. overwrite : bool If True, overwrite exisitng files. Returns ------- annotation_file : str The cropped annotation file. reference_file : str The cropped reference file. distance_to_surface_file : str The distance cropped file. """ if annotation_file is None: annotation_file = default_annotation_file if reference_file is None: reference_file = default_reference_file if distance_to_surface_file is None: distance_to_surface_file = default_distance_to_surface_file files = [annotation_file, reference_file, distance_to_surface_file] results = [] for f in files: if f is not None: fn = format_annotation_filename(f, orientation=orientation, slicing=slicing, postfix=postfix, directory=directory) if verbose: print('Preparing: %r' % fn) if not overwrite and io.is_file(fn): results.append(fn) continue if not io.is_file(f): raise ValueError('Cannot find annotation file: %s' % f) s = io.as_source(f) if verbose: print('Preparing: from source %r' % s) data = np.array(s.array) if not orientation is None: #permute per = res.orientation_to_permuation(orientation) data = data.transpose(per) #reverse axes reslice = False sl = [slice(None)] * data.ndim for d, o in enumerate(orientation): if o < 0: sl[d] = slice(None, None, -1) reslice = True if reslice: data = data[tuple(sl)] if slicing is not None: data = data[slicing] io.write(fn, data) results.append(fn) else: results.append(None) return results
'/home/yourname/experiment/sample8/cells_heatmap.tif' ] g1 = stat.readDataGroup(group1) g2 = stat.readDataGroup(group2) #Generated average and standard deviation maps ############################################## g1a = numpy.mean(g1, axis=0) g1s = numpy.std(g1, axis=0) g2a = numpy.mean(g2, axis=0) g2s = numpy.std(g2, axis=0) io.writeData(os.path.join(baseDirectory, 'group1_mean.raw'), rsp.sagittalToCoronalData(g1a)) io.writeData(os.path.join(baseDirectory, 'group1_std.raw'), rsp.sagittalToCoronalData(g1s)) io.writeData(os.path.join(baseDirectory, 'group2_fast_mean.raw'), rsp.sagittalToCoronalData(g2a)) io.writeData(os.path.join(baseDirectory, 'group2_fast_std.raw'), rsp.sagittalToCoronalData(g2s)) #Generate the p-values map ########################## #pcutoff: only display pixels below this level of significance pvals, psign = stat.tTestVoxelization(g1.astype('float'), g2.astype('float'), signed=True, pcutoff=0.05)
g1 = stat.readDataGroup(group1); g2 = stat.readDataGroup(group2); #Generated average and standard deviation maps ############################################## g1a = numpy.mean(g1,axis = 0); g1s = numpy.std(g1,axis = 0); g2a = numpy.mean(g2,axis = 0); g2s = numpy.std(g2,axis = 0); io.writeData(os.path.join(baseDirectory, 'group1_mean.raw'), rsp.sagittalToCoronalData(g1a)); io.writeData(os.path.join(baseDirectory, 'group1_std.raw'), rsp.sagittalToCoronalData(g1s)); io.writeData(os.path.join(baseDirectory, 'group2_fast_mean.raw'), rsp.sagittalToCoronalData(g2a)); io.writeData(os.path.join(baseDirectory, 'group2_fast_std.raw'), rsp.sagittalToCoronalData(g2s)); #Generate the p-values map ########################## #pcutoff: only display pixels below this level of significance pvals, psign = stat.tTestVoxelization(g1.astype('float'), g2.astype('float'), signed = True, pcutoff = 0.05); #color the p-values according to their sign (defined by the sign of the difference of the means between the 2 groups)
def generate_p_value_maps(src): """ generates p-value maps as per ClearMap/analysis.py #TODO: generalise function """ #Load the data (heat maps generated previously ) #make groups groupA = [ os.path.join(flds, fld) for fld in os.listdir(flds) if conditions[os.path.basename(fld)] == "homecage_control" ] groupA.sort() groupB = [ os.path.join(flds, fld) for fld in os.listdir(flds) if conditions[os.path.basename(fld)] == "CNO_control_no_reversal" ] groupB.sort() groupC = [ os.path.join(flds, fld) for fld in os.listdir(flds) if conditions[os.path.basename(fld)] == "CNO_control_reversal" ] groupC.sort() groupD = [ os.path.join(flds, fld) for fld in os.listdir(flds) if conditions[os.path.basename(fld)] == "DREADDs" ] groupC.sort() group_a = [xx + "/cells_heatmap_60um_erosion.tif" for xx in groupA] group_b = [xx + "/cells_heatmap_60um_erosion.tif" for xx in groupB] group_c = [xx + "/cells_heatmap_60um_erosion.tif" for xx in groupC] group_d = [xx + "/cells_heatmap_60um_erosion.tif" for xx in groupD] grp_a = stat.readDataGroup(group_a) grp_b = stat.readDataGroup(group_b) grp_c = stat.readDataGroup(group_c) grp_d = stat.readDataGroup(group_d) #Generated average and standard deviation maps ############################################## grp_aa = np.mean(grp_a, axis=0) grp_as = np.std(grp_a, axis=0) grp_ba = np.mean(grp_b, axis=0) grp_bs = np.std(grp_b, axis=0) grp_ca = np.mean(grp_c, axis=0) grp_cs = np.std(grp_c, axis=0) grp_da = np.mean(grp_d, axis=0) grp_ds = np.std(grp_d, axis=0) io.writeData(os.path.join(src, "group_a_mean.raw"), rsp.sagittalToCoronalData(grp_aa)) io.writeData(os.path.join(src, "group_a_std.raw"), rsp.sagittalToCoronalData(grp_as)) io.writeData(os.path.join(src, "group_b_mean.raw"), rsp.sagittalToCoronalData(grp_ba)) io.writeData(os.path.join(src, "group_b_std.raw"), rsp.sagittalToCoronalData(grp_bs)) io.writeData(os.path.join(src, "group_c_mean.raw"), rsp.sagittalToCoronalData(grp_ca)) io.writeData(os.path.join(src, "group_c_std.raw"), rsp.sagittalToCoronalData(grp_cs)) io.writeData(os.path.join(src, "group_d_mean.raw"), rsp.sagittalToCoronalData(grp_da)) io.writeData(os.path.join(src, "group_d_std.raw"), rsp.sagittalToCoronalData(grp_ds)) #Generate the p-values map ########################## #first comparison #pcutoff: only display pixels below this level of significance pvals, psign = stat.tTestVoxelization(grp_a.astype("float"), grp_d.astype("float"), signed=True, pcutoff=0.05) #color the p-values according to their sign (defined by the sign of the difference of the means between the 2 groups) pvalsc = stat.colorPValues(pvals, psign, positive=[0, 1], negative=[1, 0]) io.writeData(os.path.join(src, "pvalues_homecage_control_vs_DREADDs.tif"), rsp.sagittalToCoronalData(pvalsc.astype("float32"))) #second comparison pvals, psign = stat.tTestVoxelization(grp_a.astype("float"), grp_b.astype("float"), signed=True, pcutoff=0.05) pvalsc = stat.colorPValues(pvals, psign, positive=[0, 1], negative=[1, 0]) io.writeData( os.path.join( src, "pvalues_homecage_control_vs_CNO_control_no_reversal.tif"), rsp.sagittalToCoronalData(pvalsc.astype("float32"))) #third comparison pvals, psign = stat.tTestVoxelization(grp_b.astype("float"), grp_c.astype("float"), signed=True, pcutoff=0.05) pvalsc = stat.colorPValues(pvals, psign, positive=[0, 1], negative=[1, 0]) io.writeData( os.path.join( src, "pvalues_CNO_control_no_reversal_vs_CNO_control_reversal.tif"), rsp.sagittalToCoronalData(pvalsc.astype("float32"))) #fourth comparison pvals, psign = stat.tTestVoxelization(grp_c.astype("float"), grp_d.astype("float"), signed=True, pcutoff=0.05) pvalsc = stat.colorPValues(pvals, psign, positive=[0, 1], negative=[1, 0]) io.writeData( os.path.join(src, "pvalues_CNO_control_reversal_vs_DREADDs.tif"), rsp.sagittalToCoronalData(pvalsc.astype("float32")))
# Voxel-based statistics: ######################### #Load the data (heat maps generated previously ) g1 = stat.readDataGroup(group1vox); g2 = stat.readDataGroup(group2vox); #Generated average and standard deviation maps ############################################## g1a = numpy.mean(g1,axis = 0); g1s = numpy.std(g1,axis = 0); g2a = numpy.mean(g2,axis = 0); g2s = numpy.std(g2,axis = 0); io.writeData(os.path.join(baseDirectory, 'group1_mean.raw'), rsp.sagittalToCoronalData(g1a)); io.writeData(os.path.join(baseDirectory, 'group1_std.raw'), rsp.sagittalToCoronalData(g1s)); io.writeData(os.path.join(baseDirectory, 'group2_fast_mean.raw'), rsp.sagittalToCoronalData(g2a)); io.writeData(os.path.join(baseDirectory, 'group2_fast_std.raw'), rsp.sagittalToCoronalData(g2s)); #Generate the p-values map ########################## #pcutoff: only display pixels below this level of significance pvals, psign = stat.tTestVoxelization(g1.astype('float'), g2.astype('float'), signed = True, pcutoff = 0.05); #color the p-values according to their sign (defined by the sign of the difference of the means between the 2 groups) pvalsc = stat.colorPValues(pvals, psign, positive = [0,1], negative = [1,0]); io.writeData(os.path.join(baseDirectory, 'pvalues.tif'), rsp.sagittalToCoronalData(pvalsc.astype('float32')));
# Load detected cells from cells_raw.npy raw_source = ws.source('cells', postfix='raw') size_intensity = np.hstack( [raw_source[c][:, None] for c in ['size', 'background']]) coordinates_raw = np.hstack([raw_source[c][:, None] for c in 'xyz']) # Swap axes to go from horizontal to sagittal orientation coordinates_raw_swapped_axes = np.zeros_like(coordinates_raw) coordinates_raw_swapped_axes[:, 0] = coordinates_raw[:, 2] coordinates_raw_swapped_axes[:, 1] = coordinates_raw[:, 1] coordinates_raw_swapped_axes[:, 2] = coordinates_raw[:, 0] # Transform cell coordinates from raw 642-space to downsampled 642-space coordinates_resampled = res.resample_points( coordinates_raw_swapped_axes, sink=None, orientation=None, source_shape=io.shape(ws.filename('stitched'))[::-1], sink_shape=io.shape(ch642_downsized_file)) # Transform cell coordinates from 642-space to 488-space coordinates_aligned_to_488 = elx.transform_points( coordinates_resampled, sink=None, transform_directory=os.path.join(elastix_inverse_dir, '488_to_642'), temp_file='/tmp/elastix_input_pipeline.bin', result_directory='/tmp/elastix_output_pipeline') # Change permissions back since transformix alters permissions when it is run os.chmod( os.path.join(elastix_inverse_dir, '488_to_642', 'TransformParameters.0.txt'), 0o777)
'IA2_LT', 'IA2_LB' ] for mouse in samples: baseDirectory = '/d2/studies/ClearMap/IA_iDISCO/' + mouse heatmap = io.readData( os.path.join(baseDirectory, 'cells_heatmap_vox15.tif')) if not os.path.exists(heatmap): raise ValueError('Data does not exist. Check naming convention') for mouse in samples: baseDirectory = '/d2/studies/ClearMap/IA_iDISCO/' + mouse sampleName = mouse region = 'Background' heatmap = io.readData( os.path.join(baseDirectory, 'cells_heatmap_vox15.tif')) heatmap[outside] = 0 io.writeData( os.path.join(baseDirectory, sampleName + '_' + region + '_isolated.tif'), heatmap) #convert sagittal to coronal io.writeData( os.path.join(baseDirectory, sampleName + '_' + region + '_isolated_coronal.tif'), rsp.sagittalToCoronalData( os.path.join(baseDirectory, sampleName + '_' + region + '_isolated.tif')))
c = stat.readDataGroup(ctrl_du_heatmaps) o = stat.readDataGroup(obv_du_heatmaps) d = stat.readDataGroup(dmn_du_heatmaps) ca = np.mean(c, axis=0) cstd = np.std(c, axis=0) oa = np.mean(o, axis=0) ostd = np.std(o, axis=0) da = np.mean(d, axis=0) dstd = np.std(d, axis=0) #write io.writeData(os.path.join(pvaldst, "dorsal_up/control_mean.raw"), rsp.sagittalToCoronalData(ca)) io.writeData(os.path.join(pvaldst, "dorsal_up/control_std.raw"), rsp.sagittalToCoronalData(cstd)) io.writeData(os.path.join(pvaldst, "dorsal_up/observers_mean.raw"), rsp.sagittalToCoronalData(oa)) io.writeData(os.path.join(pvaldst, "dorsal_up/observers_std.raw"), rsp.sagittalToCoronalData(ostd)) io.writeData(os.path.join(pvaldst, "dorsal_up/demonstrators_mean.raw"), rsp.sagittalToCoronalData(da)) io.writeData(os.path.join(pvaldst, "dorsal_up/demonstrators_std.raw"), rsp.sagittalToCoronalData(dstd)) #Generate the p-values map ##########################