def sweep_params(params): fn, dst, rBP, fIPsz, dCSP, thresh = params brain_dst = os.path.join(dst, os.path.basename(fn)[:-4]) if not os.path.exists(brain_dst): os.mkdir(brain_dst) svdst = os.path.join( brain_dst, "rBP%02d_fIPsize%03d_dCSP%05d_thres%04d_row%02d.npy" % (rBP, fIPsz, dCSP, thresh[0][0], thresh[1][0])) # if not os.path.exists(svdst): #if params have not been tried already img = tifffile.imread(fn) #read as z,y,x img = img.astype("uint16") #find spots c = detectSpots( img, detectSpotsParameter=None, correctIlluminationParameter=None, removeBackgroundParameter={"size": (rBP, rBP)}, findIntensityParameter={ "size": (fIPsz, fIPsz, fIPsz), "method": "Max" }, #size is based on cell size/resolution detectCellShapeParameter={"threshold": dCSP}, verbose=False) #c is a tuple output, 0 = points, 1 = intensities #INTENSITY OR SIZE BASED THRESHOLDING points, intensities = thresholdPoints(c[0].astype(int), c[1].astype(int), threshold=thresh[0], row=thresh[1]) np.save(svdst, points.astype(int)) #save cells wth volume name, c in z,y,x print("thresholded and saved to %s\nnumber of cells = %d" % (svdst, len(c[0]))) return svdst
points = points.astype('int16') #threshold intensities dataShape = detectCellShape(imgD, points, threshold=cellShapeThresh, verbose=True) cellSizesPre = findCellSize(dataShape, maxlabel=points.shape[0]) io.writeData(os.path.join(resultDir, fName + '_CellShapes.tif'), 255 * dataShape.astype('int32') / dataShape.max()) #cintensity = findIntensity(imgD, points); #points, intensities = thresholdPoints(points,cintensity, threshold = (5,20), row = (1,1)); points, cellSizesPost = thresholdPoints(points, cellSizesPre, threshold=pointsThresh, row=threshType) #pdb.set_trace() #io.writeData(os.path.join(homeDir, 'Results/OverlayWatershed.tif'), overlay_Img); overlay_Img = plt.fredOverlayPoints(cfos_fn, points, pointColor=[200, 0, 0]) io.writeData(os.path.join(resultDir, fName + '_PointsOriginalImg.tif'), overlay_Img) overlay_Img = plt.overlayPoints(imgD, points, pointColor=[200, 0, 0]) io.writeData(os.path.join(resultDir, fName + '_PointsFilterDoG.tif'), overlay_Img)
if result == "ENDPROCESS": print("Jobid > # of jobs required, ending job") except: print("Jobid > # of jobs required, ending job") print("\n finished step 4 - cell detection \n") ###########################################STEP 5######################################################## out = join_results_from_cluster_helper(**dct["ImageProcessingParameter"]) print("\n finished step 5 \n") ###########################################STEP 6######################################################## threshold = (500,3000); row = (2,2) points, intensities = io.readPoints(dct["ImageProcessingParameter"]["sink"]) #Thresholding: the threshold parameter is either intensity or size in voxel, depending on the chosen "row" #row = (0,0) : peak intensity from the raw data #row = (1,1) : peak intensity from the DoG filtered data #row = (2,2) : peak intensity from the background subtracted data #row = (3,3) : voxel size from the watershed points, intensities = thresholdPoints(points, intensities, threshold = threshold, row = row) #change dst to match parameters sweeped dst = (os.path.join(brain, "clearmap_cluster_output/cells_rBP%s_fIPmethod%s_fIPsize%s_dCSP%s.npy" % (rBP_size, fIP_method, fIP_size, dCSP_threshold)), os.path.join(brain, "clearmap_cluster_output/intensities_rBP%s_fIPmethod%s_fIPsize%s_dCSP%s.npy" % (rBP_size, fIP_method, fIP_size, dCSP_threshold))) io.writePoints(dst, (points, intensities)); print("\n finished step 6 \n")
def output_analysis( threshold=(20, 900), row=(3, 3), check_cell_detection=False, **params): """Wrapper for analysis: Inputs ------------------- Thresholding: the threshold parameter is either intensity or size in voxel, depending on the chosen "row" Row: row = (0,0) : peak intensity from the raw data row = (1,1) : peak intensity from the DoG filtered data row = (2,2) : peak intensity from the background subtracted data row = (3,3) : voxel size from the watershed Check Cell detection: (For the testing phase only, remove when running on the full size dataset) """ dct = pth_update(set_parameters_for_clearmap(**params)) points, intensities = io.readPoints( dct["ImageProcessingParameter"]["sink"]) #Thresholding: the threshold parameter is either intensity or size in voxel, depending on the chosen "row" #row = (0,0) : peak intensity from the raw data #row = (1,1) : peak intensity from the DoG filtered data #row = (2,2) : peak intensity from the background subtracted data #row = (3,3) : voxel size from the watershed points, intensities = thresholdPoints(points, intensities, threshold=threshold, row=row) #points, intensities = thresholdPoints(points, intensities, threshold = (20, 900), row = (2,2)); io.writePoints(dct["FilteredCellsFile"], (points, intensities)) ## Check Cell detection (For the testing phase only, remove when running on the full size dataset) ####################### # if check_cell_detection: # import ClearMap.Visualization.Plot as plt # pointSource= os.path.join(BaseDirectory, FilteredCellsFile[0]); # data = plt.overlayPoints(cFosFile, pointSource, pointColor = None, **cFosFileRange); # io.writeData(os.path.join(BaseDirectory, "cells_check.tif"), data); # Transform point coordinates ############################# points = io.readPoints( dct["CorrectionResamplingPointsParameter"]["pointSource"]) points = resamplePoints(**dct["CorrectionResamplingPointsParameter"]) points = transformPoints( points, transformDirectory=dct["CorrectionAlignmentParameter"] ["resultDirectory"], indices=False, resultDirectory=None) dct["CorrectionResamplingPointsInverseParameter"]["pointSource"] = points points = resamplePointsInverse( **dct["CorrectionResamplingPointsInverseParameter"]) dct["RegistrationResamplingPointParameter"]["pointSource"] = points points = resamplePoints(**dct["RegistrationResamplingPointParameter"]) points = transformPoints( points, transformDirectory=dct["RegistrationAlignmentParameter"] ["resultDirectory"], indices=False, resultDirectory=None) io.writePoints(dct["TransformedCellsFile"], points) # Heat map generation ##################### points = io.readPoints(dct["TransformedCellsFile"]) intensities = io.readPoints(dct["FilteredCellsFile"][1]) #Without weigths: vox = voxelize(points, dct["AtlasFile"], **dct["voxelizeParameter"]) if not isinstance(vox, str): io.writeData(os.path.join(dct["OutputDirectory"], "cells_heatmap.tif"), vox.astype("int32")) #With weigths from the intensity file (here raw intensity): dct["voxelizeParameter"]["weights"] = intensities[:, 0].astype(float) vox = voxelize(points, dct["AtlasFile"], **dct["voxelizeParameter"]) if not isinstance(vox, str): io.writeData( os.path.join(dct["OutputDirectory"], "cells_heatmap_weighted.tif"), vox.astype("int32")) #Table generation: ################## #With integrated weigths from the intensity file (here raw intensity): try: ids, counts = countPointsInRegions(points, labeledImage=dct["AnnotationFile"], intensities=intensities, intensityRow=0) table = numpy.zeros(ids.shape, dtype=[("id", "int64"), ("counts", "f8"), ("name", "a256")]) table["id"] = ids table["counts"] = counts table["name"] = labelToName(ids) io.writeTable( os.path.join(dct["OutputDirectory"], "Annotated_counts_intensities.csv"), table) #Without weigths (pure cell number): ids, counts = countPointsInRegions(points, labeledImage=dct["AnnotationFile"], intensities=None) table = numpy.zeros(ids.shape, dtype=[("id", "int64"), ("counts", "f8"), ("name", "a256")]) table["id"] = ids table["counts"] = counts table["name"] = labelToName(ids) io.writeTable( os.path.join(dct["OutputDirectory"], "Annotated_counts.csv"), table) except: print("Table not generated.\n") print("Analysis Completed") return
#Detect Maxima imgMaxima = findExtendedMaxima(imgD, hMax=None, verbose=True, threshold=3) points = findCenterOfMaxima(img, imgMaxima) points = points.astype('int16') #threshold intensities dataShape = detectCellShape(imgD, points, threshold=5) cellSizesPre = findCellSize(dataShape, maxlabel=points.shape[0]) io.writeData(os.path.join(homeDir, 'Results/CellShapes.tif'), 20 * dataShape.astype('int32')) #cintensity = findIntensity(imgD, points); #points, intensities = thresholdPoints(points,cintensity, threshold = (5,20), row = (1,1)); points, cellSizesPost = thresholdPoints(points, cellSizesPre, threshold=(5, 100), row=(3, 3)) #pdb.set_trace() #io.writeData(os.path.join(homeDir, 'Results/OverlayWatershed.tif'), overlay_Img); overlay_Img = plt.fredOverlayPoints(input_fn, points, pointColor=[200, 0, 0]) io.writeData(os.path.join(homeDir, 'Results/PointsOriginalImg.tif'), overlay_Img) overlay_Img = plt.fredOverlayPoints(os.path.join(homeDir, 'Results/FilterDoG.tif'), points, pointColor=[200, 0, 0]) io.writeData(os.path.join(homeDir, 'Results/PointsFilterDoG.tif'), overlay_Img) #################################################################################################
def output_analysis_helper(threshold=(20, 900), row=(3, 3), **params): ''' Function to change elastix result directory before running 'step 6' i.e. point transformix to atlas. ''' dct = pth_update(set_parameters_for_clearmap(**params)) dct['RegistrationAlignmentParameter']["resultDirectory"] = os.path.join( params["outputdirectory"], 'clearmap_cluster_output/elastix_auto_to_sim_atlas') points, intensities = io.readPoints( dct['ImageProcessingParameter']["sink"]) #Thresholding: the threshold parameter is either intensity or size in voxel, depending on the chosen "row" #row = (0,0) : peak intensity from the raw data #row = (1,1) : peak intensity from the DoG filtered data #row = (2,2) : peak intensity from the background subtracted data #row = (3,3) : voxel size from the watershed points, intensities = thresholdPoints(points, intensities, threshold=threshold, row=row) #points, intensities = thresholdPoints(points, intensities, threshold = (20, 900), row = (2,2)); io.writePoints(dct['FilteredCellsFile'], (points, intensities)) # Transform point coordinates ############################# points = io.readPoints( dct['CorrectionResamplingPointsParameter']["pointSource"]) points = resamplePoints(**dct['CorrectionResamplingPointsParameter']) points = transformPoints( points, transformDirectory=dct['CorrectionAlignmentParameter'] ["resultDirectory"], indices=False, resultDirectory=None) dct['CorrectionResamplingPointsInverseParameter']["pointSource"] = points points = resamplePointsInverse( **dct['CorrectionResamplingPointsInverseParameter']) dct['RegistrationResamplingPointParameter']["pointSource"] = points points = resamplePoints(**dct['RegistrationResamplingPointParameter']) points = transformPoints( points, transformDirectory=dct['RegistrationAlignmentParameter'] ["resultDirectory"], indices=False, resultDirectory=None) io.writePoints(dct['TransformedCellsFile'], points) # Heat map generation ##################### points = io.readPoints(dct['TransformedCellsFile']) intensities = io.readPoints(dct['FilteredCellsFile'][1]) #Without weigths: vox = voxelize(points, dct['AtlasFile'], **dct['voxelizeParameter']) if not isinstance(vox, basestring): io.writeData(os.path.join(dct['OutputDirectory'], 'cells_heatmap.tif'), vox.astype('int32')) #With weigths from the intensity file (here raw intensity): dct['voxelizeParameter']["weights"] = intensities[:, 0].astype(float) vox = voxelize(points, dct['AtlasFile'], **dct['voxelizeParameter']) if not isinstance(vox, basestring): io.writeData( os.path.join(dct['OutputDirectory'], 'cells_heatmap_weighted.tif'), vox.astype('int32')) #Table generation: ################## #With integrated weigths from the intensity file (here raw intensity): ids, counts = countPointsInRegions(points, labeledImage=dct['AnnotationFile'], intensities=intensities, intensityRow=0) table = np.zeros(ids.shape, dtype=[('id', 'int64'), ('counts', 'f8'), ('name', 'a256')]) table["id"] = ids table["counts"] = counts table["name"] = labelToName(ids) io.writeTable( os.path.join(dct['OutputDirectory'], 'Annotated_counts_intensities.csv'), table) #Without weigths (pure cell number): ids, counts = countPointsInRegions(points, labeledImage=dct['AnnotationFile'], intensities=None) table = np.zeros(ids.shape, dtype=[('id', 'int64'), ('counts', 'f8'), ('name', 'a256')]) table["id"] = ids table["counts"] = counts table["name"] = labelToName(ids) io.writeTable(os.path.join(dct['OutputDirectory'], 'Annotated_counts.csv'), table) print('Analysis Completed') return