Fix MIMIC-III sqlite import using str.strip instead of suffix removal#1989
Open
Chessing234 wants to merge 1 commit intoMIT-LCP:mainfrom
Open
Fix MIMIC-III sqlite import using str.strip instead of suffix removal#1989Chessing234 wants to merge 1 commit intoMIT-LCP:mainfrom
Chessing234 wants to merge 1 commit intoMIT-LCP:mainfrom
Conversation
In mimic-iii/buildmimic/sqlite/import.py, the table name was derived via
`f.strip(".csv.gz")`. `str.strip` treats the argument as a set of
characters to remove from both ends, not a suffix, so it also eats any
trailing '.', 'c', 's', 'v', 'g', or 'z' in the filename.
MIMIC-III's distributed filenames are uppercase (e.g. PATIENTS.csv.gz),
so in practice the stripping halts at the first uppercase character and
the correct table name survives. But if a user has lowercase copies of
the files (case-insensitive filesystems, archives re-packed with lower
case), `services.csv.gz`.strip(".csv.gz") returns "service" and the
table is created under the wrong name.
Replace with `f[: -len(".csv.gz")]` to remove exactly the suffix. This
matches the intent and keeps 3.8 compatibility (no removesuffix).
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.
Bug
In `mimic-iii/buildmimic/sqlite/import.py`, the target table name is derived with:
```python
df.to_sql(f.strip(".csv.gz").lower(), CONNECTION_STRING)
```
Root cause
`str.strip(chars)` treats the argument as a set of characters to remove from both ends, not a literal suffix. With `".csv.gz"` the set is `{'.', 'c', 's', 'v', 'g', 'z'}`, so in addition to the suffix it also strips any trailing character that happens to be in that set.
MIMIC-III's official filenames are uppercase (`PATIENTS.csv.gz`), and the stripping halts at the first non-matching uppercase character, so the correct table name survives in the common path. But if a user has lowercase copies of the files (case-insensitive filesystems, archives re-packed with lowercase names), names like `services.csv.gz` become:
```python
The trailing `s` of `services` is silently eaten, and the table is created under the wrong name.
Fix
Replace `f.strip(".csv.gz")` with `f[: -len(".csv.gz")]` on both call sites. This removes exactly the known suffix and is compatible with Python 3.8 (no `str.removesuffix`).