1

I have made a script to get patient data from a database.

It works for the first few hundreds of patients, but at some random point i get the error:

*** lua run error BatchDicomMove.lua:91: attempt to compare nil with string in 'dofile('BatchDicomMove.lua')'

Even despite I explicitly checking whether the argument is nil or not. Furthermore when I make changes in the code (like adding an empty line). The error just occur in another place where a. is accessed.

What could be wrong?

-- execute this script by calling: dgate "--lua:dofile('BatchDicomMove.lua')"

-- MOP: Query=STUDY|SERIES Move=STUDY+StudyInstanceUID
-- GEPACS_RSD: Query=* Move=SERIES

inputFile       = 'PatientList.txt'
srcAet          = 'NUKALFA'
destAet         = 'ONCO_PACS_RD'
modalities      = {'CT','PT'}
includeStr      = {'Standard','standard','AC'}  -- if non are desired enter {}
includeOrAnd    = 'or'                          -- choose 'and', 'or'
excludeStr      = {'NAC','CTAC'}                -- if non are desired enter {}
excludeOrAnd    = 'nor'                         -- choose 'neither', 'nor'
select          = 'newest'                      -- choose 'newest', 'oldest', 'all'
relativeToTime  = 'before'                      -- choose 'before', 'after', 'exact'




