Skip to content

Commit 9f5e602

Browse files
kbighorseactions-user
authored andcommitted
Formatting changes
1 parent 6c2e12b commit 9f5e602

4 files changed

Lines changed: 50 additions & 44 deletions

File tree

alembic/versions/95d8b982cd5d_add_nma_chemistry_lineage_relations.py

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
1. thing_id FK column to NMA_Chemistry_SampleInfo (NOT NULL, no orphans)
99
2. NMA_MinorTraceChemistry table (child of NMA_Chemistry_SampleInfo)
1010
"""
11+
1112
from typing import Sequence, Union
1213

1314
from alembic import op
1415
import sqlalchemy as sa
1516

1617

17-
revision: str = '95d8b982cd5d'
18-
down_revision: Union[str, Sequence[str], None] = '2f6e9d3a1c45'
18+
revision: str = "95d8b982cd5d"
19+
down_revision: Union[str, Sequence[str], None] = "2f6e9d3a1c45"
1920
branch_labels: Union[str, Sequence[str], None] = None
2021
depends_on: Union[str, Sequence[str], None] = None
2122

@@ -24,49 +25,46 @@ def upgrade() -> None:
2425
"""Upgrade schema."""
2526
# Add thing_id FK to NMA_Chemistry_SampleInfo (NOT NULL - no orphans)
2627
op.add_column(
27-
'NMA_Chemistry_SampleInfo',
28-
sa.Column('thing_id', sa.Integer(), nullable=False)
28+
"NMA_Chemistry_SampleInfo", sa.Column("thing_id", sa.Integer(), nullable=False)
2929
)
3030
op.create_foreign_key(
31-
'fk_chemistry_sample_info_thing',
32-
'NMA_Chemistry_SampleInfo',
33-
'thing',
34-
['thing_id'],
35-
['id'],
36-
ondelete='CASCADE'
31+
"fk_chemistry_sample_info_thing",
32+
"NMA_Chemistry_SampleInfo",
33+
"thing",
34+
["thing_id"],
35+
["id"],
36+
ondelete="CASCADE",
3737
)
3838

3939
# Create NMA_MinorTraceChemistry table
4040
op.create_table(
41-
'NMA_MinorTraceChemistry',
42-
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
43-
sa.Column('chemistry_sample_info_id', sa.Integer(), nullable=False),
44-
sa.Column('analyte', sa.String(50), nullable=True),
45-
sa.Column('sample_value', sa.Float(), nullable=True),
46-
sa.Column('units', sa.String(20), nullable=True),
47-
sa.Column('symbol', sa.String(10), nullable=True),
48-
sa.Column('analysis_method', sa.String(100), nullable=True),
49-
sa.Column('analysis_date', sa.Date(), nullable=True),
50-
sa.Column('notes', sa.Text(), nullable=True),
51-
sa.Column('analyses_agency', sa.String(100), nullable=True),
52-
sa.Column('uncertainty', sa.Float(), nullable=True),
53-
sa.Column('volume', sa.Float(), nullable=True),
54-
sa.Column('volume_unit', sa.String(20), nullable=True),
55-
sa.PrimaryKeyConstraint('id'),
41+
"NMA_MinorTraceChemistry",
42+
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
43+
sa.Column("chemistry_sample_info_id", sa.Integer(), nullable=False),
44+
sa.Column("analyte", sa.String(50), nullable=True),
45+
sa.Column("sample_value", sa.Float(), nullable=True),
46+
sa.Column("units", sa.String(20), nullable=True),
47+
sa.Column("symbol", sa.String(10), nullable=True),
48+
sa.Column("analysis_method", sa.String(100), nullable=True),
49+
sa.Column("analysis_date", sa.Date(), nullable=True),
50+
sa.Column("notes", sa.Text(), nullable=True),
51+
sa.Column("analyses_agency", sa.String(100), nullable=True),
52+
sa.Column("uncertainty", sa.Float(), nullable=True),
53+
sa.Column("volume", sa.Float(), nullable=True),
54+
sa.Column("volume_unit", sa.String(20), nullable=True),
55+
sa.PrimaryKeyConstraint("id"),
5656
sa.ForeignKeyConstraint(
57-
['chemistry_sample_info_id'],
58-
['NMA_Chemistry_SampleInfo.OBJECTID'],
59-
ondelete='CASCADE',
57+
["chemistry_sample_info_id"],
58+
["NMA_Chemistry_SampleInfo.OBJECTID"],
59+
ondelete="CASCADE",
6060
),
6161
)
6262

6363

6464
def downgrade() -> None:
6565
"""Downgrade schema."""
66-
op.drop_table('NMA_MinorTraceChemistry')
66+
op.drop_table("NMA_MinorTraceChemistry")
6767
op.drop_constraint(
68-
'fk_chemistry_sample_info_thing',
69-
'NMA_Chemistry_SampleInfo',
70-
type_='foreignkey'
68+
"fk_chemistry_sample_info_thing", "NMA_Chemistry_SampleInfo", type_="foreignkey"
7169
)
72-
op.drop_column('NMA_Chemistry_SampleInfo', 'thing_id')
70+
op.drop_column("NMA_Chemistry_SampleInfo", "thing_id")

db/nma_legacy.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,9 @@ class ChemistrySampleInfo(Base):
218218
def validate_thing_id(self, key, value):
219219
"""Prevent orphan ChemistrySampleInfo - must have a parent Thing."""
220220
if value is None:
221-
raise ValueError("ChemistrySampleInfo requires a parent Thing (thing_id cannot be None)")
221+
raise ValueError(
222+
"ChemistrySampleInfo requires a parent Thing (thing_id cannot be None)"
223+
)
222224
return value
223225

224226

@@ -312,7 +314,9 @@ class NMAMinorTraceChemistry(Base):
312314
def validate_chemistry_sample_info_id(self, key, value):
313315
"""Prevent orphan NMAMinorTraceChemistry - must have a parent ChemistrySampleInfo."""
314316
if value is None:
315-
raise ValueError("NMAMinorTraceChemistry requires a parent ChemistrySampleInfo")
317+
raise ValueError(
318+
"NMAMinorTraceChemistry requires a parent ChemistrySampleInfo"
319+
)
316320
return value
317321

318322

tests/test_nma_chemistry_lineage.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,11 @@ def test_reverse_lineage_navigation(shared_well):
463463
session.refresh(well)
464464

465465
# Reverse navigation - filter to just this sample_info
466-
matching = [si for si in well.chemistry_sample_infos if si.object_id == sample_info.object_id]
466+
matching = [
467+
si
468+
for si in well.chemistry_sample_infos
469+
if si.object_id == sample_info.object_id
470+
]
467471
assert len(matching) == 1
468472
assert len(matching[0].minor_trace_chemistries) == 1
469473
assert matching[0].minor_trace_chemistries[0] == mtc

transfers/backfill/chemistry_sampleinfo.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def __init__(self, *args, batch_size: int = 1000, **kwargs):
4444
# Cache Thing lookups to prevent N+1 queries
4545
self._thing_id_cache = {}
4646
self._build_thing_id_cache()
47-
47+
4848
def _build_thing_id_cache(self):
4949
"""Build cache of Thing.name -> thing.id to prevent orphan records."""
5050
with session_ctx() as session:
@@ -57,29 +57,29 @@ def _get_dfs(self) -> tuple[pd.DataFrame, pd.DataFrame]:
5757
# Filter to only include rows where Thing exists (prevent orphan records)
5858
cleaned_df = self._filter_to_valid_things(input_df)
5959
return input_df, cleaned_df
60-
60+
6161
def _filter_to_valid_things(self, df: pd.DataFrame) -> pd.DataFrame:
6262
"""
6363
Filter to only include rows where SamplePointID matches an existing Thing.
6464
Prevents orphan ChemistrySampleInfo records.
65-
65+
6666
Uses cached Thing lookups for performance.
6767
"""
6868
# Use cached Thing names (keys of thing_id_cache)
6969
valid_point_ids = set(self._thing_id_cache.keys())
70-
70+
7171
# Filter to rows where SamplePointID exists as a Thing.name
7272
before_count = len(df)
7373
filtered_df = df[df["SamplePointID"].isin(valid_point_ids)].copy()
7474
after_count = len(filtered_df)
75-
75+
7676
if before_count > after_count:
7777
skipped = before_count - after_count
7878
logger.warning(
7979
f"Filtered out {skipped} ChemistrySampleInfo records without matching Things "
8080
f"({after_count} valid, {skipped} orphan records prevented)"
8181
)
82-
82+
8383
return filtered_df
8484

8585
def _transfer_hook(self, session: Session) -> None:
@@ -97,13 +97,13 @@ def _transfer_hook(self, session: Session) -> None:
9797
)
9898
continue
9999
row_dicts.append(row_dict)
100-
100+
101101
if skipped_count > 0:
102102
logger.warning(
103103
f"Skipped {skipped_count} ChemistrySampleInfo records without valid Thing "
104104
f"(orphan prevention)"
105105
)
106-
106+
107107
rows = self._dedupe_rows(row_dicts, key="OBJECTID")
108108

109109
insert_stmt = insert(ChemistrySampleInfo)

0 commit comments

Comments
 (0)