def create_datafield(xres, yres, xreal, yreal): """ Create and Return datafield, i.e. Two-dimensional data representation with xrey,res pixel wide with pyhsical dimensions xreal,yreal""" d = gwy.DataField(xres, yres, xreal, yreal, 1) #1 means initializing with 0's for i in range(xres): for j in range(yres): d.set_val(i, j, i) # draws linear gradient x,y = get_pixel_size(d) print ("Size of created datafield {0} is {1}x{2} pixel".format(d,x,y)) xr,yr = get_real_size(d) print ("Size of created datafield {0} is {1}x{2} meter".format(d,xr,yr)) return d
def load(filename, mode=None): if not (zipfile.is_zipfile(filename)): return with zipfile.ZipFile(filename, "r") as zp: filenames = zp.namelist() c = gwy.Container() d = gwy.DataField(100, 100, 100, 100, 1) for i in range(100): for j in range(100): d.set_val(i, j, i) # draws linear gradient c.set_object_by_name("/0/data", d) return c
def volume_data(c, inhalt, einheit_xy, einheit_z, dim, pixel, dim_y=0, pixel_y=0, titel=None): """ :type c: gwy.Container :type inhalt: list :type einheit_xy: gwy.SIUnit :type einheit_z: gwy.SIUnit :type dim: float :type pixel: int :param dim_y: Wenn die Dimension von x und y verschieden sind :type dim_y: int :param pixel_y: Für nicht quadratische Datenfelder :type pixel_y: int :type titel: str """ if pixel_y == 0: pixel_y = pixel if dim_y == 0: dim_y = dim # Neues, nicht initialisiertes Datenfeld erstellen: vd = gwy.DataField(pixel, pixel_y, dim, dim_y, False) vd.set_si_unit_xy(einheit_xy) vd.set_si_unit_z(einheit_z) # Belegen: c_feld = c_double * (pixel * pixel_y) zgr = c_feld.from_address(vd.get_data_pointer()) zgr[:] = inhalt if not hasattr(c, 'n_sd'): c.n_sd = 0 name = '/' + str(c.n_sd) + '/data' c.set_object_by_name(name, vd) if titel is not None: c.set_string_by_name(name + '/title', titel) c.n_sd += 1
def laplacian_of_gaussian(container, datafield_id=0, add_to_data_browser=True): """ Apply a Laplacian of Gaussian filter- useful for finding blobs on images. """ # get copy of data to filter data_key = os.path.join(os.sep, str(datafield_id), "data") # mask_key = os.path.join(os.sep, str(datafield_id), 'mask') datafield = container.get_object_by_name(data_key) log_datafield = datafield.duplicate() # apply gaussian filter log_datafield.filter_gaussian(3) # define laplacian kernel kernel_size = 5 kernel_laplacian = gwy.DataField(kernel_size, kernel_size, datafield.get_xreal(), datafield.get_yreal(), False) # fmt: off # 5x5 2d laplacian kernel kernel_laplacian.set_data([0, 0, -1, 0, 0, \ 0, -1, -2, -1, 0, \ -1, -2, 16, -2, -1, \ 0, -1, -2, -1, 0, \ 0, 0, -1, 0, 0, \ ]) # fmt: on # apply laplacian filter log_datafield.convolve(kernel_laplacian) # get original data channel name datafield_title = gwy.gwy_app_get_data_field_title(container, datafield_id) # add datafield to container and make container active and return its id datafield_log_id = gwy.gwy_app_data_browser_add_data_field( log_datafield, container, add_to_data_browser) # set LoG data title gwy.gwy_app_set_data_field_title(container, datafield_log_id, "LoG_" + datafield_title) return log_datafield, datafield_log_id
def set_custom(c, name, objekt, permanent=True): """ :type c: gwy.Container :type name: str :type objekt: object :var permanent: Wenn wahr, dann bleibt das Objekt im Gwyddion-Container enthalten. Das kann sehr speicherintensiv sein. Deshalb kann es sinnvoll sein, diesen Parameter auf falsch zu setzen, wodurch das Objekt durch den ersten Aufruf von get_custom(c, name) aus dem Container gelöscht wird. :type permanent: bool """ ser = StringIO() cPickle.dump(objekt, ser, HIGHEST_PROTOCOL) ser.seek(0, SEEK_END) byte = ser.tell() go = gwy.DataField((byte + 1) / 4, 1, 1, 1, False) c_feld = c_byte * byte zgr = c_feld.from_address(go.get_data_pointer()) zgr[:] = [ord(n) for n in ser.getvalue()] c.set_object_by_name(SPECKVIEW + name, go) c.set_int32_by_name(SPECKVIEW + name + BYTE, byte) c.set_boolean_by_name(SPECKVIEW + name + '/visible', False) c.set_boolean_by_name(SPECKVIEW + name + PERM, permanent)
def load(filename, mode=None): ### Load returns a container object, initialize here mainC = gwy.Container() ### Load the file into the Nanonispy Grid object grid = Grid(filename) ### Unpack the file for clarity, assuming the header format is constant. dim_x, dim_y = grid.header.get("dim_px") real_x, real_y = grid.header.get("size_xy") pos_x, pos_y = grid.header.get("pos_xy") channels = grid.header.get("channels") num_sweep_signal = grid.header.get("num_sweep_signal") num_parameters = grid.header.get("num_parameters") experimental_parameters = grid.header.get("experimental_parameters") sweep_size = abs( grid.signals.get("sweep_signal")[0] - grid.signals.get("sweep_signal")[-1]) ### Need to be able to load a arbitrary amount of 3D channel data for i in range(len(channels)): ###Create the gwy Brick object and set dim/units tmpBrick = gwy.Brick(dim_x, dim_y, num_sweep_signal, real_x, real_y, sweep_size, 1) tmpBrick.set_si_unit_x(gwy.SIUnit("m")) tmpBrick.set_si_unit_y(gwy.SIUnit("m")) tmpBrick.set_si_unit_z(gwy.SIUnit("V")) if "(V)" in channels[i]: tmpBrick.set_si_unit_w(gwy.SIUnit("V")) else: tmpBrick.set_si_unit_w(gwy.SIUnit("A")) ### Transfer data to grid object volData = grid.signals.get(channels[i]) for x in range(dim_x): for y in range(dim_y): for z in range(num_sweep_signal): tmpBrick.set_val(x, y, z, volData[x, y, z]) ###Load Brick into main Container mainC.set_object_by_name("/brick/" + str(i), tmpBrick) ###Create a preview by slicing the volume data tmpPreviewDataField = gwy.DataField(dim_x, dim_y, real_x, real_y, 1) tmpBrick.extract_plane(tmpPreviewDataField, 0, 0, 0, dim_x, dim_y, -1, True) mainC.set_object_by_name("/brick/" + str(i) + "/preview", tmpPreviewDataField) ### set title of volume data mainC.set_string_by_name("/brick/" + str(i) + "/title", str(channels[i])) ### Make sure data is visible upon loading mainC.set_boolean_by_name("/brick/" + str(i) + "/visible", False) ### TO DO: figure out how to store the meta data in Container ###Load the topograph to display as well topoDataField = gwy.DataField(dim_x, dim_y, real_x, real_y, 1) topo = grid.signals.get('topo') for x in range(dim_x): for y in range(dim_y): topoDataField.set_val(x, y, topo[x, y]) topoDataField.set_si_unit_xy(gwy.SIUnit('m')) topoDataField.set_si_unit_z(gwy.SIUnit('m')) mainC.set_object_by_name("/0/data", topoDataField) mainC.set_string_by_name("/0/data/title", "topo") mainC.set_boolean_by_name("/0/data/visible", True) return mainC
def testFunction(filename): with open(filename) as f: I = gwy.DataField(8, 8, 1.0, 1.0) return filename f.close()