Module gwyutils
[hide private]
[frames] | no frames]

Source Code for Module gwyutils

  1  import gwy 
  2   
3 -def save_dfield_to_png(container, datafield_name, filename, run_type):
4 """ 5 Save desired datafield given by name stored in container to file. 6 7 @param container: gwy.Container which has datafield of given name 8 @param datafield_name: datafield name in string representation (like '/0/data') 9 @param filename: expected filename 10 @param run_type: select of interactive (RUN_INTERACTIVE) or noninteractive mode (RUN_NONINTERACTIVE) 11 12 @deprecated: This function was a workaround for old image export. Nowadays 13 it is strictly worse than the two lines:: 14 15 gwy.gwy_app_data_browser_select_data_field(container, datafield_num) 16 gwy.gwy_file_save(container, filename, run_type) 17 18 because it also unnecessarily forces the data field to be shown in a window. 19 """ 20 gwy.gwy_app_data_browser_reset_visibility(container, gwy.VISIBILITY_RESET_SHOW_ALL) 21 datafield_num = int(datafield_name.split('/')[1]) 22 gwy.gwy_app_data_browser_select_data_field(container, datafield_num) 23 gwy.gwy_file_save(container, filename, run_type)
24
25 -def get_data_fields(container):
26 """ 27 Get list of available datafield stored in given container 28 29 @param container: gwy.Container with datafields 30 31 @return: a list of datafields 32 33 @deprecated: This function produces nonsense. It returns all kinds of data 34 fields in on bag: images, masks, presentations, brick and surface previews, 35 etc. Instead, use C{L{gwy.gwy_app_data_browser_get_data_ids}(@container)} 36 and similar functions to get lists of specific data types. 37 """ 38 l = [] 39 for key in container.keys(): 40 x = container.get_value(key) 41 if type(x) is gwy.DataField: 42 l.append(x) 43 44 return l
45
46 -def get_data_fields_dir(container):
47 """ 48 Get list of available datafield stored in given container as directory where key is key name and value is DataField object. 49 50 @param container: gwy.Container with datafields 51 52 @return: a directory of datafields 53 54 @deprecated: This function produces nonsense. It returns all kinds of data 55 fields in on bag: images, masks, presentations, brick and surface previews, 56 etc. Instead, use C{L{gwy.gwy_app_data_browser_get_data_ids}(@container)} 57 and similar functions to get lists of specific data types. 58 """ 59 d = {} 60 for key in container.keys_by_name(): 61 x = container.get_value_by_name(key) 62 if type(x) is gwy.DataField: 63 d[key] = x 64 65 return d
66
67 -def get_current_datafield():
68 """ 69 Shorthand for 70 C{L{gwy.gwy_app_data_browser_get_current}(gwy.APP_DATA_FIELD)} 71 72 @return: current datafield 73 """ 74 return gwy.gwy_app_data_browser_get_current(gwy.APP_DATA_FIELD)
75
76 -def get_current_container():
77 """ 78 Shorthand for 79 C{L{gwy.gwy_app_data_browser_get_current}(gwy.APP_CONTAINER)} 80 81 @return: current container 82 """ 83 return gwy.gwy_app_data_browser_get_current(gwy.APP_CONTAINER)
84 85 try: 86 import numpy as np 87 except ImportError: 88 pass 89 else:
90 - def data_line_data_as_array(line):
91 """Create a view the DataLine's data as numpy array. 92 93 The array can point to an invalid memory location after using other 94 gwyddion functions and lead to application crash. Use with care. 95 96 @param line: the L{gwy.DataLine} to view 97 @return: array viewing the data 98 """ 99 class gwydfdatap(): 100 def __init__(self,addr,shape): 101 data = (addr, False) 102 self.__array_interface__= { 103 'shape' : shape, 104 'data' : data, 105 'typestr' : "|f8", 106 'version' : 3}
107 108 addr = line.get_data_pointer() 109 shape = (line.get_res(),) 110 return np.array(gwydfdatap(addr, shape), copy=False) 111
112 - def data_field_data_as_array(field):
113 """Create a view the DataField's data as numpy array. 114 115 The array can point to an invalid memory location after using other 116 gwyddion functions and lead to application crash. Use with care. 117 118 @param field: the L{gwy.DataField} to view 119 @return: array viewing the data 120 """ 121 class gwydfdatap(): 122 def __init__(self,addr,shape): 123 data = (addr, False) 124 self.__array_interface__= { 125 'shape' : shape, 126 'data' : data, 127 'typestr' : "|f8", 128 'version' : 3}
129 130 addr = field.get_data_pointer() 131 shape = (field.get_yres(), field.get_xres()) 132 return np.array(gwydfdatap(addr, shape), copy=False) 133
134 - def brick_data_as_array(brick):
135 """Create a view the Brick's data as numpy array. 136 137 The array can point to an invalid memory location after using other 138 gwyddion functions and lead to application crash. Use with care. 139 140 @param brick: the L{gwy.Brick} to view 141 @return: array viewing the data 142 """ 143 class gwydfdatap(): 144 def __init__(self,addr,shape): 145 data = (addr, False) 146 self.__array_interface__= { 147 'shape' : shape, 148 'data' : data, 149 'typestr' : "|f8", 150 'version' : 3}
151 152 addr = brick.get_data_pointer() 153 shape = (brick.get_zres(), brick.get_yres(), brick.get_xres()) 154 return np.array(gwydfdatap(addr, shape), copy=False) 155
156 - def data_field_get_data(datafield):
157 """Gets the data from a data field. 158 159 The returned array is a copy of the data. 160 But it can be safely stored without ever referring to invalid memory. 161 162 @param datafield: The data field. 163 @deprecated: This helper function is pointless. Just use 164 C{numpy.array(@datafield)}. 165 """ 166 return data_field_data_as_array(datafield).copy()
167
168 - def data_field_set_data(datafield, data):
169 """Sets the data of a data field. 170 171 The data shape must correspond to the data field shape. 172 """ 173 dest = data_field_data_as_array(datafield) 174 if dest.shape != data.shape: 175 raise ValueError("Data needs same size as the DataField.") 176 dest[:] = data
177
178 - def brick_get_data(brick):
179 """Gets the data from a brick. 180 181 The returned array is a copy of the data. 182 But it can be safely stored without ever referring to invalid memory. 183 184 @param brick: The data brick. 185 @deprecated: This helper function is pointless. Just use 186 C{numpy.array(@brick)}. 187 """ 188 return brick_data_as_array(brick).copy()
189
190 - def brick_set_data(brick, data):
191 """Sets the data of a brick. 192 193 The data shape must correspond to the brick shape. 194 """ 195 dest = brick_data_as_array(brick) 196 if dest.shape != data.shape: 197 raise ValueError("Data needs same size as the Brick.") 198 dest[:] = data
199