简体   繁体   English

所有正负坐标值的Python组合

[英]Python combination of all positive and negative coordinate values

I have a code that calculates the distance formula for 3D coordinates from this input file 我有一个代码,可以从此输入文件计算3D坐标的距离公式

在此处输入图片说明

  input_file="mock_data.csv"
  cmd=pd.read_csv(input_file)
  subset = cmd[['carbon','x_coord', 'y_coord','z_coord']]
  coordinate_values = [tuple(x) for x in subset.values]

  atoms = coordinate_values
  atomPairs = itertools.combinations(atoms, 2)
  for pair in atomPairs:
    x1 = pair[0][1]
    y1 = pair[0][2]
    z1 = pair[0][3]
    x2 = pair[1][1]
    y2 = pair[1][2]
    z2 = pair[1][3]

    """Define the values for the distance between the atoms"""
    def calculate_distance(x1,y1,x2,y2,z1,z2):
       dist=math.sqrt((x2 - x1)**2 + (y2 - y1)**2 + (z2-z1)**2)
       return dist
    d=calculate_distance(x1,y1,x2,y2,z1,z2)

I currently have the code worked out to calculate the distance between each 'carbon'. 目前,我已经制定了代码来计算每个“碳”之间的距离。 My problem is that my coordinates are all absolute values - each coordinate could be positive or negative. 我的问题是我的坐标都是绝对值-每个坐标可以是正或负。 I want to calculate the distances between each carbon for all possible coordinates, ie all positive and negative combinations of each 3D coordinates. 我想针对所有可能的坐标,即每个3D坐标的所有正负组合,计算每个碳之间的距离。

Quick example: 'carbon' 1 has coordinates (1.08, 0.49, 0.523) but this could also be (-1.08, -0.49, -0.523), (-1.08, 0.49, 0.523), (-1.08, -0.49, 0.523), (-1.08, 0.49, -0.523), (1.08, -0.49, 0.523), (1.08, -0.49, -0.523), (1.08, 0.49, -0.523), making for a total of eight possibilities for each coordinate system. 快速示例:“碳” 1的坐标为(1.08,0.49,0.523),但是也可以为(-1.08,-0.49,-0.523),(-1.08,0.49,0.523),(-1.08,-0.49,0.523) ,(-1.08、0.49,-0.523),(1.08,-0.49、0.523),(1.08,-0.49,-0.523),(1.08、0.49,-0.523),每个坐标系总共有八种可能性。

I need a code to go through all of these possible coordinate values to calculate the distances I have already coded for. 我需要一个代码来检查所有这些可能的坐标值,以计算出我已经编码的距离。

Here is a fairly simple example. 这是一个非常简单的示例。

import numpy as np
from itertools import product
from scipy.spatial.distance import cdist

base = np.array([-1,1]) #accounts for signed coord

x = 1*base              #change coord values
y = 2*base
z = 3*base

coords = list(product(x,y,z))    #cartesian product

distances = cdist(coords,coords) #better implementation of distance

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM