Skip to content

Linear

DragLineManager

Bases: DragPointCollection

Collection of DragPoints representing a straight line.

Source code in itfit/fit_functions/linear/linear.py
class DragLineManager(DragPointCollection):
    """Collection of DragPoints representing a straight line."""

    @staticmethod
    def function(x, m, n):
        """Straight line function.

        Parameters:
            x (float):
                independent variable.
            m (float):
                slope.
            n (float):
                value at `x=0`.

        Returns:
            (float):
                `f(x) = m*x+n`
        """
        return m*x + n

    @staticmethod
    def gradient(x, m, n):
        """Straigth line gradient.

        Parameters:
            x (float):
                independent variable.
            m (float):
                slope.
            n (float):
                value at 'x = 0'.

        Returns:
            (np.array):
                ' ( x, 1 )'
        """
        dfdm = x 
        dfdn = 1
        return np.array([[dfdm], [dfdn]])

    @staticmethod
    def get_args_length():
        """Gets number of arguments of `function`.

        Returns:
            (int): Number of arguments of `function`.
        """
        return 2

    def __init__(self, dragpoints: list[DragPoint], blit_manager: BlitManager):
        """Line between multiple DragPoints. Updates with them.

        Parameters:
            dragpoints (list[DragPoint]): line vertices.
            blit_manager (BlitManager): used for automtic ploting.
        """
        super().__init__(dragpoints, blit_manager)
        self.update()

    def update(self, *args, **kargs):
        """Updates line data with DragObjects positions"""
        x0, x1 = self.get_xdata()
        m, n = self.get_args()

        if (m,n) == (0,0):
            self.poly.set_xdata(self.get_xdata_display())
            self.poly.set_ydata(self.get_ydata_display())

        # create x and y data
        dx = abs(x0-x1)*0.5
        x = np.linspace(min(x0,x1)-dx, max(x0,x1)+dx, 250)
        y = self.function(x, m, n)

        # from data coordinates to display coordinates
        xy = np.array((x,y)).T.reshape(-1, 2)
        x_data, y_data = self.set_xy(xy).T

        # set new data
        self.poly.set_xdata(x_data)
        self.poly.set_ydata(y_data)

    def get_args(self):
        """Gives linear function parameters.

        Returns:
            (Tuple[float, float]):
                `m` and `n` of `f(x)=m*x + n`.
        """
        x0, x1 = self.get_xdata()
        y0, y1 = self.get_ydata()

        if (x1-x0)==0:
            return 0.,0.
        m:float = (y1 - y0)/(x1 - x0)
        n:float = m*(-x1)+y1
        return m, n

__init__(dragpoints, blit_manager)

Line between multiple DragPoints. Updates with them.

Parameters:

Name Type Description Default
dragpoints list[DragPoint]

line vertices.

required
blit_manager BlitManager

used for automtic ploting.

required
Source code in itfit/fit_functions/linear/linear.py
def __init__(self, dragpoints: list[DragPoint], blit_manager: BlitManager):
    """Line between multiple DragPoints. Updates with them.

    Parameters:
        dragpoints (list[DragPoint]): line vertices.
        blit_manager (BlitManager): used for automtic ploting.
    """
    super().__init__(dragpoints, blit_manager)
    self.update()

function(x, m, n) staticmethod

Straight line function.

Parameters:

Name Type Description Default
x float

independent variable.

required
m float

slope.

required
n float

value at x=0.

required

Returns:

Type Description
float

f(x) = m*x+n

Source code in itfit/fit_functions/linear/linear.py
@staticmethod
def function(x, m, n):
    """Straight line function.

    Parameters:
        x (float):
            independent variable.
        m (float):
            slope.
        n (float):
            value at `x=0`.

    Returns:
        (float):
            `f(x) = m*x+n`
    """
    return m*x + n

get_args()

Gives linear function parameters.

Returns:

Type Description
Tuple[float, float]

m and n of f(x)=m*x + n.

Source code in itfit/fit_functions/linear/linear.py
def get_args(self):
    """Gives linear function parameters.

    Returns:
        (Tuple[float, float]):
            `m` and `n` of `f(x)=m*x + n`.
    """
    x0, x1 = self.get_xdata()
    y0, y1 = self.get_ydata()

    if (x1-x0)==0:
        return 0.,0.
    m:float = (y1 - y0)/(x1 - x0)
    n:float = m*(-x1)+y1
    return m, n

get_args_length() staticmethod

Gets number of arguments of function.

Returns:

Type Description
int

Number of arguments of function.

Source code in itfit/fit_functions/linear/linear.py
@staticmethod
def get_args_length():
    """Gets number of arguments of `function`.

    Returns:
        (int): Number of arguments of `function`.
    """
    return 2

gradient(x, m, n) staticmethod

Straigth line gradient.

Parameters:

Name Type Description Default
x float

independent variable.

required
m float

slope.

required
n float

value at 'x = 0'.

required

Returns:

Type Description
np.array

' ( x, 1 )'

Source code in itfit/fit_functions/linear/linear.py
@staticmethod
def gradient(x, m, n):
    """Straigth line gradient.

    Parameters:
        x (float):
            independent variable.
        m (float):
            slope.
        n (float):
            value at 'x = 0'.

    Returns:
        (np.array):
            ' ( x, 1 )'
    """
    dfdm = x 
    dfdn = 1
    return np.array([[dfdm], [dfdn]])

