Formula

There are two primary goals for this Formula class.

  1. Parse and normalize a single formula

  2. Count, order, filter a list of formulas

Import

[1]:
from bobleesj.utils.parsers.formula import Formula

Formula

Parse

[2]:
formula = Formula("NdSi2")
print("Parsed formula:", formula.parsed_formula)
print("Elements:", formula.elements)
print("Indices:", formula.indices)
print("Element count:", formula.element_count)
print("Max, Min, Avg of indices:", formula.max_min_avg_index)
Parsed formula: [('Nd', 1.0), ('Si', 2.0)]
Elements: ['Nd', 'Si']
Indices: [1.0, 2.0]
Element count: 2
Max, Min, Avg of indices: (2.0, 1.0, 1.5)

Normalize formula

[3]:
# Normalized index values, default is 6 decimal places
print("Indices:", formula.get_normalized_indices())
print("Formula:", formula.get_normalized_formula())
print("Parsed formula:", formula.get_normalized_parsed_formula())
Indices: [0.333333, 0.666667]
Formula: Nd0.333333Si0.666667
Parsed formula: [('Nd', 0.333333), ('Si', 0.666667)]
[4]:
# Round to 3 decimal places
print("Indices:", formula.get_normalized_indices(decimals=3))
print("Formula:", formula.get_normalized_formula(decimals=3))
print("Parsed formula:", formula.get_normalized_parsed_formula(decimals=3))
Indices: [0.333, 0.667]
Formula: Nd0.333Si0.667
Parsed formula: [('Nd', 0.333), ('Si', 0.667)]

Sort formulas into composition type unary, binary, ternary, etc.

Count, order, filter a list of formulas

Count

[5]:
# Count all formulas in a list
Formula.count(["Cu", "NdSi2", "ThOs", "NdSi2Th2", "YNdThSi2"])
[5]:
5
[6]:
# Count the number of unique formulas
Formula.count_unique(["CuNdSi2", "CuNdSi2", "YNdThSi2"])
[6]:
2
[7]:
# Count the number of occurrences of each unique formula
Formula.count_individual(["NdSi2", "NdSi2", "YNdThSi2"])
[7]:
{'NdSi2': 2, 'YNdThSi2': 1}
[8]:
# Count and display duplicate formulas only
Formula.count_duplicates(["NdSi2", "NdSi2", "NdSi2Th2", "NdSi2Th2", "ThOs"])
[8]:
{'NdSi2': 2, 'NdSi2Th2': 2}
[9]:
# Count the number of formula provided
Formula.count_by_formula(["NdSi2", "NdSi2", "NdSi2Th2", "NdSi2Th2", "ThOs"], "NdSi2")
[9]:
2
[10]:
# Count by formula
Formula.count_by_formula(["NdSi2", "NdSi2", "NdSi2Th2", "NdSi2Th2", "ThOs"], "NdSi2")
[10]:
2
[11]:
# Count by compposition
Formula.count_by_composition(["NdSi2", "NdSi2", "NdSi2Th2", "NdSi2Th2", "ThOs"])
[11]:
{2: 3, 3: 2}
[12]:
# Get unique elements only
Formula.get_unique_elements(["NdSi2", "NdSi2", "NdSi2Th2", "NdSi2Th2", "ThOs"])
[12]:
{'Nd', 'Os', 'Si', 'Th'}
[13]:
# Get unique formulas only
Formula.get_unique_formulas(["NdSi2", "NdSi2", "NdSi2Th2", "NdSi2Th2", "ThOs"])
[13]:
{'NdSi2', 'NdSi2Th2', 'ThOs'}
[14]:
# Get element counts (ignore Stoichiometry)
Formula.get_element_count(["NdSi2", "NdSi2", "NdSi2Th2", "NdSi2Th2", "ThOs"])
[14]:
{'Nd': 4, 'Si': 4, 'Th': 3, 'Os': 1}

Order

[15]:
# Order formulas A->Z
Formula.order_by_alphabetical(["AB2", "AB", "BC2D2", "BBC2"])
[15]:
['AB', 'AB2', 'BBC2', 'BC2D2']
[16]:
# Order formulas Z->A
Formula.order_by_alphabetical(["AB2", "AB", "BC2D2", "BBC2"], reverse=True)
[16]:
['BC2D2', 'BBC2', 'AB2', 'AB']

Filter

[17]:
# Filter a list of formula by composition
sorted_formulas = Formula.filter_by_composition(["Cu", "NdSi2", "ThOs", "NdSi2Th2", "YNdThSi2"])
[18]:
# Filter by composition (e.g., binary)
Formula.filter_by_single_composition(["NdSi2", "NdSi2", "NdSi2Th2", "NdSi2Th2", "ThOs"], 2)
[18]:
['NdSi2', 'NdSi2', 'ThOs']
[19]:
# Filter by elements containing
Formula.filter_by_elements_containing(["NdSi2", "NdSi2", "NdSi2Th2", "NdSi2Th2", "ThOs"], ["Nd", "Si"])
[19]:
['NdSi2', 'NdSi2', 'NdSi2Th2', 'NdSi2Th2']
[20]:
# Filter by elements matching
Formula.filter_by_elements_matching(["NdSi2", "NdSi", "NdSi2Th2", "NdSi2Th2", "ThOs"], ["Nd", "Si"])
[20]:
['NdSi2', 'NdSi']