Typorithm · medium

Numerical files and verified readback

pythonnumpynumerical-computingindependent-practice
Saved on this device
Full lesson · 4 learning steps Open any exercise for focused practice
1

Step 1 of 4 · intro card

Numerical files and verified readback

0%

Concept and contract

Persist and reload a numeric array in an owned temporary directory with an explicit non-pickle boundary.

A numerical file is a serialization boundary. A saved .npy array records dtype and shape as well as element values, unlike a display string that may omit metadata or round numbers. The worked example owns a temporary directory, saves one finite float64 matrix, loads it back, checks its metadata and returns an independent array. No user-supplied path, remote download or production data is involved.

Pickled object arrays are a different and potentially executable representation. This numeric-only contract explicitly uses allow_pickle=False. That option is one boundary, not a claim that arbitrary files are harmless or that size limits and source trust no longer matter. The lab uses only its own tiny generated arrays.

Mechanism and worked example

The function constructs and validates the matrix before writing.

The function constructs and validates the matrix before writing. Within TemporaryDirectory it chooses a fixed filename, calls np.save and np.load with non-pickle settings, and compares the loaded dtype and shape with the expected contract. The returned copy survives after the directory is cleaned up. The lab observes the actual load/save options as well as the returned numerical result.

This is genuine file I/O in a computer lab, not a simulated success message in the reader. Closing the file and releasing the temporary directory are part of the resource lifetime. A memory-mapped array would introduce a different lifetime and platform behavior; it should not be returned from this example without additional ownership decisions.

Complete worked example

python

from pathlib import Path
from tempfile import TemporaryDirectory
import numpy as np

def inspect_example(rows):
    data = np.asarray(rows, dtype=np.float64)
    if data.ndim != 2 or 0 in data.shape or not np.isfinite(data).all():
        raise ValueError("expected a non-empty finite matrix")
    with TemporaryDirectory(prefix="numpy-learning-") as directory:
        path = Path(directory) / "measurements.npy"
        np.save(path, data, allow_pickle=False)
        loaded = np.load(path, allow_pickle=False)
        if loaded.dtype != np.dtype("float64") or loaded.shape != data.shape:
            raise ValueError("readback metadata changed")
        return loaded.copy()
Reasoning
The function constructs and validates the matrix before writing. Within TemporaryDirectory it chooses a fixed filename, calls np.save and np.load with non-pickle settings, and compares the loaded dtype and shape with the expected contract. The returned copy survives after the directory is cleaned up. The lab observes the actual load/save options as well as the returned numerical result. This is genuine file I/O in a computer lab, not a simulated success message in the reader. Closing the file and releasing the temporary directory are part of the resource lifetime. A memory-mapped array would introduce a different lifetime and platform behavior; it should not be returned from this example without additional ownership decisions.
Open in local Python playground ↗

Focused decision: context is in the complete example

python

np.save(path, data, allow_pickle=False)
loaded = np.load(path, allow_pickle=False)
Focused decision
Type this short numerical decision after reading its complete context. The complete imports, array construction, validation, and result checks remain in the worked example. Typing records practice; only the downloadable computer lab executes Python.

Counterexamples and common mistakes

The controlled mistake enables pickle when loading even though the interface promises numeric-only deserialization.

The controlled mistake enables pickle when loading even though the interface promises numeric-only deserialization. Tiny numerical fixtures might still return the same values, so output-only assertions would miss that changed boundary. The lab therefore checks the actual option passed to load. This demonstrates testing a configuration contract without executing a malicious payload or touching another file.

Another mistake is verifying only that save returned without an exception. Readback can reveal an unexpected shape, dtype or path. A production archive reader also needs bounded input and decompressed-size handling; this tiny owned .npy example is intentionally not advertised as a hardened arbitrary-archive parser.

Controlled mistake: inspect the failing numerical contract

python

from pathlib import Path
from tempfile import TemporaryDirectory
import numpy as np

def inspect_example(rows):
    data = np.asarray(rows, dtype=np.float64)
    if data.ndim != 2 or 0 in data.shape or not np.isfinite(data).all():
        raise ValueError("expected a non-empty finite matrix")
    with TemporaryDirectory(prefix="numpy-learning-") as directory:
        path = Path(directory) / "measurements.npy"
        np.save(path, data, allow_pickle=False)
        loaded = np.load(path, allow_pickle=True)
        if loaded.dtype != np.dtype("float64") or loaded.shape != data.shape:
            raise ValueError("readback metadata changed")
        return loaded.copy()
Reasoning
The controlled mistake enables pickle when loading even though the interface promises numeric-only deserialization. Tiny numerical fixtures might still return the same values, so output-only assertions would miss that changed boundary. The lab therefore checks the actual option passed to load. This demonstrates testing a configuration contract without executing a malicious payload or touching another file. Another mistake is verifying only that save returned without an exception. Readback can reveal an unexpected shape, dtype or path. A production archive reader also needs bounded input and decompressed-size handling; this tiny owned .npy example is intentionally not advertised as a hardened arbitrary-archive parser.
Open in local Python playground ↗

