4343
4444import codecs
4545from collections import OrderedDict
46- from configparser import ConfigParser
46+
4747import logging
4848import numpy as np
4949import os
5050import scipy .ndimage
5151from math import floor , ceil
5252
53+ try :
54+ from configparser import ConfigParser
55+ except ImportError as e :
56+ #a compatibility hack for python2 if it does not have conifgparser
57+ import sys
58+ if sys .version_info [0 ] == 2 :
59+ from ConfigParser import ConfigParser
60+ else :
61+ raise e
62+
5363#define which array index corresponds to which color
5464rgbMap = {"red" :0 ,"green" :1 ,"blue" :2 }
5565
@@ -276,9 +286,9 @@ def rectangle_mask(self,x0, y0, width, height, angle=0.0):
276286 y0 : scalar
277287 y-value of rectangle center (in cm)
278288 width : scalar
279- width (x-dimension) of the rectangle
289+ half- width (x-dimension) of the rectangle
280290 height : scalar
281- height (y-dimension) of the rectangle
291+ half- height (y-dimension) of the rectangle
282292 angle : scalar, optional
283293 counter clockwise rotation angle of the rectangle
284294 test : bool, optional
@@ -287,7 +297,7 @@ def rectangle_mask(self,x0, y0, width, height, angle=0.0):
287297 Returns
288298 -------
289299 mask : ndarray
290- a mask to index the array an return a rectangular area
300+ a mask to index the array and return a rectangular area
291301 """
292302
293303 angle_rad = angle * np .pi / 180.
@@ -298,7 +308,8 @@ def rectangle_mask(self,x0, y0, width, height, angle=0.0):
298308 #must be shifted by 0.5 pixels width.
299309 y , x = np .mgrid [- y0 + 0.5 / self .DPC :(self .shape [0 ]+ .5 )/ self .DPC - y0 :1.0 / self .DPC ,
300310 - x0 + 0.5 / self .DPC :(self .shape [1 ]+ .5 )/ self .DPC - x0 :1.0 / self .DPC ]
301-
311+
312+
302313 #condition for a rectangle
303314 mask = np .logical_and (np .abs (x * np .cos (angle_rad )- y * np .sin (angle_rad )) <= width ,
304315 np .abs (x * np .sin (angle_rad )+ y * np .cos (angle_rad )) <= height )
@@ -318,9 +329,9 @@ def rectangle_stats(self,x0, y0, width, height, angle=0.0, test=False):
318329 y0 : scalar
319330 y-value of rectangle center (in cm)
320331 width : scalar
321- width (x-dimension) of the rectangle
332+ half- width (x-dimension) of the rectangle
322333 height : scalar
323- height (y-dimension) of the rectangle
334+ half- height (y-dimension) of the rectangle
324335 angle : scalar, optional
325336 counter clockwise rotation angle of the rectangle
326337 test : bool, optional
@@ -459,7 +470,7 @@ def profile(self, x0, y0, x1, y1, interpolation="nearest",test=False):
459470# for testing
460471if __name__ == '__main__' :
461472 #load some calibrations
462- calibs = load_calibrations (os .path .join (os .path .curdir , "Calibration " ))
473+ calibs = load_calibrations (os .path .join (os .path .abspath ( os . path . curdir ), "calibrations " ))
463474 #create a simple scan (~4x4 cm)
464475 scan = np .zeros ((470 ,470 ,3 ),dtype = "uint8" )
465476
@@ -471,6 +482,8 @@ def profile(self, x0, y0, x1, y1, interpolation="nearest",test=False):
471482
472483 doseDistribution = DoseArray (300. ,calibs ["example" ],scan ,255 )
473484
485+ doseDistribution .rectangle_stats (1.0 ,1.0 ,0.5 ,0.5 ,0. ,True )
486+
474487 mask = doseDistribution .rectangle_mask (0.4 ,0.5 ,0.2 ,0.1 ,0.0 )
475488 print (doseDistribution [mask ].shape )
476489
0 commit comments