Skip to content

Commit 7e42f37

Browse files
authored
Merge pull request #2063 from Chessing234/fix/sqlite-import-limit-and-strip
Fix sqlite --limit on chunks + strip() CSV table names
2 parents 4bd2f14 + 765d610 commit 7e42f37

3 files changed

Lines changed: 20 additions & 6 deletions

File tree

mimic-iii/buildmimic/oracle/add_oracle_rowdelimiter.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ def main(argv):
6363
print('Cannot find input file {}'.format(fn_in))
6464
sys.exit(2)
6565

66-
fn_out=fn_in.strip('.csv')+'_output.csv'
66+
fn_out = (
67+
fn_in[: -len(".csv")] + "_output.csv"
68+
if fn_in.lower().endswith(".csv")
69+
else fn_in + "_output.csv"
70+
)
6771
print('\n'+'~'*40)
6872
print('Input filename = {}'.format(fn_in))
6973
print('Delimiter = {}'.format(delimiter))

mimic-iii/buildmimic/sqlite/import.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,32 @@
1010
CHUNKSIZE = 10 ** 6
1111
CONNECTION_STRING = "sqlite:///{}".format(DATABASE_NAME)
1212

13+
14+
def _table_name_from_csv(filename: str) -> str:
15+
"""Derive SQL table name from a CSV path (literal suffix, not str.strip)."""
16+
name = filename
17+
for suffix in (".csv.gz", ".csv"):
18+
if name.lower().endswith(suffix):
19+
name = name[: -len(suffix)]
20+
break
21+
return name.lower()
22+
23+
1324
if os.path.exists(DATABASE_NAME):
1425
msg = "File {} already exists.".format(DATABASE_NAME)
1526
print(msg)
1627
sys.exit()
1728

1829
for f in glob("*.csv.gz"):
1930
print("Starting processing {}".format(f))
31+
table = _table_name_from_csv(f)
2032
if os.path.getsize(f) < THRESHOLD_SIZE:
2133
df = pd.read_csv(f, index_col="ROW_ID")
22-
df.to_sql(f.strip(".csv.gz").lower(), CONNECTION_STRING)
34+
df.to_sql(table, CONNECTION_STRING)
2335
else:
2436
# If the file is too large, let's do the work in chunks
2537
for chunk in pd.read_csv(f, index_col="ROW_ID", chunksize=CHUNKSIZE):
26-
chunk.to_sql(
27-
f.strip(".csv.gz").lower(), CONNECTION_STRING, if_exists="append"
28-
)
38+
chunk.to_sql(table, CONNECTION_STRING, if_exists="append")
2939
print("Finished processing {}".format(f))
3040

3141
print("Should be all done!")

mimic-iv/buildmimic/sqlite/import.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def main():
165165
else:
166166
# If the file is too large, let's do the work in chunks
167167
for chunk in pd.read_csv(f, chunksize=CHUNKSIZE, low_memory=False, dtype=mimic_dtypes):
168-
chunk = process_dataframe(chunk)
168+
chunk = process_dataframe(chunk, subjects=subjects)
169169
chunk.to_sql(tablename, connection, if_exists="append", index=False)
170170
row_counts[tablename] += len(chunk)
171171
print("done!")

0 commit comments

Comments
 (0)