def divide_certain(slide_path: str, out_dir: str, logger, width=96) -> None: """The origin slide is too large, the function can segment the large one into small tiles. In this project, we set the height equals to the width. It aims to tile images in 10X power using less resource as possibel""" slide, level, base10, dimension_ref, d_step, size = divide_prepare( slide_path, width, logger) # begin segment tiles cwp = os.getcwd() case_name = get_name(slide_path) out_path = os.path.join(cwp, out_dir, case_name) os.makedirs(out_path, exist_ok=True) # set start points of tiles height = width x_bound = dimension_ref[0] - width y_bound = dimension_ref[1] - height for x in tqdm(range(0, x_bound, d_step)): for y in range(0, y_bound, d_step): loc = (x, y) # print(loc, level, size) small_image = slide.read_region(location=loc, level=level, size=size) if is_useless(small_image): continue if not base10: small_image = small_image.resize((width, height)) fp = os.path.join(out_path, '{}-{}-{}-{}.tiff'.format(x, y, width, height)) small_image.save(fp)
def __init__(self,filename,dirname): self.filename=tools.get_name(filename) self.variables={} self.dirname=dirname self.comments={} self.order_variables=[]
def __init__(self, filename, dirname): self.filename = tools.get_name(filename) self.variables = {} self.dirname = dirname self.comments = {} self.order_variables = []
def combined_graph(scores, episode_numbers, name, coordinates=None, linears=None, scatter=False): """ method prints point graph and interpolation graph with gaussian filter of learning progress """ if linears is not None: for key, value in linears.items(): plt.plot([0, episode_numbers[-1]], [key, value], 'k-', linewidth=0.8) if scatter: plt.plot(episode_numbers, scores, 'ro', color='goldenrod', markersize=1) score_gf = gaussian_filter(scores, sigma=0.01791*episode_numbers[-1]) plt.plot(episode_numbers, score_gf, color='teal', linewidth=1) plt.ylabel("Score") plt.xlabel("Episode") plt.xlim([0,coordinates[0]]) if min(scores) < 0: plt.ylim([min(scores),coordinates[1]]) else: plt.ylim([0,coordinates[1]]) name = get_name(name) plt.savefig("./{}" .format(name), bbox_inches='tight') plt.clf() print("[Graph of learning progress visualization was saved to \"./{}\".]" .format(name))
def test_get_name(self): """ test for get_name """ expected = "4-Hydroxybenzaldehyde" Cnumber = "C00002657" actual = tools.get_name(Cnumber) time.sleep(2) self.assertEqual(actual, expected)
def visualize_model(model, plot_mdl=[True, False]): """ method prints model to stdout and pdf """ if plot_mdl[0]: model.summary() if plot_mdl[1]: name = get_name("model") plot_model(model, to_file=name, show_shapes=True, show_layer_names=False)
def batch_tiling(path, out_dir, logger): filter_func = None # filter_func = lambda x:get_name(x) not in cache slides = list(filter(filter_func, get_files(path))) for slide_path in slides: name = get_name(slide_path) try: logger.info(f'start {name}') divide(slide_path, out_dir) except: logger.exception(f'{name} encountered error in batch')
def eval_using_area_under_curve(generation_directory): # get the name and the area under curve for each child in this directory areas = [] for dir in [x for x in generation_directory.iterdir() if x.is_dir()]: area_file = dir / 'auc.txt' area_val = float(area_file.read_text()) child_name = get_name(dir) dna_file = dir / 'dna.dna' dna = dna_file.read_text() areas.append( (area_val , dna , child_name)) areas.sort(key=lambda x: x[0]) generate_generation_report(generation_directory , areas) return areas[0][1] , areas[1][1]
def divide(slide_path: str, out_dir: str, level=0, width_rel=96, mag=10) -> None: """The origin slide is too large, the function can segment the large one into small tiles. In this project, we set the height equals to the width. Slide_path: the path of the target slide; level: varying definition of images, 0 is the largest, int; width_rel: the width and the length of output images, int. mag: the magnitude or the object power, float""" # Read slide. level 0 is mag X40 or X20. # here we downsize ((level 0 's mag / mag)**2) times large_image = openslide.OpenSlide(slide_path) ori_mag = int(large_image.properties.get('openslide.objective-power')) time = ori_mag / mag tile = int(width_rel * time) # get reference and target location, use a reference level instead of the maxiumn power level may make reduce the cose of resize dimensions = large_image.level_dimensions dimension_ref = dimensions[0] dimension = dimensions[level] ratio = dimension[0] / dimension_ref[0] # begin segment tiles case_name = get_name(slide_path) # print(case_name) out_path = os.path.join(out_dir, case_name) os.makedirs(out_path, exist_ok=True) # calculate individual size height_rel = width_rel width = int(tile * ratio) height = int(width) size = (width, height) x_bound = dimension_ref[0] - width y_bound = dimension_ref[1] - height for x in tqdm(range(0, x_bound, tile)): for y in (range(0, y_bound, tile)): # locate start point loc = (x, y) # get the small image small_image = large_image.read_region(location=loc, level=level, size=size) # filter the useless image if is_useless(small_image): continue # save the small image resize_image = small_image.resize((width_rel, height_rel)) fp = os.path.join(out_path, '{}-{}-{}-{}.tiff'.format(x, y, width, height)) resize_image.save(fp)
def heat_map(array, graph_name, axes): """ method creates heatmap """ fig, ax = plt.subplots() im = ax.imshow(array, cmap=cm.YlOrRd) if not axes: im.axes.get_yaxis().set_visible(False) im.axes.get_xaxis().set_visible(False) fig.tight_layout() graph_name = get_name(graph_name) plt.savefig("./{}".format(graph_name), bbox_inches='tight') print("[Heatmap was made.]")
def visualize_model(self, model): """ method prints model to stdout and pdf """ if not self.plotted: if self.plot_mdl[0]: model.summary() if self.plot_mdl[1]: name = get_name("model") plot_model(model, to_file=name, show_shapes=True, show_layer_names=False) print("[PDF blueprint of neural network was saved.]") self.plotted = True
def import_settings(self, filename): comment = "" f = open(filename) for line in f: line = line.strip() if (line[0] == "#"): comment = comment + line continue line = line.split('=') self.variables[line[0]] = line[1] self.order_variables.append(line[0]) self.comments[line[0]] = comment comment = "" filename = tools.get_name(filename) self.filename = filename f.close()
def import_settings(self,filename): comment="" f=open(filename) for line in f : line=line.strip() if (line[0] == "#") : comment=comment+line continue line=line.split('=') self.variables[line[0]]=line[1] self.order_variables.append(line[0]) self.comments[line[0]]=comment comment="" filename=tools.get_name(filename) self.filename=filename f.close()
def set_name(self, flat): name = get_name(flat) self.name_line.setText(str(name))
def convert(img_p: str, out_path: str) -> None: img = cv2.imread(img_p) name = get_name(img_p) cv2.imwrite(os.path.join(out_path, f'{name}.tiff'), img)
def set_name(self, flat): name = get_name(flat) self.name_value.setText(str(name)) if self.save_button is not None: self.save_button.setEnabled(True)