EGS Brachy
An egs++ user code for rapid brachytherapy calculations
Loading...
Searching...
No Matches
test.py
Go to the documentation of this file.
1"""
2
3A test of the egs_brachy Monte Carlo volume correction routines. The volume of
4phantom voxels overlapped by sources and other phantoms are calculated by
5egs_brachy and compared with analytical values.
6
7"""
8
9import csv
10import math
11import re
12
13EGSINP = "vc.egsinp"
14TIME_LIMIT_S_PER_MHZ = 1 / 2993.# s / MHz
15
16expected_volumes = {
17 "source_volume": 4/3.*math.pi*0.1**3,
18 "bounding_shape_volume": 4/3.*math.pi*0.11**3,
19 "extra_bounding_shape_vol": 8**3,
20 "box_reg_0": 10**3 - 6**3,
21 "phantom_reg_0": 6*6*3 - 0.5*(4./3*math.pi*1**3),
22 "phantom_reg_1": 6*6*3 - 0.5*(4./3*math.pi*1**3),
23 "sph_phantom_reg_0": 4/3.*math.pi*(0.3**3 - 0.1**3),
24 "sph_phantom_reg_1": 4./3*math.pi*(1**3 - 0.3**3)
25}
26
27
28def approx_equal( a, b, eps=0.001 ):
29 same_sign = a*b >= 0
30 return same_sign and abs(math.log( a ) - math.log(b)) <= eps
31
32
33def read_vols(phant, inp_name):
34 """Read voxel values for the phanton named 'phant' and
35 return the region volumes for that phantom"""
36
37 path = "%s.%s.voxels" % (inp_name, phant)
38 f = open(path, "r")
39 dialect = csv.Sniffer().sniff(f.read(1024))
40 f.seek(0)
41 reader = csv.reader(f, dialect)
42 header = next(reader)
43 reg_vols = []
44 for row in reader:
45 reg_vols.append((float(row[1]), float(row[2])))
46
47 return reg_vols
48
49
50def compare_results(egslst, inp_name):
51
52 box_vols = read_vols("box", inp_name)
53 phant_vols = read_vols("phantom", inp_name)
54 sph_phant_vols = read_vols("sph_phantom", inp_name)
55
56 vsource = float(re.findall("Volume of Source\s*=\s*(.*)\s*cm", egslst, re.IGNORECASE)[0])
57 bounding_vols = list(map(float, re.findall("Bounding shape volume\s*=\s*(.*)\s*cm", egslst, re.IGNORECASE)))
58
59 actual = {
60 "source_volume": vsource,
61 "bounding_shape_volume": bounding_vols[0],
62 "extra_bounding_shape_vol": bounding_vols[1],
63 "box_reg_0": box_vols[0][0],
64 "phantom_reg_0": phant_vols[0][0],
65 "phantom_reg_1": phant_vols[1][0],
66 "sph_phantom_reg_0": sph_phant_vols[0][0],
67 "sph_phantom_reg_1": sph_phant_vols[1][0],
68 }
69
70 all_close = True
71 for k,v in list(expected_volumes.items()):
72 if not approx_equal(v, actual[k]):
73 print(("Vol comparison failed for %s: Actual=%E Expected=%E" % (k, actual[k], v)))
74 all_close = False
75
76 return all_close, actual, expected_volumes
approx_equal(a, b, eps=0.001)
Definition test.py:28
read_vols(phant, inp_name)
Definition test.py:33