Skip to content

Commit

Permalink
Merge branch 'main' into auto_detector
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-l-kong authored Dec 13, 2023
2 parents 0f3a42e + 6d4d5ae commit e32ca4b
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 2 deletions.
87 changes: 87 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 @@ -919,3 +923,86 @@ 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(
run_folder: 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:
run_folder (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.
"""
# Verify that detector_volt_type is valid
misc_utils.verify_in_list(
specified_volt_type=detector_volt_type, valid_volt_types=["hvAdc", "hvDac"]
)

# Get all the JSON run files
json_files = sorted(glob.glob(os.path.join(run_folder, "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:
# Load the JSON file
run_json_data = read_json_file(json_file, encoding="utf-8")

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

# For hvAdc and hvDac, the key associated with the voltage is different
voltage_key = "value" if detector_volt_type == "hvAdc" else "currentSetPoint"

# Extract the Detector voltage
# Assuming the "hvAdc" and/or "hvDac" field is always present and has the "Detector" entry
detector_voltage = [
item[voltage_key]
for item in run_json_data[detector_volt_type]
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,
)
11 changes: 9 additions & 2 deletions templates/4b_normalize_image_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,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
38 changes: 38 additions & 0 deletions tests/normalize_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import json
import os
import pathlib
import shutil
import tempfile
from typing import List
from unittest.mock import patch

import natsort
Expand All @@ -10,6 +13,7 @@
import pytest
import xarray as xr
from alpineer import io_utils, load_utils, test_utils
from pytest import TempPathFactory
from pytest_cases import parametrize_with_cases

from toffy import normalize
Expand Down Expand Up @@ -767,3 +771,37 @@ def test_check_detector_voltage():
),
):
normalize.check_detector_voltage(tmp_dir)


@parametrize("detector_volt_type", ["hvAdc", "hvDac"])
def test_plot_detector_voltage(tmp_path_factory: TempPathFactory, detector_volt_type: str):
run_folder: pathlib.Path = tmp_path_factory.mktemp("run_folder")
mph_run_dir: pathlib.Path = tmp_path_factory.mktemp("mph_run_dir")
json_files: List[str] = [f"fov-{i}-scan-1.json" for i in np.arange(1, 4)]

for i, file in enumerate(json_files):
sample_data = {
"hvAdc": [{"name": "Detector", "value": 1 * (i + 1)}, {"name": "Not Detector"}],
"hvDac": [
{"name": "Detector", "currentSetPoint": 10 * (i + 1)},
{"name": "Not Detector"},
],
}

write_json_file(run_folder / file, sample_data, encoding="utf-8")

normalize.plot_detector_voltage(
run_folder=run_folder, mph_run_dir=mph_run_dir, detector_volt_type=detector_volt_type
)

detector_gain_values_path: pathlib.Path = mph_run_dir / "detector_gain_values.csv"
assert os.path.exists(detector_gain_values_path)

detector_gain_values: pd.DataFrame = pd.read_csv(detector_gain_values_path)
expected_voltage_vals: np.ndarray = (
np.array([1, 2, 3]) if detector_volt_type == "hvAdc" else np.array([10, 20, 30])
)
assert np.all(detector_gain_values["FOVs"] == np.arange(1, 4))
assert np.all(detector_gain_values["Detector Voltage"] == expected_voltage_vals)

assert os.path.exists(mph_run_dir / "detector_gain_plot.png")

0 comments on commit e32ca4b

Please sign in to comment.