def load_volume_from_h5(self,h5filename): """ Load the volume from an HDF5 file """ hf5 = tables.openFile(h5filename, 'r') X0,Y0,Z0 = self.info['X0'], self.info['Y0'], self.info['Z0'] H,W,D = self.info['Height'], self.info['Width'], self.info['Depth'] np_tensor_3d = hf5.root.full_image[Z0:Z0+D, Y0:Y0+H, X0:X0+W] hf5.close() self.imgs = [] self.pixels = [] for z in range(D): img_z = Image.fromarray(np_tensor_3d[z,:,:]) self.imgs.append(img_z) self.pixels.append(img_z.load()) tee.log(z+1, 'images read into stack (from h5 file)')
def compute_hues(self, C, n_neighbors=6): # colorize randomly for i, c in enumerate(C): c.index = i colors = {c.index: int(random.uniform(0, 255)) for c in C} if which('minizinc') is None: for c in C: c.hue = colors[c.index] / 255.0 return # If we have minizinc then create a knn graph and run a constraint program to colorize nicely edges = [] X = np.array([[c.x, c.y, c.z] for c in C]) kdtree = cKDTree(X) purkinje_radius = 16 for c in C: distances, neighbors = kdtree.query([c.x, c.y, c.z], n_neighbors) for d, n in zip(distances, neighbors): if d < 3 * purkinje_radius and c.index < C[n].index: edges.append([c.index, C[n].index]) if len( edges ) < 1000: # otherwise solving the constraint program might be too costly with open(self.savedir + '/' + '/edges.dzn', 'w') as ostream: print('n=%d;' % len(C), file=ostream) print('num_edges=%d;' % len(edges), file=ostream) print('E = array2d(1..num_edges, 1..2, [', file=ostream) print(','.join( map(str, [e[i] for e in edges for i in range(2)])), file=ostream) print(']);', file=ostream) tee.log('Running minizinc on', len(edges), 'edges') curdir = os.getcwd() os.chdir(SHARE_DIR) mzn_cmd = ('minizinc color.mzn -d %s/edges.dzn -o %s/mnz_sol.txt' % (self.savedir, self.savedir)) tee.log(mzn_cmd) os.system(mzn_cmd) os.chdir(curdir) istream = open(self.savedir + '/mnz_sol.txt') colors = {} for line in istream.readlines(): items = line.strip().split() if items[0] == 'c': colors[int(items[1])] = int(items[2]) for c in C: c.hue = colors[c.index] / 255.0
def compute_hues(self, C, n_neighbors=6): # colorize randomly for i, c in enumerate(C): c.index = i colors = {c.index: int(random.uniform(0, 255)) for c in C} if which('minizinc') is None: for c in C: c.hue = colors[c.index]/255.0 return # If we have minizinc then create a knn graph and run a constraint program to colorize nicely edges = [] X = np.array([[c.x, c.y, c.z] for c in C]) kdtree = cKDTree(X) purkinje_radius = 16 for c in C: distances, neighbors = kdtree.query([c.x, c.y, c.z], n_neighbors) for d,n in zip(distances,neighbors): if d < 3*purkinje_radius and c.index < C[n].index: edges.append([c.index, C[n].index]) if len(edges) < 1000: # otherwise solving the constraint program might be too costly with open(self.savedir+'/'+'/edges.dzn', 'w') as ostream: print('n=%d;' % len(C), file=ostream) print('num_edges=%d;' % len(edges), file=ostream) print('E = array2d(1..num_edges, 1..2, [', file=ostream) print(','.join(map(str, [e[i] for e in edges for i in range(2)])), file=ostream) print(']);', file=ostream) tee.log('Running minizinc on', len(edges), 'edges') curdir = os.getcwd() os.chdir(SHARE_DIR) mzn_cmd = ('minizinc color.mzn -d %s/edges.dzn -o %s/mnz_sol.txt' % (self.savedir, self.savedir)) tee.log(mzn_cmd) os.system(mzn_cmd) os.chdir(curdir) istream = open(self.savedir+'/mnz_sol.txt') colors = {} for line in istream.readlines(): items = line.strip().split() if items[0] == 'c': colors[int(items[1])] = int(items[2]) for c in C: c.hue = colors[c.index]/255.0
def main(args): st = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') tee.log('find_cells.py running on',platform.node(),st) mkdir_p(args.outdir+'/'+args.substack_id) if args.pair_id is None: tee.logto('%s/%s/log.txt' % (args.outdir, args.substack_id)) args.outdir=args.outdir+'/'+args.substack_id else: tee.logto('%s/%s/log_%s.txt' % (args.outdir, args.substack_id, args.pair_id)) args.outdir=args.outdir+'/'+args.substack_id+'/'+args.pair_id mkdir_p(args.outdir) timers = [mscd.pca_analysis_timer, mscd.mean_shift_timer, mscd.ms_timer, mscd.patch_ms_timer] timers.extend([volume.save_vaa3d_timer, volume.save_markers_timer]) timers.extend([threshold.multi_kapur_timer]) for t in timers: t.reset() substack = volume.SubStack(args.indir, args.substack_id) substack.load_volume(pair_id=args.pair_id) if args.local: mscd.pms(substack, args) else: mscd.ms(substack, args) for t in timers: if t.n_calls > 0: tee.log(t) st = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') tee.log('find_cells.py finished on',platform.node(),st)
def load_volume_from_3D(self, batch, convert_to_gray=True, flip=False, ignore_info_files=False, h5filename=None, pair_id=None): """Loads a sequence of images into a stack Parameters ---------- batch : np.array Tensor of shape W,H,D with values between 0 and 1 convert_to_gray : bool Should be set to true if reading from RGB tiff files flip : bool Flip along vertical (Y) axis ignore_info_files : bool If true, don't trust filenames in the info.json file h5filename : str If not none, read from this HDF5 file rather than TIFF files """ self.imgs = [] self.pixels = [] for z in range(batch.shape[0]): data = (batch[z, :, :, ] * 255).astype('uint8') #data = data.squeeze(0) #tee.log(data.shape) img_z = Image.fromarray(data, mode='L') if flip: # when reading a stack saved in vaa3d format Y coordinates are reversed (used by save_substack only) img_z = img_z.transpose(Image.FLIP_TOP_BOTTOM) if convert_to_gray: img_z = img_z.convert('L') self.imgs.append(img_z) self.pixels.append(img_z.load()) tee.log(z + 1, 'images read into stack') pass
def load_volume(self, convert_to_gray=True, flip=False, ignore_info_files=False, h5filename=None, pair_id=None): """Loads a sequence of images into a stack Parameters ---------- convert_to_gray : bool Should be set to true if reading from RGB tiff files flip : bool Flip along vertical (Y) axis ignore_info_files : bool If true, don't trust filenames in the info.json file h5filename : str If not none, read from this HDF5 file rather than TIFF files """ if h5filename is not None: self.load_volume_from_h5(h5filename) return self.imgs = [] self.pixels = [] if pair_id is not None: idir = self.indir + '/' + self.substack_id + '/' + pair_id files = sorted([ idir + '/' + f for f in os.listdir(idir) if f[0] != '.' and valid_suffix(f) ]) elif ignore_info_files: idir = self.indir + '/' + self.substack_id files = sorted([ idir + '/' + f for f in os.listdir(idir) if f[0] != '.' and valid_suffix(f) ]) else: files = [self.indir + '/' + fname for fname in self.info['Files']] if len(files) == 0: raise Exception('No valid files found') for z, image_file in enumerate(files): img_z = Image.open(image_file) if flip: # when reading a stack saved in vaa3d format Y coordinates are reversed (used by save_substack only) img_z = img_z.transpose(Image.FLIP_TOP_BOTTOM) if convert_to_gray: img_z = img_z.convert('L') self.imgs.append(img_z) self.pixels.append(img_z.load()) if z % 25 == 0: tee.log(image_file, end='') else: tee.log('.', end='') tee.log(z, 'images read into stack')
def load_volume(self, convert_to_gray=True, flip=False, ignore_info_files=False, h5filename=None, pair_id=None): """Loads a sequence of images into a stack Parameters ---------- convert_to_gray : bool Should be set to true if reading from RGB tiff files flip : bool Flip along vertical (Y) axis ignore_info_files : bool If true, don't trust filenames in the info.json file h5filename : str If not none, read from this HDF5 file rather than TIFF files """ if h5filename is not None: self.load_volume_from_h5(h5filename) return self.imgs = [] self.pixels = [] if pair_id is not None: idir = self.indir+'/'+self.substack_id+'/'+pair_id files = sorted([idir+'/'+f for f in os.listdir(idir) if f[0] != '.' and valid_suffix(f)]) elif ignore_info_files: idir = self.indir+'/'+self.substack_id files = sorted([idir+'/'+f for f in os.listdir(idir) if f[0] != '.' and valid_suffix(f)]) else: files = [self.indir+'/'+fname for fname in self.info['Files']] if len(files) == 0: raise Exception('No valid files found') for z, image_file in enumerate(files): img_z = Image.open(image_file) if flip: # when reading a stack saved in vaa3d format Y coordinates are reversed (used by save_substack only) img_z = img_z.transpose(Image.FLIP_TOP_BOTTOM) if convert_to_gray: img_z = img_z.convert('L') self.imgs.append(img_z) self.pixels.append(img_z.load()) if z % 25 == 0: tee.log(image_file, end='') else: tee.log('.', end='') tee.log(z, 'images read into stack')
def main(args): st = datetime.datetime.fromtimestamp( time.time()).strftime('%Y-%m-%d %H:%M:%S') tee.log('find_cells.py running on', platform.node(), st) mkdir_p(args.outdir + '/' + args.substack_id) if args.pair_id is None: tee.logto('%s/%s/log.txt' % (args.outdir, args.substack_id)) args.outdir = args.outdir + '/' + args.substack_id else: tee.logto('%s/%s/log_%s.txt' % (args.outdir, args.substack_id, args.pair_id)) args.outdir = args.outdir + '/' + args.substack_id + '/' + args.pair_id mkdir_p(args.outdir) timers = [ mscd.pca_analysis_timer, mscd.mean_shift_timer, mscd.ms_timer, mscd.patch_ms_timer ] timers.extend([volume.save_vaa3d_timer, volume.save_markers_timer]) timers.extend([threshold.multi_kapur_timer]) for t in timers: t.reset() # dovremmo passare le informazinoi su w,h,d tramite pslist substack = volume.SubStack(args.indir, args.substack_id) # ignore_info_files setted to true to avoid errors substack.load_volume(pair_id=args.pair_id, ignore_info_files=True) #todo la nostra versione è questa #substack.load_volume_from_3D() #todo lo script va lanciato per ogni volume if args.local: mscd.pms(substack, args) else: mscd.ms(substack, args) for t in timers: if t.n_calls > 0: tee.log(t) st = datetime.datetime.fromtimestamp( time.time()).strftime('%Y-%m-%d %H:%M:%S') tee.log('find_cells.py finished on', platform.node(), st)
def main(args): st = datetime.datetime.fromtimestamp( time.time()).strftime('%Y-%m-%d %H:%M:%S') tee.log('find_cells.py running on', platform.node(), st) mkdir_p(args.outdir + '/' + args.substack_id) if args.pair_id is None: tee.logto('%s/%s/log.txt' % (args.outdir, args.substack_id)) args.outdir = args.outdir + '/' + args.substack_id else: tee.logto('%s/%s/log_%s.txt' % (args.outdir, args.substack_id, args.pair_id)) args.outdir = args.outdir + '/' + args.substack_id + '/' + args.pair_id mkdir_p(args.outdir) timers = [ mscd.pca_analysis_timer, mscd.mean_shift_timer, mscd.ms_timer, mscd.patch_ms_timer ] timers.extend([volume.save_vaa3d_timer, volume.save_markers_timer]) timers.extend([threshold.multi_kapur_timer]) for t in timers: t.reset() substack = volume.SubStack(args.indir, args.substack_id) substack.load_volume(pair_id=args.pair_id) if args.local: mscd.pms(substack, args) else: mscd.ms(substack, args) for t in timers: if t.n_calls > 0: tee.log(t) st = datetime.datetime.fromtimestamp( time.time()).strftime('%Y-%m-%d %H:%M:%S') tee.log('find_cells.py finished on', platform.node(), st)
mean_precision = np.mean(np.array(precisions)) precisions.append(mean_precision) mean_recall = np.mean(np.array(recalls)) recalls.append(mean_recall) mean_F1 = np.mean(np.array(F1s)) F1s.append(mean_F1) var_precision = np.var(np.array(precisions)) precisions.append(var_precision) var_recall = np.var(np.array(recalls)) recalls.append(var_recall) var_F1 = np.var(np.array(F1s)) F1s.append(var_F1) dict = { 'img_name': img_names, 'precision': precisions, 'recall': recalls, 'F1': F1s } df = pd.DataFrame(dict, columns=['img_name', 'precision', 'recall', 'F1']) df.to_csv(save_path) for t in timers: tee.log(t)