In [1]:
from scipy import interpolate as interpl
import numpy as np
import os
from pylinac import log_analyzer as lga

class Dyn_to_Dose:
    def __init__(self,my_dir):
        """class to import a folder full of dynalog files for a treatment"""
        self.my_logs = lga.MachineLogs();
        self.flip_fluences = list()
        self.interp_fluences = list()
        
        #TODO more rigorous checking of my_dir
        if not os.path.exists(my_dir):
            self.my_logs.load_dir_UI()
        else:
            self.my_logs.load_dir(my_dir)
            
    def do_calcs(self, res_mm=1.0):
        if len(self.my_logs) == 0:
            print("No valid log files loaded ...")
            return
        for log in self.my_logs:
            log.fluence.actual.calc_map(resolution=res_mm)
            
    def do_flip(self):
        for log in self.my_logs:
            tmp = log.fluence.actual.calc_map(resolution=1.0)
            self.flip_fluences.append(np.flipud(tmp))
            
    def make_interp(self,y_dim, x_dim):
        """y_dim must be 1D array containing co-ords in cms
        for fluence and x_dim 1D array likewise. x is parallel to leaf motion
        """
        flu_y = [-19.5, -18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.75, 
         -9.25, -8.75, -8.25, -7.75, -7.25, -6.75, -6.25, -5.75, -5.25, -4.75, -4.25, 
         -3.75, -3.25, -2.75, -2.25, -1.75, -1.25, -0.75, -0.25, 0.25, 0.75, 1.25,
         1.75, 2.25, 2.75, 3.25, 3.75, 4.25, 4.75, 5.25, 5.75, 6.25, 6.75, 7.25, 7.75, 
         8.25, 8.75, 9.25, 9.75, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5, 16.5, 17.5, 18.5,
         19.5]
        flu_x = np.linspace(-19.95, 19.95, 400) #TODO get rid of hardcoding dimensions of 1mm res
        
        for flu in self.flip_fluences:
            
            #first create interpolating object tmp
            tmp = interpl.RectBivariateSpline(flu_y, flu_x, flu)
            
            # now create new interpolated fluences and store
            self.interp_fluences.append(tmp(y_dim, x_dim))

In [2]:
my_dyn2dose = Dyn_to_Dose('/home/mpre/tmp_dynalogs_la4/OBRIEN_Michael')


6 logs found. 
0 logs skipped. 
Log loaded:
1 of 6
2 of 6
3 of 6
4 of 6
5 of 6
6 of 6

In [3]:
my_dyn2dose.do_calcs(1.0)

In [4]:
my_dyn2dose.do_flip()

In [5]:
pin_x = np.linspace(-12.7, 12.7, 128)
pin_y = np.linspace(-9.1, 9.1, 92)
my_dyn2dose.make_interp(pin_y, pin_x)

In [55]:
import matplotlib.pyplot as plt
%matplotlib inline
fig = plt.figure(figsize=(6, 3.2))

ax1 = fig.add_subplot(111)
plt.imshow(my_dyn2dose.interp_fluences[2])


Out[55]:
<matplotlib.image.AxesImage at 0x7f08cd2d2048>

In [56]:
import PinDoseMap

In [57]:
my_dose_map = PinDoseMap.PinDoseMap('/home/mpre/Documents/10')

In [58]:
my_dyn2dose.make_interp(my_dose_map.y_pos, my_dose_map.x_pos)

In [59]:
my_dose_map.do_plot()



In [60]:
%matplotlib inline
fig = plt.figure(figsize=(6, 3.2))

ax1 = fig.add_subplot(111)
plt.imshow(my_dyn2dose.interp_fluences[2])


Out[60]:
<matplotlib.image.AxesImage at 0x7f08cd224f60>

In [61]:
my_flu_sample = my_dyn2dose.interp_fluences[2][25,31]

In [62]:
my_flu_sample


Out[62]:
0.0007352582390363772

In [63]:
my_dose_sample = my_dose_map.itrp_dose_array[25,31]
my_dose_sample


Out[63]:
0.81899999999999973

In [64]:
my_norm = 0.81899999999999973 / 0.66608057743943438

In [66]:
my_norm


Out[66]:
1.2295809662374821

In [95]:
new_flu = my_norm * my_dyn2dose.interp_fluences[2]

In [96]:
my_dyn2dose.my_logs[2]


Out[96]:
<pylinac.log_analyzer.MachineLog at 0x7f08cd8466a0>

In [97]:
def print_attributes(obj):
    for attr in obj.__dict__:
        print (attr, getattr(obj, attr))
        
print_attributes(my_dyn2dose.my_logs[2])


_filename /home/mpre/tmp_dynalogs_la4/OBRIEN_Michael/B20150331133847_377479.dlg
fluence <pylinac.log_analyzer.Fluence_Struct object at 0x7f08cd8599b0>
header <pylinac.log_analyzer.Dlog_Header object at 0x7f08cd856748>
_cursor 0
axis_data <pylinac.log_analyzer.Dlog_Axis_Data object at 0x7f08cd856860>

In [98]:
print_attributes(my_dyn2dose.my_logs[2].axis_data.jaws)


x1 <pylinac.log_analyzer.Head_Axis object at 0x7f08cd856a90>
y1 <pylinac.log_analyzer.Head_Axis object at 0x7f08cd856a20>
y2 <pylinac.log_analyzer.Head_Axis object at 0x7f08cd856a58>
x2 <pylinac.log_analyzer.Head_Axis object at 0x7f08cd856ac8>

In [99]:
print( my_dyn2dose.my_logs[2].axis_data.jaws.y2.actual)


[ 3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.
  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.
  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.
  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.
  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.
  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.
  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.
  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.
  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.
  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.
  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.
  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.  3.
  3.  3.]

In [100]:
print_attributes(my_dyn2dose.my_logs[2].fluence.actual._jaws.x2)


actual [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
  1.  1.]

In [101]:
print_attributes(my_dyn2dose.my_logs[2].fluence.actual._jaws.x1)


actual [ 7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.
  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.
  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.
  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.
  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.
  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.
  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.
  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.
  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.
  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.
  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.
  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.  7.
  7.  7.]

In [101]:


In [102]:
print_attributes(my_dyn2dose.my_logs[2].fluence.actual._jaws.y1)


actual [ 4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.
  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.
  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.
  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.
  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.
  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.
  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.
  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.
  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.
  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.
  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.
  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.
  4.  4.]

In [103]:
rows = np.linspace(-2.5, 3.5, 61)

In [104]:
rows


Out[104]:
array([-2.5, -2.4, -2.3, -2.2, -2.1, -2. , -1.9, -1.8, -1.7, -1.6, -1.5,
       -1.4, -1.3, -1.2, -1.1, -1. , -0.9, -0.8, -0.7, -0.6, -0.5, -0.4,
       -0.3, -0.2, -0.1,  0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,
        0.8,  0.9,  1. ,  1.1,  1.2,  1.3,  1.4,  1.5,  1.6,  1.7,  1.8,
        1.9,  2. ,  2.1,  2.2,  2.3,  2.4,  2.5,  2.6,  2.7,  2.8,  2.9,
        3. ,  3.1,  3.2,  3.3,  3.4,  3.5])

In [105]:
cols = np.linspace(-7., 1., 81)

In [106]:
cols


Out[106]:
array([-7. , -6.9, -6.8, -6.7, -6.6, -6.5, -6.4, -6.3, -6.2, -6.1, -6. ,
       -5.9, -5.8, -5.7, -5.6, -5.5, -5.4, -5.3, -5.2, -5.1, -5. , -4.9,
       -4.8, -4.7, -4.6, -4.5, -4.4, -4.3, -4.2, -4.1, -4. , -3.9, -3.8,
       -3.7, -3.6, -3.5, -3.4, -3.3, -3.2, -3.1, -3. , -2.9, -2.8, -2.7,
       -2.6, -2.5, -2.4, -2.3, -2.2, -2.1, -2. , -1.9, -1.8, -1.7, -1.6,
       -1.5, -1.4, -1.3, -1.2, -1.1, -1. , -0.9, -0.8, -0.7, -0.6, -0.5,
       -0.4, -0.3, -0.2, -0.1,  0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,
        0.7,  0.8,  0.9,  1. ])

In [111]:
my_dyn2dose.make_interp(cols, rows)

In [114]:
plt.imshow(my_dyn2dose.interp_fluences[2])


Out[114]:
<matplotlib.image.AxesImage at 0x7f08ccda2128>

In [115]:
my_tmp = interpl.RectBivariateSpline(my_dose_map.y_pos, my_dose_map.x_pos, my_dose_map.raw_dose_array)

In [116]:
my_pin_zoom = my_tmp(rows, cols)

In [117]:
plt.imshow(my_pin_zoom)


Out[117]:
<matplotlib.image.AxesImage at 0x7f08ccd002b0>

In [118]:
from scipy import ndimage as ndi

In [119]:
blur3 = ndi.filters.gaussian_filter(my_dyn2dose.interp_fluences[2], 3.0, mode='reflect')

plt.imshow(blur3)


Out[119]:
<matplotlib.image.AxesImage at 0x7f08ccc6e0f0>

In [120]:
my_diff1 = blur3 - my_pin_zoom


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-120-068b324dc2e2> in <module>()
----> 1 my_diff1 = blur3 - my_pin_zoom

ValueError: operands could not be broadcast together with shapes (92,128) (61,81) 

In [121]:
plt.imshow(my_diff1)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-121-7be2c5581e30> in <module>()
----> 1 plt.imshow(my_diff1)

NameError: name 'my_diff1' is not defined

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: