def __loadDm3(name): import dm3_lib as dm3 data = dm3.DM3(name) w = Wave() w.data = data.imagedata w.x = np.arange(0, data.pxsize[0] * w.data.shape[0], data.pxsize[0]) if w.data.ndim >= 2: w.y = np.arange(0, data.pxsize[0] * w.data.shape[1], data.pxsize[0]) w.note = {} try: w.note['unit'] = data.pxsize[1] w.note['specimen'] = data.info['specimen'].decode() w.note['date'] = data.info['acq_date'].decode() w.note['mag'] = float(data.info['mag'].decode()) w.note['time'] = data.info['acq_time'].decode() w.note['voltage'] = float(data.info['hv'].decode()) w.note['mode'] = data.info['mode'].decode() w.note['exposure'] = data.tags[ 'root.ImageList.1.ImageTags.DataBar.Exposure Time (s)'] w.note['hbin'] = data.tags[ 'root.ImageList.1.ImageTags.Acquisition.Parameters.Detector.hbin'] w.note['vbin'] = data.tags[ 'root.ImageList.1.ImageTags.Acquisition.Parameters.Detector.vbin'] except: pass return w
def load_files(file_path_holo_1, file_path_holo_2, file_path_holo_ref, em_data): data_1 = dm3_lib.DM3(file_path_holo_1) data_2 = dm3_lib.DM3(file_path_holo_2) data_ref = dm3_lib.DM3(file_path_holo_ref) em_data.holo_1 = data_1.imagedata em_data.holo_2 = data_2.imagedata em_data.holo_ref = data_ref.imagedata if data_1.pxsize[1].decode("ascii") == 'nm': em_data.pixel = data_1.pxsize[0] * 10**(-9) else: em_data.pixel = data_1.pxsize[0] * 10**(-9) print("Improper pixel unit, forced to be nm") # Test files '''em_data.holo_1 = np.loadtxt("/media/alex/Work/PhD/Research/Holography EM/Experiments/2018-04-30 - Holo Isobel T8-1/"
def dm3_extract(filepath): """ DM3 data is assumed to be an image unless stated in the session Inputs: filepath: string, location of data on drive Outputs: image_data: array, """ # Read in the data as a numpy array dm3_file = dm3.DM3(filepath) image_data = dm3_file.imagedata print(image_data.shape) # Due to the lack of a universal schema for metatags at the current time # we leave it to the user to proscribe the relevant calibrations return image_data
def load_files(file_path_smh, file_path_ic, datastruct): """Load the files by asking the controller the filepaths smh and ic provided by the user and store the appropriate data in the data structure object (dictionary). Before storing, the data are verified""" dm3_meta_smh = dm3_lib.DM3(file_path_smh) dm3_meta_ic = dm3_lib.DM3(file_path_ic) verify_i(dm3_meta_smh.imagedata, dm3_meta_ic.imagedata) pixel_smh = dm3_meta_smh.pxsize pixel_ic = dm3_meta_ic.pxsize verify_p(pixel_smh[0], pixel_ic[0]) verify_p_unit(pixel_smh[1].decode("ascii"), pixel_ic[1].decode("ascii")) data.SMGData.store(datastruct, 'ISMHexp', dm3_meta_smh.imagedata) data.SMGData.store(datastruct, 'p', pixel_smh[0]) data.SMGData.store(datastruct, 'ICref', dm3_meta_ic.imagedata) data.SMGData.store(datastruct, 'pref', pixel_ic[0]) print('Files loaded') print('Pixel size SMH: ', pixel_smh[0], 'nm') print('Pixel size Reference: ', pixel_ic[0], 'nm')
def _valid_dm3(filepath): """ Check the DM3 file is valid by opening it. :param filepath: The path to the TIFF file. :type filepath: str """ try: dm3.DM3(filepath) return True except struct.error: return False
def _dm3_extractor(file): """ Extract metadata and image data from a DM3 file. :param file: The the DM3 file. :type file: The file-like object. :return A tuple contain the metadata and image data. """ dm3_file = dm3.DM3(file) info = {k: v.decode('utf8') for k, v in dm3_file.info.items()} image = Image.fromarray(dm3_file.imagedata) image_data = tobytes(image) return (info, image_data)
def BrowseFolder(self): self.imagePath_content, _ = QFileDialog.getOpenFileName( self, "open", "/home/", "All Files (*);; Image Files (*.png *.tif *.jpg *.ser *.dm3)") if self.imagePath_content: self.imagePath.setText(self.imagePath_content) file_name = self.imagePath_content.split('/')[-1] suffix = '.' + file_name.split('.')[-1] if suffix == '.ser': import serReader ser_data = serReader.serReader(self.imagePath_content) ser_array = np.array(ser_data['imageData'], dtype='float64') self.imarray_original = ser_array ser_array = (map01(ser_array) * 255).astype('uint8') self.ori_image = Image.fromarray(ser_array, 'L') else: if suffix == '.dm3': import dm3_lib as dm3 data = dm3.DM3(self.imagePath_content).imagedata self.imarray_original = np.array(data) data = np.array(data, dtype='float64') data = (map01(data) * 255).astype('uint8') self.ori_image = Image.fromarray(data, mode='L') else: if suffix == '.tif': im = Image.open(self.imagePath_content).convert('L') self.imarray_original = np.array(im, dtype='float64') self.ori_image = Image.fromarray( (map01(self.imarray_original) * 255).astype('uint8'), mode='L') else: self.ori_image = Image.open( self.imagePath_content).convert('L') self.imarray_original = np.array(self.ori_image) self.width, self.height = self.ori_image.size pix_image = PIL2Pixmap(self.ori_image) pix_image.scaled(self.ori.size(), QtCore.Qt.KeepAspectRatio) self.ori.setPixmap(pix_image) self.ori.show() self.ori_content = self.ori_image
def loadImage(self, image_path): image_type = os.path.splitext(image_path)[-1][1:] if image_type == 'dm3': import dm3_lib as dm3 dm3f = dm3.DM3(image_path) image_data = dm3f.imagedata params = { 'title': 'Image', 'xlabel': 'x_direction', 'ylabel': 'y_direction', 'vmin': 0, 'vmax': np.max(image_data) } # plot_tools.plot_2d(image_data, params) elif image_type == 'mrc': import mrcfile with mrcfile.open(image_path) as mrc: image_data = mrc.data params = None plot_tools.plot_2d(image_data, params) return image_data
# parse command line arguments args = parser.parse_args() if args.verbose: debug = 1 filepath = args.file # get filename filename = os.path.split(filepath)[1] fileref = os.path.splitext(filename)[0] # pyplot interactive mode plt.ion() plt.close('all') # parse DM3 file dm3f = dm3.DM3(filepath, debug=debug) # get some useful tag data and print print("file:", dm3f.filename) print("file info.:") print(dm3f.info) print("scale: %.3g %s/px" % dm3f.pxsize) cuts = dm3f.cuts print("cuts:", cuts) # dump image Tags in txt file if args.dump: dm3f.dumpTags(savedir) # get image data aa = dm3f.imagedata
def test_dm3_stem_acquire(passive_acquisition_server, tmpdir, mock_dm3_tiltseries_writer): id = 1234 angle_regex = r'.*_([n,p]{1}[\d,\.]+)degree.*\.dm3' request = jsonrpc_message({ 'id': id, 'method': 'connect', 'params': { 'path': tmpdir.strpath, 'fileNameRegex': angle_regex, 'fileNameRegexGroups': ['angle'], 'groupRegexSubstitutions': { 'angle': [{ 'n': '-', 'p': '+' }] } } }) response = requests.post(passive_acquisition_server.url, json=request) assert response.status_code == 200, response.content request = jsonrpc_message({ 'id': id, 'method': 'stem_acquire' }) tilt_series = [] tilt_series_metadata = [] for _ in range(0, 1000): response = requests.post(passive_acquisition_server.url, json=request) assert response.status_code == 200, response.content # No images left to fetch result = response.json()['result'] if result is None: continue url = result['imageUrl'] response = requests.get(url) tilt_series.append(response.content) if 'meta' in result: tilt_series_metadata.append(result['meta']) if len(tilt_series) == mock_dm3_tiltseries_writer.series_size: break # make sure we got all the images assert len(tilt_series) == mock_dm3_tiltseries_writer.series_size # Now check we got the write images for (i, (filename, fp)) in enumerate(test_dm3_tilt_series()): dm3_file = dm3.DM3(fp) expected_metadata \ = {k: v.decode('utf8') for k, v in dm3_file.info.items()} expected_metadata['fileName'] = filename angle = re.match(angle_regex, filename).group(1) angle = angle.replace('n', '-') angle = angle.replace('p', '+') expected_metadata['angle'] = angle assert tilt_series_metadata[i] == expected_metadata md5 = hashlib.md5() md5.update(tilt_series[i]) image = Image.fromarray(dm3_file.imagedata) tiff_image_data = tobytes(image) expected_md5 = hashlib.md5() expected_md5.update(tiff_image_data) assert md5.hexdigest() == expected_md5.hexdigest()
cavc = np.average(c_old, axis=0) c_pred = cavc / sum(cavc) pass # Run the minimizer results = minimize(partial(regressLL, s, yObs), c_pred, method='SLSQP', bounds=bnds, constraints=cons, jac=False) results = results.x results = np.asarray(results) c_new = results.reshape(int(len(results) / nb_c), nb_c) return c_new, c_pred dm3f = dm3.DM3("data8.dm3") x = dm3f.imagedata nb_c = 3 # nombre de composantes nb_iter = 1 # nombre d'itérations [c, s ] = HyperspectralSegmentationMCR_LLM.mcr_llm(x, nb_c, nb_iter) # appel de l'algorithme plt.contourf(c[:, 0, :])
def TestOnDataset(image_dir, save_dir, model_path, cuda=True): filepath = image_dir savepath = save_dir file_name = filepath.split('/')[-1] suffix = '.' + file_name.split('.')[-1] if suffix == '.ser': import serReader ser_data = serReader.serReader(filepath) ser_array = np.array(ser_data['imageData'], dtype='float64') ser_array = (map01(ser_array) * 255).astype('uint8') ori_img = Image.fromarray(ser_array, 'L') savepath = savepath.replace('.ser', '.png') else: if suffix == '.dm3': import dm3_lib as dm3 data = dm3.DM3(filepath).imagedata data = np.array(data, dtype='float64') data = (map01(data) * 255).astype('uint8') ori_img = Image.fromarray(data, mode='L') savepath = savepath.replace('.dm3', '.png') else: if suffix == '.tif': im = Image.open(filepath).convert('L') imarray_original = np.array(im, dtype='float64') ori_img = Image.fromarray( (map01(imarray_original) * 255).astype('uint8'), mode='L') savepath = savepath.replace('.tif', '.png') else: ori_img = Image.open(filepath).convert('L') #ori_img = Image.open(filepath).convert('L') width, height = ori_img.size overlap = 2 # if an image is larger than 1024x1024, then the image will be cut to 4 parts. # this parameter represents the width of the overlap region between them. # divide the image into four parts if width > 1024 or height > 1024: crop_size = width // 2 new_img = [ ori_img.crop((0, 0, crop_size + overlap, crop_size + overlap)), ori_img.crop((crop_size - overlap, 0, width, crop_size + overlap)), ori_img.crop( (0, crop_size - overlap, crop_size + overlap, height)), ori_img.crop( (crop_size - overlap, crop_size - overlap, width, height)) ] else: new_img = [ori_img] im_outs = [] # a flag that denotes whether this image needs padding or not. # an image or a region of an image needs padding process # if the width or height can't be divided by 4. padding_flag = False for i in range(0, len(new_img)): temp_img = new_img[i] transform = ToTensor() test = transform(temp_img) ori_height = test.size()[1] ori_width = test.size()[2] if test.size()[1] % 4 or test.size()[2] % 4: new_height = np.ceil( float(test.size()[1]) / 4 ) * 4 # force the new one's height and width can be divided by 4 new_width = np.ceil(float(test.size()[2]) / 4) * 4 temp = torch.FloatTensor(1, int(new_height), int(new_width)).zero_() temp[:, 0:test.size()[1], 0:test.size()[2]] = test test = temp padding_flag = True # this function is used to loading model and return the output output = load_model(test, model_path, cuda) if padding_flag: output = output[:, :, 0:ori_height, 0:ori_width] padding_flag = False for_save = output[0, :, :, :] im_outs.append(for_save) to_pil = ToPILImage() # this part is for concatenating overlap regions and all parts of images if len(im_outs) > 1: im_save = Image.new('L', (width, height)) crop_size = width // 2 if overlap: overlap_region1 = torch.max(torch.cat([ im_outs[0][:, :-2 * overlap, -2 * overlap:], im_outs[1][:, :-2 * overlap, :2 * overlap] ], 0), 0, keepdim=True) overlap_region2 = torch.max(torch.cat([ im_outs[1][:, -2 * overlap:, 2 * overlap:], im_outs[3][:, :2 * overlap, 2 * overlap:] ], 0), 0, keepdim=True) overlap_region3 = torch.max(torch.cat([ im_outs[2][:, 2 * overlap:, -2 * overlap:], im_outs[3][:, 2 * overlap:, :2 * overlap] ], 0), 0, keepdim=True) overlap_region4 = torch.max(torch.cat([ im_outs[0][:, -2 * overlap:, :-2 * overlap], im_outs[2][:, :2 * overlap, :-2 * overlap] ], 0), 0, keepdim=True) overlap_region_c = torch.max(torch.cat([ im_outs[0][:, -2 * overlap:, -2 * overlap:], im_outs[1][:, -2 * overlap:, :2 * overlap], im_outs[2][:, :2 * overlap, :2 * overlap], im_outs[3][:, :2 * overlap, -2 * overlap:] ], 0), 0, keepdim=True) im_outs[0][:, :-2 * overlap, -2 * overlap:] = overlap_region1[0] im_outs[0][:, -2 * overlap:, :-2 * overlap] = overlap_region4[0] im_outs[0][:, -2 * overlap:, -2 * overlap:] = overlap_region_c[0] im_outs[1][:, -2 * overlap:, 2 * overlap:] = overlap_region2[0] im_outs[2][:, 2 * overlap:, -2 * overlap:] = overlap_region3[0] if cuda: imout_pil = to_pil(im_outs[0].cpu().data).convert('L') else: imout_pil = to_pil(im_outs[0].data).convert('L') im_save.paste(imout_pil, (0, 0)) if cuda: imout_pil = to_pil(im_outs[1][:, :, 2 * overlap:].cpu().data).convert('L') else: imout_pil = to_pil(im_outs[1][:, :, 2 * overlap:].data).convert('L') im_save.paste(imout_pil, (crop_size + overlap, 0)) if cuda: imout_pil = to_pil(im_outs[2][:, 2 * overlap:, :].cpu().data).convert('L') else: imout_pil = to_pil(im_outs[2][:, 2 * overlap:, :].data).convert('L') im_save.paste(imout_pil, (0, crop_size + overlap)) if cuda: imout_pil = to_pil(im_outs[3][:, 2 * overlap:, 2 * overlap:].cpu().data).convert('L') else: imout_pil = to_pil(im_outs[3][:, 2 * overlap:, 2 * overlap:].data).convert('L') im_save.paste(imout_pil, (crop_size + overlap, crop_size + overlap)) else: if cuda: im_save = to_pil(im_outs[0].cpu().data).convert('L') else: im_save = to_pil(im_outs[0].data).convert('L') # this part is for saving results. # the default result is saving both original image and result in one image. # if only the result is needed, then modifications can be done here. # ori_img is the original one and im_save is the final result. cat_image = Image.new('L', (width * 2, height)) cat_image.paste(ori_img, (0, 0)) cat_image.paste(im_save, (width, 0)) cat_image.save(savepath)
def import_EELS_dm3(filename): data = DM3.DM3(filename) # data = dm.dmReader(filename) tags = make_dict_from_tags(data._storedTags) imagedata = np.transpose(data.imagedata, axes=(1, 2, 0)) return imagedata, tags
def cuts(self): """Returns display range (cuts).""" return dm3.DM3(self.filename).cuts
def info(self): return dm3.DM3(self.filename).info
def outputcharset(self): return dm3.DM3(self.filename).outputcharset
def imagedata(self): """Extracts image data as numpy.array""" return dm3.DM3(self.filename).imagedata
def makePNGThumbnail(self, tn_file=''): """Save thumbnail as PNG file.""" return dm3.DM3(self.filename).makePNGThumbnail(tn_file=tn_file)
def thumbnaildata(self): """Returns thumbnail data as numpy.array""" return dm3.DM3(self.filename).thumbnaildata
def thumbnail(self): """Returns thumbnail as PIL Image.""" return dm3.DM3(self.filename).thumbnail
except np.linalg.LinAlgError: cavc = np.average(c_old, axis=0) c_pred = cavc / sum(cavc) pass # Run the minimizer results = minimize(partial(regressLL, s, yObs), c_pred, method='SLSQP', bounds=bnds, constraints=cons, jac=False) results = results.x results = np.asarray(results) c_new = results.reshape(int(len(results) / nb_c), nb_c) return c_new, c_pred dm3f = dm3.DM3("EELS Spectrum Image (dark ref corrected).dm3") x = dm3f.imagedata nb_c = 3 # nombre de composantes nb_iter = 25 # nombre d'itérations [c, s] = HyperspectralSegmentationMCR_LLM.mcr_llm3d( x, nb_c, nb_iter) # appel de l'algorithme matplotlib.pyplot.plot(c)
def pxsize(self): """Returns pixel size and unit.""" return dm3.DM3(self.filename).pxsize
def image(self): """Extracts image data as PIL Image""" return dm3.DM3(self.filename).image
def tags(self): """Returns all image Tags.""" return dm3.DM3(self.filename).tags