Skip to content

motion_correct

fptools.preprocess.steps.motion_correct

Classes:

  • MotionCorrect

    A Processor that estimates and corrects for motion artifacts.

MotionCorrect

Bases: ProcessorThatPlots

A Processor that estimates and corrects for motion artifacts.

Methods:

  • __call__

    Effect this processing step.

  • __init__

    Initialize this processor.

  • plot

    Plot the effects of this processing step. Will show the motion estimate and the corrected signal.

Source code in fptools/preprocess/steps/motion_correct.py
class MotionCorrect(ProcessorThatPlots):
    """A `Processor` that estimates and corrects for motion artifacts."""

    def __init__(self, signals: PairedSignalList):
        """Initialize this processor.

        Args:
            signals: list of signal names to be downsampled
        """
        self.signals = signals

    def __call__(self, session: Session) -> Session:
        """Effect this processing step.

        Args:
            session: the session to operate upon

        Returns:
            Session with the processing step applied
        """
        for sig1, sig2 in self.signals:
            exp = session.signals[sig1]
            ctr = session.signals[sig2]

            # create a signal to hold the motion estimate data
            est_motion = exp.copy(f"{exp.name}_motion_est")
            est_motion.units = "AU"
            session.add_signal(est_motion)

            # calculate the motion estimate and corrected signal
            exp.signal, est_motion.signal = estimate_motion(exp.signal, ctr.signal)

        return session

    def plot(self, session: Session, ax: Axes):
        """Plot the effects of this processing step. Will show the motion estimate and the corrected signal.

        Args:
            session: the session being operated upon
            ax: matplotlib Axes for plotting onto
        """
        palette = sns.color_palette("colorblind", n_colors=len(self.signals))
        for i, (signame, _) in enumerate(self.signals):
            sig = session.signals[signame]
            mot = session.signals[f"{signame}_motion_est"]
            plot_signal(sig, ax=ax, show_indv=True, color=palette[i], indv_c=palette[i], agg_kwargs={"label": sig.name})
            plot_signal(
                mot,
                ax=ax,
                show_indv=True,
                color=sns.desaturate(palette[i], 0.3),
                indv_c=sns.desaturate(palette[i], 0.3),
                agg_kwargs={"label": mot.name},
            )
        ax.set_title("Motion Estimation and Correction")
        ax.legend(loc="upper left")
        sns.move_legend(ax, loc="upper left", bbox_to_anchor=(1, 1))

__call__(session)

Effect this processing step.

Parameters:

  • session (Session) –

    the session to operate upon

Returns:

  • Session

    Session with the processing step applied

Source code in fptools/preprocess/steps/motion_correct.py
def __call__(self, session: Session) -> Session:
    """Effect this processing step.

    Args:
        session: the session to operate upon

    Returns:
        Session with the processing step applied
    """
    for sig1, sig2 in self.signals:
        exp = session.signals[sig1]
        ctr = session.signals[sig2]

        # create a signal to hold the motion estimate data
        est_motion = exp.copy(f"{exp.name}_motion_est")
        est_motion.units = "AU"
        session.add_signal(est_motion)

        # calculate the motion estimate and corrected signal
        exp.signal, est_motion.signal = estimate_motion(exp.signal, ctr.signal)

    return session

__init__(signals)

Initialize this processor.

Parameters:

  • signals (PairedSignalList) –

    list of signal names to be downsampled

Source code in fptools/preprocess/steps/motion_correct.py
def __init__(self, signals: PairedSignalList):
    """Initialize this processor.

    Args:
        signals: list of signal names to be downsampled
    """
    self.signals = signals

plot(session, ax)

Plot the effects of this processing step. Will show the motion estimate and the corrected signal.

Parameters:

  • session (Session) –

    the session being operated upon

  • ax (Axes) –

    matplotlib Axes for plotting onto

Source code in fptools/preprocess/steps/motion_correct.py
def plot(self, session: Session, ax: Axes):
    """Plot the effects of this processing step. Will show the motion estimate and the corrected signal.

    Args:
        session: the session being operated upon
        ax: matplotlib Axes for plotting onto
    """
    palette = sns.color_palette("colorblind", n_colors=len(self.signals))
    for i, (signame, _) in enumerate(self.signals):
        sig = session.signals[signame]
        mot = session.signals[f"{signame}_motion_est"]
        plot_signal(sig, ax=ax, show_indv=True, color=palette[i], indv_c=palette[i], agg_kwargs={"label": sig.name})
        plot_signal(
            mot,
            ax=ax,
            show_indv=True,
            color=sns.desaturate(palette[i], 0.3),
            indv_c=sns.desaturate(palette[i], 0.3),
            agg_kwargs={"label": mot.name},
        )
    ax.set_title("Motion Estimation and Correction")
    ax.legend(loc="upper left")
    sns.move_legend(ax, loc="upper left", bbox_to_anchor=(1, 1))