Skip to content
Open
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
6 changes: 5 additions & 1 deletion mimic-iii/buildmimic/oracle/add_oracle_rowdelimiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ def main(argv):
print('Cannot find input file {}'.format(fn_in))
sys.exit(2)

fn_out=fn_in.strip('.csv')+'_output.csv'
fn_out = (
fn_in[: -len(".csv")] + "_output.csv"
if fn_in.lower().endswith(".csv")
else fn_in + "_output.csv"
)
print('\n'+'~'*40)
print('Input filename = {}'.format(fn_in))
print('Delimiter = {}'.format(delimiter))
Expand Down
18 changes: 14 additions & 4 deletions mimic-iii/buildmimic/sqlite/import.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,32 @@
CHUNKSIZE = 10 ** 6
CONNECTION_STRING = "sqlite:///{}".format(DATABASE_NAME)


def _table_name_from_csv(filename: str) -> str:
"""Derive SQL table name from a CSV path (literal suffix, not str.strip)."""
name = filename
for suffix in (".csv.gz", ".csv"):
if name.lower().endswith(suffix):
name = name[: -len(suffix)]
break
return name.lower()


if os.path.exists(DATABASE_NAME):
msg = "File {} already exists.".format(DATABASE_NAME)
print(msg)
sys.exit()

for f in glob("*.csv.gz"):
print("Starting processing {}".format(f))
table = _table_name_from_csv(f)
if os.path.getsize(f) < THRESHOLD_SIZE:
df = pd.read_csv(f, index_col="ROW_ID")
df.to_sql(f.strip(".csv.gz").lower(), CONNECTION_STRING)
df.to_sql(table, CONNECTION_STRING)
else:
# If the file is too large, let's do the work in chunks
for chunk in pd.read_csv(f, index_col="ROW_ID", chunksize=CHUNKSIZE):
chunk.to_sql(
f.strip(".csv.gz").lower(), CONNECTION_STRING, if_exists="append"
)
chunk.to_sql(table, CONNECTION_STRING, if_exists="append")
print("Finished processing {}".format(f))

print("Should be all done!")
2 changes: 1 addition & 1 deletion mimic-iv/buildmimic/sqlite/import.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def main():
else:
# If the file is too large, let's do the work in chunks
for chunk in pd.read_csv(f, chunksize=CHUNKSIZE, low_memory=False, dtype=mimic_dtypes):
chunk = process_dataframe(chunk)
chunk = process_dataframe(chunk, subjects=subjects)
chunk.to_sql(tablename, connection, if_exists="append", index=False)
row_counts[tablename] += len(chunk)
print("done!")
Expand Down
Loading