Skip to content

Commit

Permalink
Bokeh plot with echem + nmr spectrum
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminCharmes committed Jan 28, 2025
1 parent a4af733 commit d040cd4
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 4 deletions.
93 changes: 91 additions & 2 deletions pydatalab/src/pydatalab/apps/insitu/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from typing import List, Tuple, Union

import bokeh.embed
import numpy as np
import pandas as pd
from bokeh.layouts import row
from bokeh.plotting import figure
from datalab_app_plugin_nmr_insitu import fitting_data, process_data

Expand Down Expand Up @@ -37,19 +39,34 @@ def process_and_store_data(self) -> bool:
ppm2 = float(self.data.get("ppm2", self.defaults["ppm2"]))

# Process data
nmr_data, df = process_data(
nmr_data, df, echem_df = process_data(
item_id="bc_insitu_block",
folder_name="Example-TEGDME.zip",
nmr_folder_name="2023-08-11_jana_insituLiLiTEGDME-02_galv",
echem_folder_name="LiLiTEGDMEinsitu_02",
ppm1=ppm1,
ppm2=ppm2,
)

self.data["nmr_data"] = {
"processed_data": df.to_dict(),
"parameters": nmr_data.get("parameters", {}),
"nmr_data": nmr_data.to_dict(),
}

echem_df_clean = echem_df.copy()
echem_df_clean = echem_df_clean.replace([np.inf, -np.inf], np.nan)
echem_df_clean = echem_df_clean.fillna("null")

for column in echem_df_clean.columns:
if echem_df_clean[column].dtype == "float64":
echem_df_clean[column] = echem_df_clean[column].replace("null", None)
echem_df_clean[column] = echem_df_clean[column].astype("float")
elif echem_df_clean[column].dtype == "int64":
echem_df_clean[column] = echem_df_clean[column].replace("null", None)
echem_df_clean[column] = echem_df_clean[column].astype("int")

self.data["echem_df"] = {"processed_data": echem_df_clean.to_dict()}

fitting_results = fitting_data(nmr_data, df)

self.data["fitting_results"] = {
Expand Down Expand Up @@ -169,6 +186,78 @@ def generate_insitu_nmr_plot(
plot_combined, theme=DATALAB_BOKEH_THEME
)

#! Temp echem
echem_df = pd.DataFrame(self.data["echem_df"]["processed_data"])

plot = selectable_axes_plot(
echem_df,
x_options=["time/s"],
y_options=["Voltage"],
plot_points=True,
plot_line=True,
plot_height=700,
plot_width=400,
)
self.data["bokeh_plot_data_3"] = bokeh.embed.json_item(plot, theme=DATALAB_BOKEH_THEME)

#!

echem_plot = figure(
x_axis_label="Voltage",
y_axis_label="Time (s)",
plot_height=700,
plot_width=400,
)

nmr_plot = figure(
x_axis_label="ppm",
y_axis_label="Intensity",
plot_height=700,
plot_width=400,
# x_range=(max(ppm_values), min(ppm_values))
y_axis_location="right",
)

echem_plot.line(
echem_df["Voltage"],
echem_df["time/s"],
line_color="blue",
)

nmr_data = self.data["nmr_data"]["nmr_data"]
if "index" in nmr_data:
nmr_df = pd.DataFrame(
data=nmr_data["data"], columns=nmr_data["columns"], index=nmr_data["index"]
)
else:
nmr_df = pd.DataFrame(nmr_data)

ppm_values = nmr_df["ppm"].values

num_experiments = len(nmr_df.columns) - 1
colors = [
f"#{int(255 * i/num_experiments):02x}00{int(255 * (1-i/num_experiments)):02x}"
for i in range(num_experiments)
]
for i in range(1, num_experiments + 1):
col_name = str(i)
if col_name in nmr_df.columns:
nmr_plot.line(
ppm_values,
nmr_df[col_name],
line_color=colors[i - 1],
line_alpha=0.5,
)

nmr_plot.legend.click_policy = "hide"
echem_plot.legend.click_policy = "hide"

combined_plot = row(echem_plot, nmr_plot)

self.data["bokeh_plot_data_4"] = bokeh.embed.json_item(
combined_plot, theme=DATALAB_BOKEH_THEME
)

return df, ["Plot successfully generated"]

except Exception as e:
Expand Down
31 changes: 29 additions & 2 deletions webapp/src/components/datablocks/NMRInsituBlock.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,28 @@
updateBlock();
"
/>
<button @click="print">Print</button>
<div v-if="ppmParseError" class="alert alert-danger mt-2 mx-auto">
{{ ppmParseError }}
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-6 pr-2">
<!-- <div class="col-6 pr-2">
<BokehPlot :key="'bokeh_plot1'" :bokeh-plot-data="bokehPlotData1" />
</div>
<div class="col-6 pl-2">
<BokehPlot :key="'bokeh_plot2'" :bokeh-plot-data="bokehPlotData2" />
<div class="mb-2">
<button v-show="file_id" class="btn btn-secondary" @click="togglePlot">
Switch to {{ showPlot2 ? "echem" : "fitting" }}
</button>
</div>
<BokehPlot v-if="showPlot2" :key="'bokeh_plot2'" :bokeh-plot-data="bokehPlotData2" />
<BokehPlot v-else :key="'bokeh_plot3'" :bokeh-plot-data="bokehPlotData3" />
</div> -->
<div id="bokehPlotContainer" class="col-xl-8 col-lg-8 col-md-11 mx-auto">
<BokehPlot :bokeh-plot-data="bokehPlotData4" />
</div>
</div>
</DataBlockBase>
Expand Down Expand Up @@ -82,6 +92,7 @@ export default {
data() {
return {
ppmParseError: "",
showPlot2: true,
};
},
computed: {
Expand All @@ -93,6 +104,14 @@ export default {
return this.$store.state.all_item_data[this.item_id]["blocks_obj"][this.block_id]
.bokeh_plot_data_2;
},
bokehPlotData3() {
return this.$store.state.all_item_data[this.item_id]["blocks_obj"][this.block_id]
.bokeh_plot_data_3;
},
bokehPlotData4() {
return this.$store.state.all_item_data[this.item_id]["blocks_obj"][this.block_id]
.bokeh_plot_data_4;
},
blockInfo() {
return this.$store.state.blocksInfos["insitu"];
},
Expand All @@ -115,6 +134,14 @@ export default {
this.$store.state.all_item_data[this.item_id]["blocks_obj"][this.block_id],
);
},
togglePlot() {
this.showPlot2 = !this.showPlot2;
},
print() {
console.log("#%$#$%#$%#%$%#$$%#%$#%$#");
console.log(this.$store.state.all_item_data[this.item_id]["blocks_obj"][this.block_id]);
console.log("#%$#$%#$%#%$%#$$%#%$#%$#");
},
},
};
</script>
Expand Down

0 comments on commit d040cd4

Please sign in to comment.