Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CSV export method to Field class #379

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

MuellerSeb
Copy link
Member

closes #367

This new method csv_export allows to export all fields of a field class together with the pos tuple into a csv file.
Structured fields will be converted to unstructured to get flat arrays.

Example:

from gstools import SRF, Gaussian

x = y = range(3)
model = Gaussian(dim=2)
srf = SRF(model, seed=20170519)
srf((x, y), mesh_type="structured")
srf.csv_export("fields.csv")

Resulting file fields.csv:

x,y,field
0.0,0.0,-0.22138986032350452
0.0,1.0,-0.38477418693294124
0.0,2.0,-0.11175947311945715
1.0,0.0,-0.7538723514717599
1.0,1.0,0.3501084035555686
1.0,2.0,1.62100148329647
2.0,0.0,0.7236152951349311
2.0,1.0,-0.058499764834117715
2.0,2.0,0.3130225131979303

@MuellerSeb MuellerSeb added the enhancement New feature or request label Feb 8, 2025
@MuellerSeb MuellerSeb added this to the 1.7 milestone Feb 8, 2025
@MuellerSeb MuellerSeb requested a review from LSchueler February 8, 2025 16:02
@MuellerSeb MuellerSeb self-assigned this Feb 8, 2025
@LSchueler
Copy link
Member

Hey hey,

thank you for the initiative!
Originally, we decided against implementing import and export support, primarily because was that Python has many great tools supporting import and export. Those tools can effectively be used as pre- and postprocessing options for GSTools.
However, at least supporting rudimentary import and export methods can be very convenient. That said, everything else beyond rudimentary support would either amount in a lot of work or large dependencies like Polars or Pandas.
Additionally, CSV is a format which shouldn't receive even more standalone support than it has already, as summarized poignantly here. So, how about this compromise: We support export functionality through the main Numpy export functions, like

I think this should be easily doable, by providing another argument with which you can choose your export function/ format. I think it would even be okay for me, if CSV stays the default format.
What do you think?

@MuellerSeb
Copy link
Member Author

Friends don't let friends export to CSV

😂

This implementation is just numpy.savetxt with a header-line for the names and a "," to separate the values (hence CSV is quite a good term here).
I think it is so straight forward, that I don't mind having it and people actually asked for it: #366

For further IO stuff I would like to be able to pickle and unpickle our objects and/or use (safer) serialization approaches like json.

Should we rename it to savetxt? I just wanted to be consistent with vtk_export.

@LSchueler
Copy link
Member

Yes, I had a look at your implementation. But from a quick check, it seems that savetxt, save, ... all look very similar, they also take variable names, ...
You can even pickle your variables ;-)

I like the name of the function and I think you should keep csv_export.

Why would you want to specifically support json export? I don't think that json is a very fitting format for GSTools and would much more prefer other formats.

@MuellerSeb
Copy link
Member Author

But json for example to store CovModel configuration? I think this would be really cool.

@MuellerSeb
Copy link
Member Author

I think all the others export routines are more meant to be used for storing and loading again, since they are not human readable file formats.
I don't want to implement a "load-from-file" method at the moment and as you said, the users could use the Python eco-system to create files as they want.

@LSchueler
Copy link
Member

I think all the others export routines are more meant to be used for storing and loading again, since they are not human readable file formats. I don't want to implement a "load-from-file" method at the moment and as you said, the users could use the Python eco-system to create files as they want.

Exactly, the npy- and npz-files are binary and Numpy understands their content, making the reading much easier than CSV files. I actually use an npz-file for one of the plurigaussian examples

So, I don't really get your argument against supporting Numpy's other save functions.

@LSchueler
Copy link
Member

But json for example to store CovModel configuration? I think this would be really cool.

We should probably shift this discussion to another time ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ENH] CSV export method for Field class
2 participants