Skip to content

Commit

Permalink
add human readable optional output
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Jan 8, 2025
1 parent d6e76f6 commit f8347c7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/bimorph_mirror_analysis/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,18 @@ def calculate_voltages(
help="The path to save the output\
optimal voltages to, optional.",
),
human_readable: str | None = typer.Option(
None,
help="The path to save the human readable pencil beam scan table. \
If the --human-readable flag is not supplied, the table is not saved.",
),
):
file_type = file_path.split(".")[-1]
if human_readable is not None:
pivoted, *_ = read_bluesky_plan_output(file_path)
print("1")
pivoted.to_csv(human_readable)

optimal_voltages = calculate_optimal_voltages(file_path)
optimal_voltages = np.round(optimal_voltages, 2)
date = datetime.datetime.now().date()
Expand Down
39 changes: 38 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from unittest.mock import patch

import numpy as np
import pandas as pd
import pytest
from typer.testing import CliRunner

Expand All @@ -19,7 +20,7 @@
[False],
],
)
def test_app(outpath: str | bool):
def test_outpath_option(outpath: str | bool):
with (
patch("bimorph_mirror_analysis.__main__.np.savetxt") as mock_np_save,
patch(
Expand All @@ -46,6 +47,42 @@ def test_app(outpath: str | bool):
assert "The optimal voltages are: [72.14, 50.98, 18.59]" in result.stdout


@pytest.mark.parametrize(
["human_readable"],
[
["tests/data/out.csv"],
[False],
],
)
def test_human_readable_option(human_readable: str | bool):
with (
patch("bimorph_mirror_analysis.__main__.np.savetxt") as mock_np_save,
patch(
"bimorph_mirror_analysis.__main__.calculate_optimal_voltages"
) as mock_calculate_optimal_voltages,
patch.object(pd.DataFrame, "to_csv") as mock_to_csv,
):
mock_calculate_optimal_voltages.return_value = np.array([72.14, 50.98, 18.59])
if type(human_readable) is str:
result = runner.invoke(
app,
[
"calculate-voltages",
"tests/data/raw_data.csv",
"--human-readable",
f"{human_readable}",
],
)
mock_to_csv.assert_called_once()
else:
result = runner.invoke(
app, ["calculate-voltages", "tests/data/raw_data.csv"]
)
mock_np_save.assert_called_once()
mock_calculate_optimal_voltages.assert_called_with("tests/data/raw_data.csv")
assert "The optimal voltages are: [72.14, 50.98, 18.59]" in result.stdout


def test_cli_version():
cmd = [
sys.executable,
Expand Down

0 comments on commit f8347c7

Please sign in to comment.