def run(): # get all containers containers = gwy.gwy_app_data_browser_get_containers() # save each container as gwy file with file name for c in containers: # get datafield 0 for each container for func to operate on gwy.gwy_app_data_browser_select_data_field(c, 0) gwy.gwy_process_func_run("arc_revolve", c, gwy.RUN_IMMEDIATE)
def heightediting(data, k): gwy.gwy_app_data_browser_select_data_field(data, k) # Flatten the data gwy.gwy_process_func_run('flatten_base', data, gwy.RUN_IMMEDIATE) datafield = gwy.gwy_app_data_browser_get_current(gwy.APP_DATA_FIELD) mask = gwy.DataField.new_alike(datafield, False) datafield.grains_mark_height(mask, 30, False) # Re-do polynomial correction with masked height s["/module/polylevel/masking"] = 1 gwy.gwy_process_func_run('polylevel', data, gwy.RUN_IMMEDIATE) # Re-do align rows with masked heights s["/module/linematch/masking"] = 1 gwy.gwy_process_func_run('align_rows', data, gwy.RUN_IMMEDIATE) # # Remove scars # gwy.gwy_process_func_run('scars_remove', data, gwy.RUN_IMMEDIATE) # Gaussian filter to remove noise current_data = gwy.gwy_app_data_browser_get_current(gwy.APP_DATA_FIELD) filter_width = 5 * dx * 1e9 current_data.filter_gaussian(filter_width) # Set zero to mean value gwy.gwy_process_func_run('zero_mean', data, gwy.RUN_IMMEDIATE) return data
def load_data(self ): self.channel_id = self.combobox_channels.get_active() model = self.combobox_channels.get_model() active = self.combobox_channels.get_active() if active >= 0: self.channel_str = model[self.channel_id][0] #print self.channel_str self.direction_id = self.combobox_directions.get_active() #if self.gwydata.param['full_path'][-6:]=='Z_mtrx': # data_id = self.channel_id #else: data_id = self.channel_id * 2 + self.direction_id #print self.channel_id,self.direction_id, data_id self.data_id_str = '/'+str(data_id)+'/' print data_id,self.data_id_str self.d_origin = self.c[self.data_id_str + 'data'] #self.d = self.d_origin.duplicate() if self.d_origin: self.c.set_object_by_name(self.data_id_str + 'data',self.d_origin) else: self.d_origin = self.c[self.data_id_str + 'data'] gwy.gwy_app_data_browser_select_data_field(self.c,data_id) self.get_active_process() print self.process_id if self.process_id == 1: gwy.gwy_app_data_browser_select_data_field(self.c, data_id) gwy.gwy_process_func_run("level", self.c, gwy.RUN_IMMEDIATE) elif self.process_id == 2: gwy.gwy_process_func_run("align_rows", self.c, gwy.RUN_IMMEDIATE) elif self.process_id == 3: gwy.gwy_process_func_run("level", self.c, gwy.RUN_IMMEDIATE) gwy.gwy_process_func_run("align_rows", self.c, gwy.RUN_IMMEDIATE) self.d = self.c[self.data_id_str + 'data'] d_process = self.d.duplicate() self.data_min = self.d.get_min() self.data_max = self.d.get_max() self.data_dif = self.data_max - self.data_min self.scale_min_current = self.scale_min.get_value() self.scale_max_current = self.scale_max.get_value() bottom = self.data_min + self.scale_min_current/100*self.data_dif top = self.data_min + self.scale_max_current/100*self.data_dif #print bottom,top if bottom <= top: d_process.clamp(bottom, top) #self.d.clamp(bottom, top) self.d = d_process else: pass
def process_topo(self, data_ch_id): """Processes the data with Gwyddion Python module. Gwyddion topography processing functions: see config file. Before saving the image, the colorrange needs adjustment. The colorrange settings are stored in the container for each spm file. (see online gwyfile-format) Args: data_ch_id (int): Channel of the container should be processed. """ for ch in data_ch_id: gwy.gwy_app_data_browser_select_data_field(self.container, ch) self.run_gwy_func = { gwy.RUN_IMMEDIATE: config.run_gwy_immediate_func } for k, funcs in self.run_gwy_func.iteritems(): for func in funcs: gwy.gwy_process_func_run(func, self.container, k) self.match_ch_topo = "/" + str(ch) + "/base/range-type" self.container[self.match_ch_topo] = 2
def gwybas(filename): #add the gwyddion folders to the path, making sure they can be found import sys sys.path.append('C:\Program Files (x86)\Gwyddion\\bin') sys.path.append('C:\Program Files (x86)\Gwyddion\share\gwyddion\pygwy') #import gwyddion import gwy import gwyutils #load the file and add to data browser c = gwy.gwy_file_load(filename, gwy.RUN_IMMEDIATE) gwy.gwy_app_data_browser_add(c) #Set the right settings for the align_rows command settings = gwy.gwy_app_settings_get() settings['/module/linematch/direction'] = int(gwy.ORIENTATION_HORIZONTAL) settings['/module/linematch/do_extract'] = False settings['/module/linematch/do_plot'] = False settings[ '/module/linematch/method'] = 0 # 0: poly, 1: median, 2: median of diff,3: modus,4: matching, 5: trimemd mean, 6: trimmed mean of diff settings['/module/linematch/masking'] = 2 settings['/module/linematch/max_degree'] = 3 #Order of polynominal settings['/module/linematch/trim_fraction'] = 0.05 #print the datafield ID's corresponding to the different channels of the AFM, such as height, phase and error, usually the first one (0) is the height # print gwy.gwy_app_data_browser_get_data_ids(c) #itterate over the different datafield ID's/AFM channels, and do processing on them for datafield_id in gwy.gwy_app_data_browser_get_data_ids(c): # datafield = c['/%d/data' % datafield_id] #set the color range to automatic with tials cut off (corresponding to number 2) c['/%d/base/range-type' % datafield_id] = 2 #select the datafield_ID/AFM channel to process gwy.gwy_app_data_browser_select_data_field(c, datafield_id) #level the plane gwy.gwy_process_func_run("level", c, gwy.RUN_IMMEDIATE) #align the rows, with settings chosen above line 19-25 gwy.gwy_process_func_run("align_rows", c, gwy.RUN_IMMEDIATE) #remove scars a couple of times (button bashing) gwy.gwy_process_func_run("scars_remove", c, gwy.RUN_IMMEDIATE) gwy.gwy_process_func_run("scars_remove", c, gwy.RUN_IMMEDIATE) gwy.gwy_process_func_run("scars_remove", c, gwy.RUN_IMMEDIATE) gwy.gwy_process_func_run("scars_remove", c, gwy.RUN_IMMEDIATE) gwy.gwy_process_func_run("scars_remove", c, gwy.RUN_IMMEDIATE) #fix lowest point to zero gwy.gwy_process_func_run('fix_zero', c, gwy.RUN_IMMEDIATE) #define new file names, I chose to simply add the wanted extention to the original filename newname = filename + '.gwy' newname2 = filename + '.jpg' #find the ID/Channel corresponding to the height, of this one a JPG will be created, .gwy will contain all channels ids = gwy.gwy_app_data_browser_find_data_by_title(c, 'Height') gwy.gwy_app_data_browser_select_data_field(c, ids[0]) gwy.gwy_file_save(c, newname, gwy.RUN_NONINTERACTIVE) gwy.gwy_file_save(c, newname2, gwy.RUN_NONINTERACTIVE) #remove the current container, makes room for the next file gwy.gwy_app_data_browser_remove(c)
def otsuthresholdgrainfinding(data, k): # 'align_rows' function # s["/module/linematch/direction"] = 0 # s["/module/linematch/do_extract"] = False # s["/module/linematch/do_plot"] = False # s["/module/linematch/masking"] = 2 # s["/module/linematch/max_degree"] = 0 # s["/module/linematch/method"] = 0 # uses median # s["/module/linematch/trim_fraction"] = float(0.05) # Select channel 'k' of the file gwy.gwy_app_data_browser_select_data_field(data, k) datafield = gwy.gwy_app_data_browser_get_current(gwy.APP_DATA_FIELD) mask = gwy.DataField.new_alike(datafield, False) # Apply a 1.5 pixel gaussian filter data_field = gwy.gwy_app_data_browser_get_current(gwy.APP_DATA_FIELD) data_field.filter_gaussian(1.5) # # Mask data that are above thresh*sigma from average height. # # Sigma denotes root-mean square deviation of heights. # # This criterium corresponds to the usual Gaussian distribution outliers detection if thresh is 3. # datafield.mask_outliers(mask, 1) # # Shift contrast - equivalent to 'fix zero' - essential for next step to work datafield.add(-datafield.get_min()) # Calculate min, max and range of data to allow calculation of relative value for grain thresholding min_datarange = datafield.get_min() max_datarange = datafield.get_max() datarange = max_datarange + min_datarange # Calculate Otsu threshold for data o_threshold = datafield.otsu_threshold() o_threshold = o_threshold + min_datarange # Calculate relative threshold for grain determination rel_threshold = 100 * (o_threshold / datarange) # Mask grains using either relative threshold of the data, i.e. a percentage # this can be set manually i.e. 35 or # as the otsu threshold expressed as a percentage which is rel_threshold # this will fail unless the min_datarange is negative datafield.grains_mark_height(mask, rel_threshold, False) # # Invert mask to maks things below the membrane mask.grains_invert() gwy.gwy_process_func_run("align_rows", data, gwy.RUN_IMMEDIATE) # # gwy.gwy_process_func_run("level", data, gwy.RUN_IMMEDIATE) # gwy.gwy_process_func_run('flatten_base', data, gwy.RUN_IMMEDIATE) # Select channel 'k' of the file gwy.gwy_app_data_browser_select_data_field(data, k) datafield = gwy.gwy_app_data_browser_get_current(gwy.APP_DATA_FIELD) mask = gwy.DataField.new_alike(datafield, False) # # Mask data that are above thresh*sigma from average height. # # Sigma denotes root-mean square deviation of heights. # # This criterium corresponds to the usual Gaussian distribution outliers detection if thresh is 3. # datafield.mask_outliers(mask, 1) # # Shift contrast - equivalent to 'fix zero' - essential for next step to work datafield.add(-datafield.get_min()) # Calculate min, max and range of data to allow calculation of relative value for grain thresholding min_datarange = datafield.get_min() max_datarange = datafield.get_max() datarange = max_datarange + min_datarange # Calculate Otsu threshold for data o_threshold = datafield.otsu_threshold() o_threshold = o_threshold + min_datarange # Calculate relative threshold for grain determination rel_threshold = 100 * (o_threshold / datarange) # Mask grains using either relative threshold of the data, i.e. a percentage # this can be set manually i.e. 35 or # as the otsu threshold expressed as a percentage which is rel_threshold # this will fail unless the min_datarange is negative datafield.grains_mark_height(mask, rel_threshold, False) gwy.gwy_process_func_run('zero_mean', data, gwy.RUN_IMMEDIATE) # # Invert mask to make things below the membrane mask.grains_invert() # Calculate pixel width in nm dx = datafield.get_dx() # Calculate minimum feature size in pixels (integer) minsize = int(2000e-9 / dx) # Remove grains smaller than (size) in integer pixels mask.grains_remove_by_size(minsize) mask.grains_invert() gwy.gwy_process_func_run('zero_mean', data, gwy.RUN_IMMEDIATE) mask.grains_invert() # Numbering grains for grain analysis grains = mask.number_grains() print max(grains) # Update data to show mask, comment out to remove mask s['/module/pixmap/draw_mask'] = True data['/0/mask/red'] = 0.1234 #excluding mask, zero mean stats = datafield.area_get_stats_mask(mask, gwy.MASK_EXCLUDE, 0, 0, datafield.get_xres(), datafield.get_yres()) datafield.add(-stats[0]) return data, mask, datafield, grains
def editfile(data, k): # select each channel of the file in turn # this is run within the for k in chosen_ids loop so k refers to the index of each chosen channel to analyse # NONINTERACTIVE is only for file modules gwy.gwy_app_data_browser_select_data_field(data, k) # align rows (b) gwy.gwy_process_func_run("align_rows", data, gwy.RUN_IMMEDIATE) # flatten the data (c) gwy.gwy_process_func_run("level", data, gwy.RUN_IMMEDIATE) # align rows (d) gwy.gwy_process_func_run("align_rows", data, gwy.RUN_IMMEDIATE) datafield = gwy.gwy_app_data_browser_get_current(gwy.APP_DATA_FIELD) mask = gwy.DataField.new_alike(datafield, False) datafield.grains_mark_height(mask, 10, False) # Re-do polynomial correction with masked height (e) s["/module/polylevel/masking"] = 0 gwy.gwy_process_func_run('polylevel', data, gwy.RUN_IMMEDIATE) # Re-do align rows with masked heights (f) s["/module/linematch/masking"] = 0 gwy.gwy_process_func_run('align_rows', data, gwy.RUN_IMMEDIATE) # Re-do level with masked heights (g) s["/module/polylevel/masking"] = 1 gwy.gwy_process_func_run('polylevel', data, gwy.RUN_IMMEDIATE) # flatten base (h) gwy.gwy_process_func_run('flatten_base', data, gwy.RUN_IMMEDIATE) # remove scars (i) gwy.gwy_process_func_run('scars_remove', data, gwy.RUN_IMMEDIATE) # Fix zero (j) gwy.gwy_process_func_run('zero_mean', data, gwy.RUN_IMMEDIATE) # Apply a 1.5 pixel gaussian filter (k) data_field = gwy.gwy_app_data_browser_get_current(gwy.APP_DATA_FIELD) data_field.filter_gaussian(1) # # Shift contrast - equivalent to 'fix zero' # datafield.add(-data_field.get_min()) return data
import gwy import gwyutils import sys filename = sys.argv[1] # '/Users/pabloherrero/sabat/stm_data/november19_experiments/QE1/2019-11-12-QE1_001.sxm' f = open(filename, "r") container = gwy.gwy_app_file_load(filename) ids = gwy.gwy_app_data_browser_get_data_ids(container) cons = gwy.gwy_app_data_browser_get_containers() for c in cons: dfields = gwyutils.get_data_fields_dir(c) for key in dfields.keys(): datafield = dfields[key] print(datafield.get_xreal()) gwy.gwy_app_data_browser_select_data_field(container, ids[0]) container['/%u/base/palette' % ids[0]] = 'Gold' gwy.gwy_process_func_run("level", container, gwy.RUN_INTERACTIVE) gwy.gwy_process_func_run("scars_remove", container, gwy.RUN_INTERACTIVE) gwy.gwy_file_save(container, '%s-%02u.png' % (basename, 0), gwy.RUN_NONINTERACTIVE)
def runbatch(root, cwd, pdr, pngexp, ratio): # Export PNG with scalebar s = gwy.gwy_app_settings_get() s['/module/pixmap/title_type'] = 0 s['/module/pixmap/ztype'] = 0 s['/module/pixmap/xytype'] = 0 s['/module/pixmap/draw_maskkey'] = False # ... (*lots* of possible settings, see ~/.gwyddion/settings) Files_To_Open = [ f for f in listdir(cwd) if isfile(join(cwd,f)) ] try: mkdir(join(cwd,'Processed')) except Exception as sym: print ('Already Exist') Tobe_Saved = join(cwd, 'Processed') filename_save = cwd.split('/')[-1] print (Files_To_Open) #Load first file to use as Merged file for filename in Files_To_Open: print(filename) try: Temp = gwy.gwy_file_load(join(cwd,filename), RUN_NONINTERACTIVE) print(type(Temp)) if type(Temp) == gwy.Container : print('right type') Cont_Dest = Temp Files_To_Open.remove(filename) break Files_To_Open.remove(filename) print('loadedbutnot') except Exception as sym: print('except') print ("not proper file"+str(sym)+"\n") continue #Add into current browser and Make Visible on display gwy.gwy_app_data_browser_add(Cont_Dest) Cont_Dest.set_boolean_by_name('/0/data/visible', 1) print (Files_To_Open) #File Merge #First Container DataFields = gwyutils.get_data_fields_dir(Cont_Dest) for key in DataFields.keys(): title = Cont_Dest.get_string_by_name(key+"/title") if (title == 'Amplitude') : Cont_Dest.remove_by_prefix('/'+key.split('/')[1]+'/') Cont_Dest.set_string_by_name(key+'/title', title+'.'+Files_To_Open[0]) #Rest of Containers for filename in Files_To_Open : #print (orgfile, join(cwd,filename)) try: Temp_Source = gwy.gwy_file_load(join(cwd,filename), RUN_NONINTERACTIVE) if type(Temp_Source) == gwy.Container: Cont_Source = Temp_Source pass else: continue except Exception as sym: print ("not proper file"+sym+"\n") continue DataFields = gwyutils.get_data_fields_dir(Cont_Source) for key in DataFields.keys(): ID = key.split('/')[1] title = Cont_Source.get_string_by_name(key+"/title") if (title == 'Height') : Cont_Source.set_string_by_name(key+'/title', title+'.'+filename) gwy.gwy_app_data_browser_copy_channel(Cont_Source, int(ID), Cont_Dest) print (key, title) gwy_app_data_browser_remove(Cont_Source) del Cont_Source print (gc.collect()) #Change Palette, Flatten, Correct line, Remove Scars, Change Scale DataFields = gwyutils.get_data_fields_dir(Cont_Dest) for key in DataFields.keys(): ID = key.split('/')[1] title = Cont_Dest.get_string_by_name(key+"/title") print (title+'\n') # Subtract polynomial background coeffs = DataFields[key].fit_polynom(3, 3) DataFields[key].subtract_polynom(3, 3, coeffs) DataFields[key].data_changed() #Get X Y scale si = {'x' : 'um' , 'y' : 'um'} size_x = DataFields[key].get_xreal()*1000000 if (size_x < 1.0): size_x = size_x * 1000 si['x'] = 'nm' size_y = DataFields[key].get_yreal()*1000000 if (size_y < 1.0): size_y = size_y * 1000 si['y'] = 'nm' scale = str(size_x)+si['x']+'by'+str(size_y)+si['y'] title = title + '_'+ scale # Line and scar correction (run module functions) gwy.gwy_app_data_browser_select_data_field(Cont_Dest, int(ID)) gwy.gwy_process_func_run("line_correct_median", Cont_Dest, gwy.RUN_IMMEDIATE) gwy.gwy_process_func_run("scars_remove", Cont_Dest, gwy.RUN_IMMEDIATE) gwy.gwy_process_func_run("fix_zero", Cont_Dest, gwy.RUN_IMMEDIATE) #Get Color Type colorr = Cont_Dest.get_int32_by_name('/'+ID+'/base/range-type') #Change_Color Palette Cont_Dest.set_string_by_name('/'+ID+'/base/palette', 'Gold') #Get Height Distribution and get Percentile color set range #Get CDH histogram = gwy.DataLine(1, 1, False) DataFields[key].cdh(histogram, 512) data = histogram.get_data() #Get Percentile Range Data_Range = DataFields[key].get_min_max() Histogram_pct = [(float(index))/512 for index, value in enumerate(data) if (data[index] >= ratio[1] and data[index-1] <= ratio[1]) or (data[index] <= ratio[0] and data[index+1] >= ratio[0])] Range = Data_Range[1]-Data_Range[0] Color_Range = {'min': Data_Range[0]+Range*Histogram_pct[0], 'max':Data_Range[0]+Range*Histogram_pct[1]} Cont_Dest.set_int32_by_name('/0/base/range-type' , 1) Cont_Dest.set_double_by_name('/0/base/min', Color_Range['min']) Cont_Dest.set_double_by_name('/0/base/max', Color_Range['max']) #Change Color Range into (Full:0, Manual:1, Auto:2, Adaptive:3) Cont_Dest.set_int32_by_name('/'+ID+'/base/range-type', 2) print (title) gwy.gwy_file_save(Cont_Dest, Tobe_Saved+'/'+str(title)+'%d.png' % int(ID), gwy.RUN_NONINTERACTIVE) Cont_Dest.set_boolean_by_name('/'+ID+'/data/visible', 0) gwy.gwy_file_save(Cont_Dest,Tobe_Saved+'/'+filename_save+'.gwy', gwy.RUN_NONINTERACTIVE) gwy_app_data_browser_remove(Cont_Dest)
configureGwySettings(s) #Process both trace and retrace for i in height_ids: ### ### CORRECTING THE DATA WITH GWYDDION FUNCTIONS ### HAVE INCREDIBLY UNPLEASANT LOOKING EDITING OF SETTINGS TO DO FOR EACH FUNCTION ### PROBABLY A NEATER WAY TO DO THIS BUT CBA TO LOOK FOR IT NOW ### COPIED THE SETTINGS FROM THE ~/.gwyddion/settings file ### gwy.gwy_app_data_browser_select_data_field(gwy_rawdata, i) #level the data gwy.gwy_process_func_run('level', gwy_rawdata, gwy.RUN_IMMEDIATE) #flatten the data gwy.gwy_process_func_run('flatten_base', gwy_rawdata, gwy.RUN_IMMEDIATE) #remove scars gwy.gwy_process_func_run('scars_remove', gwy_rawdata, gwy.RUN_IMMEDIATE) #execute 'align_rows' function #s["/module/linematch/method"] = 2 #gwy.gwy_process_func_run('align_rows', gwy_rawdata, gwy.RUN_IMMEDIATE) #Gaussian filter to remove noise current_data = gwy.gwy_app_data_browser_get_current(gwy.APP_DATA_FIELD)