def _get_data(gwydf, xres, yres):
        """Get data array from <GwyDataField*> object

        Args:
            gwydf (GwyDataField*):
                GwyDataField object from Libgwyfile
            xres (int): Horizontal dimension of the data field in pixels
            yres (int): Vertical dimension of the data field in pixels

        Returns:
            data (2D numpy array, float64): data from the data field

        """

        error = ffi.new("GwyfileError*")
        errorp = ffi.new("GwyfileError**", error)

        data = ffi.new("double[]", xres * yres)
        datap = ffi.new("double**", data)

        if lib.gwyfile_object_datafield_get(gwydf, errorp,
                                            ffi.new("char[]", b'data'), datap,
                                            ffi.NULL):
            data_buf = ffi.buffer(datap[0], xres * yres * ffi.sizeof(data))
            data_array = np.frombuffer(data_buf,
                                       dtype=np.float64,
                                       count=xres * yres).reshape((xres, yres))
            return data_array
        else:
            raise GwyfileErrorCMsg(errorp[0].message)
Beispiel #2
0
    def _side_effect(self, *args):
        """
        Check args of gwyfile_object_graphmodel_get func
        and write self.curves_array in 'curves' field
        """

        # first arg is GwyDatafield returned by get_gwyitem_object
        self.assertEqual(args[0], self.gwygraphmodel)

        # second arg is GwyfileError**
        assert ffi.typeof(args[1]) == ffi.typeof(ffi.new("GwyfileError**"))

        # last arg in Null
        self.assertEqual(args[-1], ffi.NULL)

        # combine fields names and fields pointers in one dictionary
        arg_keys = [ffi.string(key).decode('utf-8') for key in args[2:-1:2]]
        arg_pointers = [pointer for pointer in args[3:-1:2]]
        arg_dict = dict(zip(arg_keys, arg_pointers))

        arg_dict['curves'][0] = self.curves_array

        # C func returns true if the graphmodel object loock acceptable
        truep = ffi.new("bool*", True)
        return truep[0]
Beispiel #3
0
    def _get_curves(gwygraphmodel, ncurves):
        """Get list of GwyGraphCurveModel object pointers

        Args:
            gwygraphmodel (GwyGraphModel*):
                GwyGraphModel object from Libgwyfile C library
            ncurves (int):
                number of curves in the GwyGraphModel object

        Returns:
            curves (list):
                list of GwyGraphCurveModel* Libgwyfile objects
        """

        error = ffi.new("GwyfileError*")
        errorp = ffi.new("GwyfileError**", error)
        curves_arrayp = ffi.new("GwyfileObject***")

        curves = []

        if lib.gwyfile_object_graphmodel_get(gwygraphmodel, errorp,
                                             ffi.new("char[]", b"curves"),
                                             curves_arrayp, ffi.NULL):
            curves_array = curves_arrayp[0]
            curves = [curves_array[curve_id] for curve_id in range(ncurves)]
            return curves
        else:
            raise GwyfileErrorCMsg(errorp[0].message)
Beispiel #4
0
    def _get_selection_points(cls, gwysel, nsel):
        """Get all points of selection from the gwysel object

        Args:
            gwysel (GwyfileObject*):
                GwySelection object from Libgwyfile
                e.g. GwySelectionPoint object for point selection
            nsel (int):
                number of selections of this type in gwysel

        Returns:
            [(x1, y1), ..., (xN, yN)]: list of tuples with point coordinates
                                       or None if there are no selections

        """

        error = ffi.new("GwyfileError*")
        errorp = ffi.new("GwyfileError**", error)

        if nsel == 0:
            return None
        else:
            data = ffi.new("double[]", 2 * nsel * cls._npoints)
            datap = ffi.new("double**", data)

        if cls._get_sel_func(gwysel, errorp, ffi.new("char[]", b'data'), datap,
                             ffi.NULL):
            data = datap[0]
            points = [(data[i * 2], data[i * 2 + 1])
                      for i in range(nsel * cls._npoints)]
        else:
            raise GwyfileErrorCMsg(errorp[0].message)

        return points
