Re: [Numpy-discussion] Any help from Numpy community?
Birdada Simret birdada85 at gmail.com writes: Any help from Numpy community [[ 0. 1.54 0. 0. 0. 1.08 1.08 1.08 ] [ 1.54 0. 1.08 1.08 1.08 0. 0. 0. ] [ 0. 1.08 0. 0. 0. 0. 0. 0. ] [ 0. 1.08 0. 0. 0. 0. 0. 0. ] [ 0. 1.08 0. 0. 0. 0. 0. 0. ] [ 1.08 0. 0. 0. 0. 0. 0. 0. ] [ 1.08 0. 0. 0. 0. 0. 0. 0. ] [ 1.08 0. 0. 0. 0. 0. 0. 0. ]] the above is the numpy array matrix. the numbers represents: C-C: 1.54 and C-H=1.08 So I want to write this form as C of index i is connected to C of index j C of index i is connected to H of index j (C(i),C(j)) # key C(i) and value C(j) (C(i),H(j)) # key C(i) and value H(j) ; the key C(i) can be repeated to fulfil as much as the values of H(j) To summarize, the out put may look like: C1 is connected to C2 C1 is connected to H1 C1 is connected to H3 C2 is connected to H2 etc Any guide is greatly appreciated, thanks birda ___ NumPy-Discussion mailing list NumPy-Discussion at scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion Birda, I think this will get you some of the way there: import numpy as np x = ... # Here's your 2D atomic distance array # Create an indexing array index = np.arange( x.size ).reshape( x.shape ) # Find the non-zero indices items = index[ x != 0 ] # You only need the first half because your array is symmetric items = items[ : items.size/2] rows = items / x.shape[0] cols = items % x.shape[0] print 'Rows: ', rows print 'Columns:', cols print 'Atomic Distances:', x[rows, cols] Hope it helps. Ryan ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Any help from Numpy community?
Birda, I think this will get you some of the way there: import numpy as np x = ... # Here's your 2D atomic distance array # Create an indexing array index = np.arange( x.size ).reshape( x.shape ) # Find the non-zero indices items = index[ x != 0 ] # You only need the first half because your array is symmetric items = items[ : items.size/2] rows = items / x.shape[0] cols = items % x.shape[0] print 'Rows: ', rows print 'Columns:', cols print 'Atomic Distances:', x[rows, cols] Hope it helps. Ryan ___ NumPy-Discussion mailing list NumPy-Discussion at scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion Whoops. That doesn't quite work. You shouldn't drop half the items array like that. This will work better (maybe ?): import numpy as np x = ... # Here's your 2D atomic distance array index = np.arange( x.size ).reshape( x.shape ) items = index[ x != 0 ] rows = items / x.shape[0] cols = items % x.shape[0] # This index mask should take better advantage of the array symmetry mask = rows cols print 'Rows: ', rows[mask] print 'Columns:', cols[mask] print 'Atomic Distances:', x[rows[mask], cols[mask]] Ryan ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Any help from Numpy community?
Hi Ryan,Thank you very much indeed, I'm not sure if I well understood your code, let say, for the example array matrix given represents H3C-CH3 connection(bonding). the result from your code is: Rows:[0 0 0 0 1 1 1] # is these for C indices? Columns: [1 2 3 4 5 6 7] # is these for H indices? but it shouldn't be 6 H's? Atomic Distances: [ 1. 1. 1. 1. 1. 1. 1.] # ofcourse this is the number of connections or bonds. In fact, if I write in the form of dictionary: row indices as keys and column indices as values, {0:1, 0:2, 0:3, 0:4, 1:5, 1:6, 1:7}, So, does it mean C[0] is connected to H[1], C[0] is connected to H[2] , H[1],,C[1] is connected to H[7]? But I have only 6 H's and two C's in this example (H3C-CH3) I have tried some thing like: but still no luck ;( import numpy as np from collections import defaultdict dict = defaultdict(list) x=2d numpy array I = x.shape[0] J = x.shape[1] d={} for i in xrange(0, I, 1): for j in xrange(0, J, 1): if x[i,j] 0: dict[i].append(j) # the result is: dict: {0: [1, 2, 3, 4], 1: [0, 5, 6, 7], 2: [0], 3: [0], 4: [0], 5: [1], 6: [1], 7: [1]}) keys: [0, 1, 2, 3, 4, 5, 6, 7] values: [[1, 2, 3, 4], [0, 5, 6, 7], [0], [0], [0], [1], [1], [1]] #The H indices can be found by H_rows = np.nonzero(x.sum(axis=1)== 1) result=H_rows : [2, 3, 4, 5, 6, 7] # six H's I am trying to connect this indices with the dict result but I am confused! So, now I want to produce a dictionary or what ever to produce results as: H[2] is connected to C[?] H[3] is connected to C[?] H[4] is connected to C[?], . Thanks for any help . On Thu, Mar 14, 2013 at 2:26 PM, Ryan rnelsonc...@gmail.com wrote: Birda, I think this will get you some of the way there: import numpy as np x = ... # Here's your 2D atomic distance array # Create an indexing array index = np.arange( x.size ).reshape( x.shape ) # Find the non-zero indices items = index[ x != 0 ] # You only need the first half because your array is symmetric items = items[ : items.size/2] rows = items / x.shape[0] cols = items % x.shape[0] print 'Rows: ', rows print 'Columns:', cols print 'Atomic Distances:', x[rows, cols] Hope it helps. Ryan ___ NumPy-Discussion mailing list NumPy-Discussion at scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion Whoops. That doesn't quite work. You shouldn't drop half the items array like that. This will work better (maybe ?): import numpy as np x = ... # Here's your 2D atomic distance array index = np.arange( x.size ).reshape( x.shape ) items = index[ x != 0 ] rows = items / x.shape[0] cols = items % x.shape[0] # This index mask should take better advantage of the array symmetry mask = rows cols print 'Rows: ', rows[mask] print 'Columns:', cols[mask] print 'Atomic Distances:', x[rows[mask], cols[mask]] Ryan ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Any help from Numpy community?
Birdada Simret birdada85 at gmail.com writes: Hi Ryan,Thank you very much indeed, I'm not sure if I well understood your code, let say, for the example array matrix given represents H3C-CH3 connection(bonding). the result from your code is: Rows: [0 0 0 0 1 1 1] # is these for C indices? Columns: [1 2 3 4 5 6 7] # is these for H indices? but it shouldn't be 6 H's? Atomic Distances: [ 1. 1. 1. 1. 1. 1. 1.] # ofcourse this is the number of connections or bonds. In fact, if I write in the form of dictionary: row indices as keys and column indices as values, {0:1, 0:2, 0:3, 0:4, 1:5, 1:6, 1:7}, So, does it mean C[0] is connected to H[1], C[0] is connected to H[2] , H[1],,C[1] is connected to H[7]? But I have only 6 H's and two C's in this example (H3C-CH3) I have tried some thing like: but still no luck ;( import numpy as np from collections import defaultdict dict = defaultdict(list) x=2d numpy array I = x.shape[0] J = x.shape[1] d={} for i in xrange(0, I, 1): for j in xrange(0, J, 1): if x[i,j] 0: dict[i].append(j) # the result is: dict: {0: [1, 2, 3, 4], 1: [0, 5, 6, 7], 2: [0], 3: [0], 4: [0], 5: [1], 6: [1], 7: [1]}) keys: [0, 1, 2, 3, 4, 5, 6, 7] values: [[1, 2, 3, 4], [0, 5, 6, 7], [0], [0], [0], [1], [1], [1]] #The H indices can be found by H_rows = np.nonzero(x.sum(axis=1)== 1) result=H_rows : [2, 3, 4, 5, 6, 7] # six H's I am trying to connect this indices with the dict result but I am confused! So, now I want to produce a dictionary or what ever to produce results as: H[2] is connected to C[?] H[3] is connected to C[?] H[4] is connected to C[?], . Thanks for any help . On Thu, Mar 14, 2013 at 2:26 PM, Ryan rnelsonchem at gmail.com wrote: Birda, I don't know how your getting those values from my code. Here's a slightly modified and fully self-contained version that includes your bonding matrix: import numpy as np x = np.array( [[ 0., 1.54, 0., 0., 0., 1.08, 1.08, 1.08 ], [ 1.54, 0., 1.08, 1.08, 1.08, 0., 0., 0. ], [ 0., 1.08, 0., 0., 0., 0., 0., 0. ], [ 0., 1.08, 0., 0., 0., 0., 0., 0. ], [ 0., 1.08, 0., 0., 0., 0., 0., 0. ], [ 1.08, 0., 0., 0., 0., 0., 0., 0. ], [ 1.08, 0., 0., 0., 0., 0., 0., 0. ], [ 1.08, 0., 0., 0., 0., 0., 0., 0. ]] ) atoms = np.array(['C1', 'C2', 'H3', 'H4', 'H5', 'H6', 'H7', 'H8']) index = np.arange( x.size ).reshape( x.shape ) items = index[ x != 0 ] rows = items / x.shape[0] cols = items % x.shape[0] mask = rows cols print 'Rows: ', rows[mask] print 'Columns:', cols[mask] print 'Bond Atom 1: ', atoms[ rows[mask] ] print 'Bond Atom 2: ', atoms[ cols[mask] ] print 'Atomic Distances:', x[rows[mask], cols[mask]] If I copy that into a file and run it, I get the following output: Rows:[0 0 0 0 1 1 1] Columns: [1 5 6 7 2 3 4] Bond Atom 1: ['C1' 'C1' 'C1' 'C1' 'C2' 'C2' 'C2'] Bond Atom 2: ['C2' 'H6' 'H7' 'H8' 'H3' 'H4' 'H5'] Atomic Distances: [ 1.54 1.08 1.08 1.08 1.08 1.08 1.08] Honestly, I did not think about your code all that much. Too many 'for' loops for my taste. My code has quite a bit of fancy indexing, which I could imagine is also quite confusing. If you really want a dictionary type of interface that still let's you use Numpy magic, I would take a look at Pandas (http://pandas.pydata.org/) Ryan ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Any help from Numpy community?
Oh, thanks alot. can the atoms = np.array(['C1', 'C2', 'H3', 'H4', 'H5', 'H6', 'H7', 'H8']) able to make general? I mean, if I have a big molecule, it seems difficult to label each time. Ofcourse I'm new to python(even for programing) and I didn't had any knowhow about pandas, but i will try it. any ways, it is great help, many thanks Ryan Birda On Thu, Mar 14, 2013 at 5:03 PM, Ryan rnelsonc...@gmail.com wrote: Birdada Simret birdada85 at gmail.com writes: Hi Ryan,Thank you very much indeed, I'm not sure if I well understood your code, let say, for the example array matrix given represents H3C-CH3 connection(bonding). the result from your code is: Rows:[0 0 0 0 1 1 1] # is these for C indices? Columns: [1 2 3 4 5 6 7] # is these for H indices? but it shouldn't be 6 H's? Atomic Distances: [ 1. 1. 1. 1. 1. 1. 1.] # ofcourse this is the number of connections or bonds. In fact, if I write in the form of dictionary: row indices as keys and column indices as values, {0:1, 0:2, 0:3, 0:4, 1:5, 1:6, 1:7}, So, does it mean C[0] is connected to H[1], C[0] is connected to H[2] , H[1],,C[1] is connected to H[7]? But I have only 6 H's and two C's in this example (H3C-CH3) I have tried some thing like: but still no luck ;( import numpy as np from collections import defaultdict dict = defaultdict(list) x=2d numpy array I = x.shape[0] J = x.shape[1] d={} for i in xrange(0, I, 1): for j in xrange(0, J, 1): if x[i,j] 0: dict[i].append(j) # the result is: dict: {0: [1, 2, 3, 4], 1: [0, 5, 6, 7], 2: [0], 3: [0], 4: [0], 5: [1], 6: [1], 7: [1]}) keys: [0, 1, 2, 3, 4, 5, 6, 7] values: [[1, 2, 3, 4], [0, 5, 6, 7], [0], [0], [0], [1], [1], [1]] #The H indices can be found by H_rows = np.nonzero(x.sum(axis=1)== 1) result=H_rows : [2, 3, 4, 5, 6, 7] # six H's I am trying to connect this indices with the dict result but I am confused! So, now I want to produce a dictionary or what ever to produce results as: H[2] is connected to C[?] H[3] is connected to C[?] H[4] is connected to C[?], . Thanks for any help . On Thu, Mar 14, 2013 at 2:26 PM, Ryan rnelsonchem at gmail.com wrote: Birda, I don't know how your getting those values from my code. Here's a slightly modified and fully self-contained version that includes your bonding matrix: import numpy as np x = np.array( [[ 0., 1.54, 0., 0., 0., 1.08, 1.08, 1.08 ], [ 1.54, 0., 1.08, 1.08, 1.08, 0., 0., 0. ], [ 0., 1.08, 0., 0., 0., 0., 0., 0. ], [ 0., 1.08, 0., 0., 0., 0., 0., 0. ], [ 0., 1.08, 0., 0., 0., 0., 0., 0. ], [ 1.08, 0., 0., 0., 0., 0., 0., 0. ], [ 1.08, 0., 0., 0., 0., 0., 0., 0. ], [ 1.08, 0., 0., 0., 0., 0., 0., 0. ]] ) atoms = np.array(['C1', 'C2', 'H3', 'H4', 'H5', 'H6', 'H7', 'H8']) index = np.arange( x.size ).reshape( x.shape ) items = index[ x != 0 ] rows = items / x.shape[0] cols = items % x.shape[0] mask = rows cols print 'Rows: ', rows[mask] print 'Columns:', cols[mask] print 'Bond Atom 1: ', atoms[ rows[mask] ] print 'Bond Atom 2: ', atoms[ cols[mask] ] print 'Atomic Distances:', x[rows[mask], cols[mask]] If I copy that into a file and run it, I get the following output: Rows:[0 0 0 0 1 1 1] Columns: [1 5 6 7 2 3 4] Bond Atom 1: ['C1' 'C1' 'C1' 'C1' 'C2' 'C2' 'C2'] Bond Atom 2: ['C2' 'H6' 'H7' 'H8' 'H3' 'H4' 'H5'] Atomic Distances: [ 1.54 1.08 1.08 1.08 1.08 1.08 1.08] Honestly, I did not think about your code all that much. Too many 'for' loops for my taste. My code has quite a bit of fancy indexing, which I could imagine is also quite confusing. If you really want a dictionary type of interface that still let's you use Numpy magic, I would take a look at Pandas (http://pandas.pydata.org/) Ryan ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
[Numpy-discussion] Any help from Numpy community?
* Any help from Numpy community [[ 0. 1.540. 0. 0.1.08 1.08 1.08 ] [ 1.540. 1.081.08 1.080. 0. 0. ] [0. 1.08 0. 0. 0.0. 0. 0. ] [0. 1.08 0. 0. 0.0. 0.0.] [ 0.1.080. 0. 0.0. 0.0.] [ 1.08 0. 0. 0. 0.0. 0.0. ] [ 1.08 0. 0. 0. 0.0. 0.0. ] [ 1.08 0. 0. 0. 0.0. 0.0. ]] the above is the numpy array matrix. the numbers represents: C-C: 1.54 and C-H=1.08 So I want to write this form as C of index i is connected to C of index j C of index i is connected to H of index j (C(i),C(j)) # key C(i) and value C(j) (C(i),H(j)) # key C(i) and value H(j) ; the key C(i) can be repeated to fulfil as much as the values of H(j) To summarize, the out put may look like: C1 is connected to C2 C1 is connected to H1 C1 is connected to H3 C2 is connected to H2 etc Any guide is greatly appreciated, thanks birda * ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion