def get_gaussian_sigma(): """ Convenience function that gets the gaussian sigma value from the filters toolbox setting. Size (FWHM) relates to sigma as FWHM = 2*sqrt(2*ln(2)) * sigma. Returns ------- sigma: float Standard deviation of gaussian kernel. """ settings = gwy.gwy_app_settings_get() # get sigma from settings, ie. vlaue last used in toolbox->filters size = settings["/module/filter/gauss_size"] # size (FWHM) relates to sigma as FWHM = 2*sqrt(2*ln(2)) * sigma return size / (2.0 * math.sqrt(2.0 * math.log(2.0)))
def set_settings(self): """Sets the setting which are supplied by the user editable config file.""" self.settings = gwy.gwy_app_settings_get() self.settings["/module/linematch/direction"] = int( gwy.ORIENTATION_HORIZONTAL) self.settings["/module/linematch/do_extract"] = config.do_extract self.settings["/module/linematch/do_plot"] = config.do_plot self.settings["/module/linematch/method"] = config.method self.settings["/module/linematch/masking"] = config.masking self.settings["/module/linematch/max_degree"] = config.max_degree self.settings["/module/median-bg/radius"] = config.radius self.settings["/module/median-bg/do_extract"] = config.do_extract self.settings["/module/rotate/angle"] = config.angle self.settings["/module/rotate/create_mask"] = config.create_mask self.settings["/module/rotate/interp"] = config.rotate_interp self.settings["/module/rotate/resize"] = config.resize self.settings["/module/rotate/show_grid"] = config.show_grid self.settings["/module/scale/interp"] = config.scale_interp self.settings["/module/scale/proportional"] = config.proportional self.settings["/module/scale/aspectratio"] = config.aspectratio self.settings["/module/asciiexport/add-comment"] = config.add_comment
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)
import gwyutils, gwy, gc from os import listdir, mkdir, getcwd from os.path import isfile, join cwd = '/home/june/pygwy/cont' Files_To_Open = [ f for f in listdir(cwd) if isfile(join(cwd,f)) ] #mkdir(join(cwd,'Processed')) filename_save = getcwd().split('/')[-1] # 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) #Load first file to use as Merged file Cont_Dest = gwy.gwy_file_load(join(cwd,Files_To_Open[0]), RUN_NONINTERACTIVE) gwy.gwy_app_data_browser_add(Cont_Dest) #Make Visible Cont_Dest.set_boolean_by_name('/0/data/visible', 1) #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[1:] :
#!/usr/bin/env python2 import gwy # Set the settings for each function from the saved settings file (~/.gwyddion/settings) s = gwy.gwy_app_settings_get() # Turn colour bar off s["/module/pixmap/ztype"] = 0 # Define the settings for image processing functions e.g. align rows here # s['/module/linematch/method'] = 2 # 'align_rows' function s["/module/linematch/direction"] = 0 s["/module/linematch/do_extract"] = False s["/module/linematch/do_plot"] = False s["/module/linematch/masking"] = 1 s["/module/linematch/max_degree"] = 0 s["/module/linematch/method"] = 1 s["/module/linematch/trim_fraction"] = float(0.05) 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
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)