Skip to content
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
23 changes: 20 additions & 3 deletions tests/test_sensor_transfer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import pandas as pd

from transfers.sensor_transfer import _coerce_wi_mic_gain
from transfers.sensor_transfer import _coerce_wi_mic_gain, _coerce_wi_int


def test_coerce_wi_mic_gain_numeric():
Expand All @@ -13,12 +13,29 @@ def test_coerce_wi_mic_gain_numeric():
def test_coerce_wi_mic_gain_strings():
assert _coerce_wi_mic_gain("1") is True
assert _coerce_wi_mic_gain("0") is False
assert _coerce_wi_mic_gain(" true ") is True
assert _coerce_wi_mic_gain("False") is False


def test_coerce_wi_mic_gain_handles_none_like():
assert _coerce_wi_mic_gain(None) is None
assert _coerce_wi_mic_gain(" ") is None
assert _coerce_wi_mic_gain(pd.NA) is None
assert _coerce_wi_mic_gain(np.nan) is None


def test_coerce_wi_int_numeric():
assert _coerce_wi_int(1) == 1
assert _coerce_wi_int(1.9) == 1
assert _coerce_wi_int(0.0) == 0


def test_coerce_wi_int_strings():
assert _coerce_wi_int("2") == 2
assert _coerce_wi_int(" 3.0 ") == 3
assert _coerce_wi_int("true") is None


def test_coerce_wi_int_none_like():
assert _coerce_wi_int(None) is None
assert _coerce_wi_int(" ") is None
assert _coerce_wi_int(pd.NA) is None
assert _coerce_wi_int(np.nan) is None
44 changes: 28 additions & 16 deletions transfers/sensor_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,36 @@
}


def _coerce_wi_int(value):
if value is None or (isinstance(value, str) and not value.strip()):
return None
if isinstance(value, bool):
return int(value)
try:
if pd.isna(value):
return None
except TypeError:
pass
try:
return int(float(value))
except (TypeError, ValueError):
return None


def _coerce_wi_mic_gain(value):
if value is None or (isinstance(value, str) and not value.strip()):
return None
if isinstance(value, str):
value = value.strip()
if pd.isna(value):
return None
if isinstance(value, bool):
return value
try:
if pd.isna(value):
return None
except TypeError:
pass
Comment thread
jirhiker marked this conversation as resolved.
try:
return bool(int(float(value)))

Copilot AI Jan 30, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The _coerce_wi_mic_gain function now removes the string-based boolean parsing (e.g., 'true', 'false', 'yes', 'no') and only handles numeric values. If the source data contains string boolean representations, this change could cause those values to be coerced to None instead of being properly converted. Verify that the source data only contains numeric boolean representations.

Copilot uses AI. Check for mistakes.
except (ValueError, TypeError):
lowered = str(value).strip().lower()
if lowered in {"true", "t", "yes", "y"}:
return True
if lowered in {"false", "f", "no", "n"}:
return False
return None
except (TypeError, ValueError):
return None
Comment thread
jirhiker marked this conversation as resolved.


class SensorTransferer(ThingBasedTransferer):
Expand Down Expand Up @@ -238,12 +250,12 @@ def _group_step(self, session: Session, row: pd.Series, db_item: Base):
hanging_cable_length=row.HangingCableLength,
hanging_point_height=row.HangingPointHgt,
hanging_point_description=row.HangingPointDescription,
nma_WI_Duration=row.WI_Duration,
nma_WI_EndFrequency=row.WI_EndFrequency,
nma_WI_Magnitude=row.WI_Magnitude,
nma_WI_Duration=_coerce_wi_int(row.WI_Duration),
nma_WI_EndFrequency=_coerce_wi_int(row.WI_EndFrequency),
nma_WI_Magnitude=_coerce_wi_int(row.WI_Magnitude),
nma_WI_MicGain=_coerce_wi_mic_gain(row.WI_MicGain),
nma_WI_MinSoundDepth=row.WI_MinSoundDepth,
nma_WI_StartFrequency=row.WI_StartFrequency,
nma_WI_MinSoundDepth=_coerce_wi_int(row.WI_MinSoundDepth),
nma_WI_StartFrequency=_coerce_wi_int(row.WI_StartFrequency),
)
session.add(deployment)
logger.info(
Expand Down
Loading
Loading