diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..b3ada6a Binary files /dev/null and b/.DS_Store differ diff --git a/analysis/analysis.py b/analysis/analysis.py index bba0edd..30b495a 100755 --- a/analysis/analysis.py +++ b/analysis/analysis.py @@ -23,12 +23,12 @@ # yaml formats npfloat_representer = lambda dumper,value: dumper.represent_float(float(value)) nparray_representer = lambda dumper,value: dumper.represent_list(value.tolist()) -float_representer = lambda dumper,value: dumper.represent_scalar(u'tag:yaml.org,2002:float', "{:<.8e}".format(value)) +float_representer = lambda dumper,value: dumper.represent_scalar('tag:yaml.org,2002:float', "{:<.8e}".format(value)) unicode_representer = lambda dumper,value: dumper.represent_unicode(value.encode('utf-8')) yaml.add_representer(float,float_representer) yaml.add_representer(np.float_,npfloat_representer) yaml.add_representer(np.ndarray,nparray_representer) -yaml.add_representer(unicode,unicode_representer) +yaml.add_representer(str,unicode_representer) # matplotlib controls plt.rcParams['svg.fonttype'] = 'none' # to embed fonts in output ('path' is to convert as text as paths) @@ -39,7 +39,7 @@ def default_parameters(): """Generate a default parameter dictionary.""" - print "Loading default parameters" + print("Loading default parameters") params={} # dimensions params['dimensions']={} @@ -66,20 +66,16 @@ def hist_dimensions(cells, outputdir='.', bins=['auto','auto','auto','auto'], un # initialization if mode == 'um': - titles = [u'length (\u03BCm)', u'width (\u03BCm)', u'area (\u03BCm\u00B2)', u'volume (\u03BCm\u00B3)'] + titles = ['length (\u03BCm)', 'width (\u03BCm)', 'area (\u03BCm\u00B2)', 'volume (\u03BCm\u00B3)'] attrs = ['height_um','width_um','area_um2', 'volume_um3'] else: - titles = [u'length (px)', u'width (px)', u'area (px\u00B2)', u'volume (px\u00B3)'] + titles = ['length (px)', 'width (px)', 'area (px\u00B2)', 'volume (px\u00B3)'] attrs = ['height','width','area', 'volume'] nattrs = len(attrs) - if bins is None: - bins = ['auto' for i in range(nattrs)] - elif len(bins) != nattrs: + if len(bins) != nattrs: raise ValueError("bins has the wrong dimensions!") - if units_dx is None: - units_dx = [None for i in range(nattrs)] - elif len(units_dx) != nattrs: + if len(units_dx) != nattrs: raise ValueError("units_dx has the wrong dimensions!") ncells = len(cells) @@ -90,7 +86,7 @@ def hist_dimensions(cells, outputdir='.', bins=['auto','auto','auto','auto'], un # make lists data = [ [] for i in range(nattrs)] - keys = cells.keys() + keys = list(cells.keys()) for n in range(ncells): key = keys[n] cell = cells[key] @@ -118,13 +114,13 @@ def hist_dimensions(cells, outputdir='.', bins=['auto','auto','auto','auto'], un for i in range(nattrs): attr = attrs[i] - print "attr = {}".format(attr) + print("attr = {}".format(attr)) ax = axes[i] # compute histogram hist,edges = np.histogram(data[i], bins=bins[i], density=False) nbins = len(edges)-1 - print "nbins = {:d}".format(nbins) + print("nbins = {:d}".format(nbins)) # plot histogram color = 'grey' @@ -165,7 +161,7 @@ def hist_dimensions(cells, outputdir='.', bins=['auto','auto','auto','auto'], un for ext in exts: fileout = os.path.join(outputdir,filename+ext) fig.savefig(fileout, bbox_inches='tight', pad_inches=0) - print "Fileout: {: h: @@ -359,15 +359,15 @@ def write_crop(img, mask, points, bname, tiff_dir='.', mask_dir='.', pad_x=5, pa cells.append(cell) # write tiff - if params['write_cropped']: + if not namespace.lean: write_crop(img, mask, points, bname=cell_id, tiff_dir=tiff_dir, mask_dir=mask_dir,debug=namespace.debug, **params['crops']) ncells = len(cells) - print "ncells = {:d} collected".format(ncells) + print("ncells = {:d} collected".format(ncells)) # write down the cell dictionary celldict = {cell['id']: cell for cell in cells} celldict = make_dict_serializable(celldict) pathtocells = os.path.join(outputdir, 'collection.js') write_dict2json(pathtocells,celldict) - print "{:<20s}{: 0): - X[l:-l,l:-l] = np.copy(myimg) - Xpad = X[l:2*l,l:-l] - X[:l,l:-l] = Xpad[::-1,:] - Xpad = X[-2*l:-l,l:-l] - X[-l:,l:-l] = Xpad[::-1,:] - - Xpad = X[:,l:2*l] - X[:,:l] = Xpad[:,::-1] - Xpad = X[:,-2*l:-l] - X[:,-l:] = Xpad[:,::-1] - else: - X = np.copy(myimg) - - # make footprint for average - footprint_func = lambda n,m : (n**2 + m**2 <= l**2) | ((hnew-n)**2 + m**2 <= l**2) | ((hnew-n)**2 + (wnew-m)**2 <= l**2) | (n**2 + (wnew-m)**2 <= l**2) - hh,ww = np.meshgrid(hrange,wrange,indexing='ij') - F = np.float_(footprint_func(hh,ww)) - fsum = np.sum(F) # normalization so that it is a mean - F /= fsum - #print("fsum = {:.6f}".format(fsum)) - # important to check than without lognorm, with l=0, it reproduces the input after fft and ifft. - - Xcpy = np.copy(X) - # log transform - if (logtransform): - X = np.log(X) - - # fourier transform - Xtilde = np.fft.fftn(X) - Ftilde = np.fft.fftn(F) - X = np.real(np.fft.ifftn(Xtilde*Ftilde)) - #print(np.max(X-Xcpy), np.median(X-Xcpy)) - - # log detransform - if (logtransform): - X = np.exp(X) - bg = X[l:-l,l:-l] - -# # test -# import matplotlib.pyplot as plt -# xmin = np.min(myimg) -# xmax = np.max(myimg) -# fig=plt.figure() -# ax = fig.add_subplot(1,2,1) -# #im = ax.imshow(F) -# im = ax.imshow(Xcpy, vmin=xmin,vmax=xmax) -# fig.colorbar(im,ax=ax) -# -# ax = fig.add_subplot(1,2,2) -# #ax.imshow(np.real(Ftilde)) -# im = ax.imshow(X, vmin=xmin,vmax=xmax) -# -# fig.colorbar(im,ax=ax) -# -# fig.savefig('footprint.png') -# plt.close() -# sys.exit() -# # test - - # restore scaling - bg = cv2.blur(bg,ksize=(size,size)) # maybe not essential, to smooth background values - #bg = np.array(bg, dtype=np.float32)/255 - bg = scale* bg - offset - bg = np.array(bg, dtype=img.dtype) - return bg - - def preprocess_image(tiff_file, outputdir='.', invert=None, bg_subtract=True, bg_size=200, debug=False): """ INPUT: @@ -296,9 +184,8 @@ def preprocess_image(tiff_file, outputdir='.', invert=None, bg_subtract=True, bg """ # method for background subtraction - #get_background = get_background_medianblur + get_background = get_background_medianblur #get_background = get_background_checkerboard - get_background = lambda arr, size: get_background_logmean_fft(arr, size=size, logtransform=True) # pre-processing bname = os.path.splitext(os.path.basename(tiff_file))[0] @@ -324,11 +211,11 @@ def preprocess_image(tiff_file, outputdir='.', invert=None, bg_subtract=True, bg if invert == None: invert = [] elif invert == 'all': - invert = range(nchannels) + invert = list(range(nchannels)) # adjust background sliding window size bg_size = 2*int(bg_size/2) + 1 # make odd - print "bg_size = {:d}".format(bg_size) + print("bg_size = {:d}".format(bg_size)) img0 = np.copy(img) # process channels @@ -348,7 +235,7 @@ def preprocess_image(tiff_file, outputdir='.', invert=None, bg_subtract=True, bg if bg_subtract: # print "Background subtraction..." # method for background subtraction using sliding window - img_bg[c] = get_background(arr, bg_size) + img_bg[c] = get_background(arr, size=bg_size) idx = arr > img_bg[c] # print np.unique(arr) # print np.unique(img_bg[c]) @@ -375,15 +262,15 @@ def preprocess_image(tiff_file, outputdir='.', invert=None, bg_subtract=True, bg fname = os.path.basename(tiff_file) fileout = os.path.join(outputdir,fname) ti.imwrite(fileout, img, imagej=True, photometric='minisblack', metadata=meta) - print "{:<20s}{: 0.) ntot = len(np.ravel(eimg)) - print "nz = {:d} / {:d} sparcity index = {:.2e}".format(nz, ntot, float(nz)/float(ntot)) + print("nz = {:d} / {:d} sparcity index = {:.2e}".format(nz, ntot, float(nz)/float(ntot))) efname = bname #efile = os.path.join(outputdir,efname+'.txt') #efile = os.path.join(outputdir,efname+'.pkl') @@ -287,7 +287,7 @@ def get_estimator_boundingbox(tiff_file, channel=0, outputdir='.', w0=1, w1=100, #np.savetxt(fout, eimg) #pkl.dump(eimg,fout) ssp.save_npz(efile, ssp.coo_matrix(eimg), compressed=False) - print "{:<20s}{: 0): - print "Computing OTSU thresholds..." + print("Computing OTSU thresholds...") thresholds = get_OTSU(tiff_files, outputdir=outputdir) diff --git a/roles/params.yml b/roles/params.yml new file mode 100755 index 0000000..92021fd --- /dev/null +++ b/roles/params.yml @@ -0,0 +1,56 @@ +process_nd2: + colors: + fovs: + xcrop: [100,1000] + ycrop: [100,1000] + +preprocess_images: + invert: [2] + bg_subtract: True + bg_size: 200 + +# um/px: 0.11 +segmentation: + channel: 2 + method: bounding_box + estimator_params: + bounding_box: + acut: 0.8 + aratio_min: 2.5 + aratio_max: 100 + h0: 20 + h1: 200 + w0: 8.5 + w1: 10.5 + border_pad: 5 + threshold: + mask_params: + threshold: 0.95 + +collection: + # normally the px2um is saved in the metadata.txt file + px2um: null + crops: + pad_x: 5 + pad_y: 5 + +dimensions: + bins: + - 32 + - 16 + - 32 + - 32 + units_dx: + - 2 + - 0.1 + - 2 + - 2 + mode: um + +dimensions_other: + bins: + - 32 + - 32 + units_dx: + - 2 + - 0.1