Coverage for src/instawell/figures/fig_02_averaged.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_averaged_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 averaged_data_path = ctx.experiment_dir / StepFiles.AVERAGED_DATA
32 if not averaged_data_path.exists():
33 raise FileNotFoundError(f"Averaged data file not found: {averaged_data_path}")
34 data_df = pd.read_csv(averaged_data_path)
36 # ensure the first column is 'Temperature'
37 if data_df.columns[0] != "Temperature":
38 raise ValueError("The first column must be 'Temperature'.")
40 plotting_df = data_df.melt(id_vars=["Temperature"], var_name="unqcond", value_name="value")
41 # ensure the necessary columns are present
42 plotting_df = split_unqcon_column(plotting_df)
43 required_columns = [
44 "Temperature",
45 "unqcond",
46 "value",
47 "concentration",
48 "ligand",
49 "protein",
50 "buffer",
51 ]
53 for col in required_columns:
54 if col not in plotting_df.columns:
55 raise ValueError(f"Missing required column: {col} in the data.")
56 grouped_data = plotting_df.groupby(["ligand", "protein", "buffer"])
58 for (ligand, protein, buffer), group in grouped_data:
59 fig = px.line(
60 group,
61 x="Temperature",
62 y="value",
63 color="unqcond",
64 title=f"Avg Raw Data for {ligand} and {protein} in {buffer} Buffer",
65 )
66 yield fig