In [26]:
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage as ndi
from scipy import interpolate as interpl
import csv
import os

class PinDoseMap:
    """A class to take in a directory of calculated dose maps.
    Assume that maps are at 100cm SPD, and 5cm deep as per local
    dose calc conventions for IMRT QA"""
        # TODO open file once and pass around file handle
    def __init__(self, my_file):
        assert os.path.exists(my_file), "check path you gave is correct and exists: %s" % my_file
        
        self.map_file = my_file
        self.hdr = None
        self.x_pos = None
        self.y_pos = None
        self.raw_dose_array = None
        # TODO fix attribute name to intrp ...
        self.itrp_dose_array = None
        self.get_hdr(self.map_file)
        self.get_dose_map(self.map_file)
        
    def get_hdr(self, map_file):
        p = dict()
        
        with open(map_file, "r") as fh:
            
            for line_elems in (x.split(':,') for x in fh.read().split('\n')):
                if line_elems[0] == '':  # empty line marks end of header
                    break
                else:
                    p[line_elems[0]] = line_elems[1]
                    
        self.hdr = p
    
    def get_dose_map(self, map_file):
        rawdat = np.genfromtxt(map_file, delimiter=',', skiprows=11,
                               missing_values='')
        self.y_pos = rawdat[1: ,0] #get everything after first position ([1: )in 1st col ( ,0])   
        self.x_pos = rawdat[0, 1:-1] #in first row ([0, ), get all except first and last ( ,1:-1])
        self.raw_dose_array = rawdat[1:, 1:-1]  # here is the dose array
        my_temp = interpl.RectBivariateSpline(self.y_pos, self.x_pos, self.raw_dose_array)
        self.itrp_dose_array = my_temp(self.y_pos, self.x_pos)
        
    def do_plot(self):
        # TODO check array exists before plotting
        fig = plt.figure(figsize=(6, 3.2))
        ax1 = fig.add_subplot(111)
        ax1.set_title('TestPinFlu')
        plt.imshow(self.itrp_dose_array)

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

In [30]:
%matplotlib inline
my_dose_map.do_plot()



In [32]:
np.amin(my_dose_map.x_pos)


Out[32]:
-8.4000000000000004

In [33]:
pwd


Out[33]:
'/home/mpre/Documents'

In [ ]: