Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.70 KB | None | 0 0
  1. --Define input folder
  2. local folder = [[D:\data\MPhys2019\PETCT-HN\SEM2\Nonrigid_inversewarp\]];  --Nonrigid registration scans are the input
  3. local output_folder = [[D:\data\MPhys2019\PETCT-HN\SEM2\Sensitivity\maximum_entity\]]; --Output folder
  4.  
  5. --local file = io.open(output_folder.."print.txt","w")
  6. local function scandir(directory)
  7.   local i, t, popen = 0, {}, io.popen
  8.   for filename in popen('dir "'..directory..'" /o:n /b'):lines() do
  9.       i = i + 1
  10.       t[i] = filename
  11.   end
  12.   return t
  13. end
  14. local function has_item(tab, val) --Check is val in tab
  15.   for index, value in ipairs(tab) do
  16.     if value == val then
  17.       return true
  18.     end
  19.   end  
  20.   return false
  21. end
  22.  
  23. local t = scandir(folder);
  24. --Comment following line to cycle through packs - paste desired pack name otherwise:
  25. --t = {'263538649.pack'}
  26.  
  27. local skip_these =  {[[empty]]}
  28. fraction=1
  29. for k,patientpack in ipairs(t) do
  30.   if string.find(patientpack, ".pack") then
  31.     patientpack_new= patientpack:gsub(".pack","")  
  32.    
  33.     skip_scan= false
  34.     if has_item(skip_these, patientpack) then skip_scan= true end
  35.     if skip_scan == false then
  36.    
  37.       loadpack( folder .. patientpack )
  38.      
  39.        
  40.         skip= false
  41.         if wm.Delineation.CTV1 == nil then
  42.           skip = true
  43.           --CTV_missing:write(patientpack_new.."\n")
  44.         end
  45.         if skip == false then
  46.          
  47.         mask= wm.Scan[1]:burn(wm.Delineation.CTV1)
  48.         wm.Scan[9]= Scan:new()
  49.         wm.Scan[9]= wm.Scan[1]:mask(mask)
  50.        
  51.         name= ("%s_scan%d_CTV_%s"):format(patientpack_new,1,"mask")
  52.         mask:write_nifty(output_folder .. name ..".nii")
  53.        
  54.         name= ("%s_scan%d_CTV_%s"):format(patientpack_new,1,"original")
  55.         wm.Scan[9]:write_nifty(output_folder .. name ..".nii")
  56.        
  57.        
  58.        
  59.         --wm.Scan[9]=mask
  60.         cb = Clipbox:new()
  61.         cb:fit(wm.Scan[9])
  62.         local minx = cb.Coordinates:getvalue(0)
  63.         local maxx = cb.Coordinates:getvalue(1)
  64.         local miny = cb.Coordinates:getvalue(2)
  65.         local maxy = cb.Coordinates:getvalue(3)
  66.         local minz = cb.Coordinates:getvalue(4)
  67.         local maxz = cb.Coordinates:getvalue(5)
  68.        
  69.        -- file.write(minx)
  70.         --io.close(file)
  71.        
  72.         local deltax= 0.1*(maxx-minx) --Want to translate CTV by 10% of the diameter in x,y,z directions respectively
  73.         local deltay= 0.1*(maxy-miny)
  74.         local deltaz= 0.1*(maxz-minz)
  75.         a=1
  76.        
  77.         --- First do x ---
  78.         wm.Scan[9]= Scan:new()
  79.         wm.Scan[9]= wm.Scan[1]
  80.         --wm.Scan[8].Adjust:setvalue(3,0,-a)
  81.         wm.Scan[9].Adjust:setvalue(3,0,-fraction*deltax)
  82.         wm.Scan[9]=wm.Scan[9]:as(wm.Scan[1])
  83.         wm.Scan[9]:mask(mask)
  84.        
  85.         name= ("%s_scan%d_CTV_%s"):format(patientpack_new,1,"x_minus")
  86.         wm.Scan[9]:write_nifty(output_folder .. name ..".nii")
  87.        
  88.         wm.Scan[9]= Scan:new()
  89.         wm.Scan[9]= wm.Scan[1]
  90.         wm.Scan[9].Adjust:setvalue(3,0,fraction*deltax)
  91.         wm.Scan[9]= wm.Scan[9]:as(wm.Scan[1])
  92.         wm.Scan[9]:mask(mask)
  93.        
  94.         name= ("%s_scan%d_CTV_%s"):format(patientpack_new,1,"x_plus")
  95.         wm.Scan[9]:write_nifty(output_folder .. name ..".nii")
  96.        
  97.         --- Now do y ---
  98.         wm.Scan[9]= Scan:new()
  99.         wm.Scan[9]= wm.Scan[1]
  100.         wm.Scan[9].Adjust:setvalue(3,1,-fraction*deltay)
  101.         wm.Scan[9]= wm.Scan[9]:as(wm.Scan[1])
  102.         wm.Scan[9]:mask(mask)
  103.        
  104.         name= ("%s_scan%d_CTV_%s"):format(patientpack_new,1,"y_minus")
  105.         wm.Scan[9]:write_nifty(output_folder .. name ..".nii")
  106.        
  107.         wm.Scan[9]= Scan:new()
  108.         wm.Scan[9]= wm.Scan[1]
  109.         wm.Scan[9].Adjust:setvalue(3,1,fraction*deltay)
  110.         wm.Scan[9]= wm.Scan[9]:as(wm.Scan[1])
  111.         wm.Scan[9]:mask(mask)
  112.        
  113.         name= ("%s_scan%d_CTV_%s"):format(patientpack_new,1,"y_plus")
  114.         wm.Scan[9]:write_nifty(output_folder .. name ..".nii")
  115.        
  116.         -- Now do z ---
  117.         wm.Scan[9]= Scan:new()
  118.         wm.Scan[9]= wm.Scan[1]
  119.         wm.Scan[9].Adjust:setvalue(3,2,-fraction*deltaz)
  120.         wm.Scan[9]= wm.Scan[9]:as(wm.Scan[1])
  121.         wm.Scan[9]:mask(mask)
  122.        
  123.         name= ("%s_scan%d_CTV_%s"):format(patientpack_new,1,"z_minus")
  124.         wm.Scan[9]:write_nifty(output_folder .. name ..".nii")
  125.        
  126.         wm.Scan[9]= Scan:new()
  127.         wm.Scan[9]= wm.Scan[1]
  128.         wm.Scan[9].Adjust:setvalue(3,2,fraction*deltaz)
  129.         wm.Scan[9]= wm.Scan[9]:as(wm.Scan[1])
  130.         wm.Scan[9]:mask(mask)
  131.        
  132.         name= ("%s_scan%d_CTV_%s"):format(patientpack_new,1,"z_plus")
  133.         wm.Scan[9]:write_nifty(output_folder .. name ..".nii")
  134.            
  135.        end  
  136.       end
  137.   end
  138. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement