In [1]:
import sqlite3
import os
import sys
print(sqlite3.version)


2.6.0

In [2]:
import platform

print(platform.python_version())
print(platform.python_version_tuple())


3.5.1
('3', '5', '1')

In [3]:
sqlitedb = os.path.join(os.path.expanduser('~'),'Box Sync', 'GradSchoolStuff', 'MastersProject', 'mimic_radreports.sqlite')
if not (os.path.exists(sqlitedb)):
    print("Specified database does not exist")
    sys.exit()

In [4]:
connection = sqlite3.connect(sqlitedb)
with connection:
    cursor = connection.cursor()
    cursor.execute('select * from sqlite_master')
    row = cursor.fetchone()
    while row:
        print(row)
        row = cursor.fetchone()


('table', 'reports', 'reports', 2, 'CREATE TABLE "reports" (\n"index" INTEGER,\n  "subject_id" INTEGER,\n  "charttime" TIMESTAMP,\n  "text" TEXT\n)')
('index', 'ix_reports_index', 'reports', 3, 'CREATE INDEX "ix_reports_index"ON "reports" ("index")')

In [5]:
# PRAGMA allows you to get metadata about a table
with connection:
    cur = connection.cursor()    
    cur.execute('PRAGMA table_info(sqlite_master)')
    data = cur.fetchall()
    for d in data:
        print(d)


(0, 'type', 'text', 0, None, 0)
(1, 'name', 'text', 0, None, 0)
(2, 'tbl_name', 'text', 0, None, 0)
(3, 'rootpage', 'integer', 0, None, 0)
(4, 'sql', 'text', 0, None, 0)

In [6]:
with connection:
    cursor = connection.cursor()
    cursor.execute('select * from sqlite_master')
    rows = cursor.fetchall()
    for row in rows:
        print('-----------------')
        print('type: ', row[0])
        print('name: ', row[1])
        print('tbl_name: ', row[2])
        print('rootpage: ', row[3])
        print('sql: ', row[4])
        #for col in row:
        #    print(col, end=" ")
        #print('')


-----------------
type:  table
name:  reports
tbl_name:  reports
rootpage:  2
sql:  CREATE TABLE "reports" (
"index" INTEGER,
  "subject_id" INTEGER,
  "charttime" TIMESTAMP,
  "text" TEXT
)
-----------------
type:  index
name:  ix_reports_index
tbl_name:  reports
rootpage:  3
sql:  CREATE INDEX "ix_reports_index"ON "reports" ("index")

In [7]:
with connection:
    cur = connection.cursor()
    cur.execute('select count(*) from reports')
    data = cur.fetchall()
    print('Total rows in REPORTS table: ', data[0][0])


Total rows in REPORTS table:  65884

In [12]:
with connection:
    cur = connection.cursor()
    cur.execute('select * from reports limit 5')
    col_names = [cn[0] for cn in cur.description]
    rows = cur.fetchall()
    for row in rows:
        for i,col in enumerate(row):
            print('++++++', col_names[i], ': ', col)
        print('----------------- END ROW -----------------')


++++++ index :  0
++++++ subject_id :  56
++++++ charttime :  2644-01-17 00:00:00
++++++ text :  


     DATE: [**2644-1-17**] 10:53 AM
     MR HEAD W & W/O CONTRAST; MR CONTRAST GADOLIN                   Clip # [**Clip Number (Radiology) 12569**]
     Reason: R ICB and HX brain mets - eval - also with DWI for CVA Do MR
      Contrast: MAGNEVIST Amt: 15
     ______________________________________________________________________________
     UNDERLYING MEDICAL CONDITION:
      [**Age over 90 **] year old woman with lung CA- mets to brain                                   
     REASON FOR THIS EXAMINATION:
      R ICB and HX brain mets - eval - also with DWI for CVA Do MRI both with and 
      without contast please
     ______________________________________________________________________________
                                     FINAL REPORT
     EXAMINATION:  MRI of the brain with and without gadolinium.
     
     INDICATION:  [**Age over 90 **] year old woman with lung cancer and right intracranial bleed
     and history of brain metastases.  Please evaluate for acute infarct.
     
     TECHNIQUE:  Multiplanar T1 and T2-weighted images of the brain with gadolinium
     according to standard departmental protocol.  No prior study for comparison.
     
     FINDINGS:  On diffusion-weighted images there is a small area of restricted
     diffusion along the falx within the left occipitotemporal lobe.  It is also
     bright on FLAIR-weighted images and may represent a subacute infarct. Clinical
     correlation is recommended.  On gradient echo images there is a large area of
     intraparenchymal hemorrhage within the right parietal lobe and left thalamus
     which following administration of gadolinium reveals ring-enhancing lesions.
     These are suspicious for hemorrhagic metastases given the patient's history.
     Additional ring-enhancing lesions throughout the supra- and infratentorial
     compartments are visualized.  There is a moderate amount of peritumoral edema
     involving the right parietal lobe lesion in addition to a second right
     parietal lesion along the falx high in the vertex.  The other areas of
     metastases reveal a minimal amount of peritumoral edema.
     


----------------- END ROW -----------------
++++++ index :  1
++++++ subject_id :  56
++++++ charttime :  2644-01-17 00:00:00
++++++ text :  


     DATE: [**2644-1-17**] 10:53 AM
     MR HEAD W & W/O CONTRAST; MR CONTRAST GADOLIN                   Clip # [**Clip Number (Radiology) 12569**]
     Reason: R ICB and HX brain mets - eval - also with DWI for CVA Do MR
      Contrast: MAGNEVIST Amt: 15
     ______________________________________________________________________________
     UNDERLYING MEDICAL CONDITION:
      [**Age over 90 **] year old woman with lung CA- mets to brain                                   
     REASON FOR THIS EXAMINATION:
      R ICB and HX brain mets - eval - also with DWI for CVA Do MRI both with and 
      without contast please
     ______________________________________________________________________________
                                     FINAL REPORT
     EXAMINATION:  MRI of the brain with and without gadolinium.
     
     INDICATION:  [**Age over 90 **] year old woman with lung cancer and right intracranial bleed
     and history of brain metastases.  Please evaluate for acute infarct.
     
     TECHNIQUE:  Multiplanar T1 and T2-weighted images of the brain with gadolinium
     according to standard departmental protocol.  No prior study for comparison.
     
     FINDINGS:  On diffusion-weighted images there is a small area of restricted
     diffusion along the falx within the left occipitotemporal lobe.  It is also
     bright on FLAIR-weighted images and may represent a subacute infarct. Clinical
     correlation is recommended.  On gradient echo images there is a large area of
     intraparenchymal hemorrhage within the right parietal lobe and left thalamus
     which following administration of gadolinium reveals ring-enhancing lesions.
     These are suspicious for hemorrhagic metastases given the patient's history.
     Additional ring-enhancing lesions throughout the supra- and infratentorial
     compartments are visualized.  There is a moderate amount of peritumoral edema
     involving the right parietal lobe lesion in addition to a second right
     parietal lesion along the falx high in the vertex.  The other areas of
     metastases reveal a minimal amount of peritumoral edema.
     


----------------- END ROW -----------------
++++++ index :  2
++++++ subject_id :  56
++++++ charttime :  2644-01-17 00:00:00
++++++ text :  


     DATE: [**2644-1-17**] 10:43 AM
     CT HEAD W/O CONTRAST                                            Clip # [**Clip Number (Radiology) 12567**]
     Reason: ? stability of bleed                                        
     Admitting Diagnosis: HEAD BLEED
     ______________________________________________________________________________
     UNDERLYING MEDICAL CONDITION:
      [**Age over 90 **] year old woman with ? hemorragic mass                                        
     REASON FOR THIS EXAMINATION:
      ? stability of bleed                                                            
     No contraindications for IV contrast
     ______________________________________________________________________________
                                     FINAL REPORT  (REVISED)
     HISTORY:  [**Age over 90 **] year-old woman with a history of metastatic lung cancer to the
     brain.  Question hemorrhagic mass.  Question stability of bleed.
     
     COMPARISON:  Head CT from [**Hospital 12568**] Hospital dated [**2644-1-16**].
     
     TECHNIQUE:  Noncontrast head CT.
     
     FINDINGS:  Compared to the prior days study, there is stable appearance of the
     right parietal intraparenchymal hemorrhage with surrounding edema.  At the
     superior margin of the parenchymal hemorrhage there is a rounded heterogeneous
     focus which could represent a metastatic lesion.  An additional 2mm hyperdense
     focus, possibly hemorrhage, is noted in the posteromedial margin of the left
     thalamus, with surroundng edema.  Low-attenuation foci seen in both basal
     ganglia and insular regions are consistent with chronic lacunar infarcts.
     There is no shift of midline structures.  The ventricles are stable in
     appearance.  The osseous and soft tissue structures are unremarkable.
     
     IMPRESSION:  Stable appearance of right parietal lobe and left thalamic
     hemorrhages, which are concerning for hemorrhagic metastasis in this patient
     with known metastatic lung carcinoma to the brain.
     
     


----------------- END ROW -----------------
++++++ index :  3
++++++ subject_id :  56
++++++ charttime :  2644-01-17 00:00:00
++++++ text :  


     DATE: [**2644-1-17**] 6:37 AM
     CHEST (PORTABLE AP)                                             Clip # [**Clip Number (Radiology) 12564**]
     Reason: NGT placement                                               
     Admitting Diagnosis: HEAD BLEED
     ______________________________________________________________________________
     UNDERLYING MEDICAL CONDITION:
      [**Age over 90 **] year old woman with SAH                                                      
     REASON FOR THIS EXAMINATION:
      NGT placement                                                                   
     ______________________________________________________________________________
                                     FINAL REPORT
     INDICATION:  A [**Age over 90 **]-year-old woman with SAH.  Nasogastric tube placement.
     
     TECHNIQUE:  Portable AP chest radiograph.
     
     There is no previous chest radiograph for comparison.
     
     FINDINGS:
     
     The patient is status post median sternotomy.  Scoliosis is noted.  The
     patient is rotated.
     
     Note is made of mild cardiomegaly.  Note is made of rounded opacity overlying
     the left hilum, which probably representing aspiration pneumonia; however,
     left hilar mass cannot be totally excluded.  Mild CHF is noted.  No
     pneumothorax is noted.  Nasogastric tube is coursing down below the left
     hemidiaphragm.
     
     IMPRESSION:
     
     Cardiomegaly and mild CHF.  Nasogastric tube as described above.  Rounded
     opacity overlying the left hilum, which probably representing aspiration
     pneumonia; however, left hilar mass cannot be totally excluded.  Please
     confirm resolution by repeated chest x-ray after treatment, and if the lesion
     persists, please evaluate by CT scan.
     
     The information is communicated with the referring physician, [**Last Name (NamePattern4) 337**]. [**First Name8 (NamePattern2) 12565**] [**Last Name (NamePattern1) 12566**]
     by telephone in the morning of [**2644-1-17**].
                                                                           


----------------- END ROW -----------------
++++++ index :  4
++++++ subject_id :  56
++++++ charttime :  2644-01-19 00:00:00
++++++ text :  


     DATE: [**2644-1-19**] 12:09 PM
     CHEST (PORTABLE AP)                                             Clip # [**Clip Number (Radiology) 12570**]
     Reason: aspiration pna?                                             
     Admitting Diagnosis: HEAD BLEED
     ______________________________________________________________________________
     UNDERLYING MEDICAL CONDITION:
       [**Age over 90 **] year old woman with ICH and nmoe with increased secretions                  
                                          
     REASON FOR THIS EXAMINATION:
      aspiration pna?                                                                 
     ______________________________________________________________________________
                                     FINAL REPORT
     This is a portable semierect chest that is dated [**2644-1-19**] compared to
     [**2644-1-17**].
     
     CLINICAL INDICATION:  Question aspiration pneumonia.
     
     Nasogastric tube terminates below the diaphragm.  The cardiac silhouette
     appears enlarged, but stable.  There is a large mass-like opacity overlying
     the left hilum, which has slightly lobulated margins.  This measures about 6
     cm in greatest diameter.  Bilateral pleural effusions are noted, moderate on
     the left and small on the right.  No pneumothorax is identified.
     
     As compared to the previous study, alveolar opacities adjacent to the
     perihilar mass have resolved.
     
     IMPRESSION:
     
     Marked improvement in left perihilar alveolar process with residual
     well-marginated mass-like opacity, concerning for neoplasm.  This could be
     initially confirmed with PA and lateral chest radiographs, but CT may be
     considered for more complete characterization.  Findings communicated to
     clinical service caring for the patient.
                                                                           


----------------- END ROW -----------------

In [ ]: