[bug] SpaceGroup object grabbing incorrect information

Hello again,

I found a potential bug in the SpaceGroup class – specifically one of its class methods. When initiating a SpaceGroup class using the SpaceGroup.from_int_number(), the hexagonal option is not working in some cases. Here’s an example that illustrates the issue:

from pymatgen.symmetry.groups import SpaceGroup

#####

# when I want to load the hexagonal form of this spacegroup, it loads correctly
hexagonal = SpaceGroup.from_int_number(166, hexagonal=True)

# the 'H' at the end of the symbol indicates this is correctly the hexagonal sg
hexagonal.symbol # outputs 'R-3mH'

# the order of the hexagonal sg is correctly 32
hexagonal.order # outputs 32

####

# when I attempt to load the rhombohedral form, it is incorrectly giving me the hexagonal
rhombohedral = SpaceGroup.from_int_number(166, hexagonal=False)

# the symbol should no longer have the 'H' at the end
rhombohedral.symbol # outputs 'R-3mH'

# the order should be 12, not 32
rhombohedral.order # outputs 32

Is this a non-issue? Maybe I’m missunderstanding the SpaceGroup objects. I’m still getting (hexagonal != rhombohedral) == True for the objects above, so maybe there’s a nuance that I’m not seeing.

Any help would be greatly appreciated!

Thanks in advance,
Jack

Any word on this? I think I found where the issue is. I can move the discussion to the Github Issues if this indeed a bug and not my misunderstanding.

I believe I found the problem by looking at the SpaceGroup.from_int_number(166, hexagonal=False) methods:

from pymatgen.symmetry.groups import SpaceGroup

SpaceGroup.from_int_number(166, hexagonal=True) # correctly returns hexagonal info
SpaceGroup.from_int_number(166, hexagonal=False) # incorrectly returns hexagonal info
SpaceGroup('R-3m') # correctly returns hexagonal info
SpaceGroup('R-3m:H') # correctly returns hexagonal info
SpaceGroup('R-3m:R') # correctly return rhombohedral info

In the from_int_number() code (located here on github), the method really is just calling sg_symbol_from_int_number() and running that result back through SpaceGroup(). The bug exists in sg_symbol_from_int_number(). More specifically, it is a logical error in the following line:

syms = list(filter(lambda s: not s.endswith("H"), syms))

This line is run when hexagonal=False. The issue is that this line will return ‘R-3m’, which then calls SpaceGroup(‘R-3m’) – this still gives the hexagonal object! We need that line to return ‘R-3m:R’, not ‘R-3m’. Therefore, I think the line should be:

syms = list(filter(lambda s: s.endswith(":R"), syms))

I’m guessing this should fix the issue. There are a total of 7 spacegroups that this should fix: 146,148,155,160,161,166,167.

When reading through this code, I think I found a second issue too (with the Structure.from_spacegroup method), but I’ll make a separate post about that.

Let me know if this is a quick fix or if I am misunderstanding the code!

-Jack

I’m not getting much response here, so I’ve moved this discussion to github issues (link: issue #1801). Read there for the updated description of the bug.