for line in io.lines(inputFile) do
    -- Loop through all patients

    date = ''
    date1 = ''
    date2 = ''
    stringSplit = string.find(line,' ')
    if stringSplit == nil then
        ptId = line
    else
        ptId = string.sub(line,0,stringSplit-1)
        date = string.sub(line,stringSplit+1)
        if string.len(date) == 18 then
            stringSplit = string.find(date,' ')
            if stringSplit == nil then
                date = ''
            else
                date = ''
                date1 = string.sub(date,0,stringSplit-1)
                date2 = string.sub(line,stringSplit+1)
            end
        elseif string.len(date) ~= 9 then
            date = ''
        end
    end
    -- ptIdAlts ensures that the patient will be found nomater if '-' is included in the ID or not
    stringSplit = string.find(ptId,'-')
    if stringSplit == nil then
        ptId = string.sub(ptId,0,7) .. '-' .. string.sub(ptId,8)
    end
    stringSplit = string.find(ptId,'-')
    ptIdAlts = {ptId,string.sub(ptId,0,stringSplit-1) .. string.sub(ptId,stringSplit+1)}

    print('  Query: patient', ptIdAlts[1], 'from', srcAet, 'to', destAet)

    -- an extra loop have been added as part of implementation of ptIdAlts
    b = {}
    for key, ptId in ipairs(ptIdAlts) do
        for key, modality in ipairs(modalities) do

            -- create query for dicom move
            q = newdicomobject()
            q.PatientID = ptId
            q.Modality = modality
            -- values to retrive should be included in the query
            q.StudyInstanceUID  = ''
            q.SeriesInstanceUID = ''
            q.PatientName = ''
            q.SeriesDescription = ''
            q.StudyDate = ''

            -- execute query for infomation on patient data on the source machine
            a = dicomquery(srcAet, 'SERIES', q) -- sets QueryRetrieveLevel at call time
            for i=1,#a do 
                b[#b+1] = a[i-1] 
            end
        end
    end

    -- Remove unwanted resultes

    c = {}
    if date ~= '' then
        for i, a in ipairs(b) do
            if a.StudyDate ~= nil then
                if relativeToTime == 'exact' then
                    if a.StudyDate == date then
                        c[#c+1] = a
                    end
                elseif relativeToTime == 'before' then
                    if a.StudyDate <= date then
                        c[#c+1] = a
                    end
                else
                    if a.StudyDate >= date then
                        c[#c+1] = a
                    end
                end
            end
        end
    elseif (date1 ~= '') and (date2 ~= '') then
        for i, a in ipairs(b) do
            if (a.StudyDate ~= nil) and (a.StudyDate >= date1) and (a.StudyDate <= date2) then
                c[#c+1] = a
            end
        end
    else
        c = b
    end
    b = c

    c = {}
    if #includeStr ~= 0 then
        for key, a in ipairs(b) do
            if a.SeriesDescription ~= nil then
                if includeOrAnd == 'or' then
                    for key, include in ipairs(includeStr) do
                        if string.match(a.SeriesDescription,include) ~= nil then
                            c[#c+1] = a
                            break
                        end
                    end
                else
                    for key, include in ipairs(includeStr) do
                        if string.match(a.SeriesDescription,include) == nil then
                            break
                        end
                        c[#c+1] = a
                    end
                end
            end
        end
        b = c
    end

    c = {}
    if #excludeStr ~= 0 then
        for key, a in ipairs(b) do
            if a.SeriesDescription ~= nil then
                if excludeOrAnd == 'nor' then
                    for key, exclude in ipairs(excludeStr) do
                        if string.match(a.SeriesDescription,exclude) ~= nil then
                            break
                        end
                        c[#c+1] = a
                    end
                else
                    for key, exclude in ipairs(excludeStr) do
                        if string.match(a.SeriesDescription,exclude) == nil then
                            c[#c+1] = a
                            break
                        end
                    end
                end
            end
        end
        b = c
    end

    c = {}
    if select == 'newest' then
        for key, modality in ipairs(modalities) do
            for i, a in ipairs(b) do
                if a.StudyDate ~= nil then
                    if c[key] == nil then
                        c[key] = a
                    elseif c[key].StudyDate < a.StudyDate then
                        c[key] = a
                    end
                end
            end
        end
    elseif select == 'oldest' then
        for key, modality in ipairs(modalities) do
            for i, a in ipairs(b) do
                if a.StudyDate ~= nil then
                    if c[key] == nil then
                        c[key] = a
                    elseif c[key].StudyDate > a.StudyDate then
                        c[key] = a
                    end
                end
            end
        end
    else
        c = b
    end
    b = c

    n = #b
    print('  Query results:', n)

    for key, a in ipairs(b) do
        cmd = newdicomobject()
        cmd.PatientID = b.PatientID
        cmd.StudyInstanceUID = b.StudyInstanceUID
        cmd.SeriesInstanceUID = b.SeriesInstanceUID

        if srcAet == 'MOP_SCP' then
            cmd.QueryRetrieveLevel = 'STUDY' -- only level supported by the MOP for dicom move
            cmd.Modality = b.Modality
        else
            cmd.QueryRetrieveLevel = 'SERIES' -- tested on GEPACS_RSD
        end

        -- execute the move
        --dicommove(srcAet, destAet, cmd);
    end
end
print('  Done.')
1
  • 1
    Which is line 91 in that snippet exactly? What line of input is it failing on? That error comes from relational operators other than == and ~= so check that you have those guarded. Commented Sep 30, 2015 at 12:23

1 Answer 1

4

There are comparisons in this file on line 90 and 94:

line 90: if a.StudyDate <= date then
line 94: if a.StudyDate >= date then

We can see that a.StudyDate is not nil because you checked for it at line 84. Therefore, my guess is that date could be nil.

Another place is on line 102:

if (a.StudyDate ~= nil) and (a.StudyDate >= date1) and (a.StudyDate <= date2)

Again, check that date1 and date2 are not nil.

In the same vein, you should probably check 'c[key].StudyDate' line 178.

Sign up to request clarification or add additional context in comments.

2 Comments

I posted an answer too, but cocked up the line numbering so thought it best to delete. However, the important point I noticed is that the code checks only for date ~= '' rather than date ~= nil.
Thanks for the advise, but date, date1, and date2 are never reported to be nil. It is always the a.<somthing> part that becomes nil. I can print the value onto the screen right before an error occurs an the value is not nil.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.