wiheto / teneto

Temporal Network Tools
GNU General Public License v3.0
85 stars 26 forks source link

Plotting slice_plot will fail if dataframe columns in wrong order #73

Closed kiview closed 3 years ago

kiview commented 4 years ago

If a TemporalNetwork object is generated from a DataFrame that complies to the format (i, j, t columns)

TemporalNetwork(from_df=t_df, nettype='bd')

there will be an IndexError when plotting a slice-plot:

~/.virtualenvs/ethereum-network-iTOQKzl-/lib/python3.8/site-packages/teneto/classes/network.py in plot(self, plottype, ij, t, ax, **plotparams)
    460             _, ax = plt.subplots(1)
    461         data_plot = teneto.utils.get_network_when(self, ij=ij, t=t)
--> 462         data_plot = teneto.utils.df_to_array(
    463             data_plot, self.netshape, self.nettype)
    464         ax = funs[plottype](data_plot, ax=ax, **plotparams)

~/.virtualenvs/ethereum-network-iTOQKzl-/lib/python3.8/site-packages/teneto/utils/utils.py in df_to_array(df, netshape, nettype)
    769                 idx = np.vstack([idx, idx[:, [1, 0, 2]]])
    770             idx = idx.astype(int)
--> 771             tnet[idx[:, 0], idx[:, 1], idx[:, 2]] = 1
    772         elif idx.shape[1] == 4:
    773             if nettype[-1] == 'u':

IndexError: index 9 is out of bounds for axis 2 with size 9

This happens specifically if the column order is t, i, j. A workaround is changing the column order in the DataFrame:

df= df[['i', 'j', 't']]
wiheto commented 3 years ago

Thanks.

Sorry it took a while to get this.

The fix is added in upcoming 0.5.3 release by adding df= df[['i', 'j', 't']] to TemporalNetwork (currently on master)

maximelucas commented 3 years ago

By doing this, the 4th possible column for weight is discarded (which breaks my code!) Could be replaced by

# Ensure order of columns
if len(df.colums)==4 :
    df = df[['i', 'j', 't', 'weight']]
elif len(df.colums)==3 :
    df = df[['i', 'j', 't']]
else : 
    print("Wrong number of columns in df")

I can submit a PR if you agree.

wiheto commented 3 years ago

Sorry for breaking your code!

Sounds like a good suggestion. Feel free to submit it.

maximelucas commented 3 years ago

No worries, thanks for making Teneto! My own code with temporal networks is here in case you're interested: https://gitlab.com/habermann_lab/phasik

I submitted a PR fixing the two issues now.