Delete studies based on age

  • Try this, using y[1], nil and ModalitiesInStudy = ''


  • Marcel,


    I'm going a different route with this. What I am doing now is gathering up all of the SUID's and writing them to a .txt file. Could you please look at my code below and tell me why it won't work? I am trying to find the following:


    all studies with a date range of '19000101-20070714'
    all studies with a Date of Birth of '19000101-19920714'
    all studies that are NOT MG


    Code
    a=DicomObject:new() a.QueryRetrieveLevel='STUDY' a.StudyInstanceUID='' --a.PatientName='*test*' --a.AccessionNumber='6419216' a.StudyDate='19000101-20070714' a.PatientBirthDate = '19000101-19920714' a.ModalitiesInStudy = '' b=dicomquery('RADARCH4', 'STUDY', a) f=io.open('suid.txt', 'wt') for i=0, #b-1 do if string.find(y[i].ModalitiesInStudy, 'MG')==nil then f:write(b[i].StudyInstanceUID .. '\n') end


    The error I'm getting is: '*** lua run error delete_UID.lua:100: attempt to index global 'y' (a nil value) in 'dofile('delete_UID.lua')''


    I can make the script above run if I take out the 'if string' and pick a modality, but I don't want to have to run this for each of our modalites once a month when it is time to delete old exams.


    this code works fine, but I want it to find all studies that are NOT MG


    thanks!

  • that worked great, it did exactly what I was expecting.


    Now, if there was only a way to tell the script 'this many days old' from 'today' for the study date instead of giving it a date range? Also, could the same be done for the date of birth?


    Thank you

  • Hi,


    The query is a standard date query, it does not handle 'so many days from today' ;->>> You will need to find a lua calender function to calculate the date from now and the given days in the right format. The good news is that date range queries are allowed for the patient birthdate as well I believe.


    Marcel

  • Marcel,


    thanks for the example! Forgive me for asking this and really do appreciate your help on this. I have tried to insert that into my code, but it fails. I've also just tried to make it into its own .lua script, but no luck there either. I guess I should insert this at the beginning?


  • Marcel,


    thanks for your help on this. You have gone out of your way! The script ran successfully on my end, but came back with over 74,000 rows! Way more than I would expect. I have a question about this line:


    start = os.date('%Y%m%d', os.time()-days*24*3600)


    I know that we are stating that 'days = 100' but is the above multiplying it by 24 & then by 3600? Is it selecting studies that are 100 days and older and not newer?


    Jamie

  • Hi,


    it just shows how you can calculate with dates. It take the clock and add or subtract as many seconds as go in a day, therefore 24*3600. The end ersilt is converted to a date in DICOM format.


    Marcel

  • Marcel,


    thanks again - I'm trying to get MORE output written in my .txt file I am creating. Can you help me? All it will write for me is the 'StudyInstanceUID'. I tried to get it to write 'AccessionNumber' as well but it won't display it correctly, the value is just placed at the end of the SUID with no spaces.


    Here is what I am currently running:


  • Hi,


    write does not write tabs or anything (have a look at the great book Programming with Lua ;->>>). Use, e.g,


    f:write(b[i].StudyInstanceUID, '\t', b[i].AccessionNumber, '\n')


    Do you already use ZeroBraneStudio? Is great for debugging.


    Marcel

  • Marcel,


    I've gotten the date range to work in ZeroBraneStudio, but when I use it in the script, it bombs with this error:


    *** lua run error test_date.lua:9: attempt to perform arithmetic on a string value in 'dofile('test_date.lua')'


    Here is my code so far:


    Code
    a=DicomObject:new() a.QueryRetrieveLevel='STUDY' a.StudyInstanceUID='' --a.PatientName='*test*' a.AccessionNumber='' days = 2192 startdate = os.date(19000101) enddate = os.date('%Y%m%d', os.time()-days*24*3600) a.StudyDate = 'startdate .. '-' .. enddate' days = 7670 startDOB = os.date(19000101) endDOB = os.date('%Y%m%d', os.time()-days*24*3600) a.PatientBirthDate = 'startDOB .. '-' .. endDOB' a.ModalitiesInStudy = '' b=dicomquery('RADARCH4', 'STUDY', a) f=io.open('suid.txt', 'wt') for i=0, #b-1 do if string.find(b[i].ModalitiesInStudy, 'MG')==nil then f:write(b[i].StudyInstanceUID, '\t', 'Date', '\t', b[i].StudyDate, '\t', 'MOD', '\t', b[i].ModalitiesInStudy .. '\n') end end f:close()


    also, this is what successfully runs in ZeroBrane Studio:


    Code
    days = 2192
    startdate = os.date(19000101)
    enddate = os.date('%Y%m%d', os.time()-days*24*3600)
    print (startdate .. '-' .. enddate)
    days = 7670
    startDOB = os.date(19000101)
    endDOB = os.date('%Y%m%d', os.time()-days*24*3600)
    print (startDOB .. '-' .. endDOB)
  • Marcel,


    I was hoping that I could run this code by you once more to make sure that it looked okay? Earlier I was writing SUIDs to a .txt file, so that I could double check it and then I was deleting based on that file. Now I have changed it around so that it will delete based on the query. Could you please give me your thoughts on this so that it doesn't also delete MG exams?


    The function of this script is to:


    1) query a StudyDate range from 19000101-six years ago from TODAY (to find all studies that are six years old and older)
    2) query a PatientBirthDate range from 19000101-21 years ago from TODAY (to find all studies with a DOB that is over 21 years of age)
    3) exclude Modality 'MG'
    4) delete everything the query found


  • Hi,


    It looks ok, but the last line should be:


    dicomdelete(b[i])


    But I would build a new DicomObject to control the dicomdelete such that you have control and can check that all fields are there, i.e. PatientID and StudyInstanceUID. dicomdelete() with a passed empty DicomObject will likely start deleting everything - use with care!


    Marcel

  • Marcel


    Thanks for looking at this. Based on your suggestion, would something like this work? Your talking about only double checking what it has deleted correct?


  • Well, Quite an intense discussion there about lua!


    I have a similar issue & would like to find out if there is a way other than via lua scripts to sort this out.


    I am using Conquest as a router and converter within a teleradiology workflow. It is not supposed to keep studies for long.


    I have issues with using disk watermarks because there are two other DICOM servers running on this cloud that also use disk space to delete older studies: 1. ClearCanvas server- I managed to get a way around CC. 2. Pacsone Server- would conflict with conquest because it is less configurable in this aspect.


    Cloud storage is limited and the final server from where people view images and reports is Pacsone. I need studies to stay here for as long as possible. Pacsone also uses free disk percentage to delete old studies


    I have set nightly threshold in Conquest to a figure well above total storage capacity so that every night all studies are deleted.


    Problem is that many times, there is a study actively being received by Conquest at 1AM. With poor internet connectivity, a study may take several minutes to even hours to be transmitted from site to Cloud and therefore through conquest.


    I did a test last night to simulate a situation where a study is being transferred at 1am when the deletion task runs: Images that were received prior to 1am are deleted. database record is deleted. images arriving after 1am are preserved but with no database entry for that study. It seems the study is also, therefore not forwarded to pacsone.


    Ideal situation: study to be deleted 3 days after it was last accessed. Sometimes a study that is 2 months old is pushed through for comparison. I would not like such a study to be immediately deleted. Alternative is 3 days after it was first received. Compromise is 3 days after study date.


    If the only way out is lua, I will have to take some time and try to figure out how lua works.


    Any suggestion will be greatly appreciated.


    Mukoya.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!