我使用brian2
来运行神经网络模拟。为了在每次模拟过程中记录数据,我创建了brian2
SpikeMonitor
类的多个实例。我想将这些监视器存储在一个dict中,使用dict理解创建。
作为测试,我在交互式会话中执行以下操作:
In [1]: import brian2
In [2]: pe_mt = brian2.PoissonGroup(1, 100 * brian2.Hz)
In [3]: record_pops = ['pe_mt']
In [4]: {'mon_' + pop: brian2.SpikeMonitor(eval(pop)) for pop in record_pops}
Out[4]: {'mon_pe_mt': <SpikeMonitor, recording spikemonitor>}
一切看起来都很棒。但现在当我将此代码移动到以下函数
时def test_record():
pe_mt = brian2.PoissonGroup(1, 100 * brian2.Hz)
record_pops = ['pe_mt']
return {'mon_' + pop: brian2.SpikeMonitor(eval(pop)) for pop in
record_pops}
并调用它,我收到以下错误
In [9]: tests.test_record()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-9-4d3d585b2c97> in <module>()
----> 1 tests.test_record()
/home/daniel/Science/dopa_net/brian/ardid/tests.py in test_record()
61 record_pops = ['pe_mt']
62 return {'mon_' + pop: brian2.SpikeMonitor(eval(pop)) for pop in
---> 63 record_pops}
64 # DEBUG ###################
65 #monitors = utils.record(['pe_mt'], 'spikes', None, None, pe_mt, None, None)
/home/daniel/Science/dopa_net/brian/ardid/tests.py in <dictcomp>((pop,))
60 # DEBUG ###################
61 record_pops = ['pe_mt']
---> 62 return {'mon_' + pop: brian2.SpikeMonitor(eval(pop)) for pop in
63 record_pops}
64 # DEBUG ###################
/home/daniel/Science/dopa_net/brian/ardid/tests.py in <module>()
NameError: name 'pe_mt' is not defined
这里发生了什么? &#39; pe_mt&#39;在函数中定义了 。
请注意,如果我将dict理解更改为列表理解,如
return [brian2.SpikeMonitor(eval(pop)) for pop in record_pops]
没有错误提出!我得到了SpikeMonitor
个对象的列表,这些对象已经适当定义。
现已删除的答案表明我使用locals()[pop]
代替eval(pop)
。请注意,这会产生一个等效错误:
In [20]: tests.test_record()
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-20-4d3d585b2c97> in <module>()
----> 1 tests.test_record()
/home/daniel/Science/dopa_net/brian/ardid/tests.py in test_record()
61 record_pops = ['pe_mt']
62 return {'mon_' + pop: brian2.SpikeMonitor(locals()[pop]) for pop in
---> 63 record_pops}
64 # DEBUG ###################
65 #monitors = utils.record(['pe_mt'], 'spikes', None, None, pe_mt, None, None)
/home/daniel/Science/dopa_net/brian/ardid/tests.py in <dictcomp>((pop,))
60 # DEBUG ###################
61 record_pops = ['pe_mt']
---> 62 return {'mon_' + pop: brian2.SpikeMonitor(locals()[pop]) for pop in
63 record_pops}
64 # DEBUG ###################
KeyError: 'pe_mt'
答案 0 :(得分:0)
一:忘记eval
,因为如果传递给它的字符串是表达式或函数调用而不是标识符,它可能会导致意外事情发生。如果确实需要按名称获取本地变量,则可以使用locals()[name]
干净利落地完成。
文档:locals
二:所有的理解和生成器表达式(除了python 2.x中的列表推导除外)have their own namespace,因此理解中的locals()
将引用那个 - 没有你的变量的那个。对于captures your local variables by default:
eval
也是如此
如果省略locals字典,则默认为globals字典。如果省略两个字典,则表达式在调用eval()的环境中执行。
你可以通过早点获得它们来解决这个问题:
def test_record():
pe_mt = brian2.PoissonGroup(1, 100 * brian2.Hz)
record_pops = ['pe_mt']
groups = locals()
return {'mon_' + pop: brian2.SpikeMonitor(eval(pop, globals(), groups)) for pop in record_pops}
# or better
return {'mon_' + pop: brian2.SpikeMonitor(groups[pop]) for pop in record_pops}
或更常规,没有locals
:
def test_record():
groups = {
"pe_mt": brian2.PoissonGroup(1, 100 * brian2.Hz),
}
return {'mon_' + key: brian2.SpikeMonitor(value) for key, value in groups.iteritems()}
答案 1 :(得分:0)
不推荐的解决方法:
def test_record():
pe_mt = brian2.PoissonGroup(1, 100 * brian2.Hz)
record_pops = ['pe_mt']
my_loc = locals()
return {'mon_' + pop: brian2.SpikeMonitor(eval(my_loc[pop])) for pop in
record_pops}
或使用普通循环来构建你的词典:
def test_record():
pe_mt = brian2.PoissonGroup(1, 100 * brian2.Hz)
record_pops = ['pe_mt']
d = {}
for pop in record_pops:
d['mon_' + pop] = brian2.SpikeMonitor(locals()[pop]))
return d
或者只是使用dict来保存对象:
def test_record():
d = {"pe_mt":brian2.PoissonGroup(1, 100 * brian2.Hz)}
record_pops = ['pe_mt']
return {'mon_' + pop: brian2.SpikeMonitor(d[pop]) for pop in record_pops}