Skip to content

Commit c0b8063

Browse files
committed
add support for CHIRP data
1 parent e5149d6 commit c0b8063

File tree

5 files changed

+130
-4
lines changed

5 files changed

+130
-4
lines changed

src/admin_console/DataImporter.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def setUploadContext(self, contextId:str):
4747
self._initialiseFileInfo()
4848

4949
def getUploadFileInfo(self) -> Dict:
50+
self.fileInfo['clinical_trial'] = self.metadata['clinical_trial']
5051
return self.fileInfo
5152

5253
def verifyUploadPacket(self) -> Tuple[bool, str]:
@@ -368,6 +369,27 @@ def insertDoseReconstrcutionFileIntoDatabase(self) -> Tuple[bool, str]:
368369
self.dbAdapter.executeUpdateOnImageDB(doseQueryStr)
369370
self.markPacketAsImported()
370371
return True, "Success"
372+
373+
def insertCHIRPDataIntoDatabase(self) -> Tuple[bool, str]:
374+
fileType = self.fileInfo["file_type"]
375+
dbTableMapping = {
376+
"patient_dose_files":"rt_dose_path",
377+
"patient_planning_cts":"rt_ct_path",
378+
"patient_structure_sets":"rt_structure_path",
379+
"patient_plans":"rt_plan_path"
380+
}
381+
if self.fileInfo['level'] == "prescription":
382+
folderPath = self.fileInfo["folder_path"][0]
383+
patientId = self.dbAdapter.getPatientId(self.metadata["patient_trial_id"])
384+
if patientId:
385+
queryStr = f"UPDATE prescription SET {dbTableMapping[fileType]} = \'{folderPath}\' WHERE patient_id = \'{patientId}\'"
386+
self.dbAdapter.executeUpdateOnImageDB(queryStr)
387+
print(queryStr)
388+
self.markPacketAsImported()
389+
return True, "Success"
390+
elif self.fileInfo['level'] == "fraction":
391+
pass
392+
371393

372394
def prepareArgumentParser():
373395
argParser = argparse.ArgumentParser(description="data Importer Tool")

src/admin_console/DatabaseAdapter.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,22 @@ def updateFractionName(self, fractionId:str, fractionName:str) -> DBUpdateResult
241241
+ "WHERE fraction_id = '" + fractionId + "';"
242242
return self.executeUpdateOnImageDB(stmt=stmt)
243243

244+
def getPatientId(self, patientTrialId:str) -> str:
245+
strQuery = "SELECT id FROM patient WHERE patient_trial_id = '" + patientTrialId + "';"
246+
if config.APP_DEBUG_MODE:
247+
print("Executing Query:", strQuery)
248+
249+
try:
250+
conn = self.getImageDBConnection()
251+
cur = conn.cursor()
252+
cur.execute(strQuery)
253+
patientId = cur.fetchone()[0]
254+
cur.close()
255+
except (Exception, pg.DatabaseError) as error:
256+
print(error, file=sys.stderr)
257+
return None
258+
return patientId
259+
244260
def insertFractionIntoDB(self, fractionDetails:Dict) -> bool:
245261
insertStmt = "INSERT INTO fraction (prescription_id, " \
246262
+ "fraction_date, fraction_number, " \

src/admin_console/application.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,10 @@ def importUploadPacket(upload_id):
9191
if config.APP_DEBUG_MODE:
9292
print("Copied files into storage", result[1])
9393
fileInfo = di.getUploadFileInfo()
94-
95-
if fileInfo['file_type'] == "fraction_folder":
94+
if fileInfo['clinical_trial'] == "CHIRP":
95+
# Only design for CHIRP data import
96+
result = di.insertCHIRPDataIntoDatabase()
97+
elif fileInfo['file_type'] == "fraction_folder":
9698
result = di.insertFractionDataIntoDatabase()
9799
result2 = di.insertImagePathIntoDatabase()
98100
if config.APP_DEBUG_MODE:

