46 if name
not in dictionary:
51 x_values = [coords[1], coords[2]]
52 y_values = [coords[0], coords[0]]
54 x_values = [coords[2], coords[2]]
55 y_values = [coords[1], coords[0]]
56 return x_values, y_values
59 def dump_geo(filename, plot, output_folder, dump_steering, steering_file):
66 for entry
in data[
"Volumes"][
"entries"]:
67 index_to_names.append(entry[
"value"][
"NAME"])
70 if not index_to_names:
71 for entry
in data[
"Surfaces"][
"entries"]:
74 if "volume" +
str(vol)
not in index_to_names:
75 index_to_names.append(
"volume" +
str(vol))
96 index_to_extends_layers_bounds_cylinders = [[]
for _
in range(len(index_to_names))]
97 index_to_extends_layers_bounds_discs = [[]
for _
in range(len(index_to_names))]
99 index_to_extends_layers_cylinders = [[]
for _
in range(len(index_to_names))]
100 index_to_extends_layers_discs = [[]
for _
in range(len(index_to_names))]
102 for entry
in data[
"Surfaces"][
"entries"]:
105 vol = entry[
"volume"]
107 if "sensitive" in entry:
117 if entry[
"value"][
"transform"][
"translation"] !=
None:
118 z_shift = entry[
"value"][
"transform"][
"translation"][2]
121 if "approach" in entry:
122 approach_index = entry[
"approach"]
124 if entry[
"value"][
"type"] ==
"CylinderSurface":
127 entry[
"value"][
"bounds"][
"values"][0],
128 z_shift - entry[
"value"][
"bounds"][
"values"][1],
129 z_shift + entry[
"value"][
"bounds"][
"values"][1],
133 index_to_extends_layers_cylinders[vol - 1].append(extends)
134 elif entry[
"value"][
"type"] ==
"DiscSurface":
137 entry[
"value"][
"bounds"][
"values"][0],
138 entry[
"value"][
"bounds"][
"values"][1],
143 index_to_extends_layers_discs[vol - 1].append(extends)
146 "WARNING: Processing surface with unknown type. Only CylinderSurface and DiscSurface are considered."
149 if "boundary" in entry:
151 vol = entry[
"volume"]
160 if entry[
"value"][
"transform"][
"translation"] !=
None:
161 z_shift = entry[
"value"][
"transform"][
"translation"][2]
163 if entry[
"value"][
"type"] ==
"CylinderSurface":
165 entry[
"value"][
"bounds"][
"values"][0],
166 z_shift - entry[
"value"][
"bounds"][
"values"][1],
167 z_shift + entry[
"value"][
"bounds"][
"values"][1],
170 index_to_extends_layers_bounds_cylinders[vol - 1].append(extends)
171 elif entry[
"value"][
"type"] ==
"DiscSurface":
173 entry[
"value"][
"bounds"][
"values"][0],
174 entry[
"value"][
"bounds"][
"values"][1],
178 index_to_extends_layers_bounds_discs[vol - 1].append(extends)
181 "WARNING: Processing surface with unknown type. Only CylinderSurface and DiscSurface are considered."
185 from itertools
import chain
187 interesting_volumes = []
190 for elements
in chain(
191 index_to_extends_layers_cylinders, index_to_extends_layers_discs
193 for coords
in elements:
196 if v_index
not in interesting_volumes:
197 interesting_volumes.append(v_index)
199 steering_map[index_to_names[v_index]].layers_no_approach.append(coords[4])
200 v_index = v_index + 1
201 if not is_disc
and v_index == len(index_to_extends_layers_cylinders):
207 for elements
in chain(
208 index_to_extends_layers_bounds_cylinders, index_to_extends_layers_bounds_discs
210 for coords
in elements:
211 if v_index
in interesting_volumes:
213 steering_map, index_to_names[v_index], v_index + 1
215 steering_map[index_to_names[v_index]].boundaries.append(coords[3])
216 v_index = v_index + 1
217 if not is_disc
and v_index == len(index_to_extends_layers_bounds_cylinders):
223 for elements
in chain(
224 index_to_extends_layers_cylinders, index_to_extends_layers_discs
226 for coords
in elements:
231 not in steering_map[index_to_names[v_index]].layers_with_approach
233 steering_map[index_to_names[v_index]].layers_with_approach.append(
236 if coords[3]
not in steering_map[index_to_names[v_index]].approaches:
237 steering_map[index_to_names[v_index]].approaches.append(coords[3])
238 v_index = v_index + 1
239 if not is_disc
and v_index == len(index_to_extends_layers_bounds_cylinders):
244 output_map = {
"SteeringField": steering_map}
245 with
open(steering_file,
"w", encoding=
"utf-8")
as f:
246 json.dump(output_map, f, default=dumper, ensure_ascii=
False, indent=4)
250 import matplotlib.pyplot
as plt
252 plt.rcParams.update({
"figure.max_open_warning": 0})
253 from matplotlib.pyplot
import cm
254 from itertools
import cycle
257 color = cm.rainbow(np.linspace(0, 1, len(index_to_extends_layers_cylinders)))
261 plt.figure(figsize=(20, 10))
266 for elements
in chain(
267 index_to_extends_layers_cylinders, index_to_extends_layers_discs
269 for coords
in elements:
274 if index_to_names[v_index]
not in is_in_legend:
279 label=
"v: " +
str(v_index + 1) +
", " + index_to_names[v_index],
281 is_in_legend.append(index_to_names[v_index])
283 plt.plot(x_values, y_values, c=color[v_index])
284 v_index = v_index + 1
285 if not is_disc
and v_index == len(index_to_extends_layers_cylinders):
291 for elements
in chain(
292 index_to_extends_layers_bounds_cylinders,
293 index_to_extends_layers_bounds_discs,
295 for coords
in elements:
296 if v_index
in interesting_volumes:
298 plt.plot(x_values, y_values, c=color[v_index])
299 v_index = v_index + 1
300 if not is_disc
and v_index == len(index_to_extends_layers_bounds_cylinders):
306 plt.title(
"Volumes and Layers (no approach layers)")
307 plt.legend(loc=
"center left", bbox_to_anchor=(1, 0.5))
308 plt.savefig(output_folder +
"/volumes_and_layers.png")
313 approach_colors = [
"black",
"blue",
"red",
"green",
"orange",
"purple",
"pink"]
314 for elements
in chain(
315 index_to_extends_layers_cylinders, index_to_extends_layers_discs
319 v_index = v_index + 1
321 plt.figure(figsize=(20, 10))
322 color_layers = cm.rainbow(np.linspace(0, 1, len(elements)))
324 for coords
in elements:
332 c=color_layers[l_index],
333 label=
"l: " +
str(coords[4]),
335 l_index = l_index + 1
339 for a_coords
in chain(
340 index_to_extends_layers_cylinders[v_index],
341 index_to_extends_layers_discs[v_index],
343 if a_coords[4] == coords[4]
and a_coords[3] > 0:
348 linestyle=(0, (5, 10)),
349 c=approach_colors[a_coords[3]],
350 label=
"l: " +
str(coords[4]) +
", a: " +
str(a_coords[3]),
353 if count == len(index_to_extends_layers_cylinders[v_index]):
356 v_index = v_index + 1
357 if not is_disc
and v_index == len(index_to_extends_layers_cylinders):
366 plt.title(index_to_names[v_index - 1])
367 plt.legend(loc=
"center left", bbox_to_anchor=(1, 0.5))
368 plt.savefig(output_folder +
"/layers_for_volume_" +
str(v_index) +
".png")
370 plt.figure(figsize=(20, 10))
375 for elements
in chain(
376 index_to_extends_layers_bounds_cylinders,
377 index_to_extends_layers_bounds_discs,
379 for coords
in elements:
381 if v_index
in interesting_volumes:
387 label=
"v: " +
str(v_index + 1) +
", b: " +
str(coords[3]),
389 v_index = v_index + 1
390 if not is_disc
and v_index == len(index_to_extends_layers_bounds_cylinders):
396 plt.title(
"Boundary surfaces")
397 plt.legend(loc=
"center left", bbox_to_anchor=(1, 0.5))
398 plt.savefig(output_folder +
"/boundaries.png")
400 plt.figure(figsize=(20, 10))
406 for elements
in chain(
407 index_to_extends_layers_cylinders, index_to_extends_layers_discs
410 v_index = v_index + 1
412 for coords
in elements:
416 if coords[3]
not in add_to_legend:
420 c=approach_colors[coords[3]],
422 label=
"approach index = " +
str(coords[3]),
424 add_to_legend.append(coords[3])
427 x_values, y_values, c=approach_colors[coords[3]], linestyle=
"--"
429 v_index = v_index + 1
430 if not is_disc
and v_index == len(index_to_extends_layers_bounds_cylinders):
436 plt.title(
"Approach layers")
437 plt.legend(loc=
"center left", bbox_to_anchor=(1, 0.5))
438 plt.savefig(output_folder +
"/approach_layers.png")
443 layers =
open(steering_file)
445 full_data = json.load(layers)
446 layer_data = full_data[
"SteeringField"]
451 dump_binning_for_material =
False
454 check_material_layers =
True
458 for entry
in data[
"Volumes"][
"entries"]:
459 index_to_names.append(entry[
"value"][
"NAME"])
462 if not index_to_names:
463 for entry
in data[
"Surfaces"][
"entries"]:
464 if "volume" in entry:
465 vol = entry[
"volume"]
466 if "volume" +
str(vol)
not in index_to_names:
467 index_to_names.append(
"volume" +
str(vol))
469 for entry
in data[
"Surfaces"][
"entries"]:
471 vol = entry[
"volume"]
473 if index_to_names[vol - 1]
in layer_data:
474 if "approach" in entry:
477 in layer_data[index_to_names[vol - 1]][
"layers_with_approach"]
478 and entry[
"approach"]
479 in layer_data[index_to_names[vol - 1]][
"approaches"]
481 entry[
"value"][
"material"][
"mapMaterial"] =
True
485 in layer_data[index_to_names[vol - 1]][
"layers_no_approach"]
487 entry[
"value"][
"material"][
"mapMaterial"] =
True
489 if entry[
"value"][
"material"][
"mapMaterial"]:
490 for val
in entry[
"value"][
"material"][
"binUtility"][
"binningdata"]:
491 if val[
"value"] ==
"binZ" or val[
"value"] ==
"binR":
492 val[
"bins"] = layer_data[index_to_names[vol - 1]][
496 val[
"bins"] = layer_data[index_to_names[vol - 1]][
501 "ERROR!!! Using binning value == 0! Check you input for",
502 index_to_names[vol - 1],
506 approach_index =
"None"
507 if "approach" in entry:
508 approach_index = entry[
"approach"]
510 if dump_binning_for_material
and entry[
"value"][
"material"][
"mapMaterial"]:
514 index_to_names[vol - 1],
520 for val
in entry[
"value"][
"material"][
"binUtility"][
"binningdata"]:
521 print(
"-->", val[
"value"],
": ", val[
"bins"])
523 if "boundary" in entry:
525 vol = entry[
"volume"]
528 index_to_names[vol - 1]
in layer_data
529 and entry[
"boundary"]
530 in layer_data[index_to_names[vol - 1]][
"boundaries"]
532 entry[
"value"][
"material"][
"mapMaterial"] =
True
533 for val
in entry[
"value"][
"material"][
"binUtility"][
"binningdata"]:
534 if entry[
"value"][
"type"] ==
"CylinderSurface":
535 if val[
"value"] ==
"binZ":
536 val[
"bins"] = layer_data[index_to_names[vol - 1]][
537 "passiveCylinderBinningZ"
540 val[
"bins"] = layer_data[index_to_names[vol - 1]][
541 "passiveCylinderBinningPhi"
543 elif entry[
"value"][
"type"] ==
"DiscSurface":
544 if val[
"value"] ==
"binR":
545 val[
"bins"] = layer_data[index_to_names[vol - 1]][
546 "passiveDiscBinningR"
549 val[
"bins"] = layer_data[index_to_names[vol - 1]][
550 "passiveDiscBinningPhi"
554 "WARNING: Processing surface with unknown type. Only CylinderSurface and DiscSurface are considered."
558 "ERROR!!! Using binning value == 0! Check you input for",
559 index_to_names[vol - 1],
563 if dump_binning_for_material
and entry[
"value"][
"material"][
"mapMaterial"]:
567 index_to_names[vol - 1],
571 for val
in entry[
"value"][
"material"][
"binUtility"][
"binningdata"]:
572 print(
"-->", val[
"value"],
": ", val[
"bins"])
576 with
open(output_file,
"w", encoding=
"utf-8")
as f:
577 json.dump(data, f, ensure_ascii=
False, indent=4)
579 if plot
and check_material_layers:
580 import matplotlib.pyplot
as plt
581 from matplotlib.pyplot
import cm
582 from itertools
import cycle
585 plt.figure(figsize=(20, 10))
587 material_layer_cylinders = [[]
for _
in range(len(index_to_names))]
588 material_layer_discs = [[]
for _
in range(len(index_to_names))]
590 material_approach_cylinders = [[]
for _
in range(len(index_to_names))]
591 material_approach_discs = [[]
for _
in range(len(index_to_names))]
593 material_boundary_cylinders = [[]
for _
in range(len(index_to_names))]
594 material_boundary_discs = [[]
for _
in range(len(index_to_names))]
596 for entry
in data[
"Surfaces"][
"entries"]:
598 if not entry[
"value"][
"material"][
"mapMaterial"]:
602 if entry[
"value"][
"transform"][
"translation"] !=
None:
603 z_shift = entry[
"value"][
"transform"][
"translation"][2]
607 vol = entry[
"volume"]
609 if entry[
"value"][
"type"] ==
"CylinderSurface":
611 entry[
"value"][
"bounds"][
"values"][0],
612 z_shift - entry[
"value"][
"bounds"][
"values"][1],
613 z_shift + entry[
"value"][
"bounds"][
"values"][1],
615 if "approach" in entry:
616 material_approach_cylinders[vol - 1].append(extends)
618 material_layer_cylinders[vol - 1].append(extends)
620 elif entry[
"value"][
"type"] ==
"DiscSurface":
622 entry[
"value"][
"bounds"][
"values"][0],
623 entry[
"value"][
"bounds"][
"values"][1],
626 if "approach" in entry:
627 material_approach_discs[vol - 1].append(extends)
629 material_layer_discs[vol - 1].append(extends)
632 "WARNING: Processing surface with unknown type. Only CylinderSurface and DiscSurface are considered."
635 if "boundary" in entry:
637 vol = entry[
"volume"]
639 if entry[
"value"][
"type"] ==
"CylinderSurface":
641 entry[
"value"][
"bounds"][
"values"][0],
642 z_shift - entry[
"value"][
"bounds"][
"values"][1],
643 z_shift + entry[
"value"][
"bounds"][
"values"][1],
645 material_boundary_cylinders[vol - 1].append(extends)
647 elif entry[
"value"][
"type"] ==
"DiscSurface":
649 entry[
"value"][
"bounds"][
"values"][0],
650 entry[
"value"][
"bounds"][
"values"][1],
653 material_boundary_discs[vol - 1].append(extends)
656 "WARNING: Processing surface with unknown type. Only CylinderSurface and DiscSurface are considered."
659 from itertools
import chain
664 for elements
in chain(material_layer_cylinders, material_layer_discs):
666 for coords
in elements:
669 plt.plot(x_values, y_values, c=
"black", label=
"layer")
672 plt.plot(x_values, y_values, c=
"black")
673 l_index = l_index + 1
674 v_index = v_index + 1
675 if not is_disc
and v_index == len(material_layer_cylinders):
682 for elements
in chain(material_approach_cylinders, material_approach_discs):
684 for coords
in elements:
687 plt.plot(x_values, y_values, c=
"red", label=
"approach")
690 plt.plot(x_values, y_values, c=
"red")
691 l_index = l_index + 1
692 v_index = v_index + 1
693 if not is_disc
and v_index == len(material_approach_cylinders):
700 for elements
in chain(material_boundary_cylinders, material_boundary_discs):
702 for coords
in elements:
705 plt.plot(x_values, y_values, c=
"blue", label=
"boundary")
708 plt.plot(x_values, y_values, c=
"blue")
709 l_index = l_index + 1
710 v_index = v_index + 1
711 if not is_disc
and v_index == len(material_boundary_cylinders):
717 plt.title(
"Layers with material")
718 plt.legend(loc=
"center left", bbox_to_anchor=(1, 0.5))
719 plt.savefig(output_folder +
"/material_layers.png")
726 parser = argparse.ArgumentParser()
731 help=
"Specify the input file for geometry visualisation",
737 help=
"Enable plot creation for geometry visualisation (Default : True)",
743 help=
"Specify the output folder for plots (Default : plots)",
749 help=
"Enable production of steering file for material mapping (Default : False)",
755 help=
"Enable editing of input file for creation of json for material mapping (Default : False)",
761 help=
"Specify the steering file to guide editing of json for material mapping",
767 help=
"Specify the output json for material mapping",
769 args = parser.parse_args()
771 print(
" --- Geometry visualisation and material map fie creation --- ")
774 if not args.geometry:
775 print(
"Error: Missing input geometry file. Please specify --geometry.")
778 if not os.path.exists(args.geometry):
779 print(
"Error: Invalid file path/name in --geometry. Please check your input!")
782 if not args.geometry.endswith(
".json"):
783 print(
"Error: Invalid file format in --geometry. Please check your input!")
786 print(
"\t parsing file : ", args.geometry)
788 print(
"\t job configured to produce plots in output folder: ", args.output_folder)
789 if not os.path.exists(args.output_folder):
790 os.mkdir(args.output_folder)
792 if args.dump_steering
and args.edit:
794 "Error: Wrong job configuration. --dump_steering and --edit can't be \
795 both true at the same time."
798 "\t Decide if you want to dump the steering file OR to read an existing file for editing the geometry file."
802 if args.dump_steering:
803 if not args.steering_file:
804 print(
"Error: Missing output steering file. Please specify --steering_file.")
806 if not args.steering_file.endswith(
".json"):
807 print(
"Error: Invalid file format in --steering_file. It must end with .json!")
810 "\t job configured to produce steering file for material mapping with name: ",
815 print(
"\t job configured to edit the input geometry file following a steering file")
816 if not args.steering_file:
817 print(
"Error: Missing input steering file. Please specify --steering_file.")
819 if not os.path.exists(args.steering_file):
821 "Error: Invalid file path/name in --steering_file. Please check your input!"
824 if not args.steering_file.endswith(
".json"):
825 print(
"Error: Invalid file format in --steering_file. Please check your input!")
827 if not args.output_map:
828 print(
"Error: Missing output map file. Please specify --output_map.")
830 if not args.output_map.endswith(
".json"):
831 print(
"Error: Invalid file format in --output_map. Please check your input!")
835 if args.plot
or args.dump_steering: