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

ENH: Add copy() method to ArrayProxy #1255

Merged
merged 3 commits into from
Dec 6, 2023

Conversation

effigies
Copy link
Member

Small addition to ArrayProxy class to allow copying. Used in #1090, but not central to it.

@codecov
Copy link

codecov bot commented Sep 19, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (41e6dad) 92.24% compared to head (1c1845f) 92.24%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1255   +/-   ##
=======================================
  Coverage   92.24%   92.24%           
=======================================
  Files          99       99           
  Lines       12452    12460    +8     
  Branches     2560     2561    +1     
=======================================
+ Hits        11486    11494    +8     
  Misses        643      643           
  Partials      323      323           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@effigies
Copy link
Member Author

@pauldmccarthy This is bound to interact with indexed gzip. If you have time for a quick read-through, I would appreciate your thoughts.

@effigies effigies mentioned this pull request Dec 3, 2023
15 tasks
@pauldmccarthy
Copy link
Contributor

pauldmccarthy commented Dec 6, 2023

Hi @effigies, apologies for the delay - I missed this mention.

On the surface, I think this looks fine. In the typical case where an ArrayProxy is created with a file path, and keep_file_open=True (rules 2 and 4 in the _should_keep_file_open docs), the two ArrayProxy instances will create independent ImageOpener instances and in turn independent IndexedGzipFile instances, so they will have nothing to do with each other.

In the less common case where an ArrayProxy is created with an IndexedGzipFile instance (rules 1 and 3 in the _should_keep_file_open docs), I think your lock sharing logic is a good idea.

If you like, you could add another (very rudimentary) test with something like this:

def test_copy_with_indexed_gzip_handle():

    import nibabel as nib

    indexed_gzip = pytest.importorskip('indexed_gzip')

    with InTemporaryDirectory():

        # ~24MB
        spec = ((50, 50, 50, 50), np.float32, 352, 1, 0)
        data = np.arange(np.prod(spec[0]), dtype=np.float32).reshape(spec[0])
        nib.Nifti1Image(data, np.eye(4)).to_filename('image.nii.gz')

        with indexed_gzip.IndexedGzipFile('image.nii.gz') as gzf:
            ap1 = nib.arrayproxy.ArrayProxy(gzf, spec)
            ap2 = ap1.copy()
            assert ap1.file_like is gzf
            assert ap2.file_like is gzf
            assert (ap1[ 0,  0,  0, :] == ap2[ 0,  0,  0, :]).all()
            assert (ap1[-1, -1, -1, :] == ap2[-1, -1, -1, :]).all()

@effigies
Copy link
Member Author

effigies commented Dec 6, 2023

Thanks for the review and test! If you think of a more strenuous test or something we can do upstream, happy to discuss more.

@effigies
Copy link
Member Author

effigies commented Dec 6, 2023

Test failure unrelated.

@effigies effigies merged commit 60c3afb into nipy:master Dec 6, 2023
51 of 52 checks passed
@effigies effigies deleted the enh/copyarrayproxy branch December 6, 2023 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants