In [1]:
import fcFinder as fc
import pyConTextNLP.itemData as itemData
import os
import importlib
In [24]:
import sys
import os
sys.path
sys.path.append(os.path.join(os.path.expanduser('~'),'Box Sync', 'Bucher_Surgical_MIMICIII/pyConText_implement/fcFinder/fcFinder.py'))
#fdir
In [25]:
sys.path
Out[25]:
In [5]:
importlib.reload(fc)
Out[5]:
In [4]:
"""import pyConTextNLP.pyConTextGraph as pyConText
from pyConTextNLP.pyConTextGraph import ConTextMarkup
from textblob import TextBlob
import networkx as nx
import os
import glob
import re
import copy
import networkx as nx
import platform
import copy
import uuid
import datetime
import time"""
''
Out[4]:
In [6]:
#import fcClasses
I'm designing a function to work as a pipeline using the module fcFinder. Here is an outline of how the function works:
As input, it takes:
itemData objectsThe function then follows this order, as demonstrated below:
markup objectsmentionAnnotation objects out of those tabObjects
In [7]:
#one annotation
modifiers = itemData.instantiateFromCSVtoitemData("/Users/alec/Box Sync/Bucher_Surgical_MIMICIII/pyConText_implement/modifiers.tsv")
targets = itemData.instantiateFromCSVtoitemData(
"file:///Users/alec/Box Sync/Bucher_Surgical_MIMICIII/pyConText_implement/targets.tsv")
In [8]:
input_report = '/Users/alec/Box Sync/Bucher_Surgical_MIMICIII/Radiology_Annotation/Adjudication/Batch_3/corpus/Yes_28226_116465_05-29-93.txt'
report = ''
with open(input_report,'r') as f0:
report += f0.read()
len(report)
report_name = os.path.basename(input_report)
report_name
Out[8]:
In [9]:
context = fc.create_context_doc(report, modifiers, targets)
In [10]:
len(context.getSectionMarkups())
Out[10]:
In [11]:
#importlib.reload(fc)
In [12]:
#Apply logic, classify tagObjects
annotations1=fc.fluid_collection_classifier(context,"textfile.txt")
In [13]:
annotation_strings = []
print(type(annotations1))
print(type(annotations1[0]))
for annotation in annotations1:
annotation_strings.append(annotation.getXML())
print(annotation.getMentionClass())
#definitive_mention = fc.fluid_collection_classifier(context)[0][0]
#definitive_mention.has_edge()
#Will this work to catch the appropriate anatomical modifiers?
#[x.getXML() for x in annotations1]
In [14]:
#print(type(definitive_mention))
#tagObject1 = definitive_mention.nodes()[0]
#tagObject1.getSpan()
In [15]:
#annotation1 = fcClasses.createAnnotations(definitive_mention,'fluid collection-definitive','Yes_28226_116465_05-29-93.txt')[0]
annotation1 = annotations1[0]
XML1 = annotation1.getXML()
print(XML1)
In [16]:
#XML1 = fcClasses.getXML(annotation1)
In [17]:
#print(XML1)
#importlib.reload(fc)
In [18]:
print(len(annotation_strings))
#print(len(fc.writeKnowtator(annotation_strings,report_name,os.path.join('..','XML_examples'))))
print(fc.writeKnowtator(annotation_strings,report_name,os.path.join('/Users/alec/Desktop/fcFinder_test','saved')))
In [19]:
input_report2 = '/Users/alec/Box Sync/Adjudication/Batch_3/corpus/No_69411_129942_04-30-58.txt'
os.getcwd()
Out[19]:
In [20]:
def fcFinder(input_report, output_dir, modifiers=modifiers, targets=targets):
if not os.path.exists(output_dir):
os.mkdir(os.path.join(output_dir,'saved'))
report = ''
with open(input_report,'r') as f0:
report += f0.read()
context = fc.create_context_doc(report, modifiers, targets)
report_name = os.path.basename(input_report)
annotations = fc.fluid_collection_classifier(context,input_report)
XML_strings = [x.getXML() for x in annotations]
return fc.writeKnowtator(XML_strings,report_name,os.path.join(output_dir,'saved'))
In [21]:
fcFinder(input_report,'/Users/alec/Desktop/fcFinder_test')
Out[21]:
In [127]:
### Need to work on this ###
def fcFinder_batches(batch_path, batch_schema,output_dir, targets=targets,modifiers=modifiers):
batches = batch_path+'//%s'%batch_schema
#return os.listdir(batch_path)
listOfBatches = glob.glob(batches)
counter = 0
#return listOfBatches
for batch in listOfBatches:
#print(batch)
corpus = os.path.join(batch,'corpus')
for file in os.listdir(corpus):
file_path = os.path.isfile(os.path.join(batch,corpus,file))
#print(file)
counter += 1
if os.path.isfile(os.path.join(batch,corpus,file)):
#print(file)
if not os.path.exists(os.path.join(output_dir,batch)):
os.mkdir(os.path.exists(output_dir,bat))
fcFinder(file_path,modifiers,targets,os.path.join(output_dir,batch))
return counter
return output_dir
return listOfBatches
fcFinder_batches('/Users/alec/Box Sync/Adjudication','Batch*','/Users/alec/Desktop/fcFinder_test')
In [71]:
fcFinder(input_report, modifiers, targets, os.getcwd())
Out[71]:
In [69]:
fcFinder(input_report2, modifiers, targets, os.getcwd())
Out[69]:
In [57]:
markup2 = context.getSectionMarkups()[0]
markup2
Out[57]:
In [ ]: