If I have a structured or a regular array, is the use of strides in the following always correct for the length of the row memory?
I would like to do tostring() but on each row, by creating a string view of the memory in a 1d array.
Thanks,
Josef
tmp = np.random.randn(4,3) tmp.ravel().view('S'+str(tmp.strides[0]))
array(['j\x94gv\xa5\x80\xe6?=\xea\xa3\xcb\xb9W\x05@4.\xa2J3\xe2\xee?', '\xe3\x89\x973My\xf7\xbf\xc1\x17\x0f\xff\xe9\x19\xb8\xbf\xdb?\x00\xc9c\xf0\xf9?', '\x1f\xc3,B\x9dQ\xa1?F\x1e\x12\x0f\x02\xfc\xd4\xbfz\xe0\xa5_G.\xd0?', '$#T\x0e\xad\x85\xfb\xbf\xf3S\xa6`\x89\x87\xdc?7]\xd9lt\xb4\xf4?'], dtype='|S24')
tmp.tostring()
'j\x94gv\xa5\x80\xe6?=\xea\xa3\xcb\xb9W\x05@4.\xa2J3\xe2\xee?\xe3\x89\x973My\xf7\xbf\xc1\x17\x0f\xff\xe9\x19\xb8\xbf\xdb?\x00\xc9c\xf0\xf9?\x1f\xc3,B\x9dQ\xa1?F\x1e\x12\x0f\x02\xfc\xd4\xbfz\xe0\xa5_G.\xd0?$#T\x0e\xad\x85\xfb\xbf\xf3S\xa6`\x89\x87\xdc?7]\xd9lt\xb4\xf4?'
tmp
array([(4.0, 0, 1), (1.0, 1, 3), (2.0, 2, 4), (4.0, 0, 1)], dtype=[('f0', '<f8'), ('f1', '<i4'), ('f2', '<i4')])
tmp.view('S'+str(tmp.strides[0]))
array(['\x00\x00\x00\x00\x00\x00\x10@\x00\x00\x00\x00\x01', '\x00\x00\x00\x00\x00\x00\xf0?\x01\x00\x00\x00\x03', '\x00\x00\x00\x00\x00\x00\x00@\x02\x00\x00\x00\x04', '\x00\x00\x00\x00\x00\x00\x10@\x00\x00\x00\x00\x01'], dtype='|S16')
josef.pktd@gmail.com wrote:
If I have a structured or a regular array, is the use of strides in the following always correct for the length of the row memory?
I would like to do tostring() but on each row, by creating a string view of the memory in a 1d array.
Maybe I'm missing what you want, but why not just:
In [15]: tmp Out[15]: array([[ 1.07810097, -1.74157351, 0.29740878], [-0.16786436, 0.45752272, -0.8038045 ], [-0.17195028, -1.16753882, 0.04329128], [ 0.45460137, -0.44584955, -0.77140505]])
In [16]: rows = []
In [17]: for r in range(tmp.shape[0]): rows.append(tmp[r,:].tostring()) ....:
In [19]: rows Out[19]: ['?\xf1?\xe6\xce\x1f9\xce\xbf\xfb\xdd|.\xc85Z?\xd3\x08\xbe\xd6\xb7\xb6\xe8', '\xbf\xc5|\x94Sx\x92\x18?\xddH\r\T\xfbT\xbf\xe9\xb8\xc45\xff\x92\xdf', '\xbf\xc6\x02w\x82\x18i\xaf\xbf\xf2\xae=/\xfe\xff\x0b?\xa6*FD\xae\xd1F',
'?\xdd\x180Z\xcet\xa5\xbf\xdc\x88\xcc\x8a\x8c\x8b\xe7\xbf\xe8\xafY\xa2\xf8\xac ']
in general, you can let numpy worry about the strides, etc.
-Chris
On Tue, Oct 6, 2009 at 4:39 PM, Christopher Barker Chris.Barker@noaa.gov wrote:
josef.pktd@gmail.com wrote:
If I have a structured or a regular array, is the use of strides in the following always correct for the length of the row memory?
I would like to do tostring() but on each row, by creating a string view of the memory in a 1d array.
Maybe I'm missing what you want, but why not just:
In [15]: tmp Out[15]: array([[ 1.07810097, -1.74157351, 0.29740878], [-0.16786436, 0.45752272, -0.8038045 ], [-0.17195028, -1.16753882, 0.04329128], [ 0.45460137, -0.44584955, -0.77140505]])
In [16]: rows = []
In [17]: for r in range(tmp.shape[0]): rows.append(tmp[r,:].tostring()) ....:
In [19]: rows Out[19]: ['?\xf1?\xe6\xce\x1f9\xce\xbf\xfb\xdd|.\xc85Z?\xd3\x08\xbe\xd6\xb7\xb6\xe8', '\xbf\xc5|\x94Sx\x92\x18?\xddH\r\T\xfbT\xbf\xe9\xb8\xc45\xff\x92\xdf', '\xbf\xc6\x02w\x82\x18i\xaf\xbf\xf2\xae=/\xfe\xff\x0b?\xa6*FD\xae\xd1F',
'?\xdd\x180Z\xcet\xa5\xbf\xdc\x88\xcc\x8a\x8c\x8b\xe7\xbf\xe8\xafY\xa2\xf8\xac ']
in general, you can let numpy worry about the strides, etc.
I wanted to avoid the python loop and thought creating the view will be faster with large arrays. But for this I need to know the memory length of a row of arbitrary types for the conversion to strings, strides was the only thing I could think of.
-Chris
-- Christopher Barker, Ph.D. Oceanographer
Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker@noaa.gov _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
josef.pktd@gmail.com wrote:
I wanted to avoid the python loop and thought creating the view will be faster with large arrays. But for this I need to know the memory length of a row of arbitrary types for the conversion to strings,
ndarray.itemsize
might do it.
-Chris
On Wed, Oct 7, 2009 at 1:20 PM, Christopher Barker Chris.Barker@noaa.gov wrote:
josef.pktd@gmail.com wrote:
I wanted to avoid the python loop and thought creating the view will be faster with large arrays. But for this I need to know the memory length of a row of arbitrary types for the conversion to strings,
ndarray.itemsize
might do it.
-Chris
Thanks, (I forgot to reply), it works and feels less low level than strides.
Josef
tmps2[0].itemsize * np.size(tmps2[0])
16
tmp[0].itemsize * np.size(tmp[0])
24
tmps2.strides[0]
16
tmp.strides[0]
24
tmp
array([[-1.414, -1.019, -1.171], [-1.273, 1.639, -0.854], [-1.795, -0.699, 0.595], [-0.865, -1.439, -0.275]])
tmps2
array([(4.0, 0, 1), (1.0, 1, 3), (2.0, 2, 4), (4.0, 0, 1)], dtype=[('f0', '<f8'), ('f1', '<i4'), ('f2', '<i4')])
-- Christopher Barker, Ph.D. Oceanographer
Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker@noaa.gov _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion