Code covered by the BSD License  

Highlights from
Loading MIMIC II data using windows, matlab and postgre SQL

image thumbnail

Loading MIMIC II data using windows, matlab and postgre SQL

by

 

27 Feb 2013 (Updated )

Loading MIMIC II data using matlab and postgre SQL

import_mimicII_data.m
% How to load mimic II data in windows with matlab and postgreSQL
% 
% 1. install postgreSQL for windows,
% 2. create database with name MIMIC2;
% 3. run schema schema_mimic2v26.sql, which is available at https://physionet.org/works/MIMICIIClinicalDatabase/files/
% 4. make sure the 'Definitions' folder is available under the "currentPath" given in the matlab code.
% 5. make sure the folders for each patient are available. file in steps 4 and 5 can be obtained from, 
% https://physionet.org/works/MIMICIIClinicalDatabase/files/
% 6. change the userName and password for your database and currentPath in the matlab code.
% 7. instal jdbc driver of postgre for matlab (e.g. add a line "C:\DB_Drivers\postgresql-9.2-1002.jdbc4.jar" in c:\Program 
% Files\MATLAB\R2012a\toolbox\local\classpath.txt), where the drive can be downloaded from the postgre website.
% 8. run the matlab script improt_mimic_data.m
% 9. run indices_mimic2v26.sql, which is available at https://physionet.org/works/MIMICIIClinicalDatabase/files/

clear; 
currentPath = 'G:\mimicData';
eval(sprintf('folderList = {%s};', sprintf('''%02d'' ', 0:32)));
eval(sprintf('patientList = {%s};', sprintf('''%03d'' ', 1:999)));
schemaName = 'mimic2v26';
databaseName = 'MIMIC2';
userName = 'postgres';
password = 'yourpassword';
tabelList = {'D_PATIENTS' 'ADMISSIONS' 'ICUSTAYEVENTS'...\
             'A_CHARTDURATIONS' 'CENSUSEVENTS' 'MEDEVENTS' 'ADDITIVES'...\
             'CHARTEVENTS' 'NOTEEVENTS' 'DELIVERIES' 'POE_ORDER' 'POE_MED' 'A_IODURATIONS'... \
	         'IOEVENTS' 'A_MEDDURATIONS' 'ICD9' 'LABEVENTS' 'TOTALBALEVENTS'... \
             'DRGEVENTS' 'MICROBIOLOGYEVENTS'... \
             'DEMOGRAPHICEVENTS' 'PROCEDUREEVENTS'... \
	         'ICUSTAY_DAYS' 'ICUSTAY_DETAIL' 'COMORBIDITY_SCORES' 'DEMOGRAPHIC_DETAIL'};
conn = database(databaseName,userName, password, 'Vendor','PostGreSQL');
fid = [];
namelist = dir(sprintf('%s\\Definitions\\*.txt', currentPath));
for i = 1:numel(namelist)
    currentTable = strtrim(regexprep(namelist(i).name, '.txt', ''));
    cmdStr{1} = sprintf('DELETE FROM %s.%s;', schemaName, currentTable);
    cmdStr{2} = sprintf('COPY %s.%s FROM ''%s\\Definitions\\%s.txt'' DELIMITERS '','' CSV HEADER;',...
                         schemaName, currentTable, currentPath, currentTable);
     [cmdStr, curs] = RunSQLCmdStr(cmdStr, conn, fid);
end

%%

for k = 1:numel(tabelList)            
    cmdStr{1} = sprintf('DELETE FROM %s.%s;', schemaName, tabelList{k});
    [cmdStr, curs] = RunSQLCmdStr(cmdStr, conn, fid);
end
%%
for i = 1:numel(folderList)
    for j = 1:numel(patientList)        
        fprintf('processing %s\\%s\\%s%s \n', currentPath, folderList{i}, folderList{i}, patientList{j})
        for k = 1:numel(tabelList)     
            fileName = sprintf('%s\\%s\\%s%s\\%s', currentPath, folderList{i}, folderList{i}, patientList{j}, sprintf('%s-%s%s.txt', tabelList{k}, folderList{i}, patientList{j}));
            if(exist(fileName, 'file'))
                cmdStr{1} = sprintf('COPY %s.%s FROM ''%s'' DELIMITERS '','' CSV HEADER;', schemaName, tabelList{k}, fileName);
                [cmdStr, curs] = RunSQLCmdStr(cmdStr, conn, fid);
            end
        end        
    end
end
close(conn);

Contact us