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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃殌 Feature: Add ability to write spectrum file to a buffer instead of to disk #506

Open
suzil opened this issue Aug 16, 2022 · 5 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@suzil
Copy link
Member

suzil commented Aug 16, 2022

馃敄 Feature description

We're working on a feature for RADIS App to allow the user to download the spectrum file (arunavabasucom/radis-app#619). To do this, we do spectrum.store(file_path), load the file from disk to serve to the user, and then delete the file. If we could instead pass a buffer object to write the file to memory, we could simplify our implementation of this feature and improve performance by skipping the write to disk. The buffer would have the same interface as someone doing fp = open(file_path) and passing fp to spectrum.store(fp), so RADIS would just write the data there and the client can take care of whether it goes to disk or to an in-memory buffer.

I propose adjusting spectrum.store(file_path) to accept either a string file path (as it is now) or a file handle-like object. If passed a file handle-like object, we would skip the open(...) lines like here:

with open(fout, "wb") as f:

馃憠 Why you want this feature!!

Simplify RADIS App implementation of serving spectrum download files to users and improve performance.

@suzil suzil added the enhancement New feature or request label Aug 16, 2022
@erwanp
Copy link
Member

erwanp commented Aug 16, 2022

It seems we can simply dump into a StringIO object and Json works fine (json_tricks probably too).

So we'd replace

with open(fout, "wb") as f:

        with open(fout, "wb") as f:
            json_tricks.dump(
                sjson,
                f,
                compression=True,  # calls gzip compression
                properties={
                    "ndarray_compact": True
                },  # use compact numpy format in json-tricks 3.15+
            )

with

        import io
        response_file = io.StringIO()
        json_tricks.dump(
              sjson,
              response_file,
              compression=True,  # calls gzip compression
              properties={
                  "ndarray_compact": True
              }, 
          )

Is that it ?

@suzil
Copy link
Member Author

suzil commented Aug 16, 2022

Yup, that looks right.

@suzil
Copy link
Member Author

suzil commented Aug 16, 2022

If you're writing binary (mode is "wb"), we'd use BytesIO() instead of StringIO().

@erwanp erwanp self-assigned this Aug 16, 2022
@anandxkumar anandxkumar added this to the 0.15 milestone Oct 28, 2022
@arunavabasucom
Copy link
Collaborator

arunavabasucom commented May 30, 2023

@erwanp, pls assign me, gonna wrap this up !!

@erwanp erwanp assigned arunavabasucom and unassigned erwanp Jul 30, 2023
@erwanp
Copy link
Member

erwanp commented Jul 30, 2023

Done

@minouHub minouHub modified the milestones: 0.15, 0.16 Mar 30, 2024
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

No branches or pull requests

5 participants