Storch-Numpy is a NumPy-like library implemented in Scala 3. It enables seamless interoperability with Python's NumPy, allowing users to read from and write to .npy and .npz files across both environments. The library supports common NumPy operations and is poised to expand with more features in the future.
- File I/O: Read and write
.npyand.npzfiles, ensuring compatibility with files generated by Python's NumPy. - Data Type Support: Support for a wide range of data types, including
bool,uint8,int16,float32, and more. - Array Operations: Implementation of common NumPy array operations such as broadcasting and transposing.
- Scala 3: This project is developed using Scala 3. We recommend version
3.6.4. - SBT: The project build tool. Version
1.10.7is recommended.
libraryDependencies += "io.github.mullerhai" % "storch-numpy_3" % "0.1.0"The necessary dependencies are already included in build.sbt:
import torch.numpy.serve.Numpy import java.nio.file.Paths // Read a .npy file def float64NpyFile( path: String = "D:\\data\\git\\storch-numpy\\src\\main\\scala\\com\\github\\dreamolight\\jnpy\\random_float64_array.npy" ): Unit = { val np = new Numpy(path) val data = np.float64Elements.toSeq data.foreach(d => print(s"$d \t")) } // Save a .npy file val data = Array(1.0, 2.0, 3.0) val shape = Array(3) val dataType = torch.numpy.enums.DType.Float64 val endian = torch.numpy.enums.Endian.Little val isFortranOrder = false val savePath = "new_random_float64_array.npy" Numpy.saveToNumpyFile(savePath, data, shape, dataType, endian, isFortranOrder)import java.nio.file.Paths import scala.util.Using import torch.numpy.extern.NpzFile // Write to a .npz file val booleanArray = Array(true, false, true) val npzFilePath = Paths.get("example.npz") Using.resource(NpzFile.write(npzFilePath)) { writer => writer.write("boolean_array", booleanArray, Array(1, 3)) println("all write finish .... \r\n ") } // Read from a .npz file Using.resource(NpzFile.read(Paths.get("example.npz"))) { reader => val readBooleanArray = reader("boolean_array").asBooleanArray() println(readBooleanArray.mkString(",")) }