예제 #1
0
def trans_to_tif(file_list, overwrite = False):
    if file_list is not None:
        for path in file_list:
            opts = ImporterOptions()
            opts.setId(path)
            opts.setVirtual(True)
            opts.setColorMode(ImporterOptions.COLOR_MODE_COMPOSITE)
            opts.setOpenAllSeries(True)

            process = ImportProcess(opts)

            try:
                process.execute()
            except:
                pass
            else:
                process.execute()

            try:
                imps = ImagePlusReader(process).openImagePlus()
            except:
                IJ.log(path + "\n" + "This file was not properly processed")
                pass

            else:
                dir_name = os.path.dirname(path)
                file_name = os.path.basename(os.path.splitext(path)[0])
                save_dir = os.path.join(dir_name, file_name)
                if not os.path.exists(save_dir):
                    os.makedirs(save_dir)

                imps = ImagePlusReader(process).openImagePlus()
                for i, imp in enumerate(imps):
                    save_path = os.path.join(save_dir, file_name + "_pos{}.tif".format(i + 1))

                    if not os.path.exists(save_path):
                        IJ.saveAsTiff(imp, save_path)
                        IJ.freeMemory()
                    else:
                        if overwrite:
                            IJ.saveAsTiff(imp, save_path)
                            IJ.freeMemory()
                        else:
                            pass
def process_imgdir( directory, close_after ) :
    print( 'Processing: ' + directory )

    original_image_filename = directory + '/uniform.tif'
    probabilities_image_filename = directory + '/probabilities.tif'
    cube_rois_filename = directory + '/CubeROIs.csv'

    cubes = []
    f = open(cube_rois_filename)
    lines = f.readlines()
    for line in lines[1:]:
        #els = [ int(el)-1 for el in line.strip().split(',') ]
        els = [ int(el) for el in line.strip().split(',') ]
        cubes += [ { 'idx': els[0], 'class': els[1], 'x1':min(els[2],els[5]), 'y1':min(els[3],els[6]), 'z1':min(els[4],els[7]), 'x2':max(els[5],els[2]), 'y2':max(els[6],els[3]), 'z2':max(els[4],els[7]) } ]
    f.close()
    print( 'Read ' + str(len(cubes)) + ' cubes from file' )

    #for cube in cubes:
    #for cube_idx in range( len( cubes ) ):
    for cube_idx in [0]:
        cube = cubes[cube_idx]
        print( cube )

        all_edt_histograms['name'] += [ directory.split( '/' )[-1] + '_' + str(cube_idx) ]
        all_skeleton_edt_histograms['name'] += [ directory.split( '/' )[-1] + '_' + str(cube_idx) ]

        cube_basename = directory + '/cube_' + str(cube_idx) + '_'

        # Check if we have a parameters file, and evaluate it if so, otherwise make one with defaults
        param_filename = cube_basename + 'parameters.py'
        # Make a default to start with
        no_params_set = False
        if not os.path.isfile( param_filename ):
            f = open( param_filename, 'w' )
            f.write( 'mesh_thresh=200\n' )
            f.write( 'interior_coords=[ (1,1,1) ]\n' )
            f.close()
            no_params_set = True

        if os.path.isfile( param_filename ):
            f = open( param_filename )
            lines = f.readlines()
            for line in lines:
                if 'mesh_thresh' in line:
                    mesh_thresh = eval(line.split('=')[1])
                if 'interior_coords' in line:
                    interior_coords = eval(line.split('=')[1])

        load_uniform = True
        if load_uniform:
            #open uniform
            uniform = IJ.openImage( original_image_filename )

            #crop uniform
            uniform.setRoi( cube['x1'], cube['y1'], cube['x2'], cube['y2'] )
            IJ.run( uniform, "Crop", "")
            IJ.run( uniform, "Make Substack...", "slices=" + str(cube['z1']) + '-' + str(cube['z2']) )
            uniform.close()
            uniform = IJ.getImage()
            IJ.saveAsTiff( uniform, cube_basename + 'raw.tif' )

            #save max_projection of uniform
            IJ.run(uniform, "Z Project...", "projection=[Max Intensity]")
            uniform_mp = IJ.getImage()
            IJ.run(uniform_mp, "Enhance Contrast", "saturated=0.35")
            IJ.saveAsTiff( uniform_mp, cube_basename + 'raw_maxproj.tif' )

        #open probabilites
        probabilities = IJ.openImage( probabilities_image_filename )

        #crop probabilites
        probabilities.setRoi( cube['x1'], cube['y1'], cube['x2'], cube['y2'] )
        IJ.run( probabilities, "Crop", "")
        IJ.run( probabilities, "Make Substack...", "slices=" + str(cube['z1']) + '-' + str(cube['z2']) )
        time.sleep( 5 )
        probabilities.close()
        probabilities = IJ.getImage()
        #IJ.saveAsTiff( probabilities, cube_basename + 'probabilities.tif' )

        time.sleep( 5 )

        #save max_projection of probabilites
        IJ.run(probabilities, "Z Project...", "projection=[Max Intensity]")
        probabilities_mp = IJ.getImage()
        IJ.run(probabilities_mp, "Enhance Contrast", "saturated=0.35")
        IJ.saveAsTiff( probabilities_mp, cube_basename + 'probabilities_maxproj.tif' )

        time.sleep( 5 )

        # blur probabilities to help smooth the isosurfaces
        blur_radius = 2
        IJ.run( probabilities, "Gaussian Blur 3D...", 'x=' + str(blur_radius) + ' y=' + str(blur_radius) + ' z=' + str(blur_radius) )

        #threshold probabilities
        #IJ.run(probabilities, "Enhance Contrast", "saturated=0.35")
        IJ.run( probabilities, "8-bit", "")
        #IJ.saveAsTiff( probabilities, cube_basename + 'probabilities.tif' )
        #for k in range( probabilities.getNSlices() ):
        #    probabilities.setSlice(k+1)
        #    probabilities.getProcessor().setThreshold( mesh_thresh - 1, mesh_thresh + 1, 0 )
        #IJ.run( probabilities, 'Convert to Mask', 'method=Default background=Dark black')
        #time.sleep( 5 )
        IJ.saveAsTiff( probabilities, cube_basename + 'probabilities.tif' )

        time.sleep( 5 )

        #mesh binary_probabilities
        #IJ.run( "3D Viewer", "")


        threedType = 2
        threedName = 'cube_' + str(cube_idx)

        IJ.runPlugIn( "ij3d.ImageJ3DViewer", probabilities.getTitle() )
        univ = Image3DUniverse.universes.get(0)
        univ.addContent( probabilities, Color3f(1,1,1), threedName, mesh_thresh, [True, False, False], 2, threedType )
        ImageJ3DViewer.select( threedName )
        ImageJ3DViewer.exportContent( 'wavefront', cube_basename + 'mesh.obj' )

        #smooth mesh
        c = univ.getSelected()
        n = c.getContent()
        ctm = n.getMesh()
        fim = customnode.FullInfoMesh( ctm.getMesh() )
        ec = customnode.EdgeContraction( fim, False )

        initial_num_verts = ec.getVertexCount()
        num_to_remove = int( initial_num_verts * 0.1 )

        #v = InteractiveMeshDecimation.simplify( ec, num_to_remove )

        enable_smoothing = False
        if enable_smoothing:
            part = num_to_remove / 10
            last = num_to_remove % 10
            ret = 0
            for i in range(10):
                IJ.showProgress(i + 1, 10)
                ret = ec.removeNext(part)
            if (last != 0):
                ret = ec.removeNext(last)
            IJ.showProgress(1)

            ctm.setMesh( fim.getMesh() )
            ImageJ3DViewer.exportContent( 'wavefront', cube_basename + 'smooth_mesh.obj' )

        # 3d viewer screenshot
        screenshot_3d = univ.takeSnapshot()
        IJ.saveAsTiff( screenshot_3d, cube_basename + 'mesh_screenshot.tif' )

        #voxelize mesh
        voxelizer = InteractiveMeshVoxelization()
        voxelizer.voxelize( ctm, probabilities.getWidth(), probabilities.getHeight(), probabilities.getStackSize() )

        #voxelization = WindowManager.getImage( ctm.getName() + '_voxelization' )
        voxelization = WindowManager.getImage( 'null_voxelization' )
        voxelization.setCalibration( probabilities.getCalibration().copy() )
        #manually 3D fill vasculature, fill with thresholdable-color
        #save selected vasculature

        #call('process3d.Flood_Fill.fill', x,y,z)
        if not no_params_set:
            fill_color = 100
            Flood_Fill.fill( voxelization, interior_coords[0][0], interior_coords[0][1], interior_coords[0][2], fill_color )

            # Threshold to extract
            #IJ.setAutoThreshold( voxelization, 'Default dark' )
            #Prefs.blackBackground = true
            #IJ.run( voxelization, 'Convert to Mask', 'method=Default background=Dark black')

            for k in range( voxelization.getNSlices() ):
                voxelization.setSlice(k+1)
                voxelization.getProcessor().setThreshold( fill_color - 1, fill_color + 1, 0 )
            IJ.run( voxelization, 'Convert to Mask', 'method=Default background=Dark black')
            IJ.saveAsTiff( voxelization, cube_basename + 'voxelization.tif' )

            # Calculate and record volume HERE

            IJ.run(voxelization, "Z Project...", "projection=[Max Intensity]")
            voxelization_mp = IJ.getImage()
            IJ.run(voxelization_mp, "Enhance Contrast", "saturated=0.35")
            IJ.saveAsTiff( voxelization_mp, cube_basename + 'voxelization_maxproj.tif' )

            # Calculate EDT
            IJ.run( voxelization, "Exact Euclidean Distance Transform (3D)", "" )
            edt = WindowManager.getImage( 'EDT' )
            IJ.run( edt, 'Fire', '' )

            # Get the histogram data in an array

            hist_nBins = int( ( hist_max - hist_min ) / hist_step )
            #edt_stats = edt.getStatistics( Measurements.MEDIAN, hist_nBins, hist_min, hist_max )
            edt_stats = StackStatistics( edt, hist_nBins, hist_min, hist_max )
            hist_data = edt_stats.getHistogram()
            hist_binLabels = [ ( hist_min + hist_step * el ) for el in range( hist_nBins ) ]

            max_radius = 20
            IJ.run( edt, 'Histogram', 'bins=' + str(hist_nBins) + ' x_min=' + str(hist_min) + ' x_max=' + str(hist_max) + ' y_max=Auto stack' )
            edt_histogram = WindowManager.getImage( 'Histogram of EDT' )
            IJ.saveAsTiff( edt_histogram, cube_basename + 'edt_histogram.tif' )

            x_unit_scale = uniform.getCalibration().getX(1)
            y_unit_scale = uniform.getCalibration().getY(1)
            z_unit_scale = uniform.getCalibration().getZ(1)

            f = open( cube_basename + 'edt_histogram.csv', 'w' )
            for k in range( len( hist_data ) ):
                f.write( str(hist_binLabels[k]) + '\t' + str(hist_data[k]*x_unit_scale*y_unit_scale*z_unit_scale) + '\n' )
                #f.write( str(hist_binLabels[k]) + '\t' + str(hist_data[k]) + '\n' )
                all_edt_histograms[hist_binLabels[k]] += [ hist_data[k] ]

            f.close()

            # Handling skeletons
            IJ.run( voxelization, "Skeletonize (2D/3D)", "")
            skeleton = voxelization # For simplicity later, but note that voxelization has been mutated
            IJ.run(skeleton, "32-bit", "")
            IJ.run(skeleton, "Calculator Plus", 'i1=' + str(skeleton.getTitle()) + ' i2=' + str(skeleton.getTitle()) + ' operation=[Scale: i2 = i1 x k1 + k2] k1=0.003921568627 k2=0' )

            IJ.run(skeleton, "Z Project...", "projection=[Max Intensity]")
            skeleton_mp = IJ.getImage()
            IJ.run(skeleton_mp, "Enhance Contrast", "saturated=0.35")
            IJ.saveAsTiff( skeleton_mp, cube_basename + 'skeleton_maxproj.tif' )

            ic = ImageCalculator()
            skeleton_edt = ic.run("Multiply 32-bit stack", edt, skeleton)
            IJ.run( skeleton_edt, 'Fire', '' )


            # Get the histogram data in an array
            hist_nBins = int( ( hist_max - hist_min ) / hist_step )
            #skeleton_edt_stats = edt.getStatistics( Measurements.MEDIAN, hist_nBins, hist_min, hist_max )
            skeleton_edt_stats = StackStatistics( edt, hist_nBins, hist_min, hist_max )
            hist_data = skeleton_edt_stats.getHistogram()
            hist_binLabels = [ ( hist_min + hist_step * el ) for el in range( hist_nBins ) ]

            IJ.run( skeleton_edt, 'Histogram', 'bins=' + str(hist_nBins) + ' x_min=' + str(hist_min) + ' x_max=' + str(hist_max) + ' y_max=Auto stack' )
            skeleton_edt_histogram = WindowManager.getImage( 'Histogram of EDT' )
            IJ.saveAsTiff( skeleton_edt_histogram, cube_basename + 'skeleton_edt_histogram.tif' )

            unit_scale = uniform.getCalibration().getX(1)

            f = open( cube_basename + 'skeleton_edt_histogram.csv', 'w' )
            for k in range( len( hist_data ) ):
                f.write( str(hist_binLabels[k]) + '\t' + str(hist_data[k] * unit_scale) + '\n' )
                all_skeleton_edt_histograms[hist_binLabels[k]] += [ hist_data[k] * unit_scale ]
            f.close()

        else:
            IJ.saveAsTiff( voxelization, cube_basename + 'voxelization.tif' )

        # Free up resources
        if close_after:
            ImageJ3DViewer.close()
            IJ.run( 'Close All', '' )
            IJ.freeMemory()
예제 #3
0
    inner_list = []
    for i, imp1 in enumerate(imps):
        inner_list.append(imp1)
        if len(inner_list) == n:
            outer_list.append(inner_list)
            inner_list = []
        if i + 1 == len(imps) and inner_list != []:
            empty_imp = create_empty_imp(imp1)
            while n - len(inner_list) > 0:            
                inner_list.append(empty_imp)
            outer_list.append(inner_list)
    return outer_list



def integrated_combine(scale, col):
    fl = get_file_list()
    imps = [IJ.openImage(f) for f in fl]
    resized_imps = [resize(imp, scale) for imp in imps]
    del imps
    imps_list = separate_imps(resized_imps, col)
    del resized_imps
    combined_imps = [combine_images(imps) for imps in imps_list]
    vertival_combined_imp = combine_images(combined_imps, "vertically")
    del combined_imps
    vertival_combined_imp.show()

integrated_combine(scale = 0.25, col = 3)

IJ.freeMemory()
from ij.plugin import FolderOpener
from ij.plugin.filter import AVI_Writer
from ij.process import ImageConverter
from ij.gui import Arrow
from ij import ImagePlus, IJ
#from loci.plugins.util import BFVirtualStack
#from loci.formats import ChannelSeparator

# Record the starting time to calculate total time cost
startTime = System.currentTimeMillis()
time_stamp = '%06d'%(startTime%1000000)

# Clean up workspace
IJ.run("Close All");
IJ.run("Collect Garbage"); # Release occupied memory
print("Used memory at start: " + IJ.freeMemory())

#-------------------
# Facility functions
#-------------------

# NOTE the difference of two similar functions!
# save_snap_shot_seq_simple saves tif series with *** annotated *** spot
# save_snap_shot_seq saves tif series with *** centered *** spot

def save_snap_shot_seq_simple(imp, xml_filename, input_folder, output_folder,
							  ZOOM_BY_Z_DEPTH=False, Z1_CLOSE_TO_COVERSLIP=False,
							  TEST_RUN=False, ANNOTATE_Z=False,
							  DRAW_DOT=True, DRAW_BOX=True, L_RECT=100,
							  trackIDs=None, z_number_to_project=1):
	
예제 #5
0
def run_it():
    # Make an error log file that can be written to
    errorFilePath = os.path.join(experimentFolder, "errorFile.txt")
    now = datetime.datetime.now()
    errorFile = open(errorFilePath, "w")
    errorFile.write("\n" + now.strftime("%Y-%m-%d %H:%M") + "\n")
    errorFile.write("#### anilyze-data ####" + "\n")
    errorFile.close()

    #Runs microscope_check and defines the scanList accordingly
    microscopeType = microscope_check(experimentFolder)

    # Call list_scans and pass it the microscopeType variable. Gets the scanList
    if microscopeType == "Olympus":
        scanList = list_scans(experimentFolder, microscopeType)
        print "The returned scanList is", len(scanList), "item(s) long"

    elif microscopeType == "Bruker":
        scanList = list_scans(experimentFolder, microscopeType)
        print "The returned scanList is", len(scanList), "item(s) long"

    # For each scan in the scanList, call the following functions:
    for scan in scanList:
        try:
            basename = os.path.basename(scan)  # get the scan name (basename)

            #start new line in errorFile
            errorFile = open(errorFilePath, "a")
            errorFile.write("\n \n -- Processing " + basename + " --" + "\n")
            errorFile.close()

            make_hyperstack(basename, scan,
                            microscopeType)  # open the hyperstack
            imp = IJ.getImage()  # select the open image
            channels = imp.getNChannels()  #gets the number of channels
            print "The number of channels is", channels

            singleplane = single_plane_check(
            )  #checks to see if sinegle z-plane
            print "The returned value of singleplane is ", singleplane

            make_MAX(singleplane
                     )  # make max projection (skips if singleplane == True)

            IJ.run("Close All")

            #finishes errorFile
            errorFile = open(errorFilePath, "a")
            errorFile.write("Congrats, it was successful!\n")
            errorFile.close()
            IJ.freeMemory()  # runs garbage collector

        except:  #if there is an exception to the above code, create or append to an errorFile with the traceback
            print "Error with ", basename, "continuing on..."
            errorFile = open(errorFilePath, "a")
            errorFile.write("\n" + now.strftime("%Y-%m-%d %H:%M") +
                            "\n")  #writes the date and time
            errorFile.write("Error with " + basename + "\n" + "\n")
            traceback.print_exc(
                file=errorFile)  # writes the error traceback to the file
            errorFile.close()

            IJ.run("Close All")
            IJ.freeMemory()  # runs garbage collector
            continue  # continue on with the next scan, even if the current one threw an error

    #close out errorFile
    errorFile = open(errorFilePath, "a")
    errorFile.write("\nDone with script.\n")
    errorFile.close()