numpy_array.py 854 Bytes
Newer Older
1
import logging
2

3
4
import numpy as np
from numpy.lib.format import open_memmap
5

6
7
from .registry import register_array_parser

8

9
10
11
12
13
14
@register_array_parser("numpy")
class NumpyArrayParser(object):
    def __init__(self):
        pass

    def read(self, path):
15
        logging.debug("Reading from %s using numpy format" % path)
16
        arr = np.load(path, mmap_mode="r")
17
        logging.debug("Done reading from %s" % path)
18
19
20
        return arr

    def write(self, path, arr):
21
        logging.debug("Writing to %s using numpy format" % path)
22
23
        # np.save would load the entire memmap array up into CPU.  So we manually open
        # an empty npy file with memmap mode and manually flush it instead.
24
        new_arr = open_memmap(path, mode="w+", dtype=arr.dtype, shape=arr.shape)
25
        new_arr[:] = arr[:]
26
        logging.debug("Done writing to %s" % path)