Skip to content

Commit

Permalink
output a valid json
Browse files Browse the repository at this point in the history
  • Loading branch information
CamDavidsonPilon committed Dec 2, 2023
1 parent fe99fe7 commit dfd3ff8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
41 changes: 19 additions & 22 deletions pioreactor/actions/od_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
from typing import Type

import click
import msgspec
from click import clear
from click import confirm
from click import echo
from click import prompt
from click import style
from msgspec.json import decode
from msgspec.json import encode
from msgspec.json import format

from pioreactor import structs
from pioreactor import types as pt
Expand Down Expand Up @@ -56,7 +56,7 @@ def introduction() -> None:
echo(
"""This routine will calibrate the current Pioreactor to (offline) OD600 readings. You'll need:
1. The Pioreactor you wish to calibrate (the one you are using)
2. At least 10mL of a culture with density the most you'll ever observe, and its OD600 measurement.
2. At least 10mL of a culture with density the most you'll ever observe, and its OD600 measurement
3. A micro-pipette
4. Accurate 10mL measurement tool
5. Sterile media, amount to be determined shortly.
Expand Down Expand Up @@ -189,7 +189,7 @@ def plot_data(

if interpolation_curve:
plt.plot(x, [interpolation_curve(x_) for x_ in x], color=204)
plt.plot_size(145, 42)
plt.plot_size(145, 26)

plt.xlim(x_min, x_max)
plt.show()
Expand Down Expand Up @@ -257,7 +257,7 @@ def get_voltage_from_adc() -> pt.Voltage:
x_max=initial_od600,
)
echo()
click.secho(
echo(
bold(
f"Test {count_of_samples+1} of {total_n_samples} [{'#' * (count_of_samples+1) }{' ' * (total_n_samples - count_of_samples - 1)}]"
)
Expand Down Expand Up @@ -392,18 +392,16 @@ def show_results_and_confirm_with_user(
echo(f"Calibration curve: {curve_to_functional_form(curve_type, curve_data)}")
r = prompt(
green(
"""
What next?
Y: confirm and save to disk
f"""
y: confirm and save to disk
n: abort completely
d: choose a new degree for polynomial fit
d: choose a new degree for polynomial fit (currently {len(curve_data)-1})
"""
),
type=click.Choice(["Y", "n", "d"]),
type=click.Choice(["y", "n", "d"]),
)
if r == "Y":
if r == "y":
return True, -1
elif r == "n":
raise click.Abort()
Expand Down Expand Up @@ -464,6 +462,8 @@ def save_results(
def get_data_from_data_file(data_file: str) -> tuple[str, str, list[float], list[float]]:
import json

click.echo(f"Pulling data from {data_file}...")

with open(data_file, "r") as f:
data = json.loads(f.read())

Expand All @@ -480,8 +480,6 @@ def get_data_from_data_file(data_file: str) -> tuple[str, str, list[float], list


def od_calibration(data_file: str | None) -> None:
from pprint import pprint

unit = get_unit_name()
experiment = get_latest_testing_experiment_name()

Expand Down Expand Up @@ -518,6 +516,7 @@ def od_calibration(data_file: str | None) -> None:
if okay_with_result:
break

echo("Saving results...")
data_blob = save_results(
curve_data_,
curve_type,
Expand All @@ -531,10 +530,10 @@ def od_calibration(data_file: str | None) -> None:
echo(style(f"Calibration curve for `{name}`", underline=True, bold=True))
echo(curve_to_functional_form(curve_type, curve_data_))
echo()
echo(style(f"Data for {name}", underline=True, bold=True))
pprint(msgspec.structs.asdict(data_blob))
echo(style(f"Data for `{name}`", underline=True, bold=True))
print(format(encode(data_blob)).decode())
echo()
echo(f"Finished calibration of {name} ✅")
echo(f"Finished calibration of `{name}` ✅")

if not config.getboolean("od_config", "use_calibration", fallback=False):
echo()
Expand Down Expand Up @@ -573,8 +572,6 @@ def curve_callable(x):


def display(name: str | None) -> None:
from pprint import pprint

def display_from_calibration_blob(data_blob: dict) -> None:
voltages = data_blob["voltages"]
ods = data_blob["od600s"]
Expand All @@ -595,7 +592,7 @@ def display_from_calibration_blob(data_blob: dict) -> None:
echo(curve_to_functional_form(data_blob["curve_type"], data_blob["curve_data_"]))
echo()
echo(style(f"Data for `{name}`", underline=True, bold=True))
pprint(data_blob)
print(format(encode(data_blob)).decode())

if name is not None:
with local_persistant_storage("od_calibrations") as c:
Expand Down Expand Up @@ -666,9 +663,9 @@ def change_current(name: str) -> None:
echo("Could not update in database on leader ❌")

if old_calibration:
echo(f"Replaced {old_calibration.name} with {new_calibration.name} ✅")
echo(f"Replaced `{old_calibration.name}` with `{new_calibration.name}` ✅")
else:
echo(f"Set {new_calibration.name} to current calibration ✅")
echo(f"Set `{new_calibration.name}` to current calibration ✅")
echo()

except Exception:
Expand All @@ -689,7 +686,7 @@ def list_() -> None:
for name in c.iterkeys():
try:
cal = decode(c[name], type=structs.subclass_union(structs.ODCalibration))
click.secho(
echo(
f"{cal.name:15s} {cal.created_at:%d %b, %Y} {cal.angle:12s} {'✅' if cal.name in current else ''}",
)
except Exception:
Expand Down
7 changes: 3 additions & 4 deletions pioreactor/actions/pump_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from click import style
from msgspec.json import decode
from msgspec.json import encode
from msgspec.json import format

from pioreactor import structs
from pioreactor.actions.pump import add_alt_media
Expand Down Expand Up @@ -507,8 +508,6 @@ def curve_callable(x):


def display(name: str | None) -> None:
from pprint import pprint

def display_from_calibration_blob(pump_calibration: dict) -> None:
volumes = pump_calibration["volumes"]
durations = pump_calibration["durations"]
Expand All @@ -522,8 +521,8 @@ def display_from_calibration_blob(pump_calibration: dict) -> None:
"poly", [pump_calibration["duration_"], pump_calibration["bias_"]]
),
)
echo(style(f"Data for {name}", underline=True, bold=True))
pprint(pump_calibration)
echo(style(f"Data for `{name}`", underline=True, bold=True))
print(format(encode(pump_calibration)).decode())

if name is not None:
with local_persistant_storage("pump_calibrations") as c:
Expand Down

0 comments on commit dfd3ff8

Please sign in to comment.