Beispiel #5
0
def write_gwycontainer_to_gwyfile(gwycontainer, filename):
    """Write gwycontainer to file.
       The file will be overwritten if it exists

    Args:
        gwycontainer (<GwyfileObject*>)
        filename (string): name of file
    """
    error = ffi.new("GwyfileError*")
    errorp = ffi.new("GwyfileError**", error)
    if not lib.gwyfile_write_file(
            gwycontainer, ffi.new("char[]", filename.encode('utf-8')), errorp):
        raise GwyfileErrorCMsg(errorp[0].message)
    def _test_pos_args_side_effect(self, *args):
        # first arg is GwyfileSelectionPoint object
        self.assertEqual(args[0], self.gwysel)

        # second arg is GwyfileError**
        self.assertEqual(ffi.typeof(args[1]),
                         ffi.typeof(ffi.new("GwyfileError**")))

        # last arg in Null
        self.assertEqual(args[-1], ffi.NULL)

        # Function should return True if object looks acceptable
        truep = ffi.new("bool*", True)
        return truep[0]
Beispiel #7
0
    def _getting_description_of_curve(self, *args):
        """
        Write self.description in 'description' field and return True
        """

        # combine fields names and fields pointers in one dictionary
        arg_keys = [ffi.string(key).decode('utf-8') for key in args[2:-1:2]]
        arg_pointers = [pointer for pointer in args[3:-1:2]]
        arg_dict = dict(zip(arg_keys, arg_pointers))

        arg_dict['description'][0] = ffi.new("char[]",
                                             self.description.encode('utf-8'))

        # C func returns true if the graphcurvemodel object loock acceptable
        truep = ffi.new("bool*", True)
        return truep[0]
    def setUp(self):
        self.cgwydf = Mock()
        self.mock_gwydf = Mock(spec=GwyDataField)
        self.mock_gwydf._get_data = GwyDataField._get_data

        patcher_lib = patch('pygwyfile.gwydatafield.lib', autospec=True)
        self.addCleanup(patcher_lib.stop)
        self.mock_lib = patcher_lib.start()

        self.falsep = ffi.new("bool*", False)
        self.truep = ffi.new("bool*", True)
        self.errorp = ffi.new("GwyfileError**")
        self.error_msg = "Test error message"

        self.xres = 256
        self.yres = 256
        self.data = np.random.rand(self.xres, self.yres)
Beispiel #9
0
    def _y_unit_is_not_empty(self, *args):
        """
        Write "m" C string to 'y_unit' field
        """

        y_unit = ffi.new("char[]", b"m")

        # combine fields names and fields pointers in one dictionary
        arg_keys = [ffi.string(key).decode('utf-8') for key in args[2:-1:2]]
        arg_pointers = [pointer for pointer in args[3:-1:2]]
        arg_dict = dict(zip(arg_keys, arg_pointers))

        arg_dict['y_unit'][0] = y_unit

        # C func returns true if the graphmodel object loock acceptable
        truep = ffi.new("bool*", True)
        return truep[0]
 def setUp(self):
     self.gwysel = Mock()
     self.nsel = 2
     self.points = [(0., 0.), (1., 1.)]
     self.cpoints = ffi.new("double[]", [0., 0., 1., 1.])
     patcher = patch.object(GwySelection, '_get_sel_func')
     self.get_sel_func = patcher.start()
     self.addCleanup(patcher.stop)
    def _side_effect_non_zero_channels(self, c_gwyfile, nchannelsp):
        """
        Returns 3 channels with ids = 0, 1 and 2
        """

        nchannelsp[0] = 3
        ids = ffi.new("int[]", [0, 1, 2])
        return ids
    def _side_effect_non_zero_graphs(self, c_gwyfile, ngraphsp):
        """
        Returns 2 graphs with ids = 1 and 2
        """

        ngraphsp[0] = 2
        ids = ffi.new("int[]", [1, 2])
        return ids
Beispiel #13
0
    def setUp(self):
        self.ncurves = 3  # number of curves in graphmodel object
        self.curves_array = ffi.new("GwyfileObject*[]", self.ncurves)
        self.gwygraphmodel = Mock()

        patcher_lib = patch('pygwyfile.gwygraph.lib', autospec=True)
        self.addCleanup(patcher_lib.stop)
        self.mock_lib = patcher_lib.start()
Beispiel #14
0
    def _positional_args_side_effect(self, *args):
        """
        Check positional args in gwyfile_object_graphcurvemodel_get call
        """

        # first arg is GwyGraphCurveModel
        self.assertEqual(args[0], self.gwycurve)

        # second arg is GwyfileError**
        assert ffi.typeof(args[1]) == ffi.typeof(ffi.new("GwyfileError**"))

        # last arg in Null
        self.assertEqual(args[-1], ffi.NULL)

        # C func returns true if the graphcurvemodel object loock acceptable
        truep = ffi.new("bool*", True)
        return truep[0]
Beispiel #15
0
 def _cdata(self):
     """ Representation of pointer selections list as C array
     """
     if self.data:
         flat_data_list = [val for point in self.data for val in point]
         cdata = ffi.new("double[]", flat_data_list)
         return cdata
     else:
         return None
Beispiel #16
0
    def _y_max_set_is_false(self, *args):
        """
        Write False in 'y_max_set' field and 0. in 'y_max' field
        """

        # combine fields names and fields pointers in one dictionary
        arg_keys = [ffi.string(key).decode('utf-8') for key in args[2:-1:2]]
        arg_pointers = [pointer for pointer in args[3:-1:2]]
        arg_dict = dict(zip(arg_keys, arg_pointers))

        truep = ffi.new("bool*", True)
        falsep = ffi.new("bool*", False)

        arg_dict['y_max_set'][0] = falsep[0]
        arg_dict['y_max'][0] = 0.

        # C func returns true if the graphmodel object loock acceptable
        return truep[0]
Beispiel #17
0
    def test_raise_exception_if_graphmodel_object_looks_unacceptable(self):
        """
        Raise GwyfileErrorCMsg if gwyfile_object_graphmodel_get returns False
        """

        falsep = ffi.new("bool*", False)
        self.mock_lib.gwyfile_object_graphmodel_get.return_value = falsep[0]
        self.assertRaises(GwyfileErrorCMsg, GwyGraphModel._get_curves,
                          self.gwygraphmodel, self.ncurves)
Beispiel #18
0
def new_gwycontainer():
    """ Create new empty GwyContainer

    Returns:
        gwycontainer (<cdata GwyfileObject*>):
            empty Gwyddion Container
    """
    gwycontainer = lib.gwyfile_object_new(ffi.new("char[]", b"GwyContainer"),
                                          ffi.NULL)
    return gwycontainer
    def test_raise_exception_in_get_selection_points(self):
        """Raise GwyfileErrorCMsg in GwySelection._get_selection_points

        Raise GwyfileErrorCMsg in GwySelection._get_selection_points
        if get_sel_func returns False
        """
        falsep = ffi.new("bool*", False)
        self.get_sel_func.return_value = falsep[0]
        self.assertRaises(GwyfileErrorCMsg, GwySelection._get_selection_points,
                          self.gwysel, self.nsel)
Beispiel #20
0
    def test_raise_exception_if_graphcurvemodel_looks_unacceptable(self):
        """
        Raise GwyfileErrorCMsg if GwyGraphCurveModel looks unacceptable
        """

        falsep = ffi.new("bool*", False)
        self.mock_lib.gwyfile_object_graphcurvemodel_get.return_value = (
            falsep[0])
        self.assertRaises(GwyfileErrorCMsg, GwyGraphCurve._get_data,
                          self.gwycurve, self.npoints)
Beispiel #21
0
    def _label_has_frame(self, *args):
        """
        Write self.label_has_frame in 'label.has_frame' field
        """

        # combine fields names and fields pointers in one dictionary
        arg_keys = [ffi.string(key).decode('utf-8') for key in args[2:-1:2]]
        arg_pointers = [pointer for pointer in args[3:-1:2]]
        arg_dict = dict(zip(arg_keys, arg_pointers))

        truep = ffi.new("bool*", True)
        falsep = ffi.new("bool*", False)

        if self.label_has_frame:
            arg_dict['label.has_frame'][0] = truep[0]
        else:
            arg_dict['label.has_frame'][0] = falsep[0]

        # C func returns true if the graphmodel object loock acceptable
        return truep[0]
    def _test_returned_value(self, *args):
        """Write self.nsel in 'nsel' and return True
        """
        # combine fields names and fields pointers in one dictionary
        arg_keys = [ffi.string(key).decode('utf-8') for key in args[2:-1:2]]
        arg_pointers = [pointer for pointer in args[3:-1:2]]
        arg_dict = dict(zip(arg_keys, arg_pointers))

        arg_dict['nsel'][0] = self.nsel

        # Function should return True if object looks acceptable
        truep = ffi.new("bool*", True)
        return truep[0]
    def _get_points_side_effect(self, *args):
        """ Write self.cpoints in 'data' field  and return True
        """
        # combine fields names and fields pointers in one dictionary
        arg_keys = [ffi.string(key).decode('utf-8') for key in args[2:-1:2]]
        arg_pointers = [pointer for pointer in args[3:-1:2]]
        arg_dict = dict(zip(arg_keys, arg_pointers))

        arg_dict['data'][0] = self.cpoints

        # Function should return True if object looks acceptable
        truep = ffi.new("bool*", True)
        return truep[0]
    def _side_effect_return_metadata(self, *args):

        arg_keys = [ffi.string(key).decode('utf-8') for key in args[2:-1:2]]
        arg_pointers = [pointer for pointer in args[3:-1:2]]
        arg_dict = dict(zip(arg_keys, arg_pointers))

        for key in arg_dict:
            if key not in ['si_unit_xy', 'si_unit_z']:
                arg_dict[key][0] = self.test_metadata_dict[key]
            else:
                metadata_value = self.test_metadata_dict[key].encode('utf-8')
                metadata_c_str = ffi.new("char[]", metadata_value)
                arg_dict[key][0] = metadata_c_str
        return self.truep[0]
