Skip to content

Commit

Permalink
Added util functions for writing csv files and creating dataframes of…
Browse files Browse the repository at this point in the history
… alloys and properties
  • Loading branch information
Robert-Forrest committed Nov 7, 2022
1 parent 1b8f8fb commit 3624b91
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions metallurgy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
characteristics based on the linear mixture of elemental properties.
"""

from typing import Union, Optional, List

import pandas as pd
from elementy import PeriodicTable

from .alloy import Alloy
Expand Down Expand Up @@ -50,6 +53,91 @@ def get_model():
return model


def to_dataframe(
alloys: Union[list, pd.DataFrame],
data: Optional[dict] = None,
properties: Optional[List[str]] = None,
) -> pd.DataFrame:
"""Convert a list of alloys to a Pandas DataFrame
:group: alloy.utils
Parameters
----------
alloys
List of alloys to create a DataFrame
data
Dictionary with keys being property names and values being lists of
property values
properties
List of properties to calculate for the alloys.
"""

if data is not None and properties is not None:
raise ValueError("Cannot set both data and properties arguments.")

if not isinstance(alloys, pd.DataFrame):
alloys_dataframe = pd.DataFrame.from_records(
[alloy.to_dict() for alloy in alloys]
)
else:
alloys_dataframe = alloys.copy()

if data is not None:
alloys_dataframe = pd.concat(
[alloys_dataframe, pd.DataFrame.from_dict(data)],
axis=1,
)
elif properties is not None:
property_values = {}
for p in properties:
property_values[p] = calculate(alloys, p)

alloys_dataframe = pd.concat(
[alloys_dataframe, pd.DataFrame.from_dict(property_values)],
axis=1,
)

return alloys_dataframe


def write_csv(
alloys: Union[list, pd.DataFrame],
data: Optional[dict] = None,
properties: Optional[List[str]] = None,
filename: str = "alloys",
):
"""Creates a CSV file of alloy data.
:group: alloy.utils
Parameters
----------
alloys
Dataset of alloys to create a CSV file.
data
Dictionary with keys being property names and values being lists of
property values
properties
List of properties to calculate for the alloys.
filename
Name of the CSV file (will be suffixed with .csv).
"""

if data is not None and properties is not None:
raise ValueError("Cannot set both data and properties arguments.")

if data is not None:
to_dataframe(alloys, data=data).to_csv(filename + ".csv", index=False)

elif properties is not None:
to_dataframe(alloys, properties=properties).to_csv(
filename + ".csv", index=False
)


__all__ = [
"periodic_table",
"Alloy",
Expand All @@ -75,4 +163,15 @@ def get_model():
"get_model",
"get_property_function",
"get_all_properties",
"write_csv",
]


# def setup_alloy_properties():
# props = get_all_properties()
# for p in props:
# if not hasattr(Alloy, p):
# setattr(Alloy, p, property(lambda self: calculate(self, p)))


# setup_alloy_properties()

0 comments on commit 3624b91

Please sign in to comment.