0

I am trying to to grow an array/matrix with each iteration within a for loop. Following is my code

import numpy as np


sino = []; 
for n in range(0, 4):
    fileName = 'D:\zDeflectionBP\data\headData_zdef\COSWeighted_trunk_' + str(n) + '.bin'
    f = open(fileName, "rb")
    data = np.fromfile(f, np.float32)
    sino = np.append(sino, data)
f.close()

fileName = 'D:\zDeflectionBP\data\headData_zdef\Head_FFS_COSWeighted.bin'
f = open(fileName, "wb")
f.write(bytes(sino))
f.close()

Each iteration the data is loaded witThere four

However, in the end, I found the size (in terms of number of bytes) of sino is twice as it should be.

For example: Each size of data: 3MB then, since I have four data, the size of the sino should be: 3MB X 4 = 12MB. But I found the size of the size is 24MB.

What is happening here? I'd like for sino to be only 12MB, which only contains data from the four data variable. How should I do it? Thanks.

Nick X Tsui
  • 2,737
  • 6
  • 39
  • 73
  • Please, stick with list append! Hardly anyone uses `np.append` correctly. If you must use `np.concatenate`. That way you are forced to pay close attention to the dimensions of the arrays that you join. – hpaulj Feb 22 '18 at 20:03
  • @hpaulj: I am sorry I don't quite understand. Should I use or not use 'np.append()', or should I use 'np.concatenate()' instead? – Nick X Tsui Feb 22 '18 at 20:11
  • Other recent questions where `np.append` gives problems: https://stackoverflow.com/questions/48913179/numpy-append-3d-vectors-without-flattening, https://stackoverflow.com/questions/48900858/issue-when-extending-a-matrix-like-numpy-array-by-adding-extra-list, https://stackoverflow.com/questions/48847798/numpy-array-append-is-not-working – hpaulj Feb 22 '18 at 20:11
  • @hpaulj: you mean I should use np.concatenate() instead? – Nick X Tsui Feb 22 '18 at 20:13

1 Answers1

2

Your sino isn't a numpy array initially but a Python list.

Numpy converts it to a 64 bit array the first time by default on a 64 bit installation, after that it stays that way, twice as large as you expected.

All the times you append data it's converted to 64 bit, since that's the format of the target.

Make sino a np.float32 array right from the start to solve the problem.

Jacques de Hooge
  • 6,750
  • 2
  • 28
  • 45