Skip to content

Instantly share code, notes, and snippets.

@curegit
Created February 27, 2024 05:42
Show Gist options
  • Save curegit/1accba67564e6d5aeaacdbcff49f6dab to your computer and use it in GitHub Desktop.
Save curegit/1accba67564e6d5aeaacdbcff49f6dab to your computer and use it in GitHub Desktop.
大規模配列のファイルからの部分読み出し例
import numpy as np
large_arr = np.random.normal(size=(3, 10000, 10000)).astype("float64")
np.save("example.npy", large_arr)
import time
import random
from numpy.lib.format import open_memmap
s = time.time()
mem_array = open_memmap("example.npy", mode="r")
channels, height, width = mem_array.shape
crop_size = 32
left = random.randrange(width - crop_size)
top = random.randrange(height - crop_size)
right = left + crop_size
bottom = top + crop_size
patch = mem_array[:, top:bottom, left:right]
print(patch)
print(f"{time.time() - s} sec")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment