kshedden / gonpy

Read and write Numpy binary files (.npy files) in Golang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to read file into 3d slice

garipovazamat opened this issue · comments

Hello! Thank you for this package. I have problem with multidimentional arrays. How can I get slice [][][]float32 if I have array with shape [51, 9, 9] in my .npy file?

Hello! You can do it like this:

np, err := gonpy.NewFileReader("3.npy")
if err != nil {
	log.Fatal(err)
}

arr := make([][][]float32, np.Shape[0])
for i := range arr {
	arr[i] = make([][]float32, np.Shape[1])
	for j := range arr[i] {
		arr[i][j] = make([]float32, np.Shape[2])
	}
}

numbers, err := np.GetFloat32()
if err != nil {
	log.Fatal(err)
}

for i, e := range numbers {
	z := i / (np.Shape[1] * np.Shape[2])
	y := (i - z*(np.Shape[1]*np.Shape[2])) / np.Shape[2]
	x := i % np.Shape[2]
	arr[z][y][x] = e
}

Or like this:

np, err := gonpy.NewFileReader("3.npy")
if err != nil {
	log.Fatal(err)
}

arr := make([][][]float32, np.Shape[0])
for i := range arr {
	arr[i] = make([][]float32, np.Shape[1])
	for j := range arr[i] {
		arr[i][j] = make([]float32, np.Shape[2])
	}
}

numbers, err := np.GetFloat32()
if err != nil {
	log.Fatal(err)
}

x, y, z := 0, 0, 0
for _, e := range numbers {
	arr[z][y][x] = e
	x++
	if x >= np.Shape[2] {
		y++
		x = 0
		if y >= np.Shape[1] {
			z++
			y = 0
		}
	}
}

I made more efficient algorithm.

np, err := gonpy.NewFileReader(filepath)
if err != nil {
	log.Fatal(err)
}

numbers, err := np.GetFloat32()
if err != nil {
	log.Fatal(err)
}

arr := make([][][]float32, 0, np.Shape[0])
matrix := make([][]float32, 0, np.Shape[1])

// reshape
for i := 0; i < len(numbers); i += np.Shape[2] {
	matrix = append(matrix, numbers[i:i+np.Shape[2]])
	if len(matrix) == cap(matrix) {
		arr = append(arr, matrix)
		matrix = make([][]float32, 0, np.Shape[1])
	}
}