src/data_service/ContentManager.py

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,84 @@ def _processTriangulationAndKimLogs(self, metadata, filename, uploadMetaData, fi
540540
)
541541
return saveFolderPath
542542
return ""
543+
544+
def _processCHIRPPresLevel(self, metadata, filename, uploadMetaData, filesSaved, fileTypeToPathMapping, formatedPath):
545+
uploadId:str = metadata["upload_context"]
546+
relativeFolderPath = fileTypeToPathMapping[metadata["file_type"]].format(
547+
clinical_trial=metadata['clinical_trial'],
548+
test_centre=metadata["test_centre"],
549+
centre_patient_no=int(metadata["centre_patient_no"])
550+
)
551+
relativePath = uploadId + relativeFolderPath + filename
552+
saveFolderPath = config.UPLOAD_FOLDER + '/' + uploadId + relativeFolderPath
553+
filesSaved.append(relativePath)
554+
555+
filePathAppended:bool = False
556+
for uploadedFileRecord in uploadMetaData["uploaded_files"]:
557+
if uploadedFileRecord["file_type"] == metadata["file_type"]:
558+
uploadedFileRecord["Files"].append(relativePath)
559+
filePathAppended = True
560+
break
561+
562+
if not filePathAppended:
563+
uploadMetaData["uploaded_files"].append(
564+
{
565+
"file_type": metadata["file_type"],
566+
"level": metadata["level"],
567+
"Files": [relativePath],
568+
"folder_path": [relativeFolderPath]
569+
}
570+
)
571+
return saveFolderPath
572+
573+
def _processCHIRPFractionLevel(self, metadata, filename, uploadMetaData, filesSaved, fileTypeToPathMapping, formatedPath):
574+
uploadId:str = metadata["upload_context"]
575+
fractionNumber = re.search(r'(?i)fx(\d+)', formatedPath).group(1)
576+
fractionName = re.search(r'/(?P<result>[^/]+)', formatedPath).group("result")
577+
relativeFolderPath = fileTypeToPathMapping[metadata["file_type"]].format(
578+
clinical_trial=metadata['clinical_trial'],
579+
test_centre=metadata["test_centre"],
580+
centre_patient_no=int(metadata["centre_patient_no"]),
581+
) + fractionName + '/'
582+
relativePath = uploadId + relativeFolderPath + filename
583+
saveFolderPath = config.UPLOAD_FOLDER + '/' + uploadId + relativeFolderPath
584+
585+
filePathAppended:bool = False
586+
for uploadedFileRecord in uploadMetaData["uploaded_files"]:
587+
if uploadedFileRecord["file_type"] == metadata["file_type"]:
588+
uploadedFileRecord["Files"].append(relativePath)
589+
if fractionNumber not in uploadedFileRecord["fraction"]:
590+
uploadedFileRecord["fraction"].append(fractionNumber)
591+
if fractionName not in uploadedFileRecord["fraction_name"]:
592+
uploadedFileRecord["fraction_name"].append(fractionName)
593+
if fractionNumber not in uploadedFileRecord["db_file_name"].keys():
594+
uploadedFileRecord["db_file_name"][fractionNumber] = relativeFolderPath
595+
if relativeFolderPath not in uploadedFileRecord["folder_path"]:
596+
uploadedFileRecord["folder_path"].append(relativeFolderPath)
597+
filePathAppended = True
598+
if not filePathAppended:
599+
pack = {
600+
fractionNumber: relativeFolderPath
601+
}
602+
uploadMetaData["uploaded_files"].append(
603+
{
604+
"file_type": metadata["file_type"],
605+
"level": metadata["level"],
606+
"fraction": [fractionNumber],
607+
"fraction_name": [fractionName],
608+
"Files": [relativePath],
609+
"folder_path": [relativeFolderPath],
610+
"db_file_name": pack
611+
}
612+
)
613+
return saveFolderPath
614+
615+
def _processCHIRP(self, metadata, filename, uploadMetaData, filesSaved, fileTypeToPathMapping, formatedPath):
616+
if metadata["level"] == "prescription":
617+
return self._processCHIRPPresLevel(metadata, filename, uploadMetaData, filesSaved, fileTypeToPathMapping, formatedPath)
618+
elif metadata["level"] == "fraction":
619+
return self._processCHIRPFractionLevel(metadata, filename, uploadMetaData, filesSaved, fileTypeToPathMapping, formatedPath)
620+
543621

544622
def acceptAndSaveFile(self, req:request):
545623
# print("Files:", req.files)
@@ -624,7 +702,9 @@ def acceptAndSaveFile(self, req:request):
624702
uploadedFile = req.files[fileFieldName]
625703
filename = secure_filename(uploadedFile.filename)
626704
formatedPath = os.path.basename(req.form["file_path"]).replace("\\", "/").replace(filename, "")
627-
if metadata["file_type"] == "fraction_folder":
705+
if metadata["clinical_trial"] == "CHIRP":
706+
saveFolderPath = self._processCHIRP(metadata, filename, uploadMetaData, filesSaved, fileTypeToPathMapping, formatedPath)
707+
elif metadata["file_type"] == "fraction_folder":
628708
saveFolderPath = self._processImageFractionFolder(metadata, filename, uploadMetaData, filesSaved, fileTypeToPathMapping, formatedPath)
629709
elif metadata["file_type"] == "DICOM_folder" or metadata["file_type"] == "DVH_folder":
630710
saveFolderPath = self._processDoseReconstructionPlan(metadata, filename, uploadMetaData, fileTypeToPathMapping, filesSaved, formatedPath)

src/data_service/templates/upload_paths_template.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,11 @@
2222
"DVH_folder":"/{clinical_trial}/{test_centre}/Dose Reconstruction/DVH/Patient {centre_patient_no}/",
2323
"triangulation_folder":"/{clinical_trial}/{test_centre}/Triangulation/Patient {centre_patient_no}/",
2424
"image_folder": "/{clinical_trial}/{test_centre}/Patient Images/Projection images/Patient {centre_patient_no}/",
25-
"trajectory_log_folder": "/{clinical_trial}/{test_centre}/Trajectory Logs/Patient {centre_patient_no}/"
25+
"trajectory_log_folder": "/{clinical_trial}/{test_centre}/Trajectory Logs/Patient {centre_patient_no}/",
26+
"patient_dose_files":"/{clinical_trial}/{test_centre}/Patient Dose Files/Patient {centre_patient_no}/",
27+
"patient_planning_cts":"/{clinical_trial}/{test_centre}/Patient Planning CTs/Patient {centre_patient_no}",
28+
"patient_structure_sets":"/{clinical_trial}/{test_centre}/Patient Structure Sets/Patient {centre_patient_no}",
29+
"patient_plans":"/{clinical_trial}/{test_centre}/Patient Plans/Patient {centre_patient_no}",
30+
"patient_cbct_images":"/{clinical_trial}/{test_centre}/Patient CBCT Images/Patient {centre_patient_no}/",
31+
"couch_registrations_files":"/{clinical_trial}/{test_centre}/Couch Registrations/Patient {centre_patient_no}/"
2632
}

0 commit comments

Comments
 (0)