Export and Plotting
Export
Array to data frame
FlodymArray objects have a to_df() method, which exports them to a pandas DataFrame:
[54]:
from flodym.example_objects import get_example_array
array = get_example_array()
df = array.to_df()
df.head()
[54]:
| value | ||
|---|---|---|
| Time | Material | |
| 1980 | Fe | 0.759354 |
| Cu | 0.667689 | |
| Mn | 0.598759 | |
| 1981 | Fe | 0.929487 |
| Cu | 0.527619 |
MFA system to dictionary and pickle file
The easiest way to store an MFA object is to pickle it (see the pickle documentation for details). However, whoever loads it from the pickle file needs flodym to read it.
flodym has a method to convert the MFASystem object to a nested dictionary, which does not need flodym. There are two data types the array values can be converted to: numpy arrays and pandas DataFrames. Let’s try both:
[55]:
from flodym.export import convert_to_dict
from flodym.example_objects import get_example_mfa
# printing function
def print_types(dict_in):
first_items = {
k: next(iter(v.values())) if isinstance(v, dict) else v[0] for k, v in dict_in.items()
}
type_strings = [f" {k}: {type(v)}" for k, v in first_items.items()]
print("Keys and data types:")
print("\n".join(type_strings))
mfa = get_example_mfa()
mfa.compute()
print("Pandas:")
df_dict = convert_to_dict(mfa, type="pandas")
print_types(df_dict)
print()
print("Numpy:")
np_dict = convert_to_dict(mfa, type="numpy")
print_types(np_dict)
Pandas:
Keys and data types:
dimension_names: <class 'str'>
dimension_items: <class 'list'>
processes: <class 'str'>
flows: <class 'pandas.core.frame.DataFrame'>
flow_dimensions: <class 'tuple'>
flow_processes: <class 'tuple'>
stocks: <class 'pandas.core.frame.DataFrame'>
stock_dimensions: <class 'tuple'>
stock_processes: <class 'str'>
Numpy:
Keys and data types:
dimension_names: <class 'str'>
dimension_items: <class 'list'>
processes: <class 'str'>
flows: <class 'numpy.ndarray'>
flow_dimensions: <class 'tuple'>
flow_processes: <class 'tuple'>
stocks: <class 'numpy.ndarray'>
stock_dimensions: <class 'tuple'>
stock_processes: <class 'str'>
Note that parameters are not included, as they are normally not computed within the MFA.
You can write this dictionary to a pickle file with the following command. (You can also pickle the mfa directly, of course, but we haven’t included that - it’s just one line of code. Refer to the pickle documentation for more information.)
[56]:
# turn on logging
import logging
logging.basicConfig(level=logging.INFO)
from flodym.export import export_mfa_to_pickle
export_path = "output_data/mfa.pickle"
export_mfa_to_pickle(mfa=mfa, export_path=export_path)
INFO:root:Data saved to output_data/mfa.pickle
To CSV
flodym has functions to export MFA flows and stocks to csv files. Let’s start with flows:
[57]:
import os
from flodym.export import export_mfa_flows_to_csv, export_mfa_stocks_to_csv
export_dir = "output_data/flows"
export_mfa_flows_to_csv(mfa, export_directory=export_dir)
# print files list
print("\nFiles in folder:\n")
print("\n".join(os.listdir(export_dir)))
# print header of first file
print("\nFirst file Header:\n")
with open(os.path.join(export_dir, os.listdir(export_dir)[0])) as input_file:
head = [next(input_file) for _ in range(5)]
print("".join(head))
INFO:root:Data saved in directory output_data/flows
Files in folder:
demolition__landfills.csv
demolition__remelting.csv
remelting__slag_piles.csv
remelting__sysenv.csv
shredder__remelting.csv
shredder__sysenv.csv
sysenv__demolition.csv
sysenv__shredder.csv
First file Header:
Time,Material,value
1980,Fe,5.700000000000005
1980,Cu,5.292
1980,Mn,0.030000000000000027
1981,Fe,6.080000000000005
As you can see, the flow names are modified to make for valid file names.
The same can be done for stocks:
[58]:
import os
from flodym.export import export_mfa_stocks_to_csv
export_dir = "output_data/stocks"
export_mfa_stocks_to_csv(mfa, export_directory=export_dir, with_in_and_out=True)
# print files list
print("\nFiles in folder:\n")
print("\n".join(os.listdir(export_dir)))
INFO:root:Data saved in directory output_data/stocks
Files in folder:
landfills_inflow.csv
landfills_outflow.csv
landfills_stock.csv
slag_piles_inflow.csv
slag_piles_outflow.csv
slag_piles_stock.csv
If inflow and outflow printing is switched on, 3 files are created per stock.
Sankey plotting
flodym can print Sankey plots of mfa systems:
[59]:
from flodym.export import PlotlySankeyPlotter
from plotly.colors import qualitative
# set up a dictionary of how to color the flows
colors = {"default": "gray"}
colorful_flows = [f for f in mfa.flows.values() if "Material" in f.dims.names]
colors.update({f.name: ("Material", qualitative.Dark24) for f in colorful_flows})
# plot
plotter = PlotlySankeyPlotter(
mfa=mfa, split_flows_by="Material", slice_dict={"t": 2000}, flow_color_dict=colors
)
fig = plotter.plot()
fig.show(renderer="notebook")
This isn’t the best-suited example for Sankey plotting, and the plotly customizability is limited, but it gives you a first impression of the flows.
Plotting yourself
Obviously, you can always export a FlodymArray to a DataFrame and use your own plotly routine, or use the numpy array in the values attribute. This is done in the examples.
Array plotting using ArrayPlotter
flodym has some implemented functionality that is especially suited for plotting 3-dimensional data. It can be handy if you want a quick impression of three-dimensional data (which is then differentiated by x-axis index, subplot, and lines). Two versions exist: One for pyplot, and one for plotly.
Example 2 shows how to use it. Refer to the API reference for details.