Skip to content
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

Improvements to the documentation of the Lobsterenv module #3079

Merged
merged 25 commits into from
Jun 19, 2023

Conversation

JaGeo
Copy link
Member

@JaGeo JaGeo commented Jun 19, 2023

I improved the documentation of the LobsterEnv module which was triggered by our updates to LobsterPy (JaGeo/LobsterPy#81) and suggestions by @ajjackson

@janosh : happy to hear additional thoughts from you.

@JaGeo
Copy link
Member Author

JaGeo commented Jun 19, 2023

@janosh, I tried to add more type hinting but mypy was raising very weird errors.

number of identified bonds to the selected sites (int),
labels (from ICOHPLIST) for all identified bonds returned as a list of site names, e.g.,
['Ag3', 'O5'] [the latter is useful for plotting summed COHP plots] (list),
list of the central isite for each identified interaction (list)
Copy link
Contributor

Choose a reason for hiding this comment

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

The return value of the function also has an "atoms" entry but I don't see that here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, you are correct. I somehow merged labels and atoms in the description.

@ajjackson
Copy link
Contributor

Where functions are returning a large tuple containing various types and lengths of data, it might be helpful to wrap them up as a NamedTuple. This would be backward-compatible as you still can access the elements by index, but also allows:

  • clearer type-hinting than a dict would have, taking advantage of the immutability
  • named attribute access for more intelligible code and lower chance of accessing the wrong element by mistake
  • easier interactive exploration e.g. in a Jupyter notebook or IPython terminal; we get something a bit more self-describing than a bare list of tuple, but not a complicated new class that might have surprising behaviours

Be aware that while a lot of the old docs and tutorials recommend to use the namedtuple function to produce them, we can now build them with class for clearer type-hinting. See examples at https://docs.python.org/3/library/typing.html#typing.NamedTuple

So we could do something like

class ICOHPNeighboursInfo(NamedTuple):
    total: float
    interaction_sums: List[float]
    n_bonds: int
    labels: List[str]
    atoms: List[SomeAtomTypeOrOther]
    central_isites: List[int]

class LobsterNeighbors(NearNeighbors):
...
    def get_info_icohps_to_neighbors(
        self,
        isites: Optional[List[int]] = None,
        onlycation_isites: bool = True
            ) -> ICOHPNeighboursInfo:
    ...
    return ICOHPNeigboursInfo(summed_icohps, list_icohps, number_bonds, labels, atoms, final_isites)

@JaGeo
Copy link
Member Author

JaGeo commented Jun 19, 2023

Thanks, @ajjackson , this is a great suggestion. I will implement it as it is much cleaner.

@JaGeo
Copy link
Member Author

JaGeo commented Jun 19, 2023

@ajjackson , I implemented the named tuple

filename_ICOHP: str = None,
valences: list[int | float] = None,
limits: tuple[float, float] | None = None,
structure: Structure = None,
Copy link
Member

Choose a reason for hiding this comment

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

@janosh, I tried to add more type hinting but mypy was raising very weird errors.

Is the structure arg really optional here? What happens if you don't supply a structure?

Copy link
Member Author

Choose a reason for hiding this comment

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

You are correct. Same is true for "filename_ICOHP".

pymatgen/io/lobster/lobsterenv.py Outdated Show resolved Hide resolved
pymatgen/io/lobster/lobsterenv.py Outdated Show resolved Hide resolved
Copy link
Member

@janosh janosh left a comment

Choose a reason for hiding this comment

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

Thanks @JaGeo! 👍

@janosh janosh enabled auto-merge (squash) June 19, 2023 15:29
@janosh janosh merged commit fa10dfa into materialsproject:master Jun 19, 2023
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.

3 participants