Coverage for src/instawell/figures/fig_05_derivative.py: 0%
24 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-07 15:47 -0600
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-07 15:47 -0600
1import logging
3import pandas as pd
4import plotly.express as px
6from instawell.core.exp_context import ExperimentContext
7from instawell.core.steps import StepFiles
8from instawell.utils.utils import split_unqcon_column
10# set logging level to INFO
12logger = logging.getLogger(__name__)
15def create_derivative_figures_generator(ctx: ExperimentContext):
16 """
17 Generates Plotly figures from grouped data.
19 This function groups the input DataFrame by 'ligand', 'protein', and
20 'buffer', then yields a line plot figure for each group.
22 Args:
23 data_df: A pandas DataFrame containing the data to plot.
24 It must include 'ligand', 'protein', 'buffer',
25 'Temperature', 'value', and 'well_unqcond' columns.
27 Yields:
28 A Plotly figure object for each group.
29 """
30 # Load the organized data
31 derivative_data_path = ctx.experiment_dir / StepFiles.DERIVATIVE_DATA
32 if not derivative_data_path.exists():
33 raise FileNotFoundError(f"Derivative data file not found: {derivative_data_path}")
34 data_df = pd.read_csv(derivative_data_path)
35 # ensure the first column is 'Temperature'
36 if data_df.columns[0] != "Temperature":
37 raise ValueError("The first column must be 'Temperature'.")
39 plotting_df = data_df.melt(id_vars=["Temperature"], var_name="unqcond", value_name="value")
40 # ensure the necessary columns are present
41 plotting_df = split_unqcon_column(plotting_df)
42 required_columns = [
43 "Temperature",
44 "unqcond",
45 "value",
46 "concentration",
47 "ligand",
48 "protein",
49 "buffer",
50 ]
52 for col in required_columns:
53 if col not in plotting_df.columns:
54 raise ValueError(f"Missing required column: {col} in the data.")
55 grouped_data = plotting_df.groupby(["ligand", "protein", "buffer"])
57 for (ligand, protein, buffer), group in grouped_data:
58 fig = px.line(
59 group,
60 x="Temperature",
61 y="value",
62 color="unqcond",
63 title=f"Derivative Data for {ligand} and {protein} in {buffer} Buffer",
64 )
65 yield fig