|
| 1 | +package org.jetbrains.kotlinx.multik.api.io |
| 2 | + |
| 3 | +import org.jetbrains.bio.npy.NpyArray |
| 4 | +import org.jetbrains.bio.npy.NpyFile |
| 5 | +import org.jetbrains.bio.npy.NpzFile |
| 6 | +import org.jetbrains.kotlinx.multik.api.Multik |
| 7 | +import org.jetbrains.kotlinx.multik.ndarray.data.* |
| 8 | +import java.io.File |
| 9 | +import java.nio.file.Path |
| 10 | +import kotlin.io.path.Path |
| 11 | +import kotlin.io.path.notExists |
| 12 | + |
| 13 | +public inline fun <reified T : Number, reified D : Dimension> Multik.readNPY(fileName: String): NDArray<T, D> = |
| 14 | + readNPY(Path(fileName), DataType.ofKClass(T::class), dimensionClassOf<D>()) |
| 15 | + |
| 16 | +public inline fun <reified T : Number, reified D : Dimension> Multik.readNPY(file: File): NDArray<T, D> = |
| 17 | + readNPY(file.toPath(), DataType.ofKClass(T::class), dimensionClassOf<D>()) |
| 18 | + |
| 19 | +public inline fun <reified T : Number, reified D : Dimension> Multik.readNPY(path: Path): NDArray<T, D> { |
| 20 | + if (path.notExists()) throw NoSuchFileException(path.toFile()) |
| 21 | + return readNPY(path, DataType.ofKClass(T::class), dimensionClassOf<D>()) |
| 22 | +} |
| 23 | + |
| 24 | +public fun <T : Any, D : Dimension> Multik.readNPY(path: Path, dtype: DataType, dim: D): NDArray<T, D> { |
| 25 | + if (dtype.isComplex()) throw Exception("NPY format only supports Number types") |
| 26 | + val npyArray: NpyArray = NpyFile.read(path) |
| 27 | + require(npyArray.shape.size == dim.d) { "Not match dimensions: shape of npy array = ${npyArray.shape.joinToString()}, and dimension = ${dim.d}" } |
| 28 | + |
| 29 | + val data: MemoryView<T> = when (dtype) { |
| 30 | + DataType.DoubleDataType -> MemoryViewDoubleArray(npyArray.asDoubleArray()) |
| 31 | + DataType.FloatDataType -> MemoryViewFloatArray(npyArray.asFloatArray()) |
| 32 | + DataType.IntDataType -> MemoryViewIntArray(npyArray.asIntArray()) |
| 33 | + DataType.LongDataType -> MemoryViewLongArray(npyArray.asLongArray()) |
| 34 | + DataType.ShortDataType -> MemoryViewShortArray(npyArray.asShortArray()) |
| 35 | + DataType.ByteDataType -> MemoryViewByteArray(npyArray.asByteArray()) |
| 36 | + else -> throw Exception("not supported complex arrays") |
| 37 | + } as MemoryView<T> |
| 38 | + |
| 39 | + return NDArray(data, shape = npyArray.shape, dim = dim) |
| 40 | +} |
| 41 | + |
| 42 | +public fun Multik.readNPZ(fileName: String): List<NDArray<out Number, out DimN>> = |
| 43 | + readNPZ(Path(fileName)) |
| 44 | + |
| 45 | +public fun Multik.readNPZ(file: File): List<NDArray<*, out DimN>> = |
| 46 | + readNPZ(file.toPath()) |
| 47 | + |
| 48 | +public fun Multik.readNPZ(path: Path): List<NDArray<out Number, out DimN>> { |
| 49 | + return NpzFile.read(path).use { |
| 50 | + val entries = it.introspect() |
| 51 | + entries.map { entry -> |
| 52 | + val npyArray = it[entry.name] |
| 53 | + val data = when (entry.type.kotlin) { |
| 54 | + DataType.DoubleDataType.clazz -> MemoryViewDoubleArray(npyArray.asDoubleArray()) |
| 55 | + DataType.FloatDataType.clazz -> MemoryViewFloatArray(npyArray.asFloatArray()) |
| 56 | + DataType.IntDataType.clazz -> MemoryViewIntArray(npyArray.asIntArray()) |
| 57 | + DataType.LongDataType.clazz -> MemoryViewLongArray(npyArray.asLongArray()) |
| 58 | + DataType.ShortDataType.clazz -> MemoryViewShortArray(npyArray.asShortArray()) |
| 59 | + DataType.ByteDataType.clazz -> MemoryViewByteArray(npyArray.asByteArray()) |
| 60 | + else -> TODO() |
| 61 | + } |
| 62 | + NDArray(data, shape = npyArray.shape, dim = dimensionOf(npyArray.shape.size)) |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +public fun <T : Number, D : Dimension> Multik.writeNPY(fileName: String, ndArray: NDArray<T, D>): Unit = |
| 68 | + this.writeNPY(Path(fileName), ndArray) |
| 69 | + |
| 70 | +public fun <T : Number, D : Dimension> Multik.writeNPY(file: File, ndArray: NDArray<T, D>): Unit = |
| 71 | + this.writeNPY(file.toPath(), ndArray) |
| 72 | + |
| 73 | +public fun <T : Number, D : Dimension> Multik.writeNPY(path: Path, ndArray: NDArray<T, D>): Unit { |
| 74 | + when (ndArray.dtype) { |
| 75 | + DataType.DoubleDataType -> NpyFile.write(path, ndArray.data.getDoubleArray(), ndArray.shape) |
| 76 | + DataType.FloatDataType -> NpyFile.write(path, ndArray.data.getFloatArray(), ndArray.shape) |
| 77 | + DataType.IntDataType -> NpyFile.write(path, ndArray.data.getIntArray(), ndArray.shape) |
| 78 | + DataType.LongDataType -> NpyFile.write(path, ndArray.data.getLongArray(), ndArray.shape) |
| 79 | + DataType.ShortDataType -> NpyFile.write(path, ndArray.data.getShortArray(), ndArray.shape) |
| 80 | + DataType.ByteDataType -> NpyFile.write(path, ndArray.data.getByteArray(), ndArray.shape) |
| 81 | + else -> TODO() |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +public fun Multik.writeNPZ(path: Path, vararg ndArrays: NDArray<out Number, out Dimension>): Unit = |
| 86 | + this.writeNPZ(path, ndArrays.asList()) |
| 87 | + |
| 88 | +public fun Multik.writeNPZ(path: Path, ndArrays: List<NDArray<out Number, out Dimension>>): Unit { |
| 89 | + NpzFile.write(path).use { |
| 90 | + ndArrays.forEachIndexed { ind, array -> |
| 91 | + when (array.dtype) { |
| 92 | + DataType.DoubleDataType -> it.write("arr_$ind", array.data.getDoubleArray(), array.shape) |
| 93 | + DataType.FloatDataType -> it.write("arr_$ind", array.data.getFloatArray(), array.shape) |
| 94 | + DataType.IntDataType -> it.write("arr_$ind", array.data.getIntArray(), array.shape) |
| 95 | + DataType.LongDataType -> it.write("arr_$ind", array.data.getLongArray(), array.shape) |
| 96 | + DataType.ShortDataType -> it.write("arr_$ind", array.data.getShortArray(), array.shape) |
| 97 | + DataType.ByteDataType -> it.write("arr_$ind", array.data.getByteArray(), array.shape) |
| 98 | + else -> TODO() |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments