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

Make cutout stream the result to save memory usage. #105

Open
ijiraq opened this issue May 1, 2019 · 0 comments
Open

Make cutout stream the result to save memory usage. #105

ijiraq opened this issue May 1, 2019 · 0 comments
Assignees
Labels

Comments

@ijiraq
Copy link
Collaborator

ijiraq commented May 1, 2019

Using an HDUList in this way can consume significant memory.

result_hdu_list = fits.HDUList([hdu_list[0]])

@andamian Using a Streaming HDU would help.

import numpy as np
from memory_profiler import profile



@profile
def do_cutout(f):
    fout = open('cutout.fits', 'wb')
    fits.PrimaryHDU(data=None, header=None).writeto(fout, output_verify='fix')
    fout.close()
    for i in range(1000):
        fout = open('cutout.fits', 'ab+')
        ext = np.random.choice(f[1:-1])
        x = int(np.random.random()*(ext.header['NAXIS1']) - 300)
        y = int( np.random.random()*(ext.header['NAXIS2']) - 300)
        header = ext.header
        header['NAXIS1']=300
        header['NAXIS2']=300
        shdu = fits.StreamingHDU(fout, header)
        shdu.write(ext.data[x:x+300, y:y+300])
        fout.close()

do_cutout(fits.open('1118695p.fits.fz', do_not_scale_image_data=True))

This approach uses about 100MB of RAM but does 1000 cutouts, which takes multiple GB to store.

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

No branches or pull requests

2 participants