pyNetLogo icon indicating copy to clipboard operation
pyNetLogo copied to clipboard

Error while executing repeat_report

Open kaustuvchatterjee opened this issue 2 years ago • 7 comments

OS: Fedora 36 Netlogo version: 6.2.2 pyNetlogo version 0.4.2 python version 3.10

Netlogo Model:

to setup
  clear-all
  reset-ticks
  crt 10
end

to go
  ask turtles [
    move-to one-of patches
  ]
  tick
end

pyNetlogo Code:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import pyNetLogo

netlogo_path = './Applications/NetLogo'
netlogo_version = '6.2'
jvm_home = './Applications/NetLogo/runtime/lib/amd64/server/libjvm.so'

netlogo = pyNetLogo.NetLogoLink(gui=True, netlogo_home=netlogo_path, netlogo_version=netlogo_version, jvm_home=jvm_home)

model_path = './NetLogo/test.nlogo'
netlogo.load_model(model_path)

netlogo.command('setup')
df = netlogo.repeat_report('[xcor] of turtles',100)

Error: ValueError Traceback (most recent call last) /tmp/ipykernel_66101/872701698.py in 1 netlogo.command('setup') ----> 2 df = netlogo.repeat_report('[xcor] of turtles',100)

~/.local/lib/python3.10/site-packages/pyNetLogo/core.py in repeat_report(self, netlogo_reporter, reps, go, include_t0) 615 for entry in result.split()]) 616 --> 617 results_df.loc[:, key] = result 618 619 os.remove(value)

~/.local/lib/python3.10/site-packages/pandas/core/indexing.py in setitem(self, key, value) 721 722 iloc = self if self.name == "iloc" else self.obj.iloc --> 723 iloc._setitem_with_indexer(indexer, value, self.name) 724 725 def _validate_key(self, key, axis: int):

~/.local/lib/python3.10/site-packages/pandas/core/indexing.py in _setitem_with_indexer(self, indexer, value, name) 1730 self._setitem_with_indexer_split_path(indexer, value, name) 1731 else: -> 1732 self._setitem_single_block(indexer, value, name) 1733 1734 def _setitem_with_indexer_split_path(self, indexer, value, name: str):

~/.local/lib/python3.10/site-packages/pandas/core/indexing.py in _setitem_single_block(self, indexer, value, name) 1966 1967 # actually do the set -> 1968 self.obj._mgr = self.obj._mgr.setitem(indexer=indexer, value=value) 1969 self.obj._maybe_update_cacher(clear=True) 1970

~/.local/lib/python3.10/site-packages/pandas/core/internals/managers.py in setitem(self, indexer, value) 353 354 def setitem(self: T, indexer, value) -> T: --> 355 return self.apply("setitem", indexer=indexer, value=value) 356 357 def putmask(self, mask, new, align: bool = True):

~/.local/lib/python3.10/site-packages/pandas/core/internals/managers.py in apply(self, f, align_keys, ignore_failures, **kwargs) 325 applied = b.apply(f, **kwargs) 326 else: --> 327 applied = getattr(b, f)(**kwargs) 328 except (TypeError, NotImplementedError): 329 if not ignore_failures:

~/.local/lib/python3.10/site-packages/pandas/core/internals/blocks.py in setitem(self, indexer, value) 981 values, len(values[indexer]), value # type: ignore[arg-type] 982 ) --> 983 values[indexer] = value 984 985 if transpose:

ValueError: could not broadcast input array from shape (101,10) into shape (101,)

kaustuvchatterjee avatar Jun 22 '22 15:06 kaustuvchatterjee

The problem is that your reporter returns more then one value, while the current code assumes that a reporter returns only a single value.

quaquel avatar Jun 22 '22 17:06 quaquel

Hi. Thanks for the prompt reply. However, when I query for xcor of sheep in the wolf sheep predation model it returns a list of xcors for each tick. What am I missing? How do I get the xcors of all turtles at each tick? Thanks for your help.

kaustuvchatterjee avatar Jun 23 '22 11:06 kaustuvchatterjee

that is actually a good question that warrants some investigation. My hunch is that it has to do with how jpype (the python library that bridges to java) automatically converts stuff and the knock-on effects of this in pynetlogo.

quaquel avatar Jun 23 '22 12:06 quaquel

can you test what happens if you do

df = netlogo.repeat_report(['[xcor] of turtles',
                            '[ycor] of turtles' ],100)

quaquel avatar Jun 23 '22 12:06 quaquel

I have tried with all turtle attributes, both inbuilt and user created. Same result. Could it possibly be something to do with the netlogo version the model is created in? I used 6.2.2

kaustuvchatterjee avatar Jun 23 '22 13:06 kaustuvchatterjee

I think the issue can be resolved by changing line 617 in core.py to results_df.loc[:, key] = list(result)

kaustuvchatterjee avatar Jun 25 '22 07:06 kaustuvchatterjee

Thanks for the suggested fix, I will have to test this once I have a bit more time.

quaquel avatar Aug 05 '22 06:08 quaquel