def SharedPreprocess(self, params, comm_title): print('Annotator folder is being generated for', comm_title) targ = Params() targ.SetUserInfoAnnotator(params['Empty Folder for Annotator']) if os.path.isdir(targ.surfaces_path) == False: os.makedirs(targ.surfaces_path, exist_ok=True) os.makedirs(targ.surfaces_whole_path, exist_ok=True) if os.path.isdir(targ.skeletons_path) == False: os.makedirs(targ.skeletons_path, exist_ok=True) os.makedirs(targ.skeletons_whole_path, exist_ok=True) if os.path.isdir(targ.volume_path) == False: os.makedirs(targ.volume_path, exist_ok=True) if os.path.isdir(targ.paint_path) == False: os.makedirs(targ.paint_path, exist_ok=True) return targ
def AnalyzeAnnotFile(self, params): targ = Params() targ.SetUserInfoAnnotator(params['Empty Folder for Annotator']) surface_path = targ.surfaces_whole_path paint_path = targ.paint_path whole_mesh_filenames = glob.glob(os.path.join(surface_path, "*.stl")) ids_volumes = {} ph = params['Pitch in X (um)'] pw = params['Pitch in Y (um)'] pz = params['Pitch in Z (um)'] ## with h5py.File(targ.volume_file, 'r') as f: ids_volume = f['volume'][()] ids_volume = (ids_volume > 0).astype(np.int) new_labels = np.zeros_like(ids_volume).astype(np.int) ## ## Load surface meshes id = 1 whole_mesh_filename = os.path.join(surface_path, str(id).zfill(10) + ".stl") whole_mesh = trimesh.load(whole_mesh_filename) surf_vertices = whole_mesh.vertices surf_faces = whole_mesh.faces surf_vertices[:, 0] /= pw surf_vertices[:, 1] /= ph surf_vertices[:, 2] /= pz pitch = 1 whole_mesh_name_wo_ext = os.path.splitext( os.path.basename(whole_mesh_filename))[0] part_mesh_name_wildcard = os.path.normpath( os.path.join(paint_path, whole_mesh_name_wo_ext + "-*.pickle")) part_mesh_filenames = glob.glob(part_mesh_name_wildcard) ## Check whether painted meshes if part_mesh_filenames == []: return False ids = [] for part_mesh_filename in part_mesh_filenames: with open(part_mesh_filename, 'rb') as file: data = pickle.load(file) closed_mesh = self.GetClosedTrimesh(surf_vertices, surf_faces, data['painted']) if closed_mesh.volume is None: continue ### id = os.path.basename(part_mesh_filename) id = os.path.splitext(id)[0] id = int(id.split('-')[1]) print('ID: ', id, ', Volume:', closed_mesh.volume) ids.append(id) #filename = os.path.join(targ.paint_path, str(id).zfill(4)+'.stl') #closed_mesh.export(file_obj=filename) #continue ### part_faces = closed_mesh.faces part_verts = closed_mesh.vertices unique_ids_verts = np.unique(np.ravel(part_faces)) unique_verts = part_verts[unique_ids_verts] wmin = np.floor(np.min(unique_verts[:, 0])).astype(int) hmin = np.floor(np.min(unique_verts[:, 1])).astype(int) zmin = np.floor(np.min(unique_verts[:, 2])).astype(int) wmax = np.floor(np.max(unique_verts[:, 0])).astype(int) hmax = np.floor(np.max(unique_verts[:, 1])).astype(int) zmax = np.floor(np.max(unique_verts[:, 2])).astype(int) print('wmin, wmax, wdiff: ', wmin, wmax, wmax - wmin) print('hmin, hmax, hdiff: ', hmin, hmax, hmax - hmin) print('zmin, zmax, zdiff: ', zmin, zmax, zmax - zmin) ## Trimesh # v = self.GetVolumeTrimesh(closed_mesh) ## PyVista v = self.GetVolumePyVista(part_verts, part_faces, wmin, hmin, zmin, wmax, hmax, zmax) #print('dir(v) : ', dir(v)) #print('v.keys(): ', v.keys()) #print('v[12]: ', v[12]) wnum = v.shape[0] hnum = v.shape[1] znum = v.shape[2] wmin += 1 hmin += 1 zmin += 1 print('wnum, hnum, znum : ', wnum, hnum, znum) new_labels[wmin:wmin + wnum, hmin:hmin + hnum, zmin:zmin + znum] += v.astype(np.int) * id #### #### Dilution, clarification, etc #### new_labels_processed = np.zeros_like(ids_volume) for id in ids: ## Pickup the target area print('Dilution, id: ', id) target_area = morphology.binary_dilation(new_labels == id, selem=morphology.ball(1), out=None).astype(np.int) ## Pickup segmented areas labels_to_pickup_segmentation = morphology.label( ids_volume * (target_area == 0)) us, counts = np.unique(labels_to_pickup_segmentation, return_counts=True) #print('us : ', us) #print('segmentation count: ', counts) segmented_us = us[counts < 30] #print('segmented us : ', segmented_us) segments = np.zeros_like(ids_volume) for segmented_u in segmented_us: segments += ( labels_to_pickup_segmentation == segmented_u).astype( np.int) ## Merge target area with segmented areas if they are connected. target_plus_segment = target_area * ids_volume + segments labels_to_remove_segmented_target = morphology.label( target_plus_segment > 0) u, counts = np.unique(labels_to_remove_segmented_target, return_counts=True) labels_segmented_target_removed = ( labels_to_remove_segmented_target == u[counts.argsort()[-2]] ).astype(np.int) ## Assign the id to (target area in cytosol) and (segmented area). new_labels_processed += labels_segmented_target_removed * id with h5py.File('labels.hdf5', 'w') as f: f.create_dataset('dendrite', data=new_labels_processed) with h5py.File('labeled_cytosol.hdf5', 'w') as f: f.create_dataset('dendrite', data=new_labels_processed + ids_volume) return ids_volume, new_labels