【发布时间】:2015-10-14 03:12:39
【问题描述】:
我对 Python 还是很陌生,我并没有编写大部分代码,我只是想让它工作。我有以下两个 .py 文件:
这个叫FnSim.py-
import numpy,sys, os
import math
def intFn(kVars,params): # Do not change the name of this function
sinTh = kVars['y']/(numpy.sqrt(kVars['y']**2+(1000.)**2))
I = 4*((numpy.sin((numpy.pi*.7*sinTh)/.0006)/((.7*sinTh)/.0006))**2)*numpy.cos((numpy.pi*1.2*sinTh)/.0006)**2
if not math.isnan(I):
return I
else:
print 'Hello'
return 0.0
'''
sinTh = kVars['y']/(numpy.sqrt(kVars['y']**2+(params['R0'])**2))
A1 = numpy.complex(params['A1r'],params['A1i'])*numpy.e**(((-numpy.pi*params['p']*sinTh)/.0006)*1j)
A2 = numpy.complex(params['A2r'],params['A2i'])*numpy.e**(((numpy.pi*params['p']*sinTh)/.0006)*1j)
return (A1+A2)*numpy.conjugate(A1+A2)
'''
def simFn():
"""
This is the function that will do the actual simulating. Fill out the filepaths below.
"""
nTrueDir="./data/nTrueSF.npy"#Location to save the nTrue File
inputKVDir="./data/flatY.txt"#Loaction of the Generated MC KV text File
outputWeightDir="./data/wnListSF.npy"#Location where the weighted mask file will be saved
iList = numpy.load("./data/iListSF.npy")#Location of the list of intensities.
iMax = iList.max()
print iMax
gS.simulate(nTrueDir,inputKVDir,outputWeightDir,iList,iMax)
from generalSim import generalSim
inputKVDir="./data/FlatY.txt"#Loaction of the Generated MC KV File(Same as above)
gS = generalSim(KVDir = inputKVDir)
if sys.argv[1] == "i":
numpy.save("./data/iListSF.npy",gS.calcIList({'A2r': 186.55622562665766, 'R0': 1000.0, 'A1r': 186.5549385015023, 'p': 1.2010987369259563, 'A2i': 0.0, 'A1i': 0.4681942082158097}))
elif sys.argv[1] == "s":
simFn()
这个叫generalSim.py-
import numpy
import os, sys
import fileinput
from FnSim import intFn
from random import random
class generalSim (object):
def __init__(self,KVDir):
self.KVDir = KVDir
def calcIList(self,params):
n = 0
iList = numpy.zeros(shape = (1))
for line in fileinput.input([self.KVDir]):
iList.resize(n+1)
kvAs = line.split(",")
kvAx = {kvA.split('=')[0]:float(kvA.split('=')[1]) for kvA in kvAs}
iList[n] = intFn(kvAx,params)
sys.stdout.write(str(n)+"\r")
sys.stdout.flush()
n+=1
return iList
def simulate(self,nTrueDir,inputKVDir,outputWeightDir,iList,iMax):
nTrueList = [((1.0/(iList.shape[0]))*(iList.sum(0)))]
numpy.save(nTrueDir,nTrueList)
wList=iList[:]/iMax
wnList=numpy.zeros(shape=(wList.shape[0]))
for wn in range(len(wList)):
if wList[wn]>random():
wnList[wn] = 1
numpy.save(outputWeightDir,wnList)
我在标有“数据”的文件夹中还有一个名为 FlatY.txt 的文本文件。 FlatY.txt 包含一百万行 Y 值。
我需要运行这些文件,以便创建 FnSim.py 中列出的 .npy 文件。我不太了解 Linux,但我的目的是将文件放在“数据”文件夹中。但是,如果我尝试编译 FnSim.py,控制台会显示:
Traceback (most recent call last):
File "FnSim.py", line 38, in <module>
from generalSim import generalSim
File "/home/gendreau/workspace/generalSim.py", line 5, in <module>
from FnSim import intFn
File "/home/gendreau/workspace/FnSim.py", line 38, in <module>
from generalSim import generalSim
ImportError: cannot import name generalSim
当我尝试运行 generalSim.py 时,我得到了这个:
Traceback (most recent call last):
File "generalSim.py", line 5, in <module>
from FnSim import intFn
File "/home/gendreau/workspace/FnSim.py", line 41, in <module>
if sys.argv[1] == "i":
IndexError: list index out of range
有什么办法可以解决这些问题并生成必要的文件?
【问题讨论】:
-
你有一个循环导入;
generalSim导入FnSim哪个导入generalSim。尽量不要那样做。generalSim模块只执行到第 5 行,之后的任何内容都不存在且无法导入。 -
另外,请使用工具栏上的
{}按钮在粘贴代码后对其进行格式化。不要尝试通过缩进单个行来“修复”格式;缩进在 Python 代码中太重要了,你不能像这样离开帖子,因为你留下的缩进不正确,它根本不会运行。
标签: python linux python-2.7 numpy importerror