Skip to content

Commit

Permalink
Make ArrayWrapper pickleable (#425)
Browse files Browse the repository at this point in the history
* make array wrapper pickleable

* fix requirements.txt

* whitespace
  • Loading branch information
alimanfoo authored Sep 16, 2024
1 parent 7a5b8f9 commit 0593694
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
6 changes: 6 additions & 0 deletions allel/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ def __init__(self, data):
raise TypeError('values must be array-like')
self._values = data

def __getstate__(self):
return self._values

def __setstate__(self, state):
self._values = state

@property
def values(self):
"""The underlying array of values.
Expand Down
24 changes: 23 additions & 1 deletion allel/test/model/ndarray/test_arrays.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# -*- coding: utf-8 -*-

# stdlib imports
import pickle
import unittest

# third-party imports
import numpy as np
import unittest
import pytest


Expand Down Expand Up @@ -140,6 +144,12 @@ def test_no_subclass(self):
self.assertNotIsInstance(g.T, GenotypeArray)
self.assertNotIsInstance(g.astype('f4'), GenotypeArray)

def test_pickle(self):
g = self.setup_instance(diploid_genotype_data)
g2 = pickle.loads(pickle.dumps(g))
assert isinstance(g2, GenotypeArray)
aeq(g, g2)


# noinspection PyMethodMayBeStatic
class HaplotypeArrayTests(HaplotypeArrayInterface, unittest.TestCase):
Expand Down Expand Up @@ -208,6 +218,12 @@ def test_slice_types(self):
assert isinstance(s, np.int8)
assert not isinstance(s, HaplotypeArray)

def test_pickle(self):
h = self.setup_instance(haplotype_data)
h2 = pickle.loads(pickle.dumps(h))
assert isinstance(h2, HaplotypeArray)
aeq(h, h2)


# noinspection PyMethodMayBeStatic
class AlleleCountsArrayTests(AlleleCountsArrayInterface, unittest.TestCase):
Expand Down Expand Up @@ -283,6 +299,12 @@ def test_reduce_types(self):
x = getattr(ac, m)(axis=1)
assert isinstance(x, np.ndarray)

def test_pickle(self):
ac = self.setup_instance(allele_counts_data)
ac2 = pickle.loads(pickle.dumps(ac))
assert isinstance(ac2, AlleleCountsArray)
aeq(ac, ac2)


# noinspection PyMethodMayBeStatic
class GenotypeVectorTests(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ scipy
matplotlib
seaborn
pandas
pomegranate
protopunica
scikit-learn
h5py
numexpr
Expand Down

0 comments on commit 0593694

Please sign in to comment.