benmaier / tacoma

Temporal networks in Python. Provides fast tools to analyze temporal contact networks and simulate dynamic processes on them using Gillespie's SSA.
http://tacoma.benmaier.org
Other
105 stars 7 forks source link

`tn.edges` does not support `.append` method #20

Open ulfaslak opened 5 years ago

ulfaslak commented 5 years ago

Commonly I will have some code like the following:

# Setup
tn = tc.edge_lists()
tn = num_nodes
tn = list(range(num_timesteps))
tn = num_timesteps

# Add edges conditional on criterion
for t in range(0, num_timesteps):
    edges_t = []
    for i in range(num_nodes):
        for j in range(i+1, num_nodes):
            if some_criterion(i, j):
                edges_t.append((i, j))
    tn.edges.append(edges_t)  # <-- `edges_t` DOESN'T ACTUALLY APPEND TO `tn.edges`

As indicated by the last comment, appending to tn.edges like this does nothing so after the loop has run tn.edges == []. Also no error is raised. Instead, when trying to do edge_activity_plot(tn) I received ValueError: min() arg is an empty sequence

Easy to reproduce just set tn.edges = [] and call edge_activity_plot(tn). I recommend either raising an error when appending to tn.edges, or enabling appending to tn.edges somehow.

benmaier commented 5 years ago

yeah, this is because I have not made vectors opaque yet (see https://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html?highlight=list#making-opaque-types )... I don't feel comfortable doing that atm because I don't understand the consequences yet. So far, the method of choice would be to construct a list and then pass that list to the edge_lists-instance as

# Setup
tn = tc.edge_lists()
tn.N = num_nodes
tn.t = list(range(num_timesteps))
tn.tmax = num_timesteps
edges = []

# Add edges conditional on criterion
for t in range(0, num_timesteps):
    edges_t = []
    for i in range(num_nodes):
        for j in range(i+1, num_nodes):
            if some_criterion(i, j):
                edges_t.append((i, j))
    edges.append(edges_t) 

tn.edges = edges

(see also here: http://rocs.hu-berlin.de/~tacoma/temporal_networks/construction_example.html )

I'll see whether I can raise an exception somehow, not sure how to do this.