linux - Python: Cannot import name/IndexError: List index out of range -
i'm still pretty new python, , didn't write of code, i'm trying work. have following 2 .py files:
this 1 called fnsim.py-
import numpy,sys, os import math def intfn(kvars,params): # not change name of function sinth = kvars['y']/(numpy.sqrt(kvars['y']**2+(1000.)**2)) = 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 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(): """ function actual simulating. fill out filepaths below. """ ntruedir="./data/ntruesf.npy"#location save ntrue file inputkvdir="./data/flaty.txt"#loaction of generated mc kv text file outputweightdir="./data/wnlistsf.npy"#location weighted mask file saved ilist = numpy.load("./data/ilistsf.npy")#location of list of intensities. imax = ilist.max() print imax gs.simulate(ntruedir,inputkvdir,outputweightdir,ilist,imax) generalsim import generalsim inputkvdir="./data/flaty.txt"#loaction of generated mc kv file(same 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()
this 1 called generalsim.py-
import numpy import os, sys import fileinput fnsim import intfn random import random class generalsim (object): def __init__(self,kvdir): self.kvdir = kvdir def calcilist(self,params): n = 0 ilist = numpy.zeros(shape = (1)) line in fileinput.input([self.kvdir]): ilist.resize(n+1) kvas = line.split(",") kvax = {kva.split('=')[0]:float(kva.split('=')[1]) 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])) wn in range(len(wlist)): if wlist[wn]>random(): wnlist[wn] = 1 numpy.save(outputweightdir,wnlist)
i have text file called flaty.txt in folder labeled 'data'. flaty.txt contains 1 million lines of y values.
i need run files can create .npy files listed in fnsim.py. don't know linux well, intention have files placed in 'data' folder. however, if try compile fnsim.py, console shows this:
traceback (most recent call last): file "fnsim.py", line 38, in <module> generalsim import generalsim file "/home/gendreau/workspace/generalsim.py", line 5, in <module> fnsim import intfn file "/home/gendreau/workspace/fnsim.py", line 38, in <module> generalsim import generalsim importerror: cannot import name generalsim
when try run generalsim.py, this:
traceback (most recent call last): file "generalsim.py", line 5, in <module> fnsim import intfn file "/home/gendreau/workspace/fnsim.py", line 41, in <module> if sys.argv[1] == "i": indexerror: list index out of range
what can fix resolve these problems , generate necessary files?
this kind of workaround see below, hope helps in work:
because have lot of circular import i.e. first module calls second module, calls first , on, straightforward solution needed result merge 2 files avoiding circular imports:
import numpy import os, sys import math import fileinput random import random class generalsim (object): def __init__(self,kvdir): self.kvdir = kvdir def calcilist(self,params): n = 0 ilist = numpy.zeros(shape = (1)) line in fileinput.input([self.kvdir]): ilist.resize(n+1) kvas = line.split(",") kvax = {kva.split('=')[0]:float(kva.split('=')[1]) 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])) wn in range(len(wlist)): if wlist[wn]>random(): wnlist[wn] = 1 numpy.save(outputweightdir,wnlist) def intfn(kvars,params): # not change name of function sinth = kvars['y']/(numpy.sqrt(kvars['y']**2+(1000.)**2)) = 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 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(): """ function actual simulating. fill out filepaths below. """ ntruedir="./data/ntruesf.npy"#location save ntrue file inputkvdir="./data/flaty.txt"#loaction of generated mc kv text file outputweightdir="./data/wnlistsf.npy"#location weighted mask file saved ilist = numpy.load("./data/ilistsf.npy")#location of list of intensities. imax = ilist.max() print imax gs.simulate(ntruedir,inputkvdir,outputweightdir,ilist,imax) inputkvdir="./data/flaty.txt"#loaction of generated mc kv file(same 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()
furthermore have run file passing argument i
or s
@ command line or directly erasing ignorable row script @ this:
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()
or can change code follows adding variable runmode
, can set actual running mode via this.
import numpy import os, sys import math import fileinput random import random ###### run_mode = "s" # or can change "i" ###### class generalsim (object): def __init__(self,kvdir): self.kvdir = kvdir def calcilist(self,params): n = 0 ilist = numpy.zeros(shape = (1)) line in fileinput.input([self.kvdir]): ilist.resize(n+1) kvas = line.split(",") kvax = {kva.split('=')[0]:float(kva.split('=')[1]) 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])) wn in range(len(wlist)): if wlist[wn]>random(): wnlist[wn] = 1 numpy.save(outputweightdir,wnlist) def intfn(kvars,params): # not change name of function sinth = kvars['y']/(numpy.sqrt(kvars['y']**2+(1000.)**2)) = 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 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(): """ function actual simulating. fill out filepaths below. """ ntruedir="./data/ntruesf.npy"#location save ntrue file inputkvdir="./data/flaty.txt"#loaction of generated mc kv text file outputweightdir="./data/wnlistsf.npy"#location weighted mask file saved ilist = numpy.load("./data/ilistsf.npy")#location of list of intensities. imax = ilist.max() print imax gs.simulate(ntruedir,inputkvdir,outputweightdir,ilist,imax) inputkvdir="./data/flaty.txt"#loaction of generated mc kv file(same above) gs = generalsim(kvdir = inputkvdir) #### if run_mode == "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 run_mode == "s": simfn() ####
Comments
Post a Comment