khanna-lab / cadre

Other
0 stars 0 forks source link

Creating a network projection #81

Closed khanna7 closed 2 years ago

khanna7 commented 2 years ago

https://github.com/khanna-lab/cadre/blob/1463ec215866a7c45491b2483c8ec65493b80ee5/python/pycadre/cadre_model.py#L64-L73

When I try to create a network projection in the Model class, as shown in the highlighted lines, I get the following error:

adityakhanna@adityakhanna python % python -m pycadre myparams/model_params.yaml '{"STOP_AT": 100, "N_AGENTS": 100}' 
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/repast4py/network.py", line 855, in _parse_node
    agent = create_agent(n_id, agent_type, rank, **attribs)
  File "/Volumes/GoogleDrive/My Drive/code/cadre/python/pycadre/cadre_person.py", line 224, in create_person
    return Person(nid, agent_type, rank)
TypeError: Person.__init__() takes 3 positional arguments but 4 were given

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/Volumes/GoogleDrive/My Drive/code/cadre/python/pycadre/__main__.py", line 23, in <module>
    main()
  File "/Volumes/GoogleDrive/My Drive/code/cadre/python/pycadre/__main__.py", line 17, in main
    model = cadre_model.Model(params=params_list, comm=MPI.COMM_WORLD)
  File "/Volumes/GoogleDrive/My Drive/code/cadre/python/pycadre/cadre_model.py", line 69, in __init__
    read_network(fpath, self.context, cadre_person.Person.create_person, cadre_person.Person.restore_person)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/repast4py/network.py", line 1001, in read_network
    _parse_node(line, i + 2, graph_data, ctx, create_agent)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/repast4py/network.py", line 862, in _parse_node
    raise ValueError(f'Error reading graph description file on line {line_num}: "{line}". Expected node description with format: '
ValueError: Error reading graph description file on line 2: "0 0 0". Expected node description with format: "node_id agent_type rank" following by an optional json agent attribute dictionary
adityakhanna@adityakhanna python % 

I have a network.txt file that looks correct structurally, but I don't follow the error. Any suggestions would be most appreciated.

ncollier commented 2 years ago

I can't see where the 4 arguments are coming from in:

File "/Volumes/GoogleDrive/My Drive/code/cadre/python/pycadre/cadre_person.py", line 224, in create_person return Person(nid, agent_type, rank) TypeError: Person.__init__() takes 3 positional arguments but 4 were given

Can you print the nid, agent_type, and rank in create_person?

ncollier commented 2 years ago

I think I see what the issue is. Move the create_person and restore_person functions out of the Person class. And pass them to read_network as cadre_person.create_person and cadre_person.restore_person. The lack of self when those functions are in the Persons class is confusing Python I think.

khanna7 commented 2 years ago

Thanks @ncollier! This does seem to get closer. Couple questions:

The linked code results in the following:

nid:  <built-in function id>
agent_type  0
agent_rank  0
nid:  <built-in function id>
agent_type  0
agent_rank  0
nid:  <built-in function id>
agent_type  0
agent_rank  0
...

The agent_type and agent_rank make sense, but the nid seems to be giving an incorrect result. What might that be about?

ncollier commented 2 years ago

There's a typo in the print code: print("nid: ", id) -- id instead of nid. id is a built-in python function.

khanna7 commented 2 years ago

The following works!

Thanks so much!