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