From 372d751fa64967443adcc655bed2bf3d89512822 Mon Sep 17 00:00:00 2001 From: Will Boyd Date: Tue, 17 Nov 2015 21:18:26 -0500 Subject: [PATCH 1/4] Added alpha compositing scheme to Python API Plot class to highlight domains --- openmc/plots.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/openmc/plots.py b/openmc/plots.py index acc7d4d13c..d04f9a036e 100644 --- a/openmc/plots.py +++ b/openmc/plots.py @@ -270,7 +270,7 @@ class Plot(object): Params ------ geometry : openmc.Geometry - The geometry for which the plot is created + The geometry for which the plot is defined seed : Integral The random number seed used to generate the color scheme @@ -284,7 +284,7 @@ class Plot(object): if self.color is 'mat': domains = geometry.get_all_materials() else: - domains = geometry.get_all_materials() + domains = geometry.get_all_cells() # Set the seed for the random number generator np.random.seed(seed) @@ -295,9 +295,63 @@ class Plot(object): r = np.random.randint(0, 255) g = np.random.randint(0, 255) b = np.random.randint(0, 255) - self.col_spec[domain.id] = (r, g, b) + self.col_spec[domain] = (r, g, b) + def highlight_domains(self, geometry, domains, seed=1, + alpha=0.5, background='grey'): + """Use alpha compositing to highlight one or more domains in the plot. + + This routine generates a color scheme and applies alpha compositing + to make all domains except the highlighted ones partially transparent. + + Params + ------ + geometry : openmc.Geometry + The geometry for which the plot is defined + domains : Iterable of Integral + A collection of the domain IDs to highlight in the plot + seed : Integral + The random number seed used to generate the color scheme + alpha : Real in [0,1] + The value to apply in alpha compisiting + background : 3-tuple of Integral or 'white' or 'black' or 'grey' + The background color to apply in alpha compisiting + + """ + + cv.check_iterable_type('domains', domains, Integral) + cv.check_type('alpha', alpha, Real) + cv.check_greater_than('alpha', alpha, 0., equality=True) + cv.check_less_than('alpha', alpha, 1., equality=True) + + # Get a background (R,G,B) tuple to apply in alpha compositing + if isinstance(background, basestring): + if background == 'white': + background = (255, 255, 255) + elif background == 'black': + background = (0, 0, 0) + elif background == 'grey': + background = (160, 160, 160) + else: + msg = 'The background "{}" is not defined'.format(background) + raise ValueError(msg) + + cv.check_iterable_type('background', background, Integral) + + # Generate a color scheme + self.colorize(geometry, seed) + + # Apply alpha compositing to the colors for all domains + # other than those the user wishes to highlight + for domain_id in self.col_spec: + if domain_id not in domains: + r, g, b = self.col_spec[domain_id] + r = int(((1-alpha) * background[0]) + (alpha * r)) + g = int(((1-alpha) * background[1]) + (alpha * g)) + b = int(((1-alpha) * background[2]) + (alpha * b)) + self._col_spec[domain_id] = (r, g, b) + def get_plot_xml(self): """Return XML representation of the plot From 0933cd7e8aa74d8a6f39bcc901de4e2bb2de9273 Mon Sep 17 00:00:00 2001 From: Will Boyd Date: Tue, 17 Nov 2015 21:19:42 -0500 Subject: [PATCH 2/4] Added alpha compositing routine to PlotsFile in Python API --- openmc/plots.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/openmc/plots.py b/openmc/plots.py index d04f9a036e..aedc6ed41b 100644 --- a/openmc/plots.py +++ b/openmc/plots.py @@ -459,6 +459,32 @@ class PlotsFile(object): for plot in self._plots: plot.colorize(geometry, seed) + + def highlight_domains(self, geometry, domains, seed=1, + alpha=0.5, background='grey'): + """Use alpha compositing to highlight one or more domains in the plot. + + This routine generates a color scheme and applies alpha compositing + to make all domains except the highlighted ones partially transparent. + + Params + ------ + geometry : openmc.Geometry + The geometry for which the plot is defined + domains : Iterable of Integral + A collection of the domain IDs to highlight in the plot + seed : Integral + The random number seed used to generate the color scheme + alpha : Real in [0,1] + The value to apply in alpha compisiting + background : 3-tuple of Integral or 'white' or 'black' or 'grey' + The background color to apply in alpha compisiting + + """ + + for plot in self._plots: + plot.highlight_domains(geometry, domains, seed, alpha, background) + def _create_plot_subelements(self): for plot in self._plots: xml_element = plot.get_plot_xml() From 0c7d58457e2738c1373c1e9747462434721c68fe Mon Sep 17 00:00:00 2001 From: Will Boyd Date: Wed, 18 Nov 2015 11:04:09 -0500 Subject: [PATCH 3/4] Now using American English for gray (not grey) in alpha transparency --- openmc/plots.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/openmc/plots.py b/openmc/plots.py index e0237f525d..0f12c8a93a 100644 --- a/openmc/plots.py +++ b/openmc/plots.py @@ -298,7 +298,7 @@ class Plot(object): self.col_spec[domain] = (r, g, b) def highlight_domains(self, geometry, domains, seed=1, - alpha=0.5, background='grey'): + alpha=0.5, background='gray'): """Use alpha compositing to highlight one or more domains in the plot. This routine generates a color scheme and applies alpha compositing @@ -314,7 +314,7 @@ class Plot(object): The random number seed used to generate the color scheme alpha : Real in [0,1] The value to apply in alpha compisiting - background : 3-tuple of Integral or 'white' or 'black' or 'grey' + background : 3-tuple of Integral or 'white' or 'black' or 'gray' The background color to apply in alpha compisiting """ @@ -330,7 +330,7 @@ class Plot(object): background = (255, 255, 255) elif background == 'black': background = (0, 0, 0) - elif background == 'grey': + elif background == 'gray': background = (160, 160, 160) else: msg = 'The background "{}" is not defined'.format(background) @@ -460,7 +460,7 @@ class PlotsFile(object): def highlight_domains(self, geometry, domains, seed=1, - alpha=0.5, background='grey'): + alpha=0.5, background='gray'): """Use alpha compositing to highlight one or more domains in the plot. This routine generates a color scheme and applies alpha compositing @@ -476,7 +476,7 @@ class PlotsFile(object): The random number seed used to generate the color scheme alpha : Real in [0,1] The value to apply in alpha compisiting - background : 3-tuple of Integral or 'white' or 'black' or 'grey' + background : 3-tuple of Integral or 'white' or 'black' or 'gray' The background color to apply in alpha compisiting """ From 7abbee6df69a0c50e4167b07f3173a31c258a820 Mon Sep 17 00:00:00 2001 From: Will Boyd Date: Wed, 18 Nov 2015 11:05:10 -0500 Subject: [PATCH 4/4] Revised docstring Params -> Parameters for alpha transparency --- openmc/plots.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/openmc/plots.py b/openmc/plots.py index 0f12c8a93a..636ca225cb 100644 --- a/openmc/plots.py +++ b/openmc/plots.py @@ -302,10 +302,11 @@ class Plot(object): """Use alpha compositing to highlight one or more domains in the plot. This routine generates a color scheme and applies alpha compositing - to make all domains except the highlighted ones partially transparent. + to make all domains except the highlighted ones appear partially + transparent. - Params - ------ + Parameters + ---------- geometry : openmc.Geometry The geometry for which the plot is defined domains : Iterable of Integral @@ -466,8 +467,8 @@ class PlotsFile(object): This routine generates a color scheme and applies alpha compositing to make all domains except the highlighted ones partially transparent. - Params - ------ + Parameters + ---------- geometry : openmc.Geometry The geometry for which the plot is defined domains : Iterable of Integral