Independent practice

Keep the same numerical save/load path, but return one row containing the loaded column totals.

Keep the same numerical save/load path, but return one row containing the loaded column totals. Compute the report from loaded, not from the original input variable, so the function continues to exercise readback. Preserve the non-pickle option and the finite matrix checks.

Predict a two-row matrix whose column totals differ from its row totals. The additional calculation is one reduction with keepdims; it should not alter the saved array or change the directory cleanup. The task connects a new I/O boundary to the reduction contract you already practiced.

Step 1: Return column totals computed from the actual loaded array. Step 2: Retain the working imports, input validation and array shapes. Change the stated policy, run the supplied practice cases, then explain the newly accepted or rejected boundary.

Practice starter: solve before reading the solution

python

from pathlib import Path
from tempfile import TemporaryDirectory
import numpy as np

def inspect_example(rows):
    data = np.asarray(rows, dtype=np.float64)
    if data.ndim != 2 or 0 in data.shape or not np.isfinite(data).all():
        raise ValueError("expected a non-empty finite matrix")
    with TemporaryDirectory(prefix="numpy-learning-") as directory:
        path = Path(directory) / "measurements.npy"
        np.save(path, data, allow_pickle=False)
        loaded = np.load(path, allow_pickle=False)
        if loaded.dtype != np.dtype("float64") or loaded.shape != data.shape:
            raise ValueError("readback metadata changed")
        return loaded.copy()

# Independent task: Return column totals computed from the actual loaded array.
Reasoning
Return column totals computed from the actual loaded array.
Open in local Python playground ↗
  • Hint 1: Keep both save and load calls and their options.
  • Hint 2: Reduce loaded with axis=0 and keepdims=True.

Solution, tests and trade-offs

The solution reduces loaded along axis zero and keeps a one-row result.

The solution reduces loaded along axis zero and keeps a one-row result. Numerical cases check the returned dtype, shape and sums; the I/O probe verifies that the real save and load calls still happen with allow_pickle=False. A single-row fixture confirms that reduction preserves column order even when its values happen to equal the original row.

The fixture demonstrates successful roundtrips, not atomic publication to an externally shared path. Real report publication may need a temporary file, verification and an atomic replacement strategy. Those filesystem reliability concepts belong to the earlier Python automation track and can be reused without weakening this numerical contract.

Practice solution and acceptance checks

python

from pathlib import Path
from tempfile import TemporaryDirectory
import numpy as np

def inspect_example(rows):
    data = np.asarray(rows, dtype=np.float64)
    if data.ndim != 2 or 0 in data.shape or not np.isfinite(data).all():
        raise ValueError("expected a non-empty finite matrix")
    with TemporaryDirectory(prefix="numpy-learning-") as directory:
        path = Path(directory) / "measurements.npy"
        np.save(path, data, allow_pickle=False)
        loaded = np.load(path, allow_pickle=False)
        if loaded.dtype != np.dtype("float64") or loaded.shape != data.shape:
            raise ValueError("readback metadata changed")
        return loaded.sum(axis=0, keepdims=True)
Reasoning
The solution reduces loaded along axis zero and keeps a one-row result. Numerical cases check the returned dtype, shape and sums; the I/O probe verifies that the real save and load calls still happen with allow_pickle=False. A single-row fixture confirms that reduction preserves column order even when its values happen to equal the original row. The fixture demonstrates successful roundtrips, not atomic publication to an externally shared path. Real report publication may need a temporary file, verification and an atomic replacement strategy. Those filesystem reliability concepts belong to the earlier Python automation track and can be reused without weakening this numerical contract.
Open in local Python playground ↗

Interview reasoning and next step

Explain what .npy preserves, why readback is stronger evidence than a successful write call, and why numeric data should not require pickle.

Explain what .npy preserves, why readback is stronger evidence than a successful write call, and why numeric data should not require pickle. Describe the lifetime difference between a fully loaded array and a view backed by a memory-mapped file. State what additional constraints an untrusted-file interface would need.

Type the short non-pickle save/load sequence on the PC and recall the load boundary. In the app, reason about metadata and returned values. Next, measure numerical workspace and process bounded blocks instead of assuming every vectorized expression has a small memory footprint.

Optional stretch — after the core task works: Add one independently reasoned boundary case and explain its expected value before running it.

Computer practice: https://applydjinn.com/typorithm/numpy-lab-20261106.zip. Install pinned dependencies in an isolated Python environment. Run verify.py, then change the working exercise baseline and run --practice np-files. The real NumPy arrays, numerical contracts, random state checks and temporary numeric files use owned fixtures only. No production database, feed, account or paid service is used. This lab executes Python; the PC typing trainer and native reading progress do not execute it.

Persist and reload a numeric array in an owned temporary directory with an explicit non-pickle boundary.