Skip to content

Commit

Permalink
Merge pull request #3 from raluca-san/master
Browse files Browse the repository at this point in the history
pull for update the project
  • Loading branch information
tri16cc authored Nov 27, 2018
2 parents 47dcc4c + 0c9e8c7 commit 3c0c47a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 42 deletions.
34 changes: 24 additions & 10 deletions XMLProcessing/NeedlesInfoClasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def findRegistration(self, REGISTRATION_MATRIX):
def addLesion(self, lesion):
self.lesions.append(lesion)

def addNewLesion(self, location):
lesion = Lesion(location)
def addNewLesion(self, location, time_intervention):
lesion = Lesion(location, time_intervention)
self.addLesion(lesion)
return lesion

Expand Down Expand Up @@ -84,8 +84,9 @@ def setRegistrationInfo(self, r_matrix, r_type, pp_planning, pp_validation):

class Lesion:
# location is a numpy array
def __init__(self, location):
def __init__(self, location, intervention_date):
self.needles = []
self.intervention_date = intervention_date
if location is not None and len(location) is 3:
self.location = location
else:
Expand Down Expand Up @@ -276,6 +277,7 @@ def to_dict(self, patientID, patient_name, lesionIdx, needle_idx, dict_needles,
dict_needles['NeedleNr'].append(needle_idx)
dict_needles['NeedleType'].append(self.needle_type)
dict_needles['CAS_Version'].append(self.cas_version)
dict_needles['CT_series_Plan'].append(self.ct_series)
dict_needles['TimeIntervention'].append(self.time_intervention)
dict_needles['PlannedEntryPoint'].append(self.planned.entrypoint)
dict_needles['PlannedTargetPoint'].append(self.planned.targetpoint)
Expand Down Expand Up @@ -347,6 +349,7 @@ def to_dict(self, patientID, patient_name, lesionIdx, needle_idx, dict_needles,
dict_needles['NeedleNr'].append(needle_idx)
dict_needles['NeedleType'].append(self.needle_type)
dict_needles['CAS_Version'].append(self.cas_version)
dict_needles['CT_series_Plan'].append(self.ct_series)
dict_needles['TimeIntervention'].append(self.time_intervention)
dict_needles['PlannedEntryPoint'].append(self.planned.entrypoint)
dict_needles['PlannedTargetPoint'].append(self.planned.targetpoint)
Expand Down Expand Up @@ -374,11 +377,22 @@ def to_dict(self, patientID, patient_name, lesionIdx, needle_idx, dict_needles,
dict_needles['AblationShapeIndex'].append(None)
dict_needles['AblatorType'].append(None)
dict_needles['Ablation_Segmentation_Datetime'].append(None)
dict_needles['RegistrationFlag'].append(img_registration[0].r_flag)
dict_needles['RegistrationMatrix'].append(img_registration[0].r_matrix)
dict_needles['PP_planing'].append(img_registration[0].pp_planning)
dict_needles['PP_validation'].append(img_registration[0].pp_validation)
dict_needles['RegistrationType'].append(img_registration[0].r_type)
try:
dict_needles['RegistrationFlag'].append(img_registration[0].r_flag)
dict_needles['RegistratqionMatrix'].append(img_registration[0].r_matrix)
dict_needles['PP_planing'].append(img_registration[0].pp_planning)
dict_needles['PP_validation'].append(img_registration[0].pp_validation)
dict_needles['RegistrationType'].append(img_registration[0].r_type)
except Exception as e:
print(repr(e))
print('patient id: ', patientID)
dict_needles['RegistrationFlag'].append(None)
dict_needles['RegistratqionMatrix'].append(None)
dict_needles['PP_planing'].append(None)
dict_needles['PP_validation'].append(None)
dict_needles['RegistrationType'].append(None)


return dict_needles


Expand All @@ -391,12 +405,12 @@ class NeedleToDictWriter:
needles: needles class object
"""

def needlesToDict(patientID, patient_name, lesionIdx, needles, img_registration):
def needlesToDict(patientID, patient_name, lesion_count, needles, img_registration):
if len(needles)>0:
# for each patient the dict_needles defaultdict is reset
dict_needles = defaultdict(list)
for needle_idx, needle in enumerate(needles):
needle.to_dict(patientID, patient_name, lesionIdx, needle_idx, dict_needles, img_registration)
needle.to_dict(patientID, patient_name, lesion_count, needle_idx, dict_needles, img_registration)
return dict_needles
else:
print('No needles for this lesion')
Expand Down
33 changes: 17 additions & 16 deletions XMLProcessing/mainExtractTrajectories.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@
from time import strftime
from collections import defaultdict

"""
#Raluca's Libraries

# Raluca's Libraries
import readInputKeyboard
import NeedlesInfoClasses
import parseNeedleTrajectories as parseNeedleTrajectories

import extractTrajectoriesAngles as eta
from customize_dataframe import customize_dataframe
"""
#Trini's Libraries
import XMLProcessing.NeedlesInfoClasses as NeedlesInfoClasses
import XMLProcessing.parseNeedleTrajectories as parseNeedleTrajectories

import XMLProcessing.extractTrajectoriesAngles as eta
from XMLProcessing.customize_dataframe import customize_dataframe


#Trini's Libraries
# import XMLProcessing.NeedlesInfoClasses as NeedlesInfoClasses
# import XMLProcessing.parseNeedleTrajectories as parseNeedleTrajectories
#
# import XMLProcessing.extractTrajectoriesAngles as eta
# from XMLProcessing.customize_dataframe import customize_dataframe
#
#
# %%
# rootdir = r"C:\Stockholm_IRE_Study\IRE_Stockholm_allCases"
rootdir = r"C:\Stockholm_IRE_Study\data"
rootdir = r"C:\Stockholm_IRE_Study\data_test"
outfilename = "IRE_Analysis"
flag_angles = 'n'
# rootdir = readInputKeyboard.getNonEmptyString("Root Directory given as r")
Expand All @@ -46,9 +46,10 @@

if fileExtension.lower().endswith('.xml') and (
'validation' in fileName.lower() or 'plan' in fileName.lower()):
# if fileExtension.lower().endswith('.xml') and (
# 'validation' in fileName.lower() ):
xmlFilePathName = os.path.join(subdir, file)
xmlfilename = os.path.normpath(xmlFilePathName)

xmlobj = parseNeedleTrajectories.I_parseRecordingXML(xmlfilename)

if xmlobj is 1:
Expand Down Expand Up @@ -106,10 +107,10 @@
for l_idx, lesion in enumerate(lesions):
needles = lesion.getNeedles()
needles_defaultdict = NeedlesInfoClasses.NeedleToDictWriter.needlesToDict(patientID,
patientName,
l_idx,
needles,
img_registration)
patientName,
l_idx+1,
needles,
img_registration)
needles_list.append(needles_defaultdict)
# unpack from defaultdict and list
needles_unpacked_list = defaultdict(list)
Expand Down
51 changes: 35 additions & 16 deletions XMLProcessing/parseNeedleTrajectories.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
import numpy as np
import untangle as ut
from collections import defaultdict
"""

#Raluca's Libraries
import xml.etree.ElementTree as ET
from extractTPEsXml import extractTPES
from elementExistsXml import elementExists
from splitAllPaths import splitall
"""


#Trini's Libraries
import xml.etree.ElementTree as ET
from XMLProcessing.extractTPEsXml import extractTPES
from XMLProcessing.elementExistsXml import elementExists
from XMLProcessing.splitAllPaths import splitall
# import xml.etree.ElementTree as ET
# from XMLProcessing.extractTPEsXml import extractTPES
# from XMLProcessing.elementExistsXml import elementExists
# from XMLProcessing.splitAllPaths import splitall



Expand All @@ -42,7 +42,10 @@ def extract_patient_id(filename, patient_id_xml, patient_name_flag=True):
"""
all_paths = splitall(filename)
ix_patient_folder_name = [i for i, s in enumerate(all_paths) if "Pat_" in s]
patient_folder_name = all_paths[ix_patient_folder_name[0]]
try:
patient_folder_name = all_paths[ix_patient_folder_name[0]]
except Exception as e:
print(repr(e))
patient_id = re.search("\d", patient_folder_name) # numerical id
ix_patient_id = int(patient_id.start())
underscore = re.search("_", patient_folder_name[ix_patient_id:])
Expand Down Expand Up @@ -176,7 +179,9 @@ def IV_parseNeedles(children_trajectories, lesion, needle_type, ct_series, xml_f
3. needle_type (string) MWA or IRE
OUTPUT: doesn't return anything, just sets the TPEs
"""

for singleTrajectory in children_trajectories:

ep_planning = np.array([float(i) for i in singleTrajectory.EntryPoint.cdata.split()])
tp_planning = np.array([float(i) for i in singleTrajectory.TargetPoint.cdata.split()])
# find if the needle exists already in the patient repository
Expand Down Expand Up @@ -236,18 +241,32 @@ def III_parseTrajectory(trajectories, patient, ct_series, xml_filepath, time_int
OUTPUT: list of Needle Trajectories passed to Needle Trajectories function
"""
for xmlTrajectory in trajectories:
# xmltrajectory count contains the number of lesions
# Trajectories contains all the upper-level Parent trajectories
# check whether it's IRE trajectory
ep_planning = np.array([float(i) for i in xmlTrajectory.EntryPoint.cdata.split()])
tp_planning = np.array([float(i) for i in xmlTrajectory.TargetPoint.cdata.split()])

if (xmlTrajectory['type']) and 'IRE' in xmlTrajectory['type']:
needle_type = 'IRE'
# function to check if the lesion exists based on location returning true or false
lesion = patient.findLesion(lesionlocation=tp_planning, DISTANCE_BETWEEN_LESIONS=1000)
lesion = patient.findLesion(lesionlocation=tp_planning, DISTANCE_BETWEEN_LESIONS=3)
if lesion is None:
lesion = patient.addNewLesion(tp_planning) # input parameter target point of reference trajectory

children_trajectories = xmlTrajectory
lesion = patient.addNewLesion(tp_planning, time_intervention)
needle = lesion.findNeedle(needlelocation=tp_planning, DISTANCE_BETWEEN_NEEDLES=3)
if needle is None:
needle = lesion.newNeedle(True, needle_type, ct_series)
# the reference needle has only planning data
tps = needle.setTPEs()
validation = needle.setValidationTrajectory()
planned = needle.setPlannedTrajectory()
planned.setTrajectory(ep_planning, tp_planning)
planned.setLengthNeedle()
needle.setTimeIntervention(time_intervention)
needle.setCASversion(cas_version)

children_trajectories = xmlTrajectory.Children.Trajectory
# needle level
IV_parseNeedles(children_trajectories, lesion, needle_type, ct_series, xml_filepath, time_intervention,
cas_version)

Expand All @@ -258,21 +277,21 @@ def III_parseTrajectory(trajectories, patient, ct_series, xml_filepath, time_int
# remove the lesion identification based on the distance between needles, too much variation for accurate identification
# put an absurd value for DISTANCE_BETWEEN_LESIONS
needle_type = 'IRE'
lesion = patient.findLesion(lesionlocation=tp_planning, DISTANCE_BETWEEN_LESIONS=1000)
lesion = patient.findLesion(lesionlocation=tp_planning, DISTANCE_BETWEEN_LESIONS=100)
if lesion is None:
lesion = patient.addNewLesion(tp_planning)
lesion = patient.addNewLesion(tp_planning, time_intervention)
children_trajectories = xmlTrajectory
IV_parseNeedles(children_trajectories, lesion, needle_type,
ct_series, xml_filepath, time_intervention, cas_version)

else:
# assuming 'EG_ATOMIC_TRAJECTORY' stands for MWA type of needle
needle_type = "MWA"
# drop the lesion identification for MWA. multiple needles might be
# no clear consensus for minimal distance between lesions and no info in the log version <=2.9
# DISTANCE_BETWEEN_LESIONS=1000 very large number
lesion = patient.findLesion(lesionlocation=tp_planning, DISTANCE_BETWEEN_LESIONS=1000)
lesion = patient.findLesion(lesionlocation=tp_planning, DISTANCE_BETWEEN_LESIONS=3)
if lesion is None:
lesion = patient.addNewLesion(tp_planning)
lesion = patient.addNewLesion(tp_planning, time_intervention)
children_trajectories = xmlTrajectory
IV_parseNeedles(children_trajectories, lesion, needle_type,
ct_series, xml_filepath, time_intervention, cas_version)
Expand Down

0 comments on commit 3c0c47a

Please sign in to comment.