orix icon indicating copy to clipboard operation
orix copied to clipboard

Make phase symmetry and structure immutable

Open hakonanes opened this issue 5 months ago • 6 comments

In https://github.com/pyxem/orix/issues/543#issuecomment-3031781380, I suggest to set ensure a lattice compatible with the crystal system of a phase in the constructor. If the user later on changes the symmetry of a phase, this lattice may not be valid anymore. Should we then check the lattice and give an error if it is not? I say no, and instead suggest to make some attributes of a phase, such as its symmetry, immutable.

Phase attributes I mean should be immutable:

  • point group
  • space group
  • structure

Pros:

  • All validation can be done in the constructor
  • User and developer knows that a phase's symmetry and lattice are consistent throughout its lifetime

Cons:

  • Someone may rely on setting these attributes of a phase. Their code will break

Do anyone set any of these attributes after creation? If so, I'd say this is an antipattern, they should instead create a new phase.

hakonanes avatar Jul 03 '25 11:07 hakonanes

Do anyone set any of these attributes after creation? If so, I'd say this is an antipattern, they should instead create a new phase.

I agree.

argerlt avatar Jul 03 '25 22:07 argerlt

Glad to hear it! @CSSFrancis, do you know if set the symmetry or structure of crystal phases in pyxem or diffsims after creation?

hakonanes avatar Jul 04 '25 05:07 hakonanes

Hmmm.

I'm not sure. Maybe if someone wanted to strain some crystal they might adjust the lattice. But that would keep the symmetry constant.

I'm not really sure of a good case. Probably better to make sure people don't accidentally break things

CSSFrancis avatar Jul 04 '25 12:07 CSSFrancis

Yes, but they should do so by creating a new phase with a slightly different lattice. The structure attribute of the phase class was really only meant to be a storage of data given upon creation, not something you change after the phase was created.

We can make a release candidate and ask downstream packages to test against it.

hakonanes avatar Jul 04 '25 19:07 hakonanes

I think as long as PG and SG are immutable, I could go either way on the lattice.

If you make the lattice immutable, coding up the checks becomes way easier, and you have considerably less edge cases to consider. It also makes it easier to reuse Phase objects without concern.

argerlt avatar Jul 04 '25 20:07 argerlt

Exactly. Then I'll make the change.

hakonanes avatar Jul 09 '25 11:07 hakonanes