Skip to content

Cosine

CosineFitter

Bases: GenericFitter

Cosine function fitter.

Source code in itfit/fit_functions/cosine/cosine.py
class CosineFitter(GenericFitter):
    """Cosine function fitter."""
    name = 'cosine'

    def __init__(self, app, data: DataSelection):
        """Cosine fitter following function 'f(x) = a*cos(b*x+b)'.

        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 = DragCosineManager(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)

Cosine fitter following function 'f(x) = acos(bx+b)'.

Parameters:

Name Type Description Default
app Fitter

Main application.

required
data DataSelection

Data to fit.

required
Source code in itfit/fit_functions/cosine/cosine.py
def __init__(self, app, data: DataSelection):
    """Cosine fitter following function 'f(x) = a*cos(b*x+b)'.

    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 = DragCosineManager(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()

CosineTool

Bases: GenericFitterTool

Toggles CosineTool.

Source code in itfit/fit_functions/cosine/cosine.py
class CosineTool(GenericFitterTool):
    """Toggles CosineTool."""

    # default_keymap = ''
    description = 'Cosine me please'

    def enable(self,*args):
        """Triggered when CosineTool is enabled.
        Uses BlitManager for faster rendering of DragObjects.
        """

        super().enable()
        self.fitter = CosineFitter(self.app,self.data)

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

        super().disable()

disable(*args)

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

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

    super().disable()

enable(*args)

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

Source code in itfit/fit_functions/cosine/cosine.py
def enable(self,*args):
    """Triggered when CosineTool is enabled.
    Uses BlitManager for faster rendering of DragObjects.
    """

    super().enable()
    self.fitter = CosineFitter(self.app,self.data)

DragCosineManager

Bases: DragPointCollection

Collection of DragPoints representing a cosine function.

Source code in itfit/fit_functions/cosine/cosine.py
class DragCosineManager(DragPointCollection):
    """Collection of DragPoints representing a cosine function."""

    @staticmethod
    def function(x, a, b, c, d):
        """Cosine function.

        Parameters:
            x (float):
                independent variable.
            a (float):
                Amplitude of the wave.
            b (float):
                frequency of the wave.
            c (float):
                centre of the cosine function.
            d (float):
                constant value around which the wave oscillates.

        Returns:
            (float):
                `f(x) = a*cos(b*x+c)+d`
        """
        return a*np.cos(b*x+c) + d 

    @staticmethod
    def gradient(x, a, b, c, d):
        """Cosine gradient function.

        Parameters:
            x (float):
                independent variable.
            a (float):
                Amplitude of the wave.
            b (float):
                frequency of the wave.
            c (float):
                centre of the cosine function.
            d (float):
                constant value around which the wave oscillates.

        Returns:
            (np,array):
                ( cos(b*x + c), -a*x*sin(b*x+c), -a*sin(b*x+c), 1)
        """
        dfda = np.cos(b*x + c)
        dfdb = -a * x* np.sin(b*x +c)
        dfdc = -a * np.sin(b*x + c)
        dfdd = 1
        return np.array ([[dfda], [dfdb], [dfdc], [dfdd]])

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

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

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

        Parameters:
            dragpoints (list[DragPoint]): wave points.
            blit_manager (BlitManager): used for automtic ploting.
        """
        super().__init__(dragpoints, blit_manager)
        self.point_1 = self.dragpoints[0]
        self.point_2 = self.dragpoints[1]
        self.update()

    def update(self, *args, **kargs):
        """Updates function data with DragObjects positions"""
        p1_x, p2_x = self.get_xdata()
        a,b,c,d = self.get_args()

        #create x and y data of trigonometric function that moves across two points
        dx = abs(p1_x-p2_x) * 1.5 
        x = np.linspace(min(p1_x,p2_x)-dx,max(p1_x,p2_x)+dx,250)
        y = self.function(x, a, b, c,d)

        # 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 cosine function parameters.

        Returns:
            (Tuple[float, float]):
                `a`, `b`, `c` and `d` of `f(x)=a*cos(b*x +c)+d`.
        """
        x0, x1 = self.get_xdata()
        y0, y1 = self.get_ydata()

        a = abs(y0-y1)
        b = - np.pi/2 / (x0-x1)
        c = np.pi/2 -b *x1 
        d = y1

        return a, b, c, d

__init__(dragpoints, blit_manager)

Cosine function between multiple DragPoints. Updates with them.

Parameters:

Name Type Description Default
dragpoints list[DragPoint]

wave points.

required
blit_manager BlitManager

used for automtic ploting.

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

    Parameters:
        dragpoints (list[DragPoint]): wave points.
        blit_manager (BlitManager): used for automtic ploting.
    """
    super().__init__(dragpoints, blit_manager)
    self.point_1 = self.dragpoints[0]
    self.point_2 = self.dragpoints[1]
    self.update()

function(x, a, b, c, d) staticmethod

Cosine function.

Parameters:

Name Type Description Default
x float

independent variable.

required
a float

Amplitude of the wave.

required
b float

frequency of the wave.

required
c float

centre of the cosine function.

required
d float

constant value around which the wave oscillates.

required

Returns:

Type Description
float

f(x) = a*cos(b*x+c)+d

Source code in itfit/fit_functions/cosine/cosine.py
@staticmethod
def function(x, a, b, c, d):
    """Cosine function.

    Parameters:
        x (float):
            independent variable.
        a (float):
            Amplitude of the wave.
        b (float):
            frequency of the wave.
        c (float):
            centre of the cosine function.
        d (float):
            constant value around which the wave oscillates.

    Returns:
        (float):
            `f(x) = a*cos(b*x+c)+d`
    """
    return a*np.cos(b*x+c) + d 

get_args()

Gives cosine function parameters.

Returns:

Type Description
Tuple[float, float]

a, b, c and d of f(x)=a*cos(b*x +c)+d.

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

    Returns:
        (Tuple[float, float]):
            `a`, `b`, `c` and `d` of `f(x)=a*cos(b*x +c)+d`.
    """
    x0, x1 = self.get_xdata()
    y0, y1 = self.get_ydata()

    a = abs(y0-y1)
    b = - np.pi/2 / (x0-x1)
    c = np.pi/2 -b *x1 
    d = y1

    return a, b, c, d

get_args_length()

Gets number of arguments of function.

Returns:

Type Description
int

Number of arguments of function.

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

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

gradient(x, a, b, c, d) staticmethod

Cosine gradient function.

Parameters:

Name Type Description Default
x float

independent variable.

required
a float

Amplitude of the wave.

required
b float

frequency of the wave.

required
c float

centre of the cosine function.

required
d float

constant value around which the wave oscillates.

required

Returns:

Type Description
np, array

( cos(bx + c), -axsin(bx+c), -asin(bx+c), 1)

Source code in itfit/fit_functions/cosine/cosine.py
@staticmethod
def gradient(x, a, b, c, d):
    """Cosine gradient function.

    Parameters:
        x (float):
            independent variable.
        a (float):
            Amplitude of the wave.
        b (float):
            frequency of the wave.
        c (float):
            centre of the cosine function.
        d (float):
            constant value around which the wave oscillates.

    Returns:
        (np,array):
            ( cos(b*x + c), -a*x*sin(b*x+c), -a*sin(b*x+c), 1)
    """
    dfda = np.cos(b*x + c)
    dfdb = -a * x* np.sin(b*x +c)
    dfdc = -a * np.sin(b*x + c)
    dfdd = 1
    return np.array ([[dfda], [dfdb], [dfdc], [dfdd]])

update(*args, **kargs)

Updates function data with DragObjects positions

Source code in itfit/fit_functions/cosine/cosine.py
def update(self, *args, **kargs):
    """Updates function data with DragObjects positions"""
    p1_x, p2_x = self.get_xdata()
    a,b,c,d = self.get_args()

    #create x and y data of trigonometric function that moves across two points
    dx = abs(p1_x-p2_x) * 1.5 
    x = np.linspace(min(p1_x,p2_x)-dx,max(p1_x,p2_x)+dx,250)
    y = self.function(x, a, b, c,d)

    # 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)