Repeat_report command run twice ValueError
I've been starting to use the examples from https://readthedocs.org/projects/pynetlogo/downloads/pdf/latest/, and I've found an issue in the repeat_report command as used on page 9. It appears that running repeat_report after other simulation commands leads to it raising a value error
If I run this script
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import pyNetLogo
netlogo = pyNetLogo.NetLogoLink(gui=True,netlogo_home='''/home/ben/binaries/NetLogo 6.2.2''',netlogo_version='6.2')
netlogo.load_model('./Wolf_Sheep_Predation.nlogo')
netlogo.command('setup')
test_energy = netlogo.repeat_report(['[energy] of sheep'],10) #no problem
energy_df = netlogo.repeat_report(['[energy] of wolves',
'[energy] of sheep',
'count sheep'], 200)
print(energy_df)
#netlogo.command('setup')
energy_df = netlogo.repeat_report(['[energy] of sheep',
'[energy] of sheep',
'count sheep',
'count wolves'], 1)
print(energy_df)
I get the error
ValueError: could not broadcast input array from shape (4,) into shape (2,2)
If I change the number of steps in the second repeat_report command the error remains, but the first command is robust. If I uncomment the repeat of the setup line this does not prevent the error.
Tested with the latest commit from github and from an installation through pip, on ubuntu focal.
Exactly at which line do you get the value error?
Second to final line.
Can you give me the complete error message including the stack trace, rather than just the value error?
I don't have the ability to test your code myself at the moment, so trying to figure out what is happening is a bit tricky.
The terminal output is below. The second print never happens. The first log line is repeated several times but I didn't think that was relevant to the issue.
- org.nlogo.log.LogMessage@514cd540
/home/ben/anaconda3/lib/python3.8/site-packages/pyNetLogo/core.py:605: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
result = np.array([np.array(e.split(),
[energy] of wolves [energy] of sheep count sheep
0.0 [5.0, 17.0, 18.75, 1.5, 10.5, 9.5, 19.0, 2.0, ... [4.0, 1.0, 1.0, 1.0, 2.0, 1.0, 0.5, 2.0, 4.0, ... 187.0
1.0 [1.0, 4.75, 9.5, 11.0, 4.25, 17.75, 18.0, 12.0... [0.5, 2.0, 1.0, 1.0, 4.0, 4.0, 0.25, 4.0, 0.75... 200.0
2.0 [25.0, 0.0, 25.0, 12.0, 15.0, 16.75, 8.0, 11.0... [1.0, 1.0, 1.5, 0.5, 2.0, 0.25, 2.0, 0.75, 0.2... 202.0
3.0 [24.0, 11.0, 7.0, 15.75, 5.5, 0.0, 14.375, 24.... [2.0, 1.0, 1.0, 0.5, 0.75, 1.0, 2.0, 1.5, 1.0,... 202.0
4.0 [10.0, 3.25, 6.0, 5.5, 30.5, 14.0, 9.0, 23.0, ... [0.25, 0.25, 0.75, 0.5, 0.5, 0.5, 0.25, 1.0, 0... 210.0
... ... ... ...
196.0 [21.875, 10.9375, 25.140625, 10.9375] [0.001953125, 1.9073486328125e-06, 1.525878906... 542.0
197.0 [9.9375, 20.875, 9.9375, 24.140625] [0.000244140625, 1.52587890625e-05, 0.00024414... 562.0
198.0 [8.9375, 23.140625, 8.9375, 19.875] [0.000244140625, 3.814697265625e-06, 3.8146972... 587.0
199.0 [22.140625, 7.9375, 7.9375, 18.875] [0.000244140625, 0.0001220703125, 7.6293945312... 604.0
200.0 [6.9375, 17.875, 21.140625, 6.9375] [7.62939453125e-06, 3.814697265625e-06, 1.5258... 629.0
[201 rows x 3 columns]
/home/ben/anaconda3/lib/python3.8/site-packages/pyNetLogo/core.py:605: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
result = np.array([np.array(e.split(),
Traceback (most recent call last):
File "repro.py", line 21, in <module>
energy_df = netlogo.repeat_report(['[energy] of sheep',
File "/home/ben/anaconda3/lib/python3.8/site-packages/pyNetLogo/core.py", line 621, in repeat_report
results_df.loc[:, key] = result
File "/home/ben/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py", line 692, in __setitem__
iloc._setitem_with_indexer(indexer, value, self.name)
File "/home/ben/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py", line 1637, in _setitem_with_indexer
self._setitem_single_block(indexer, value, name)
File "/home/ben/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py", line 1861, in _setitem_single_block
self.obj._mgr = self.obj._mgr.setitem(indexer=indexer, value=value)
File "/home/ben/anaconda3/lib/python3.8/site-packages/pandas/core/internals/managers.py", line 568, in setitem
return self.apply("setitem", indexer=indexer, value=value)
File "/home/ben/anaconda3/lib/python3.8/site-packages/pandas/core/internals/managers.py", line 427, in apply
applied = getattr(b, f)(**kwargs)
File "/home/ben/anaconda3/lib/python3.8/site-packages/pandas/core/internals/blocks.py", line 1035, in setitem
values[indexer] = value
ValueError: could not broadcast input array from shape (4,) into shape (2,2)
Do you deliberately want to have '[energy] of sheep' twice in that command?
I hope to be able to run this somewhere over the coming days to figure out what is going on as well as possibly modify the code in places to catch the error earlier so in the future it is clear on which reporter the issue happens.
It doesn't make a difference if its sheep or wolves in the command. This code is just to illustrate the problem, its derived from a longer notebook based on the documentation
I looked in to this error today. I tested it on a mac with netlogo 6.2, and python 3.9.7 (anaconda distribution). I made one minor change (sheep to wolves as per earlier comment) and then the code works fine. If I keep sheep twice, I do get the error you are seeing. So, your claim that it does not make a difference does not seem to hold up.
Code wise, it also makes sense. Internally, a dict is used to map reporters to output files. If you have the same reporter twice, you get a problem.
Sorry about that- the code was transcribed from a notebook and something went wrong in the transcription. Please could you consider adding a warning about duplicated keys?
I've rewritten the reproduction script as below;
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import pyNetLogo
netlogo = pyNetLogo.NetLogoLink(gui=True,netlogo_home='''/home/ben/binaries/NetLogo 6.2.2''',netlogo_version='6.2')
netlogo.load_model('./Wolf_Sheep_Predation.nlogo')
netlogo.command('setup')
energy_wolves = netlogo.report('[energy] of wolves')
counts = netlogo.repeat_report(['count wolves','count sheep'], 200, go='go')
test_energy = netlogo.repeat_report(['[energy] of sheep','[energy] of wolves'],10)
print(test_energy)
This gives the following error
- org.nlogo.log.LogMessage@514cd540
/home/ben/anaconda3/lib/python3.8/site-packages/pyNetLogo/core.py:605: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
result = np.array([np.array(e.split(),
Traceback (most recent call last):
File "repro2.py", line 15, in <module>
test_energy = netlogo.repeat_report(['[energy] of sheep','[energy] of wolves'],10)
File "/home/ben/anaconda3/lib/python3.8/site-packages/pyNetLogo/core.py", line 621, in repeat_report
results_df.loc[:, key] = result
File "/home/ben/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py", line 692, in __setitem__
iloc._setitem_with_indexer(indexer, value, self.name)
File "/home/ben/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py", line 1637, in _setitem_with_indexer
self._setitem_single_block(indexer, value, name)
File "/home/ben/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py", line 1861, in _setitem_single_block
self.obj._mgr = self.obj._mgr.setitem(indexer=indexer, value=value)
File "/home/ben/anaconda3/lib/python3.8/site-packages/pandas/core/internals/managers.py", line 568, in setitem
return self.apply("setitem", indexer=indexer, value=value)
File "/home/ben/anaconda3/lib/python3.8/site-packages/pandas/core/internals/managers.py", line 427, in apply
applied = getattr(b, f)(**kwargs)
File "/home/ben/anaconda3/lib/python3.8/site-packages/pandas/core/internals/blocks.py", line 1035, in setitem
values[indexer] = value
ValueError: could not broadcast input array from shape (11,0) into shape (11,)
I tried to replicate your error on my system but I have come up short.
Which predator-prey model are you using? I use the one available here on GitHub in the examples folder. I am also using python 3.9, rather than 3.8.