Beispiel #25
0
    def _label_position(self, *args):
        """
        Write self.label_position in 'label.position' field
        """

        # combine fields names and fields pointers in one dictionary
        arg_keys = [ffi.string(key).decode('utf-8') for key in args[2:-1:2]]
        arg_pointers = [pointer for pointer in args[3:-1:2]]
        arg_dict = dict(zip(arg_keys, arg_pointers))

        arg_dict['label.position'][0] = self.label_position
        # C func returns true if the graphmodel object loock acceptable
        truep = ffi.new("bool*", True)
        return truep[0]
Beispiel #26
0
    def _get_selection_nsel(cls, gwysel):
        """Get number of selections from the object

        Args:
            gwysel (GwyfileObject*):
                GwySelection object from Libgwyfile
                e.g. GwySelectionPoint object for point selection

        Returns:
            nsel (int):
                number of selections of this type in gwysel
        """

        error = ffi.new("GwyfileError*")
        errorp = ffi.new("GwyfileError**", error)
        nselp = ffi.new("int32_t*")

        if cls._get_sel_func(gwysel, errorp, ffi.new("char[]", b'nsel'), nselp,
                             ffi.NULL):
            nsel = nselp[0]
        else:
            raise GwyfileErrorCMsg(errorp[0].message)

        return nsel
Beispiel #27
0
    def from_gwy(filename):
        """Create Gwyfile instance from file

        Args:
            filename (string): filename including path

        Returns:
            Gwyfile:
                instnce of Gwyfile class

        """
        error = ffi.new("GwyfileError*")
        errorp = ffi.new("GwyfileError**", error)

        if not os.path.isfile(filename):
            raise OSError("Cannot find file {}".format(filename))

        c_gwyfile = lib.gwyfile_read_file(filename.encode('utf-8'), errorp)

        if not c_gwyfile:
            raise GwyfileErrorCMsg(errorp[0].message)

        gwyfile = Gwyfile(c_gwyfile)
        return gwyfile
Beispiel #28
0
    def _get_number_of_curves(self, *args):
        """
        Return 3 as a number of curves in graphmodel object
        """

        # combine fields names and fields pointers in one dictionary
        arg_keys = [ffi.string(key).decode('utf-8') for key in args[2:-1:2]]
        arg_pointers = [pointer for pointer in args[3:-1:2]]
        arg_dict = dict(zip(arg_keys, arg_pointers))

        arg_dict['ncurves'][0] = 3

        # C func returns true if the graphmodel object loock acceptable
        truep = ffi.new("bool*", True)
        return truep[0]
Beispiel #29
0
    def _x_unit_is_empty(self, *args):
        """
        Write NULL to x_unit field
        """

        # combine fields names and fields pointers in one dictionary
        arg_keys = [ffi.string(key).decode('utf-8') for key in args[2:-1:2]]
        arg_pointers = [pointer for pointer in args[3:-1:2]]
        arg_dict = dict(zip(arg_keys, arg_pointers))

        arg_dict['x_unit'][0] = ffi.NULL

        # C func returns true if the graphmodel object loock acceptable
        truep = ffi.new("bool*", True)
        return truep[0]
Beispiel #30
0
    def _getting_line_size_side_effect(self, *args):
        """
        Write self.line_size in 'line_size' field and return True
        """

        # combine fields names and fields pointers in one dictionary
        arg_keys = [ffi.string(key).decode('utf-8') for key in args[2:-1:2]]
        arg_pointers = [pointer for pointer in args[3:-1:2]]
        arg_dict = dict(zip(arg_keys, arg_pointers))

        arg_dict['line_size'][0] = self.line_size

        # C func returns true if the graphcurvemodel object loock acceptable
        truep = ffi.new("bool*", True)
        return truep[0]