Skip to content

Commit

Permalink
Initial commit of autogain detector plot scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-l-kong committed Nov 1, 2023
1 parent dff46a0 commit 8a5717f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
83 changes: 83 additions & 0 deletions src/toffy/normalize.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# adapted from https://machinelearningmastery.com/curve-fitting-with-python/
import copy
import glob
import json
import os
import pathlib
import warnings
from typing import Literal, Union

import matplotlib.pyplot as plt
import natsort as ns
Expand Down Expand Up @@ -892,3 +896,82 @@ def check_detector_voltage(run_dir):
# non-empty list of changes will raise an error
if changes_in_voltage:
raise ValueError("Changes in detector voltage were found during the run:\n" + err_str)


def plot_detector_voltage(
base_dir: Union[pathlib.Path, str],
mph_run_dir: Union[pathlib.Path, str],
detector_volt_type: Literal["hvAdc", "hvDac"] = "hvAdc",
) -> None:
"""Extracts saved detector voltage values during fov acquisition and creates a txt file and png plot with
detector voltage over fov progression.
Args:
base_dir (Union[pathlib.Path, str]):
Path to where the run data (and json files) are stored.
mph_run_dir (Union[pathlib.Path, str]):
Path to where the run metrics are stored.
detector_volt_type (Union["hvAdc", "hvDac"]):
Either "hvAdc" (default) or "hvDac" voltage metric to plot.
"""
# Get all the JSON run files
json_files = sorted(glob.glob(os.path.join(base_dir, "fov-*-scan-?.json")))

# Map each FOV to their observed detector voltage
detector_voltage_data = {"FOVs": [], "Detector Voltage": []}

# Iterate over the list of files
for json_file in json_files:
# Open the JSON file and load the data
with open(json_file, "r") as f:
json_data = json.load(f)

# Extract the FOV from the filename
fov = os.path.basename(json_file).split("-")[1]

# Extract the Detector voltage
# Assuming the "hvAdc" and/or "hvDac" field is always present and has the "Detector" entry
if detector_volt_type == "hvAdc":
detector_voltage = [
item["value"] for item in json_data["hvAdc"] if item["name"] == "Detector"
][0]
else:
detector_voltage = [
item["currentSetPoint"] for item in json_data["hvDac"] if item["name"] == "Detector"
][0]

# Add the data to our dictionary
detector_voltage_data["FOVs"].append(int(fov))
detector_voltage_data["Detector Voltage"].append(detector_voltage)

# Save data in a text log
detector_gain_file = os.path.join(mph_run_dir, "detector_gain_values.csv")
detector_gain_df = pd.DataFrame(detector_voltage_data)
detector_gain_df.to_csv(detector_gain_file, index=False)

# Create a new figure and axes
fig, ax = plt.subplots()

# Plot the data with color coding and without a line connecting the points
for (
fov,
voltage,
) in zip(detector_voltage_data["FOVs"], detector_voltage_data["Detector Voltage"]):
ax.plot(fov, voltage, marker="o", color="blue")

# Set the labels for the axes
ax.set_xlabel("FOVs")
ax.set_ylabel("Detector Voltage (v)")

# Show the plot
plt.savefig(
fname=os.path.join(mph_run_dir, "detector_gain_plot.png"),
dpi="figure",
format="png",
metadata=None,
bbox_inches=None,
pad_inches=0.1,
facecolor="auto",
edgecolor="auto",
backend=None,
)
17 changes: 14 additions & 3 deletions templates/4b_normalize_image_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
" - `run_name` should contain the exact name of the MIBI run to extract from\n",
" - `panel_path` should point to a panel csv specifying the targets on your panel (see [panel format](https://github.com/angelolab/toffy#panel-format) for more information)\n",
" - `tuning_curve_file` should point to a tuning curve contained in `toffy/tuning_curves` (`avg_norm_func_450.json`, `avg_norm_func_1300.json`, or `avg_norm_func_2600.json`).\n",
" - `autogain`: if the Ionpath autogain setting was used for this run\n",
"\n",
"`avg_norm_func_2600.json` is the curve that should be used when running the MIBI with Ionpath's default settings. `avg_norm_func_450.json` and `avg_norm_func_1300.json` are curves generated for Angelo Lab-specific parameters. Use the power supply settings to determine which curve is most applicable."
]
Expand All @@ -59,7 +60,10 @@
"panel_path = 'C:\\\\Users\\\\Customer.ION\\\\Documents\\\\panel_files\\\\my_cool_panel.csv'\n",
"\n",
"# Name of tuning curve file\n",
"tuning_curve_file = 'avg_norm_func_2600.json'"
"tuning_curve_file = 'avg_norm_func_2600.json'\n",
"\n",
"# Autogain setting of run\n",
"autogain = True"
]
},
{
Expand All @@ -85,8 +89,15 @@
"normalized_base_dir = 'D:\\\\Normalized_Images'\n",
"mph_run_dir = os.path.join('C:\\\\Users\\\\Customer.ION\\\\Documents\\\\run_metrics', run_name, 'fov_data')\n",
"\n",
"# check the run for changes in detector voltage\n",
"normalize.check_detector_voltage(os.path.join(bin_base_dir, run_name))"
"# plot the voltage change across FOVs if autogain set\n",
"# otherwise, verify the voltage across all FOVs is constant\n",
"if autogain:\n",
" normalize.plot_detector_voltage(\n",
" base_dir=os.path.join(bin_base_dir, run_name),\n",
" mph_run_dir=os.path.dirname(mph_run_dir, run_name)\n",
" )\n",
"else:\n",
" normalize.check_detector_voltage(os.path.join(bin_base_dir, run_name))"
]
},
{
Expand Down

0 comments on commit 8a5717f

Please sign in to comment.