Stop write_gsf mutating the caller's DataFrame - #104
Merged
Conversation
write_gsf filled the optional init_time and slip columns by assigning directly into the DataFrame it was given, so a frame passed in came back carrying two columns of -1 sentinels it never had. Because the assignments sat above the loc_rake validation, the mutation happened even when write_gsf went on to raise and write nothing. Fills the defaults on a shallow copy instead, and moves the loc_rake validation above the defaulting so the failure path leaves no debris. Fixes #89 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #89
Problem
write_gsffilled its optional columns by assigning straight into the caller's DataFrame:A frame handed to
write_gsfcame back carrying two columns of-1sentinels it never had. Sincewrite_gsfis a serialisation function, nothing in its signature or docstring suggests the input is consumed.Because the assignments sat above the
loc_rakecheck, the mutation also happened on the failure path —write_gsfcould raise, write no file, and still leave the caller's frame modified.Impact
Sentinel
-1values silently persisting in a caller's slip/init_time columns are the kind of thing that reads as real data downstream.Changes
source_modelling/gsf.py— fill defaults on a shallow copy (copy(deep=False); only new columns are added, the existing column data is never written through, so a deep copy would be wasted work on large fault meshes). Move theloc_rakevalidation above the defaulting so the failure path leaves nothing behind.tests/test_gsf.py— addtest_write_gsf_does_not_mutate_input, covering both the success and the raising path.No change to the file written — the same columns are serialised with the same defaults.
Verification
pytest tests/test_gsf.py— 13 passed.ruff check,ruff format,numpydoc lintclean.🤖 Generated with Claude Code