Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
color = all_colors.pop()
# Get a phase_id (always)
try:
phase_id = ids[i]
except IndexError:
phase_id = max(ids) + phase_id_iter + 1
phase_id_iter += 1
# Get a structure or None
try:
structure = structures[i]
except (IndexError, TypeError):
structure = None
d[phase_id] = Phase(
name=name, symmetry=symmetry, color=color, structure=structure
)
# To ensure color aliases are added to `used_colors`
used_colors.append(d[phase_id].color)
# Finally create dictionary of phases
self._dict = OrderedDict(sorted(d.items()))
d = {}
if isinstance(phases, list):
try:
if isinstance(next(iter(phases)), Phase):
if ids is None:
ids = np.arange(len(phases))
d = dict(zip(ids, phases))
except StopIteration:
pass
elif isinstance(phases, dict):
try:
if isinstance(next(iter(phases.values())), Phase):
d = phases
except StopIteration:
pass
elif isinstance(phases, Phase):
if ids is None:
ids = 0
d = {ids: phases}
else:
# Ensure possible single strings or single objects have
# iterables of length 1
if isinstance(names, str):
names = list((names,))
if (
isinstance(symmetries, str)
or isinstance(symmetries, Symmetry)
or isinstance(symmetries, int)
):
symmetries = list((symmetries,))
if isinstance(colors, str) or isinstance(colors, tuple):
colors = list((colors,))
def phases_in_data(self):
"""List of phases in data.
Needed because it can be useful to have phases not in data but in
`self.phases`.
"""
unique_ids = np.unique(self.phase_id)
phase_list = self.phases[np.intersect1d(unique_ids, self.phases.ids)]
if isinstance(phase_list, Phase): # One phase in data
# Get phase ID so it carries over to the new `PhaseList` object
phase = phase_list # Since it's actually a single phase
phase_id = self.phases.id_from_name(phase.name)
return PhaseList(phases=phase, ids=phase_id)
else: # Multiple phases in data
return phase_list
"""Add a phase to the list with a name, symmetry and structure."""
if key not in self.names:
# Make sure the new phase gets a new color
color_new = None
for color_name in ALL_COLORS.keys():
if color_name not in self.colors:
color_new = color_name
break
# Create new ID
if self.ids:
new_phase_id = max(self.ids) + 1
else: # `self.phase_ids` is an empty list
new_phase_id = 0
self._dict[new_phase_id] = Phase(name=key, symmetry=value, color=color_new)
else:
raise ValueError(f"{key} is already in the phase list {self.names}.")
def add_not_indexed(self):
"""Add a dummy phase to assign to not indexed data points.
The phase, named "not_indexed", has a "symmetry" equal to None,
and a white color when plotted.
"""
self._dict[-1] = Phase(name="not_indexed", symmetry=None, color="white")
self.sort_by_id()