Fusion-Power-Plant-Framework / bluemira

Bluemira is an integrated inter-disciplinary design tool for future fusion reactors. It incorporates several modules, some of which rely on other codes, to carry out a range of typical conceptual fusion reactor design activities.
https://bluemira.readthedocs.io/
GNU Lesser General Public License v2.1
57 stars 16 forks source link

`Component` display options: filtering leaves by certain rules #2207

Closed CoronelBuendia closed 1 year ago

CoronelBuendia commented 1 year ago

Description of issue / requirement to address

At the moment, when we view a Component with either show_cad or plot_2d, we have no control over what components are shown.

Given we are now starting to keep track of void geometries, it would be nice to have flexible ways of filtering which components are shown, perhaps with some sensible defaults (i.e. don't show voids by default).

For example, we may in future want to only show the children of a component which are made from e.g. EUROfer.

Note that all solutions should work for show_cad and Component.show_cad, same for plotting.

Proposed solution

The underlying mechanics can be as necessary to provide the below behaviour.

Something like (APDL-like API), for example:

from typing import Optional, Tuple, Any, List
from bluemira.materials.cache import Void, EUROfer
from from bluemira.base.components import Component

my_massive_component.show_cad("xyz", filters: Optional[List[Tuple[str, str, Any]]]]=[["u", "material", Void]])
# show everything except voids

my_massive_component.show_cad("xyz", filters: Optional[List[Tuple[str, str, Any]]]]=[["s", "material", EUROfer]])
# show only EUROfer stuff

Making this work for the show_cad and plot_2d functions could be ugly however, as technically, we can pass a list of Components, which would each need their filter rules in some iterable.

Alternative suggestions for API very welcome! Could perhaps also use a filters=List[Callable[[PhysicalComonent], bool]],

Alternative solutions

Additional context

2164 was a lacklustre attempt to hide Voids, but is not flexible at all. I will close it shortly.

@ivanmaione @je-cook FYI, please comment, as soon as we converge on an API, @je-cook is happy to address.

je-cook commented 1 year ago

I think the distinction needs to be made between how we show things (which plot options and display options cover) and what we are displaying.

In my head that means that a Component shouldnt need that filter but it could be a nice addition to the BaseManager class and its show_cad and plot. Therefore it would automatically work with both ComponentManager and Reactor.

The downside to this approach is you wouldnt get the functionality until you wrap a Component in a manager. However as the plotting and showing already lives on the BaseManager and therefore ComponentManager you wouldnt need to subclass just to get the functionality you could just do ComponentManager(comp) and work with that object for viewing and debugging/developing.

ivanmaione commented 1 year ago

I agree with @je-cook that a filter function of this kind should be added to BaseManager. However, for the sake of consistency with the existing display and plot functions, I suggest exposing this functionality through a dedicated Component function. My proposal is to avoid modifying show_cad or plot, but rather create a separate function that uses the native anytree filter/search option to return a list of leaf components for display. This list can then be used as input for the respective plotting functions.

je-cook commented 1 year ago

To achieve a fully generic filtering system is a lot of work, which would be nice to have, but as a half way house just adding an extra kwarg eg with_material where the default None would filter out Void Materials. This follows the same pattern as we currently have on both plot and show_cad on the BaseManager for with_component.

In future we can add extra that functionality into a filtering function that a component can be passed in to (and the BaseManager would probably call) but for the sake of expediting this as it is holding up EUDEMO work I would just like to do the former as a starting point.

My option would not modify the actual show_cad or plot just the methods on the BaseManager to add that kwarg for now.

CoronelBuendia commented 1 year ago

Just as a reminder: all I am requesting in terms of functionality right now is the ability to hide Void PhysicalComponents by default. That is my use case.