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
7 changes: 6 additions & 1 deletion .github/workflows/lint_sqlfluff.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ on:
jobs:
lint-mimic-iv:
runs-on: ubuntu-latest
permissions:
contents: read
checks: write
steps:
- name: checkout
uses: actions/checkout@v7
Expand Down Expand Up @@ -35,8 +38,10 @@ jobs:
shell: bash
run: sqlfluff lint --format github-annotation --annotation-level failure --nofail ${{ steps.get_files_to_lint.outputs.lintees }} > annotations.json
- name: Annotate
if: steps.get_files_to_lint.outputs.lintees != ''
uses: yuzutech/annotations-action@v0.6.0
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
title: "SQLFluff Lint"
input: "./annotations.json"
input: "./annotations.json"
ignore-unauthorized-error: true
6 changes: 4 additions & 2 deletions mimic-iii/buildmimic/duckdb/import_duckdb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ make_table_name () {
# load data into database
find "$MIMIC_DIR" -type f -regex '.*\.csv\(.gz\)*' | while IFS= read -r FILE; do
make_table_name "$FILE"
echo "Loading $FILE .. \c"
# Escape single quotes for SQL string literal
FILE_SQL=$(printf '%s' "$FILE" | sed "s/'/''/g")
printf "Loading %s .. " "$FILE"
try duckdb "$OUTFILE" <<-EOSQL
COPY $TABLE_NAME FROM '$FILE' (HEADER, DELIM ',', QUOTE '"', ESCAPE '"');
COPY $TABLE_NAME FROM '$FILE_SQL' (HEADER, DELIM ',', QUOTE '"', ESCAPE '"');
EOSQL
echo "done!"
done && echo "Successfully finished loading data into $OUTFILE."
8 changes: 4 additions & 4 deletions mimic-iii/buildmimic/postgres/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ mimic-build-gz:
@echo '------------------'
@echo ''
@sleep 2
psql "$(DBSTRING)" -v ON_ERROR_STOP=1 -f postgres_load_data_gz.sql -v mimic_data_dir=${datadir}
psql "$(DBSTRING)" -v ON_ERROR_STOP=1 -f postgres_load_data_gz.sql -v "mimic_data_dir=$(DATADIR)"
@echo ''
@echo '--------------------'
@echo '-- Adding indexes --'
Expand Down Expand Up @@ -151,7 +151,7 @@ mimic-build:
@echo '------------------'
@echo ''
@sleep 2
psql "$(DBSTRING)" -v ON_ERROR_STOP=1 -f postgres_load_data.sql -v mimic_data_dir=${DATADIR}
psql "$(DBSTRING)" -v ON_ERROR_STOP=1 -f postgres_load_data.sql -v "mimic_data_dir=$(DATADIR)"
@echo ''
@echo '--------------------'
@echo '-- Adding indexes --'
Expand Down Expand Up @@ -184,15 +184,15 @@ ifeq ("$(physionetuser)","")
@echo 'Call the makefile again with physionetuser=<USERNAME>'
@echo ' e.g. make eicu-download datadir=/path/to/data physionetuser=hello@physionet.org'
else
wget --user $(physionetuser) --ask-password -P $(DATADIR) -A csv.gz -m -p -E -k -K -np -nd "$(PHYSIONETURL)"
wget --user $(physionetuser) --ask-password -P "$(DATADIR)" -A csv.gz -m -p -E -k -K -np -nd "$(PHYSIONETURL)"
endif

mimic-demo-download:
@echo '------------------------------------------'
@echo '-- Downloading MIMIC-III from PhysioNet --'
@echo '------------------------------------------'
@echo ''
wget --user $(physionetuser) --ask-password -P $(DATADIR) -A csv.gz -m -p -E -k -K -np -nd "$(PHYSIONETDEMOURL)"
wget --user $(physionetuser) --ask-password -P "$(DATADIR)" -A csv.gz -m -p -E -k -K -np -nd "$(PHYSIONETDEMOURL)"

#This is fairly inelegant and could be tidied with a for loop and an if to check for gzip,
#but need to maintain compatibility with Windows, which baffling lacks these things
Expand Down
15 changes: 11 additions & 4 deletions mimic-iii/buildmimic/postgres/create_mimic_user.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ else
echo "User is set to '$MIMIC_USER'";
fi

# escape ' in password for SQL string literal
MIMIC_PASSWORD_SQL=$(printf '%s' "$MIMIC_PASSWORD" | sed "s/'/''/g")
# quote SQL identifiers (double any embedded ")
sql_ident () { printf '%s' "$1" | sed 's/"/""/g; s/^/"/; s/$/"/'; }
MIMIC_USER_SQL=$(sql_ident "$MIMIC_USER")
MIMIC_DB_SQL=$(sql_ident "$MIMIC_DB")

PSQL='psql'

# add in the host/port, if they were specified (not null, -n)
Expand Down Expand Up @@ -58,11 +65,11 @@ fi
if [ "$MIMIC_USER" != "postgres" ]; then
# we need to create this user via postgres
# use SUDO to login as postgres
$PSQL -U postgres -d postgres -c "DROP USER IF EXISTS $MIMIC_USER; CREATE USER $MIMIC_USER WITH PASSWORD '$MIMIC_PASSWORD';"
$PSQL -U postgres -d postgres -c "DROP USER IF EXISTS $MIMIC_USER_SQL; CREATE USER $MIMIC_USER_SQL WITH PASSWORD '$MIMIC_PASSWORD_SQL';"
fi

if [ "$MIMIC_DB" != "postgres" ]; then
# drop and recreate the database
$PSQL -U postgres -d postgres -c "DROP DATABASE IF EXISTS $MIMIC_DB;"
$PSQL -U postgres -d postgres -c "CREATE DATABASE $MIMIC_DB OWNER $MIMIC_USER;"
fi
$PSQL -U postgres -d postgres -c "DROP DATABASE IF EXISTS $MIMIC_DB_SQL;"
$PSQL -U postgres -d postgres -c "CREATE DATABASE $MIMIC_DB_SQL OWNER $MIMIC_USER_SQL;"
fi
9 changes: 8 additions & 1 deletion mimic-iii/buildmimic/sqlite/import.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,16 @@ def _table_name_from_csv(filename: str) -> str:
print(msg)
sys.exit()

# Prefer .csv.gz when both exist for the same table; also load plain .csv
# (import.sh already accepts both).
files_by_table = {}
for f in glob("*.csv"):
files_by_table[_table_name_from_csv(f)] = f
for f in glob("*.csv.gz"):
files_by_table[_table_name_from_csv(f)] = f

for table, f in sorted(files_by_table.items()):
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(table, CONNECTION_STRING)
Expand Down
6 changes: 3 additions & 3 deletions mimic-iii/buildmimic/sqlite/import.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ for FILE in *; do
TABLE_NAME=$(echo "${FILE%%.*}" | tr "[:upper:]" "[:lower:]")
case "$FILE" in
*csv)
IMPORT_CMD=".import $FILE $TABLE_NAME"
IMPORT_CMD=".import \"$FILE\" $TABLE_NAME"
;;
# need to decompress csv before load
*csv.gz)
IMPORT_CMD=".import \"|gzip -dc $FILE\" $TABLE_NAME"
IMPORT_CMD=".import \"|gzip -dc \\\"$FILE\\\"\" $TABLE_NAME"
;;
# not a data file so skip
*)
continue
;;
esac
echo "Loading $FILE."
sqlite3 $OUTFILE <<EOF
sqlite3 "$OUTFILE" <<EOF
.headers on
.mode csv
$IMPORT_CMD
Expand Down
Loading
Loading