Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable pyright strict #11

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ name = "Luke Fiddy"
version_file = "src/bimorph_mirror_analysis/_version.py"

[tool.pyright]
typeCheckingMode = "standard"
typeCheckingMode = "strict"
reportMissingImports = false # Ignore missing stubs in imported modules

[tool.pytest.ini_options]
Expand Down
20 changes: 12 additions & 8 deletions src/bimorph_mirror_analysis/maths.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@


def find_voltages(
data: np.ndarray, v: float, baseline_voltage_scan: int = 0
) -> np.ndarray:
data: np.typing.NDArray[np.float64],
v: float,
baseline_voltage_scan: int = 0,
) -> np.typing.NDArray[np.float64]:
"""Calculate voltage corrections to apply to bimorph.

Given a matrix of beamline centroid data, with columns of beamline scans at
Expand All @@ -23,8 +25,6 @@ def find_voltages(
beam scan to the target position.
"""

if not isinstance(baseline_voltage_scan, int):
raise TypeError("baseline_voltage_scan must be an integer")
if baseline_voltage_scan < -data.shape[1] or baseline_voltage_scan >= data.shape[1]:
raise IndexError(
f"baseline_voltage_scan is out of range, it must be between\
Expand All @@ -35,17 +35,21 @@ def find_voltages(
data, axis=1
) # calculate the response of each actuator by subtracting previous pencil beam

H = responses / v # response per unit charge
H = np.hstack((np.ones((H.shape[0], 1)), H)) # add columns of 1's to the left of H
H_inv = np.linalg.pinv(H) # calculate the Moore-Penrose pseudo inverse of H
interation_matrix = responses / v # response per unit charge
# add columns of 1's to the left of H
interation_matrix = np.hstack(
(np.ones((interation_matrix.shape[0], 1)), interation_matrix)
)
# calculate the Moore-Penrose pseudo inverse of H
interation_matrix_inv = np.linalg.pinv(interation_matrix)

baseline_voltage_beamline_positions = data[:, baseline_voltage_scan]

target = np.mean(baseline_voltage_beamline_positions)
Y = target - baseline_voltage_beamline_positions

voltage_corrections = np.matmul(
H_inv, Y
interation_matrix_inv, Y
) # calculate the voltage required to move the centroid to the target position

return voltage_corrections[1:] # return the voltages
8 changes: 1 addition & 7 deletions tests/script_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
["tests/data/16_actuator_data.txt", "tests/data/16_actuator_output.txt"],
],
)
def test_find_voltages_correct_output(input_path, output_path):
def test_find_voltages_correct_output(input_path: str, output_path: str):
data = np.loadtxt(input_path, delimiter=",")
v = -100
expected_output = np.loadtxt(output_path, delimiter=",")
Expand All @@ -26,9 +26,3 @@ def test_find_voltages_index_error_throw():
data = np.loadtxt("tests/data/8_actuator_data.txt", delimiter=",")
with pytest.raises(IndexError):
find_voltages(data, -100, baseline_voltage_scan=12)


def test_find_voltages_type_error_throw():
data = np.loadtxt("tests/data/8_actuator_data.txt", delimiter=",")
with pytest.raises(TypeError):
find_voltages(data, -100, baseline_voltage_scan="12") # type: ignore
Loading