[Numpy-discussion] Any help from Numpy community?
Birdada Simret
birdada85 at gmail.com
Thu Mar 14 12:50:34 EDT 2013
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 <rnelsonchem at 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 at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130314/80c3419f/attachment.html>
More information about the NumPy-Discussion
mailing list