Skip to content

feature[frontend]: improve printing of materials and material library #2356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

groberts-flex
Copy link
Contributor

@groberts-flex groberts-flex commented Apr 3, 2025

Improving of material printing to address feature request #2307.

import tidy3d as td

mat = td.material_library["W"]["RakicLorentzDrude1998"]
slab = td.Structure(
        geometry=td.Box(
            center=(0, 0, 0),
            size=(1, 1, 1),
        ),
        medium=mat,
    )

print("\n\nPrinting mat:")
print(mat)
print("\n\nPrinting slab with mat as medium:")
print(slab)

medium_dict = {"medium" : mat}
print("\n\nPrinting dict holding medium:")
print(medium_dict)

print("\n\n")

Output:

Printing mat:

td.PoleResidue(
	eps_inf=1.0, 
	poles=((0j, (4.2732115514080845e+17+0j)), ((-97233116671752.14+0j), (-4.2732115514080845e+17+0j)), ((-402605873718973.75-1471252666401400j), 7403002173803196j), ((-973090800441519.4-2745063931489722.5j), 1.2197111799530032e+16j), ((-2531099568361548-4814146946972908j), 2.9579221430831016e+16j), ((-4433222413252700-1.0493429699239636e+16j), 4.978330061510858e+16j)), 
	frequency_range=(24176811129032.258, 1208986804855426.0))

Printing slab with mat as medium:

attrs={} geometry=Box(attrs={}, type='Box', center=(0.0, 0.0, 0.0), size=(1.0, 1.0, 1.0)) name=None background_permittivity=None background_medium=None type='Structure' medium=W_RakicLorentzDrude1998

Printing dict holding medium:

{'medium': W_RakicLorentzDrude1998}

@groberts-flex groberts-flex force-pushed the groberts-flex/material_printing branch from 5b05fae to afa9bb4 Compare April 3, 2025 18:39
@groberts-flex groberts-flex changed the title feature[frontend]: improve printint of materials and material library feature[frontend]: improve printing of materials and material library Apr 3, 2025
@@ -0,0 +1,86 @@
from typing import List
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for starting me off with this util.py file @yaugenst-flex. Made some minor modifications, but overall it works well!

@groberts-flex groberts-flex force-pushed the groberts-flex/material_printing branch from afa9bb4 to 9fc3109 Compare April 3, 2025 18:43
Comment on lines +2049 to +2055

class MaterialLibrary(dict):
def __str__(self):
return summarize_material_library(self)


material_library = MaterialLibrary(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat! Didn't think of this approach, but that looks great.

Comment on lines +74 to +76
@property
def summarize_mediums(self) -> Dict[str, Union[PoleResidue, Medium2D, MultiPhysicsMedium]]:
return {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bit confused by this property, what's its purpose?

- Methods for pretty printing the `material_library` as well as materials and their variants via their string methods.

### Changed
- Updated `__repr__` for `AbstractMedium` instances that have a specified name field to use that name instead of the full string representation of the medium to reduce clutter when printing models that have these mediums.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this is a bit too technical for this change, maybe focus more on the impact this has on usability. Maybe something like "Named mediums now display by name for brevity; materials/variants print concise summaries including references."

Comment on lines +41 to +48
def test_medium_repr():
"""Test the new repr method does not error for regular media with and without names."""

test_media = [
td.Medium(permittivity=1.5**2),
td.Medium(permittivity=1.5**2, name="material"),
td.material_library["SiO2"]["Horiba"].updated_copy(name=None),
]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what this test does? __repr__ is never called here no?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I'm thinking about this some more I feel like we should do two, optionally three things:

  1. A concise __str__ representation that is pretty similar to the one currently. Maybe even shorter, like just a list of the material names. Not sure. Tbh if we have the below, might also just skip it entirely for the material library and just print the dict instead.
  2. A .show(), .describe(), or .pretty_print() (just throwing some names out there) method that prints a nicely formatted summary using rich. For the material library as a whole that could be e.g. a table. We already use rich in other places so this would not be an additionaly dependency.
  3. Optionally (but definitely nice), a _repr_pretty_ method (see here) to ensure nicely formatted output in jupyter notebooks too. I think this can largely reuse whatever we do in 2.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are mostly smoke tests, which are good to have but it'd be good if we would also test some functionality, e.g. if the material names are what we think they are, whether the __repr__ returns the string, what it does for None...
The variants and material library are a bit more annoying, we should still check if e.g. some expected strings are in there or not.

Copy link
Collaborator

@yaugenst-flex yaugenst-flex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @groberts-flex, this is much needed! I think the changes to the Medium work well as-is, and thanks for adding all the material names! Regarding pretty printing within the material library itself I added some more comments because I think we should try to make this output as pretty and useful as we can if we're already working on it. But all in all it's looking pretty good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants