Example #1
0
def processDM3s():
    """This function calls to the user to give a directory full of unprocessed dm3s.
    Note if these dm3s have been processed before this function will overwrite
    previously created DM3s. All DM3s will be converted to pngs and cleaned of
    spurious xray peaks"""
    fname = input('paste directory path of directory containing experiment directories or type q to quit: ',)
    new_directory = input('paste directory path where you want pngs to end up or type q to quit')
    if fname == 'q' or new_directory == 'q':
        sys.exit()
    dir_list = glob(fname+'/*')
    i = 0
    for d1 in dir_list:
        dir_list2 = glob(d1+'/*')
        for d2 in dir_list2:
            for dm3 in glob(d2+'/*.dm3'):
                img = dm.dmReader(dm3)['data']
                if img.shape[0] < 1024:
                    pass
                elif img.shape[0] == 2048 and img.shape[1] == 2048:
                    img = transform.resize(img,(1024,1024))
                    name = dm3.split('/')[-1].split('.')[0]
                    name = d2.split('/')[-2].split('_')[0] + '_' + name
                    imsave(new_directory+'/'+name+'.png', xray_correct(img), format="png", cmap=cm.gray)
                elif img.shape[0] == 1024 and img.shape[1] == 1024:
                    name = dm3.split('/')[-1].split('.')[0]
                    name = d2.split('/')[-2].split('_')[0] + '_' + name
                    imsave(new_directory+'/'+name+'.png', xray_correct(img), format="png", cmap=cm.gray)
        i += 1
        print('{} / {} directories complete'.format(i,len(dir_list)))
    print('done!')
Example #2
0
def plot_dm3_fromdir(directory,barSize,loc,filt = False,setScale = False):
    dm3s = glob(directory+'/*.dm3')
    labels = [dm.dmReader(file)['filename'] for file in dm3s]
    scales = [dm.dmReader(file)['pixelSize'] for file in dm3s]
    units = [dm.dmReader(file)['pixelUnit'] for file in dm3s]
    pics = [dm.dmReader(file)['data'] for file in dm3s]
    if setScale == True:
        print('scale of first image is {} {}'.format(scales[0],units[0]))
        barSize = float(input('What should length of scale bar be: '))
        print('Size of first image is {}'.format(pics[0].shape))
        locY = float(input('Y val here should scale bar be (in pixels): '))
        locX = float(input('X val here should scale bar be (in pixels): '))
        loc = (locY,locX)
    for idx in np.arange(0,len(pics)):
        plot_dm3(pics,scales,labels,units,idx,barSize,loc,filt)
    print('Done!')
Example #3
0
    def CHANGE_appendAll(self):
        """Append stored files for each new element"""
        # Separate new files to be loaded
        FoI = list(set(self.listen_files) - set(self.log_files))
        FoI.sort(key=lambda x: x[:3])
        for file in FoI:
            if self.rank == 0: print("Loading {}".format(file))
            file_path = "{}/{}".format(self.listen_dir, file)

            try:
                dmRef = dm.dmReader(file_path)
                newProj = self.center_of_mass_align(
                    self.background_subtract(dmRef['data']))
                newAngle = self.acquireProjAngle(file_path)

                self.log_tilts = np.append(self.log_tilts, newAngle)
                self.log_files = np.append(self.log_files, file)

                # Account for Python's disdain for AxAx1 arrays (compresses to 2D)
                if (len(self.log_projs) == 0):
                    dataDim = np.shape(newProj)
                    self.log_projs = np.zeros([dataDim[0], dataDim[1], 1])
                    self.log_projs[:, :, 0] = newProj
                else:
                    self.log_projs = np.dstack((self.log_projs, newProj))

            except:
                if self.rank == 0:
                    print(
                        'Could not read : {}, will preceed with reconstruction and re-download on next pass'
                        .format(file))
                break

        if self.rank == 0: self.CHANGE_saveAll()
Example #4
0
def adjust_pipeline(directory_list):
    i = 0
    for directory in directory_list:
        try:
            os.mkdir(directory + '/adjustedPNG2')
            new_directory = directory + '/adjustedPNG2'
        except:
            new_directory = directory + '/adjustedPNG2'
        dm3s = glob(directory + '/*.dm3')
        for dm3 in dm3s:
            img = dm.dmReader(dm3)['data']
            if img.shape[0] < 1024:
                pass
            elif img.shape[0] == 2048 and img.shape[1] == 2048:
                img = transform.resize(xray_correct(img), (1024, 1024),
                                       anti_aliasing=True)
                name = dm3.split('/')[-1].split('.')[0]
                imsave(new_directory + '/' + name + '.png',
                       img,
                       format="png",
                       cmap=cm.gray)
            elif img.shape[0] == 1024 and img.shape[1] == 1024:
                name = dm3.split('/')[-1].split('.')[0]
                imsave(new_directory + '/' + name + '.png',
                       exposure.equalize_adapthist(xray_correct(img)),
                       format="png",
                       cmap=cm.gray)
        i += 1
        print('{} / {} directories complete'.format(i, len(directory_list)))
    print('done!')
Example #5
0
def read_stack(filepath):
    """Read dm3 stack file and get image information."""
    data = dm.dmReader(filepath)
    x_span = data['pixelSize'][1] * len(data['data'][1])
    y_span = data['pixelSize'][2] * len(data['data'][2])
    data['label'] = filepath.split('/')[-1].split('.dm3')[0]
    data['span'] = (0, x_span, 0, y_span)
    return data
Example #6
0
def main():
    setupOptions = setupOptionsUI()
    dmData = dm.dmReader(setupOptions.imageFilePath)
    startPoint = (20, 20)
    endPoint = (1000, 2000)
    fig, axs = plt.subplots(figsize=(8, 8), nrows=1, ncols=2)
    fig.canvas.set_window_title(getNakedNameFromFilePath(setupOptions.imageFilePath))
    axs[1].set_xlabel('Reciprocal Distance (1/nm)')
    if setupOptions.useLogData:
        axs[1].set_ylabel('Log(Intensity)')
    else:
        axs[1].set_ylabel('Intensity')

    nonlogData = dmData['data']+abs(np.min(dmData['data']))
    if setupOptions.useLogData:
        plotData = np.log10(nonlogData)
    else:
        plotData = nonlogData
    plotData[plotData == -np.inf] = np.median(plotData)

    centerRow, centerCol = np.unravel_index(np.argmax(plotData, axis=None), plotData.shape)
    centerCoord = (centerCol, centerRow)

    if setupOptions.useCenteredLine:
        print("Maximum xy Center Coord:", centerCoord)

        subArrayHalfWidth = 40
        fitArrayStartRow = centerRow - subArrayHalfWidth
        fitArrayEndRow = centerRow + subArrayHalfWidth
        fitArrayStartCol = centerCol - subArrayHalfWidth
        fitArrayEndCol = centerCol + subArrayHalfWidth
        if setupOptions.useLogData:
            fitArray = np.exp(plotData[fitArrayStartRow:fitArrayEndRow, fitArrayStartCol:fitArrayEndCol])
        else:
            fitArray = plotData[fitArrayStartRow:fitArrayEndRow, fitArrayStartCol:fitArrayEndCol]
        x, y = np.meshgrid(np.linspace(fitArrayStartCol, fitArrayEndCol-1, abs(fitArrayEndCol-fitArrayStartCol)), np.linspace(fitArrayStartRow, fitArrayEndRow-1, abs(fitArrayEndRow-fitArrayStartRow)))

        initial = Parameters()
        initial.add("height", value=np.max(plotData)/3)
        initial.add("centroid_x", value=centerCol)
        initial.add("centroid_y", value=centerRow)
        initial.add("sigma_x", value=10.)
        initial.add("sigma_y", value=10.)
        initial.add("background", value=np.mean(plotData))
        fit = minimize(residuals, initial, args=(x, y, fitArray))
        centerCoord = (fit.params['centroid_x'].value, fit.params['centroid_y'].value)
        print("Fit xy Center Coord:", centerCoord)

    axs[0].imshow(plotData, interpolation='none', origin='lower')
    pixelScale = dmData['pixelSize'][0]  # in 1/nm
    p = LineInteractor(fig, axs[0], axs[1], plotData, startPoint, endPoint, pixelScale, setupOptions, centerCoord)
    plt.subplots_adjust(bottom=0.15)
    axExport = plt.axes([0.75, 0.02, 0.15, 0.05])
    bExport = Button(axExport, 'Export Data')
    bExport.on_clicked(p.exportData)
    plt.show()
