Skip to content

Allow retrieval of preceding row #374

Description

@NicolaCourtier

Hi, I've been using a wrapper that includes the row immediately preceding a cycle or step as part of the dataset. It would be great to incorporate this functionality into PyProBE.

The following function can be used with filtering, for example:

  • filter_with_preceding_row(procedure, experiment=experiment_name)
  • filter_with_preceding_row(experiment, cycle=i)
  • filter_with_preceding_row(experiment, phase="discharge", step=i).
import polars as pl


def filter_with_preceding_row(
    procedure,
    experiment: str | None = None,
    cycle: int | None = None,
    phase: str | None = None,
    step: int | None = None,
):
    """
    Obtain the PyProBE result including the preceding row of the data.
    """
    data = procedure
    if experiment is not None:
        data = data.experiment(experiment)
    if cycle is not None:
        data = data.cycle(cycle)
    if phase is not None:
        if phase == "discharge":
            data = data.discharge()
        elif phase == "charge":
            data = data.charge()
        elif phase == "rest":
            data = data.rest()
        else:
            raise ValueError("Unrecognised phase")
    if step is not None:
        data = data.step(step)

    # Add the preceding row
    start_time = data.lf.select("Time [s]").first().collect().item()
    preceding_row = procedure.lf.filter(pl.col("Time [s]") < start_time).last()
    data.lf = pl.concat((preceding_row, data.lf), how="diagonal_relaxed")

    return data

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions