<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Structure Types
Structure types are assigned by hand by ICSD curators.
Step1:  Filter for stoichiometric compounds only
Step2:  Long Formulas
Step3:  Two key insights
<ASSISTANT_TASK:>
Python Code:
# How many ternaries have been assigned a structure type?
structure_types = [line[3] for line in data if line[3] is not '']
unique_structure_types = set(structure_types)
print("There are {} ICSD ternaries entries.".format(len(data)))
print("Structure types are assigned for {} entries.".format(len(structure_types)))
print("There are {} unique structure types.".format(len(unique_structure_types)))
Explanation: Structure Types
Structure types are assigned by hand by ICSD curators.
End of explanation
def is_stoichiometric(composition):
    return np.all(np.mod(composition.values(), 1) == 0)
stoichiometric_compositions = [c for c in compositions if is_stoichiometric(c)]
print("Number of stoichiometric compositions: {}".format(len(stoichiometric_compositions)))
ternaries = set(c.formula for c in stoichiometric_compositions)
print("Number of unique stoichiometric compositions: {}".format(len(ternaries)))
data_stoichiometric = [x for x in data if is_stoichiometric(Composition(x[2]))]
from collections import Counter
struct_type_freq = Counter(x[3] for x in data_stoichiometric if x[3] is not '')
plt.loglog(range(1, len(struct_type_freq)+1),
             sorted(struct_type_freq.values(), reverse = True), 'o')
plt.xlabel("Structure Type")
plt.ylabel("Structure Type Frequency")
plt.title("Distribution of Frequencies of Structure Types")
sorted(struct_type_freq.items(), key = lambda x: x[1], reverse = True)
uniq_phases = set()
for row in data_stoichiometric:
    spacegroup, formula, struct_type = row[1:4]
    phase = (spacegroup, Composition(formula).formula, struct_type)
    uniq_phases.add(phase)
uniq_struct_type_freq = Counter(x[2] for x in uniq_phases if x[2] is not '')
uniq_struct_type_freq_sorted = sorted(uniq_struct_type_freq.items(), key = lambda x: x[1], reverse = True)
plt.loglog(range(1, len(uniq_struct_type_freq_sorted)+1),
           [x[1] for x in uniq_struct_type_freq_sorted], 'o')
plt.xlabel("Structure Type")
plt.ylabel("Structure Type Frequency")
plt.title("Distribution of Frequencies of Structure Types")
uniq_struct_type_freq_sorted
for struct_type,freq in uniq_struct_type_freq_sorted[:10]:
    print("{} : {}".format(struct_type, freq))
    fffs = [p[1] for p in uniq_phases if p[2] == struct_type]
    fmt = "  ".join(["{:14}"]*5)
    print(fmt.format(*fffs[0:5]))
    print(fmt.format(*fffs[5:10]))
    print(fmt.format(*fffs[10:15]))
    print(fmt.format(*fffs[15:20]))
Explanation: Filter for stoichiometric compounds only:
End of explanation
# What are the longest formulas?
for formula in sorted(formulas, key = lambda x: len(x), reverse = True)[:20]:
    print(formula)
Explanation: Long Formulas
End of explanation
def filter_in_set(compound, universe):
    return all((e in universe) for e in Composition(compound))
transition_metals = [e for e in Element if e.is_transition_metal]
tm_ternaries = [c for c in formulas if filter_in_set(c, transition_metals)]
print("Number of intermetallics:", len(tm_ternaries))
unique_tm_ternaries = set([Composition(c).formula for c in tm_ternaries])
print("Number of unique intermetallics:", len(unique_tm_ternaries))
unique_tm_ternaries
Explanation: Two key insights:
1. Just because there are three elements in the formula
   doesn't mean the compound is fundamentally a ternary.
   There are doped binaries which masquerade as ternaries.
   And there are doped ternaries which masquerade as quaternaries,
   or even quintenaries. Because I only asked for compositions
   with 3 elements, this data is missing.
2. ICSD has strategically placed parentheses in the formulas
   which give hints as to logical groupings. For example:
     (Ho1.3 Ti0.7) ((Ti0.64 Ho1.36) O6.67)
   is in fact in the pyrochlore family, A2B2O7.
Intermetallics
How many intermetallics does the ICSD database contain?
End of explanation
<END_TASK>