- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# C:/AngioLocalizeNoAuto/MevisLabNoAutoReg/STUMevisApp/Angiolocalize/Modules/Macros/STUDSA2D/DSA2D.py

    00001 """
    00002 * Copyright (c) 2009 Stanford University, School of Medicine All rights reserved.
    00003 *
    00004 * STANFORD ACADEMIC SOFTWARE LICENSE AGREEMENT
    00005 * FOR AngioLocalize SOURCE CODE:
    00006 *
    00007 * 1.  This is a legal agreement between you, RECIPIENT, and STANFORD UNIVERSITY.
    00008 *       By accepting, receiving, and using AngioLocalize.including any accompanying information, materials or
    00009 *       manuals (Program), you are agreeing to be bound by the terms of this Agreement.
    00010 *       If you do not agree to the terms of this Agreement, promptly return the Program to the STANFORD.
    00011
    00012 * 2.  STANFORD grants to RECIPIENT a royalty-free, nonexclusive, and nontransferable license to use the Program
    00013 *       and accompanying source code furnished hereunder, upon the terms and conditions set out below.
    00014 *       documentation and/or other materials provided with the distribution.
    00015 * 3.  RECIPIENT acknowledges that the Program is a research tool still in the development stage and that
    00016 *       it is being supplied as is, without any accompanying services, support or improvements from STANFORD.
    00017 * 4.  RECIPIENT agrees to use the Program solely for internal non-commercial purposes.
    00018 * 5.  RECIPIENT may create derivatives and further transfer the Program under the following terms and conditions:
    00019 *        A) Recipient agrees to not commercially exploit the Program and agrees all uses will be for non-commercial purposes; and,
    00020 *        B) Recipient may not mark any derivatives with the title of Program and may not to use iPad mark for any derivatives; and,
    00021 *        C) Recipient agrees to mark and acknowledge all derivatives as follows: "This is a derivative work based on AngioLocalize:
    00022 *           Frameless AVM localization with 2D Digital Subtraction Angiography (DSA) and 3D Rotational Angiography (DRA) Images developed
    00023 *           by Dimitre Hristov, Lina Liu.
    00024 *        D) Recipient agrees to make all improvements, enhancements and derivatives available under
    00025 *           similar terms and conditions to the research community.
    00026 * 6.   RECIPIENT warrants that RECIPIENT will not remove or export any part of the software or Program from the United
    00027 *       States except in full compliance with all United States and other applicable laws and regulations.
    00028 * 7.  RECIPIENT will use the Program in compliance with all applicable laws, policies and regulations including, but not
    00029 *       limited to, any approvals, informed consent and patient confidentiality principles.
    00030 * 8.   RECIPIENT will indemnify, hold harmless, and defend Stanford against any claim of any kind arising out of or
    00031 *       related to the exercise of any rights granted under this Agreement or the breach of this Agreement by RECIPIENT.
    00032 * 9.  Title and copyright to the Program and any associated documentation shall at all times remain with STANFORD,
    00033 *       and RECIPIENT agrees to preserve same.
    00034 """
    00035 #----------------------------------------------------------------------------------
    00036 #! Macro module DSA2D
    00037 #/*!
    00038 # \file    DSA2D.py
    00039 # \author  LinaLiu
    00040 # \date    2009-12-10
    00041 #
    00042 #
    00043 #*/
    00044 #----------------------------------------------------------------------------------
    00045
    00046 from mevis import *
    00047
    00048 from PythonQt import *
    00049 import time;
    00050
    00051
    00052 ## Global varibles
    00053 filename = "";
    00054
    00055 ## init the main window of DSA2D. hard coded the directory to browse and set the patient information
    00056 def initWindow():
    00057   ctx.field("PatientsName").setValue(ctx.parent().field("PatientsName").value);
    00058   ctx.field("PatientsBirthDate").setValue(ctx.parent().field("PatientsBirthDate").value);
    00059   ctx.field("PatientsSex").setValue(ctx.parent().field("PatientsSex").value);
    00060   ctx.field("PatientID").setValue(ctx.parent().field("PatientID").value);
    00061   ctx.field("OpenImage2.dicomBrowser.reload").touch();
    00062   ctx.field("OpenImage2.previewLoad.autoLoad").touch();
    00063   ctx.field("OpenImage2.imgLoad.autoLoad").touch();
    00064 ## init Module set the initial settings of some sub macroModule
    00065 def initModule():
    00066   ctx.field("Subimage.modifyX").setBoolValue(False);
    00067   ctx.field("Subimage.modifyY").setBoolValue(False);
    00068   ctx.field("Subimage.modifyX").touch();
    00069   ctx.field("Subimage.modifyY").touch();
    00070   ctx.field("Subimage.fullSize").touch();
    00071   ctx.field("Subimage.modifyZ").setBoolValue(True);
    00072   ctx.field("Subimage.sz").setDoubleValue(1);
    00073   ctx.field("Subimage.apply").touch();
    00074   ctx.field("ImgPropConvert1.changeWorldMatrix").setValue(False);
    00075   ctx.field("View2D.ext.annotation.annotationUserBottomLeft").setValue("Timepoint: $(slice) \n$(sizex),$(sizey),$(sizec) \n$(voxelx),$(voxely) \n$(SeriesDescription)");
    00076   ctx.field("View2D.ext.annotation.showOrientation").setValue("ANNO_ORIENTATION_OFF");
    00077   ctx.field("View2D.ext.annotation.annotationMode").setValue("ANNO_MODE_USER");
    00078
    00079 ## send a query to the Database and return the result set
    00080 def sendQuery(query):
    00081   ctx.field("DBConn.Statement").setValue(query);
    00082   dbModule = ctx.module("DBConn");
    00083   result = dbModule.call("sendQuery");
    00084   if (result == -1):
    00085     MLAB.logError("The following query to database failed. \n" + query + "\n.");
    00086   dbModule.call("close");
    00087   return result;
    00088
    00089 ## When user specify a new image, retrieve the DicomTag values.
    00090 def newImageName():
    00091    ctx.field("DicomTagModify.tagName0").value = "PatientsName";
    00092    ctx.field("DicomTagModify.tagName1").value = "PatientsBirthDate";
    00093    ctx.field("DicomTagModify.tagName2").value = "PatientID";
    00094    ctx.field("DicomTagModify.tagName3").value = "PatientsSex";
    00095    ctx.field("DicomTagModify.tagName4").setValue("SeriesDescription");
    00096    ctx.field("DicomTagModify.tagName5").setValue("Modality");
    00097    ctx.field("DicomTagModify.getValues").touch();
    00098
    00099    PatientN = ctx.field("DicomTagModify.tagValue0").value;
    00100    PatientDOB = ctx.field("DicomTagModify.tagValue1").value;
    00101    ID = ctx.field("DicomTagModify.tagValue2").value;
    00102    Sex = ctx.field("DicomTagModify.tagValue3").value;
    00103    if PatientN != ctx.field("PatientsName").value or PatientDOB != ctx.field("PatientsBirthDate").value or ID != ctx.field("PatientID").value or Sex != ctx.field("PatientsSex").value:
    00104      details = "";
    00105      if PatientN != ctx.field("PatientsName").value:
    00106        details += "<br>PatientName in Dicom: <b>" + PatientN + "</b> versus <b>" +  ctx.field("PatientsName").value + "</b> in records.";
    00107      if PatientDOB != ctx.field("PatientsBirthDate").value:
    00108        details +=  "<br>PatientBirthDate in Dicom: <b>" + PatientDOB + "</b> versus <b>" +  ctx.field("PatientsBirthDate").value + "</b> in records."
    00109      if ID != ctx.field("PatientID").value:
    00110        details += "<br>PatientID in Dicom:<b>" + ID + "</b> versus <b>" +  ctx.field("PatientID").value + "</b> in records.";
    00111      if Sex != ctx.field("PatientsSex").value:
    00112        details += "<br>PatientSex in Dicom:<b>" + Sex + "</b> versus <b>" +  ctx.field("PatientsSex").value + "</b> in records.\n";
    00113      answer = MLAB.showQuestion("The DICOM image info does not match patient's record. Do you want to replace the DICOM tags with patient's info in record?", details, "MeVisLab", ["OK", "Cancel"], 0);
    00114      if answer == 1:
    00115        msg = "The user canceled the operation when the patient record did not match that in database. The patient information in DICOM stays the same as follows:";
    00116        msg =+ "Patient Name:" + PatientN + "  DOB: " + PatientDOB + "  ID:" + ID + "  Sex:" + Sex;
    00117        MLAB.log(msg);
    00118        return;
    00119      else:
    00120        ##save the data into the image
    00121        ctx.field("DicomTagModify.tagValue0").value = ctx.field("PatientsName").value;
    00122        ctx.field("DicomTagModify.tagValue1").value = ctx.field("PatientsBirthDate").value;
    00123        ctx.field("DicomTagModify.tagValue2").value = ctx.field("PatientID").value;
    00124        ctx.field("DicomTagModify.tagValue3").value = ctx.field("PatientsSex").value;
    00125        ctx.field("DicomTagModify.apply").touch();
    00126        msg = "The user replaced the patient information in DICOM image with that in database\n";
    00127        msg += "Patient information saved into DICOM are as follows:\n";
    00128        msg += "Patient Name:" +  ctx.field("PatientsName").value;
    00129        msg += "\nPatient Birth Date:" + ctx.field("PatientsBirthDate").value;
    00130        msg += "\nPatient ID:" + ctx.field("PatientID").value;
    00131        msg += "\nPatient Sex:" + ctx.field("PatientsSex").value;
    00132        MLAB.log(msg);
    00133    tempimage=ctx.field("DicomTagModify.output0").image();
    00134    extent=tempimage.imageExtent();
    00135    t=extent[4];
    00136    if t != 1:
    00137      ctx.field("SwapFlipDimensions.firstAxis").value = "z";
    00138      ctx.field("SwapFlipDimensions.input0").connectFrom(ctx.field("DicomTagModify.output0"));
    00139      ctx.field("ImgSave.input0").connectFrom(ctx.field("SwapFlipDimensions.output0"));
    00140      ctx.field("View2D.inImage").connectFrom(ctx.field("SwapFlipDimensions.output0"));
    00141    else:
    00142      ctx.connectField("SetPageExt.input0", "DicomTagModify.output0");
    00143      ctx.field("ImgSave.input0").connectFrom(ctx.field("ConcatImages.output0"));
    00144      ctx.field("View2D.inImage").connectFrom(ctx.field("ConcatImages.output0"));
    00145    ## pop up the saveImage window with the patient information as the window title
    00146    wind = ctx.showWindow("SaveImgW");
    00147    title = "Patient: " + ctx.field("PatientsName").value + "   with MRO: " + ctx.field("PatientID").value + " DOB: " + ctx.field("PatientsBirthDate").value + " " + ctx.field("PatientsSex").value;
    00148    wind.setTitle(title);
    00149    wind.showMaximized();
    00150
    00151 ## save the image to a file
    00152 def saveToFile():
    00153   imageName = saveImageToFile(ctx.parent().field("PatientID").value, getFileName(),"");
    00154   ctx.field("ImgSave.filename").value = imageName;
    00155   ctx.field("ImgSave.save").touch();
    00156   MLAB.log("The image: " + imageName + " has been saved in file system.");
    00157   return imageName;
    00158
    00159 ## get the formatted FileName: MRO(time)
    00160 def getFileName():
    00161   filename = ctx.parent().field("PatientID").value + "(" + time.strftime("%Y%m%d-%H%M%S") + ")";
    00162   return filename;
    00163
    00164 ## save the image to DB
    00165 def saveToDB(dir, descr, modality):
    00166   query = "SELECT PatientID FROM Patients WHERE MRO = " + "'" + ctx.parent().field("PatientID").value + "'";
    00167   result = sendQuery(query);
    00168   if (result == "()"):
    00169     return;
    00170   patientID = 0;
    00171   for item in result:
    00172     patientID = item["patientid"];
    00173   dir = dir.replace("\\", "/");
    00174   query = "insert into images(imagetype, seriesDescr, modality, imageName,patientID) values(" + "1,'" + descr + "'," +  "'" + modality + "'," + "'" + dir + "'," + str(patientID) + ");";
    00175   result = sendQuery(query);
    00176   if (result == -1):
    00177     return;
    00178   MLAB.log("The image: " + dir +  " has been saved to the DB.");
    00179
    00180 ## the callback function executed when user clicks approval and finish on SaveImage dialog
    00181 def clickOkOnSaveImgW():
    00182   saveDir = saveToFile();
    00183   descr = ctx.field("DicomTagModify.tagValue4").value;
    00184   modality = ctx.field("DicomTagModify.tagValue5").value;
    00185   saveToDB(saveDir, descr, modality);
    00186   ctx.disconnectField("SetPageExt.input0");
    00187   ctx.window().close();
    00188
    00189 ## the Callback function executed when user clicks Cancel on SaveImage dialog
    00190 ## the SetPageExt input will be disconnect from the ImageLoad and window is closed.
    00191 def clickCancelOnSaveImgW():
    00192   ctx.disconnectField("SetPageExt.input0");
    00193   ctx.window().close();
    00194
    00195 ## show help
    00196 def help():
    00197   ctx.showHtmlHelp();
    00198
    00199 ##  function holder to clean up DSA2D
    00200 def cleanUp():
    00201   ctx.field("OpenImage2.previewLoad.close").touch();
    00202   ctx.field("OpenImage2.imgLoad.close").touch();
    00203 #//# MeVis signature v1
    00204 #//# key: MFowDQYJKoZIhvcNAQEBBQADSQAwRgJBANEfsmYse2e1dRhkQ9AQbreCq9uxwzWLoGom13MNYmyfwoJqQOEXljLFAgw2eEjaT12G4CdqKWhRxh9ANP6n7GMCARE=:VI/mB8bT4u+mRtf/ru8yUQi8BzpaS3UeL2x62YxsUYnVqCWuLrVNLiukIIjnJMKQXlc8ezmgOIcVAV7pgvgKpQ==
    00205 #//# owner: MeVis
    00206 #//# date: 2009-06-10T04:12:25
    00207 #//# hash: vHSfj+Qt8xrIkDVP4W0Rep0YH6a3+MySa0+Cs+AoqEK/IbB+f5szwmyVTENOjzz/JEgm2t31FtHmUUMFcQg5VQ==
    00208 #//# MeVis end
    00209

All Classes Namespaces Files Functions Variables Enumerator Friends