File "structure_analysis4.py", line 33, in <module> indices1, indices2, distances = neighbor_list('ijD', pos1, cutoff, self_interaction=False, bothways=True) TypeError: neighbor_list() got an unexpected keyword argument 'bothways'
时间: 2024-03-04 12:50:08 浏览: 13
根据错误提示,你在调用 `neighbor_list()` 函数时使用了一个未知的关键字参数 `bothways`,可能是函数的输入参数列表发生了变化导致的。你可以查看函数的文档或者源代码,确认该函数是否真的支持 `bothways` 参数。如果不支持,你需要修改调用该函数的代码,删除该参数。如果支持,则需要检查该参数是否输入正确。
相关问题
File "structure_analysis4.py", line 16, in <module> positions1 = sort(pos1.get_positions()[indices1]) File "/export/home/anliying/.local/lib/python3.8/site-packages/ase-3.22.1-py3.8.egg/ase/build/tools.py", line 651, in sort tags = atoms.get_chemical_symbols() AttributeError: 'numpy.ndarray' object has no attribute 'get_chemical_symbols'
这个错误是由于你正在尝试使用 `sort()` 函数对一个 Numpy 数组进行排序,但是这个函数需要的是一个 ASE Atoms 对象。在你的代码中,你需要将 Numpy 数组转换为 ASE Atoms 对象,然后再使用 `sort()` 函数。
要解决这个问题,你可以使用 `Atoms()` 函数将 Numpy 数组转换为 ASE Atoms 对象,例如:
``` python
from ase import Atoms
# 获得第一个POSCAR中指定原子的位置列表
indices1 = [i for i, atom in enumerate(pos1) if atom.symbol == atom_type]
positions1 = pos1.get_positions()[indices1]
atoms1 = Atoms(symbols=[atom_type]*len(positions1), positions=positions1)
sorted_atoms1 = sort(atoms1)
# 获得第二个POSCAR中指定原子的位置列表
indices2 = [i for i, atom in enumerate(pos2) if atom.symbol == atom_type]
positions2 = pos2.get_positions()[indices2]
atoms2 = Atoms(symbols=[atom_type]*len(positions2), positions=positions2)
sorted_atoms2 = sort(atoms2)
```
在这个示例中,我们首先从原始的 POCSAR 文件中提取了我们感兴趣的特定原子的位置,并将它们转换为 ASE Atoms 对象。然后,我们使用 `sort()` 函数对这些 Atoms 对象进行排序,并获得了排序后的 Atoms 对象。最后,我们可以使用排序后的 Atoms 对象计算结构相似性。
注意,如果你想比较两个 Atoms 对象之间的距离矩阵,你需要使用 `atoms1.get_all_distances(atoms2)` 函数,而不是 `neighbor_list()` 函数。这个函数将返回一个二维矩阵,表示 atoms1 中每个原子到 atoms2 中每个原子的距离。
修改代码,错误如下:File "structure_analysis4.py", line 33, in <module> indices1, indices2, distances = neighbor_list('ijD', pos1, cutoff, self_interaction=False, bothways=True) TypeError: neighbor_list() got an unexpected keyword argument 'bothways'。。from ase import io from ase.build import sort from ase.visualize import view from ase.neighborlist import neighbor_list import numpy as np from ase import Atoms # 加载两个POSCAR文件 pos1 = io.read('POSCAR1') pos2 = io.read('POSCAR2') # 指定原子种类 atom_type = 'C' # 获得第一个POSCAR中指定原子的位置列表 #indices1 = [i for i, atom in enumerate(pos1) if atom.symbol == atom_type] #positions1 = sort(pos1.get_positions()[indices1]) indices1 = [i for i, atom in enumerate(pos1) if atom.symbol == atom_type] positions1 = pos1.get_positions()[indices1] atoms1 = Atoms(symbols=[atom_type]*len(positions1), positions=positions1) sorted_atoms1 = sort(atoms1) # 获得第二个POSCAR中指定原子的位置列表 #indices2 = [i for i, atom in enumerate(pos2) if atom.symbol == atom_type] #positions2 = sort(pos2.get_positions()[indices2]) indices2 = [i for i, atom in enumerate(pos2) if atom.symbol == atom_type] positions2 = pos2.get_positions()[indices2] atoms2 = Atoms(symbols=[atom_type]*len(positions2), positions=positions2) sorted_atoms2 = sort(atoms2) # 计算两个位置列表之间的距离矩阵 cutoff = 5.0 # 距离截断半径 indices1, indices2, distances = neighbor_list('ijD', pos1, cutoff, self_interaction=False, bothways=True) dist_matrix1 = np.zeros((len(positions1), len(positions1))) for i, j, d in zip(indices1, indices2, distances): if i in indices1 and j in indices1: dist_matrix1[indices1.index(i), indices1.index(j)] = d indices1, indices2, distances = neighbor_list('ijD', pos2, cutoff, self_interaction=False, bothways=True) dist_matrix2 = np.zeros((len(positions2), len(positions2))) for i, j, d in zip(indices1, indices2, distances): if i in indices2 and j in indices2: dist_matrix2[indices2.index(i), indices2.index(j)] = d # 计算两个距离矩阵之间的相似性 similarity = 1.0 - np.abs(dist_matrix1 - dist_matrix2).sum() / dist_matrix1.size print('Structure similarity: ', similarity)
根错误提示,`neighbor_list()` 函数没有 `bothways` 参数。可以尝试把 `bothways=True` 这个参数删除掉。另外,第一个 `neighbor_list()` 函数的输入参数应该是 `positions1`,而不是 `pos1`。修改后的代码如下:
```python
from ase import io
from ase.build import sort
from ase.visualize import view
from ase.neighborlist import neighbor_list
import numpy as np
from ase import Atoms
# 加载两个POSCAR文件
pos1 = io.read('POSCAR1')
pos2 = io.read('POSCAR2')
# 指定原子种类
atom_type = 'C'
# 获得第一个POSCAR中指定原子的位置列表
indices1 = [i for i, atom in enumerate(pos1) if atom.symbol == atom_type]
positions1 = pos1.get_positions()[indices1]
atoms1 = Atoms(symbols=[atom_type]*len(positions1), positions=positions1)
sorted_atoms1 = sort(atoms1)
# 获得第二个POSCAR中指定原子的位置列表
indices2 = [i for i, atom in enumerate(pos2) if atom.symbol == atom_type]
positions2 = pos2.get_positions()[indices2]
atoms2 = Atoms(symbols=[atom_type]*len(positions2), positions=positions2)
sorted_atoms2 = sort(atoms2)
# 计算两个位置列表之间的距离矩阵
cutoff = 5.0 # 距离截断半径
indices1, indices2, distances = neighbor_list('ijD', positions1, cutoff, self_interaction=False)
dist_matrix1 = np.zeros((len(positions1), len(positions1)))
for i, j, d in zip(indices1, indices2, distances):
if i in indices1 and j in indices1:
dist_matrix1[indices1.index(i), indices1.index(j)] = d
indices1, indices2, distances = neighbor_list('ijD', positions2, cutoff, self_interaction=False)
dist_matrix2 = np.zeros((len(positions2), len(positions2)))
for i, j, d in zip(indices1, indices2, distances):
if i in indices2 and j in indices2:
dist_matrix2[indices2.index(i), indices2.index(j)] = d
# 计算两个距离矩阵之间的相似性
similarity = 1.0 - np.abs(dist_matrix1 - dist_matrix2).sum() / dist_matrix1.size
print('Structure similarity: ', similarity)
```
请注意,这里还有一个小问题:在计算相似性时,使用了两个距离矩阵的元素差的绝对值之和来计算距离矩阵的相似性,这种方法并不是很合适。可以考虑使用更为科学的相似性度量方法,比如结构指纹等。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)