QSD-Group / QSDsan

Quantitative Sustainable Design (QSD) of sanitation and resource recovery systems.
https://qsdsan.com
Other
30 stars 12 forks source link

Export components? #105

Closed mckfarm closed 1 year ago

mckfarm commented 1 year ago

Hi QSDSan team! I am wondering if there is any way to export the information from a components object or compiled components into a pandas dataframe or directly into a text file. I ask because I've made a custom set of components using the qs.Components command with the built-in search_ID functionality, and I would like to avoid copying and pasting this large code block between multiple files.

I've seen in the documentation that we can load in components from a file, then compile from there, but I wasn't sure how this text file was made in the first place or if it was possible to do that process in reverse.

Thanks!

yalinli2 commented 1 year ago

Hi @mckfarm great question! The better way to save the Components objs to be used elsewhere is actually by saving/loading them as pickles (a lot of things happen when creating and compiling components, so direct reverse engineering is hard), you can use qs.utils.save_pickle and qs.utils.load_pickle instead.

To save as pickle files

>>> from qsdsan.utils import create_example_components, save_pickle
>>> cmps = create_example_components()
>>> cmps.show()
CompiledComponents([H2O, CO2, N2O, NaCl, H2SO4, CH4, Methanol, Ethanol])
>>> save_pickle(cmps, 'components') # make sure you know the saved path

Then in another kernel session, you can reload the pickle file by

>>> from qsdsan.utils import load_pickle
>>> cmps_copy = load_pickle('components')
>>> cmps_copy.show()
CompiledComponents([H2O, CO2, N2O, NaCl, H2SO4, CH4, Methanol, Ethanol])

However, when looking at your question, I realized that there seems to be a block missing on the tutorial you pointed to, and there was no documentation on those pickle functions, there was also a load_pickled_cmps function that's confusing (you can ignore this function for now).

I'll keep this issue open as a reminder to update these docs (hopefully this weekend?), thank you!

mckfarm commented 1 year ago

Works perfectly, thank you!

yalinli2 commented 1 year ago

Done & closed with 126498c