Coverage for src/instawell/figures/fig_03_bgsub_raw.py: 0%

24 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-07 15:47 -0600

1import logging 

2 

3import pandas as pd 

4import plotly.express as px 

5 

6from instawell.core.exp_context import ExperimentContext 

7from instawell.core.steps import StepFiles 

8from instawell.utils.utils import split_unqcon_column 

9 

10# set logging level to INFO 

11 

12logger = logging.getLogger(__name__) 

13 

14 

15def create_bgsubtracted_figures_generator(ctx: ExperimentContext): 

16 """ 

17 Generates Plotly figures from grouped data. 

18 

19 This function groups the input DataFrame by 'ligand', 'protein', and 

20 'buffer', then yields a line plot figure for each group. 

21 

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. 

26 

27 Yields: 

28 A Plotly figure object for each group. 

29 """ 

30 # Load the organized data 

31 bg_data_path = ctx.experiment_dir / StepFiles.BG_SUB_DATA 

32 if not bg_data_path.exists(): 

33 raise FileNotFoundError(f"BG subtracted data file not found: {bg_data_path}") 

34 data_df = pd.read_csv(bg_data_path) 

35 

36 # ensure the first column is 'Temperature' 

37 if data_df.columns[0] != "Temperature": 

38 raise ValueError("The first column must be 'Temperature'.") 

39 

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 ] 

52 

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"]) 

57 

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"BG subtracted Data for {ligand} and {protein} in {buffer} Buffer", 

65 ) 

66 yield fig