MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
NpyFormat.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "TensorReader.hpp"
4#include "TensorWriter.hpp"
5
6#include <fstream>
7
8namespace MayaFlux::IO {
9
10/**
11 * @class NpyReader
12 * @brief Reader for a single NumPy .npy array, format versions 1 to 3.
13 *
14 * The array becomes one tensor named after the file stem. Fortran-ordered
15 * files keep their data in place and are described with strides. Either byte
16 * order is accepted and converted to the host's. Object, string, datetime,
17 * structured and extended-precision types are refused with a reason.
18 *
19 * Usage:
20 * @code
21 * NpyReader reader;
22 * reader.open("frames.npy");
23 * auto frames = reader.read("frames");
24 * @endcode
25 */
26class MAYAFLUX_API NpyReader : public TensorReader {
27public:
28 NpyReader() = default;
29 NpyReader(const NpyReader&) = delete;
30 NpyReader& operator=(const NpyReader&) = delete;
31 ~NpyReader() override { close(); }
32
33 /** @brief Register the format, with its writer, in the TensorFormatRegistry. */
34 static void register_with_registry();
35
36 /** @brief Whether the leading bytes are the numpy magic string. */
37 [[nodiscard]] static bool probe(std::span<const std::byte> head, uint64_t file_size);
38
39 [[nodiscard]] bool can_read(const std::string& filepath) const override;
40 bool open(const std::string& filepath, FileReadOptions options = FileReadOptions::ALL) override;
41 void close() override;
42 [[nodiscard]] bool is_open() const override { return m_open; }
43 [[nodiscard]] std::vector<std::string> get_supported_extensions() const override { return { "npy" }; }
44
45 [[nodiscard]] std::string format_name() const override { return "npy"; }
46 [[nodiscard]] std::vector<Kakshya::TensorSpec> describe() const override;
47 [[nodiscard]] std::optional<Kakshya::TensorData> read(std::string_view name) override;
48
49private:
50 std::ifstream m_file;
52 size_t m_data_offset {};
53 bool m_big_endian {};
54 bool m_open {};
55};
56
57/**
58 * @class NpyWriter
59 * @brief Writer for a single .npy array in dense C order.
60 *
61 * Takes a set holding exactly one tensor. Element types numpy has no name for,
62 * such as bfloat16 and float8, are refused.
63 */
64class MAYAFLUX_API NpyWriter : public TensorWriter {
65public:
66 bool write(const std::string& filepath, const Kakshya::TensorSet& tensors) override;
67 [[nodiscard]] std::string get_last_error() const override { return m_last_error; }
68 [[nodiscard]] std::vector<std::string> get_supported_extensions() const override { return { "npy" }; }
69
70private:
71 std::string m_last_error;
72};
73
74}
NpyReader & operator=(const NpyReader &)=delete
Kakshya::TensorSpec m_spec
Definition NpyFormat.hpp:51
NpyReader(const NpyReader &)=delete
bool is_open() const override
Check if a file is currently open.
Definition NpyFormat.hpp:42
void close() override
Close the currently open file.
Definition NpyFormat.cpp:39
std::string format_name() const override
Format identifier such as safetensors or npy.
Definition NpyFormat.hpp:45
std::vector< std::string > get_supported_extensions() const override
Get supported file extensions for this reader.
Definition NpyFormat.hpp:43
std::vector< std::string > get_supported_extensions() const override
Extensions this format writes, without dots.
Definition NpyFormat.hpp:68
bool write(const std::string &filepath, const Kakshya::TensorSet &tensors) override
Write every tensor in the set to a file.
std::string get_last_error() const override
Reason the last write failed, or empty.
Definition NpyFormat.hpp:67
Writer for a single .npy array in dense C order.
Definition NpyFormat.hpp:64
Typed FileReader boundary for formats that store named tensors.
Single-shot writer of a TensorSet to one file format.
FileReadOptions
Generic options for file reading behavior.
@ ALL
All options enabled.
Named tensors that belong together, such as a model's weights or I/O.
Definition Tensor.hpp:167
Complete description of a tensor: type, shape, layout, meaning.