On 26.12.2011, at 7:37PM, Fabian Dill wrote:
I have a problem with a structured numpy array. I create is like this: tiles = numpy.zeros((header["width"], header["height"],3), dtype = numpy.uint8) and later on, assignments such as this: tiles[x, y,0] = 3
Now uint8 is not sufficient anymore, but only for the first of the 3 values. uint16 for all of them would use too much ram (increase of 1-3 GB)
I have tried using structured arrays, but the dtype is essentially always a tuple.
tiles = numpy.zeros((header["width"], header["height"], 1), dtype = "u2,u1,u1")
tiles[x, y,0] = 0 TypeError: expected an object with a buffer interface
If you create a structured array, you probably don't want the third dimension, as the structure already spans three fields, and to assign to it you either need to address the fields explicitly (with the default field names 'f0', 'f1', 'f2'), or use an array with corresponding dtype:
dt = "u2,u1,u1" tiles = numpy.zeros((2,3), dtype=dt) tiles array([[(0, 0, 0), (0, 0, 0), (0, 0, 0)], [(0, 0, 0), (0, 0, 0), (0, 0, 0)]], dtype=[('f0', '<u2'), ('f1', '|u1'), ('f2', '|u1')])
tiles['f0'][0] = 1 tiles[0,1] = np.array((3,4,5), dtype=dt) tiles array([[(1, 0, 0), (3, 4, 5), (1, 0, 0)], [(0, 0, 0), (0, 0, 0), (0, 0, 0)]], dtype=[('f0', '<u2'), ('f1', '|u1'), ('f2', '|u1')])
Cheers, Derek
tiles = numpy.zeros((header["width"], header["height"]), dtype = [("0","<u2"),("1","|u1"),("2","|u1")]) tiles["0"][0,0] = 1 that does work now. however it's much slower in assigning and I cannot use integers for the first index. As it's now I might as well do: tiles = [numpy.zeros((header["width"], header["height"]), dtype = numpy.uint16), numpy.zeros((header["width"], header["height"]), dtype = numpy.uint8), numpy.zeros((header["width"], header["height"]), dtype = numpy.uint8)] Which allows tiles[0][0,0] = 1 The main problem is, that this change is in place within a larger project. I would like the new array to be usable just as the old one, with little to no change in code. Is there a way to achieve that? Or failing that, which is the most efficient way, when I have lots of single member handling, so the rewrite is worth it. ___________________________________________________________ SMS schreiben mit WEB.DE FreeMail - einfach, schnell und kostenguenstig. Jetzt gleich testen! http://f.web.de/?mc=021192
participants (2)
-
Derek Homeier
-
Fabian Dill