programminghistorian / jekyll

Jekyll-based static site for The Programming Historian
http://programminghistorian.org
510 stars 229 forks source link

Temporal Network Analysis with R - errors running code #2868

Open acrymble opened 1 year ago

acrymble commented 1 year ago

I tried the code exactly as posted, and started having divergent results from the tutorial in the section starting "Once we have imported this temporal data".

# Make the temporal network dynamicCollabs <- networkDynamic( thenetwork, edge.spells = PHDynamicEdges, vertex.spells = PHDynamicNodes )

Returned the following:

Edge activity in base.net was ignored Created net.obs.period to describe network Network observation period info: Number of observation spells: 1 Maximal time range observed: 1257.5 until 1325 Temporal mode: continuous Time unit: unknown Suggested time increment: NA

Which I don't think is the expected result, and suggests something went wrong.

From there, the line:

plot(dynamicCollabs)

does not return a similar looking result to the (unnumbered) figure, despite the text saying it should.

Screen Shot 2023-02-23 at 14 42 55

The next code:

filmstrip(dynamicCollabs, displaylabels = FALSE)

gives error message:

No coordinate information found in network, running compute.animation Error in as.matrix.network.adjacency(net, attrname = weight.attr, expand.bipartite = TRUE) : Multigraphs not currently supported in as.matrix.network.adjacency. Exiting.

All further code also returns error messages. I wasn't able to get this to resolve. It did work for me and my students this time last year, so I suspect an issue with either newer R versions or a dependence updating and breaking things.

rivaquiroga commented 1 year ago

I get the same errors in the Spanish version

hawc2 commented 1 year ago

This lesson might be outdated now, and may not be easy to update.

It's odd because the first problem sounds related to the data/analysis, whereas the second problem sounds related to outdated software. So at first glance it's not clear if there's two layers of issues here to address.

@ZoeLeBlanc I see you we're a reviewer on this, do you have any thoughts on this bug?

ZoeLeBlanc commented 1 year ago

Hmm we would need to see the code and data to resolve this issue. Looking at the github repo, seems like this might be a similar issue https://github.com/statnet/ndtv/issues/50 but I'm not seeing anything on that repo to suggest this library is fully deprecated

jenniferisasi commented 1 year ago

May we contact the author in case he's been working with the code lately?

ZoeLeBlanc commented 1 year ago

Interestingly the issue I posted above was someone trying to work through our lesson 😅 https://github.com/statnet/networkDynamic/issues/14#issue-1280908174 . The person claimed that this link https://bomiklee.com/post/making-network-animations-using-ndtv-package/ helped them solve the issue by making a network list (not sure what that means). I did try running some of the lesson code and was able to get the initial plots working (though did have to wrap the plot function in the R View function).

I'm not an R person, so had ChatGPT rewrite the R code from that blogpost into Python:

import pandas as pd
import networkx as nx

nv = len(vtx)

rivlist = []

for i in range(1, 5):
    rivi = riv[riv['no'] == i]
    rivii = rivi[['stateabb1', 'stateabb2']].values
    netyr = nx.Graph()
    netyr.add_nodes_from(vtx['stateabb'])

    for edge in rivii:
        netyr.add_edge(edge[0], edge[1])

    nx.set_node_attributes(netyr, {node: i for node in netyr.nodes}, 'order')
    rivlist.append(netyr)

#Also, note that in this Python code, the vtx and riv variables should be Pandas DataFrames, corresponding to the R data.frames in the original R code.

def set_vertex_attribute(graph, attribute_name, attribute_values):
    nx.set_node_attributes(graph, attribute_values, attribute_name)

set_vertex_attribute(rivlist[0], 'vtx87', vtx87)

cols1 = []
for i in range(204):
    node = rivlist[0].nodes[i]
    ccode = rivlist[0].nodes[node]['vtx87']

    if ccode < 200:
        color = "red"
    elif 200 <= ccode < 400:
        color = "orange"
    elif 400 <= ccode < 630:
        color = "yellow"
    elif 630 <= ccode < 700:
        color = "green"
    else:
        color = "blue"

    cols1.append(color)

#In this Python code, the vtx87 variable should be a dictionary with keys as the node names and values as the corresponding attributes from the R code. For example, if in R the names(vtx87) is c("USA", "CAN") and vtx87 is c(2, 20), then in Python, the vtx87 should be {"USA": 2, "CAN": 20}.

I think contacting the author would make sense since this is a pretty chunking block of code to add. Instead of rewriting all the code, we could also ask the author to make the lesson not include multigraphs as an option too.

anisa-hawes commented 1 year ago

Thank you, all. I really appreciate your comments.

I'm happy to write to the author, and ask for their input to move forwards. I will update this Issue when I have further information.

anisa-hawes commented 1 year ago

Note: I have written to the author to explain the Issue and invite their contribution.

alexbrey commented 1 week ago

Thanks for reaching out. I'm not sure when one of the packages/dependencies that caused this was updated but I believe this is a problem caused by the "multiple = TRUE" attribute for the initial static network that serves as a base for the dynamic network. It should be resolvable by changing that to "multiple = FALSE"

The "No coordinate information found in network, running compute.animation" and "Edge activity in base.net was ignored Created net.obs.period to describe network Network observation period info: Number of observation spells: 1 Maximal time range observed: 1257.5 until 1325 Temporal mode: continuous Time unit: unknown Suggested time increment: NA" are just normal alerts, not errors.

charlottejmc commented 3 days ago

Thank you @alexbrey for getting back to us on this! Your help is very much appreciated.

@hawc2, I've opened branch Issue-2868 to make this small change to the code. I also added two info boxes to notify readers that the messages returned are normal alerts, not errors.

I was wondering whether you might have a moment to look through the lesson and decide whether it would need additional testing, or whether you're happy to merge it as is?

Thank you very much!

jenniferisasi commented 3 days ago

Oh! @charlottejmc please tag me too whenever Alex approves of the changes in EN so we can add the changes/edits in the ES version (that I happen to have translated myself). ¡Gracias! Thanks @alexbrey!

hawc2 commented 2 days ago

@charlottejmc I can take a closer look.

@alexbrey that's good to hear the messages are alerts and not errors, but it sounds like @acrymble also generated a plot that looked different than the one described in the lesson?

@ZoeLeBlanc seems to have successfully generated the plot as it appears in the lesson when she reran the code though, so maybe it is working?

That's the only outstanding question I see here, otherwise seems like the changes you mentioned Charlotte should be sufficient