Example #7
0
def save_for_labeller(directory,dm3file):
    stack = dm.dmReader(dm3file)
    warnings.filterwarnings("ignore")
    if os.path.isdir(directory) == False:
        os.mkdir(directory)
    fname_base = directory + '/' + stack['filename'].split('.')[0]
    if len(stack['data'].shape) == 2:
        fname = fname_base +'.png'
        io.imsave(fname,img)
    else:
        for idx,img in enumerate(stack['data']):
            fname = fname_base + '_' + str(idx) +'.png'
            imsave(fname,median_filter(img))
    print('done!')
Example #8
0
def get_pixel_ratio(img, img_path):
    """
    Tries to read file metadata from dm file. If normal .tif images are provided
    instead, prompts user for the nm/pixel ratio, which can be found using the
    measurement tool in ImageJ or similar. For example, a scale bar of 100nm corresponds to a nm/pixel ratio of
    0.204.
    """
    try:
        try:
            metadata = dm.dmReader(f"{str(img_path)[:-4]}.dm4")
        except FileNotFoundError:
            metadata = dm.dmReader(f"{str(img_path)[:-4]}.dm3")
        ratio = metadata["pixelSize"][0]
    except FileNotFoundError:
        io.imshow(img)
        plt.show()
        ratio = click.prompt(
            f"dm4 not found. Enter the nm/pixel ratio in {img_path.stem}:",
            type=float,
            default=0.204,
        )
        plt.close()

    return ratio
def adjust_pipeline(directory_list):
    for directory in directory_list:
        try:
            os.mkdir(directory+'/adjustedPNG')
            new_directory = directory+'/adjustedPNG'
        except:
            new_directory = directory+'/adjustedPNG'
        dm3s = glob(directory+ '/*.dm3')
        for dm3 in dm3s:
            img = dm.dmReader(dm3)['data']
            if img.shape[0] < 1024:
                pass
            elif img.shape[0] == 2048 and img.shape[1] == 2048:
                img = transform.resize(img,(1024,1024))
                name = dm3.split('/')[-1].split('.')[0]
                imsave(new_directory+'/'+name+'.png', xray_correct(img), format="png", cmap=cm.gray)
            elif img.shape[0] == 1024 and img.shape[1] == 1024:
                name = dm3.split('/')[-1].split('.')[0]
                imsave(new_directory+'/'+name+'.png', xray_correct(img), format="png", cmap=cm.gray)
        print('done!')
Example #10
0
data_folder = r'C:\Users\a6q\exp_data\2020-01-24_CeO2ZnO2_on_graphene'
# set sliding window size length in pixels
win_len = 30

# get list of all files in the folder and list of all images to examine
files = glob.glob(data_folder + r'/*')
images = [f for f in files if 'cw' in f.lower() and 'saed' not in f.lower()]
start_time = time()

# loop over each image in the folder
for filename in images[:1]:
    
    # read file
    image_label = filename.split('\\')[-1].split('.dm3')[0]
    print('\n------------------------------\nFile: {}'.format(image_label))
    data = dm.dmReader(filename)
    img_span, nm_per_pixel = get_physical_image_size(data)
    grid = get_window_grid(data, win_len)#[:60]
    
    orientation = np.zeros_like(data['data']).astype(float)
    
    plot_raw_image = True
    if plot_raw_image:
        plt.imshow(data['data'], zorder=1, origin='lower',
                   cmap='gray',
                   extent=[0, img_span[0], 0, img_span[1]])
        
        plot_setup(
                title=image_label,
                labels=['Distance (nm)', 'Distance (nm)'])
        '''