-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_all_elements.py
41 lines (37 loc) · 1.19 KB
/
list_all_elements.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from matminer.featurizers import composition as cf
from matminer.utils.conversions import str_to_composition
import numpy as np
import pandas as pd
import csv
import itertools
from matminer.featurizers import composition as cf
from matminer.utils.conversions import str_to_composition
from pymatgen import Composition
from pymatgen.core.periodic_table import Element
# Read in dataset
filepath = "bmg_trg_nologan.csv"
glass_data = pd.read_csv(filepath)
glass_data = glass_data['formula']
# Convert compositions to pymatgen objects.
comps = str_to_composition(glass_data)
# Loop through all elements and list the ones that come up.
# Also keep track fo how many elements there are of each.
all_elements = []
for c in comps:
comp_contains = c.as_dict().keys()
for e in comp_contains:
if e not in all_elements:
all_elements.append(e)
with open('element_analysis.csv', 'w', newline='') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(all_elements)
# Go through every composition in the dataset
for c in comps:
comp_contains = c.as_dict().keys()
contains = []
for a in all_elements:
if a in comp_contains:
contains.append(1)
else:
contains.append(0)
writer.writerow(contains)