update(*args, **kargs)

Updates line data with DragObjects positions

Source code in itfit/fit_functions/linear/linear.py
def update(self, *args, **kargs):
    """Updates line data with DragObjects positions"""
    x0, x1 = self.get_xdata()
    m, n = self.get_args()

    if (m,n) == (0,0):
        self.poly.set_xdata(self.get_xdata_display())
        self.poly.set_ydata(self.get_ydata_display())

    # create x and y data
    dx = abs(x0-x1)*0.5
    x = np.linspace(min(x0,x1)-dx, max(x0,x1)+dx, 250)
    y = self.function(x, m, n)

    # from data coordinates to display coordinates
    xy = np.array((x,y)).T.reshape(-1, 2)
    x_data, y_data = self.set_xy(xy).T

    # set new data
    self.poly.set_xdata(x_data)
    self.poly.set_ydata(y_data)

LineFitter

Bases: GenericFitter

Linear function fitter.

Source code in itfit/fit_functions/linear/linear.py
class LineFitter(GenericFitter):
    """Linear function fitter."""
    name = 'linear'

    def __init__(self, app, data: DataSelection):
        """Linear fitter following function `f(x)=m*x + n`.

        Parameters:
            app (Fitter): 
                Main application.
            data (DataSelection): 
                Data to fit.
        """
        super().__init__(app, data)

        ## Create DragPoints and DragLines needed

        self.drag_points = [DragPoint(*self.ax.transAxes.transform((0.2,0.3)), None), 
                            DragPoint(*self.ax.transAxes.transform((0.8,0.7)), None)]
        self.drag_points_managers = [DragPointManager(p, self.app.blit_manager) for p in self.drag_points]
        self.fitter_drag_collection = DragLineManager(self.drag_points, self.app.blit_manager)
        self.function = self.fitter_drag_collection.function
        self.gradient = self.fitter_drag_collection.gradient

        ## Connect Line to Points change events
        self.drag_points_cids = [] # Connections ids for change events
        for dp in self.drag_points_managers:
            self.drag_points_cids.append(
                dp.connect(self.fitter_drag_collection.update)
            )

        ## Add created DragPoints and DragLines to BlitManager's artists
        self.app.blit_manager.artists.append(self.fitter_drag_collection)
        for dpm in self.drag_points_managers:
            self.app.blit_manager.artists.append(dpm)

        self.fig.canvas.draw_idle()

__init__(app, data)

Linear fitter following function f(x)=m*x + n.

Parameters:

Name Type Description Default
app Fitter

Main application.

required
data DataSelection

Data to fit.

required
Source code in itfit/fit_functions/linear/linear.py
def __init__(self, app, data: DataSelection):
    """Linear fitter following function `f(x)=m*x + n`.

    Parameters:
        app (Fitter): 
            Main application.
        data (DataSelection): 
            Data to fit.
    """
    super().__init__(app, data)

    ## Create DragPoints and DragLines needed

    self.drag_points = [DragPoint(*self.ax.transAxes.transform((0.2,0.3)), None), 
                        DragPoint(*self.ax.transAxes.transform((0.8,0.7)), None)]
    self.drag_points_managers = [DragPointManager(p, self.app.blit_manager) for p in self.drag_points]
    self.fitter_drag_collection = DragLineManager(self.drag_points, self.app.blit_manager)
    self.function = self.fitter_drag_collection.function
    self.gradient = self.fitter_drag_collection.gradient

    ## Connect Line to Points change events
    self.drag_points_cids = [] # Connections ids for change events
    for dp in self.drag_points_managers:
        self.drag_points_cids.append(
            dp.connect(self.fitter_drag_collection.update)
        )

    ## Add created DragPoints and DragLines to BlitManager's artists
    self.app.blit_manager.artists.append(self.fitter_drag_collection)
    for dpm in self.drag_points_managers:
        self.app.blit_manager.artists.append(dpm)

    self.fig.canvas.draw_idle()

LineTool

Bases: GenericFitterTool

Toggles Line Tool.

Source code in itfit/fit_functions/linear/linear.py
class LineTool(GenericFitterTool):
    """Toggles Line Tool."""

    # default_keymap = ''
    description = 'Line me please'

    def enable(self, *args):
        """Triggered when LineTool is enabled.
        Uses BlitManager for faster rendering of DragObjects.
        """
        super().enable()
        self.fitter = LineFitter(self.app, self.data)

    def disable(self, *args):
        """Triggered when LineTool is disabled.
        Removes DragObjects and disables BlitManager.
        """
        super().disable()

disable(*args)

Triggered when LineTool is disabled. Removes DragObjects and disables BlitManager.

Source code in itfit/fit_functions/linear/linear.py
def disable(self, *args):
    """Triggered when LineTool is disabled.
    Removes DragObjects and disables BlitManager.
    """
    super().disable()

enable(*args)

Triggered when LineTool is enabled. Uses BlitManager for faster rendering of DragObjects.

Source code in itfit/fit_functions/linear/linear.py
def enable(self, *args):
    """Triggered when LineTool is enabled.
    Uses BlitManager for faster rendering of DragObjects.
    """
    super().enable()
    self.fitter = LineFitter(self.app, self.data)