Git Product home page Git Product logo

bctpy's People

Contributors

aestrivex avatar aksimhal avatar anaston avatar ariekahn avatar clbarnes avatar hippocampusgirl avatar i-zaak avatar jacobtfisher avatar jdkent avatar julianklug avatar liadomide avatar muriloschaefer avatar pedroconst avatar poldrack avatar pulkit-khandelwal avatar rmarkello avatar rudimeier avatar sviter avatar victoris93 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bctpy's Issues

Pythonic style

Questions regarding possible contributions and how well they would be received.

  • It seems the visualization module of BCT has not been ported to bctpy. Is this intentional? Is this planned for a future version?

  • Some names are not standardized across modules. For example, clustering.py uses clustering_coef_wd, where wd stands for weighted directed, whereas core.py uses assortativity_wei for weighted networks. I know this comes from the original BCT, but I think there's a point in making this package as good-quality as possible (and that includes consistent naming conventions).

  • Related to previous one. Is there interest in trying to make the package more pythonic, instead of a direct translation of BCT? For example,

    bctpy [master]$ flake8 . | wc -l
    546

There are 546 lines which deviate from flake8 styling guidelines. Is there interest in making bctpy adhere more to python standards?

If so, I would like to discuss the best way of setting up a PR.

[py3] core_tests.test_threshold_proportional FAIL

I know that py3 isn't officially supported, but in Fedora (I'm packaging for) we want to have it support, so I want to help you with porting.

======================================================================
FAIL: core_tests.test_threshold_proportional
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python3.4/site-packages/nose/case.py", line 198, in runTest
    self.test(*self.arg)
  File "/home/brain/rpmbuild/BUILD/python3-python-bctpy-0.4.1-0.1gita266f25.fc24/test/core_tests.py", line 9, in test_threshold_proportional
    assert np.allclose(np.sum(x), 22548.51206965)
AssertionError

----------------------------------------------------------------------

PEP8 compliance

Just to make things more consistent and readable (saving lines isn't so important now bct has been parcelled out into modules). Up to you if you want to allow this, I was just going to run it through autopep8.

Unresolved references in bct.py

Sorry if these are defined elsewhere as globals.

N, line 2533 (should be n?)
fullnode, line 6689 (should be fullnodes?)
bm, line 6880 (seems to be referenced before it's instantiated)

reorder_mod() error

Hi, am trying to reorder adjacency matrices by community-membership. I get the error:

  File "<ipython-input-44-6b89f77369d9>", line 9, in <module>
    [new_mat1, newmat2] = bct.reorder_mod(adj_time1234[:,:,i], my_comm.values());
  File "/Users/Ralf/anaconda/lib/python2.7/site-packages/bct/utils/visualization.py", line 677, in reorder_mod
    #if old == om[0]:
UnboundLocalError: local variable 'old' referenced before assignment

also, in a different project I am hitting this error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-47-ff95399df8e8> in <module>()
      1 contingencytable_slices_sorted = contingencytable_slices;
      2 for i in range(n_slices):
----> 3     [new_mat1, newmat2] = bct.reorder_mod(contingencytable_slices[:,:,i], my_comm.values());
      4     contingencytable_slices_sorted[:,:,i] = newmat2;

/Users/Ralf/anaconda/lib/python2.7/site-packages/bct/utils/visualization.pyc in reorder_mod(A, ci)
    692         # NOT DONE! OE NOES
    693 
--> 694         mod_imp = np.array((om, np.sign(np.arange(m) - pos),
    695                             np.abs(np.arange(m) - pos), am[x - 1, om - 1])).T
    696         print np.shape((mod_imp[:, 3][::-1], mod_imp[:, 2]))

ValueError: operands could not be broadcast together with shapes (5,) (2,) 

I checked the matrices, but they seem fine. In fact, I am executing the code in a for loop across multiple slices/timepoints and in the latter example the error appears only later (seems to have to do with the matrix values there - in this case mostly consisting of 0s and 1s). I compared the code to the matlab version but did not fully understand the differences.
Best, Ralf.

get components and components sizes without implementing the Dulmage-Mendelsohn decomposition from scratch

Hello,

A week or so ago I'd started translating some functions from BCT into Python and I came across your repo yesterday.
One of the functions I was interested in using was the get_components. Then I saw it was not implemented yet.

My first thought to find a solution was using networkx connected components.
This function returns, for the time being, the size of the largest component.
I think that using networkx.connected_components_subgraph you could actually
get the nodes of each individual graph.

Ideally, I'd like to have BCT only with nump and scipy, but maybe it'd be a good idea to use networkx as well.
https://github.com/networkx

By the way I tested this against the matlab version using the 998 connectivity matrix presented in:

Honey CJ, Sporns O, Cammoun L, Gigandet X, Thiran JP, Meuli R, Hagmann P (2009) Predicting human resting-state functional connectivity from structural connectivity. Proc Natl Acad Sci U S A 106: 2035–2040.

and I got the same results.

import numpy
import networkx

def get_components_sizes(A):
"""
Get connected components sizes.
Returns the size of the largest component of an undirected graph specified by the binary and
undirected connection matrix A.

Parameters
----------

A : array
         binary undirected (BU) connectivity matrix.

Returns
-------

largest component : float 
        size  of the largest component

Raises
-------
      Value Error
          If A is not square.


.. warning:: requires networkx 
.. author:: Paula Sanz Leon

"""


# Just to preserve the original code. Check if the input matrix is square.
# Further checks should include: check if it's binary for the functions that require so
# and check that is undirected. 

# Check if it is square
if A.shape[0] != A.shape[1]:
    raise ValueError('The input matrix is not square')
else:
    pass

# Binarize without modifying the original matrix (in case A is weighted)
temp_A = A.copy()
temp_A[temp_A > 0] = 1.0    

# Set diagonal elements to one
if numpy.diag(A).sum() != A.shape[0]:
   numpy.fill_diagonal(temp_A, 1.0)

# build a networkx graph to get largest connected component.
components = networkx.connected_components(networkx.from_numpy_matrix(numpy.matrix(temp_A)))
# For the time being returns the size of the largest component
component_sizes = [len(x) for x in components][0]
return component_sizes

core_periphery_dir

The module core_periphery_dir from bct.algorithms has many print statements that should be deleted. It outputs a lot of stuff that I just don't need to see when I run the function.

null_model_*_sign has bugs

b02a306 fixed one bug in bct.algorithms.reference.null_model_und_sign, but the directed equivalent has the same bug. Neither are exercised in unit tests, and fail for other reasons (on py37) when that bug is fixed:

Traceback (most recent call last):
  File "/home/barnesc/.pyenv/versions/3.6.5/envs/bctpy/lib/python3.6/site-packages/nose/case.py", line 198, in runTest
    self.test(*self.arg)
  File "/home/barnesc/work/code/bctpy/test/reference_tests.py", line 14, in test_null_model_und_sign
    bct.null_model_und_sign(x)
  File "/home/barnesc/work/code/bctpy/bct/algorithms/reference.py", line 1067, in null_model_und_sign
    R = rng.permutation(m)[:np.min((m, wei_period))]
TypeError: slice indices must be integers or None or have an __index__ method

Then when that slice bound is wrapped in int(),

Traceback (most recent call last):
  File "/home/barnesc/.pyenv/versions/3.6.5/envs/bctpy/lib/python3.6/site-packages/nose/case.py", line 198, in runTest
    self.test(*self.arg)
  File "/home/barnesc/work/code/bctpy/test/reference_tests.py", line 14, in test_null_model_und_sign
    bct.null_model_und_sign(x)
  File "/home/barnesc/work/code/bctpy/bct/algorithms/reference.py", line 1075, in null_model_und_sign
    P[i[o], :] *= f
TypeError: Cannot cast ufunc multiply output from dtype('float64') to dtype('int64') with casting rule 'same_kind'

Paranthesis mistake with assortativity_bin and assortativity_wei

Hi,

For both assortativity_bin and assortativity_wei, there is a mistake in calculation of "r"

Right formulation is

term1 = np.sum(degi_degj)/K
term2 = np.square(np.sum(.5_(degi+degj))/K)
term3 = np.sum(.5_(degi_degi+degj*degj))/K
r = (term1 - term2) / (term3 - term2)

for binary and

term1 = np.sum(stri_strj)/K
term2 = np.square(np.sum(.5_(stri+strj))/K)
term3 = np.sum(.5_(stri_stri+strj*strj))/K
r = (term1 - term2) / (term3 - term2)

for weighted matrices.

Regards,
Birkan

change np.all to np.allclose?

I was wondering if it would be better to change np.all to np.allclose in reference.py as is currently implemented in modularity.py?
if not np.all(W == W.T): raise BCTParamError("Input must be undirected")

For example np.all fails in the following example but np.allclose does not:

corr_mat =np.corrcoef(a) #symmetric matrix

print(corr_mat)
print(corr_mat.T) 

print(corr_mat == corr_mat.T) 

print(np.all(corr_mat == corr_mat.T)) #fails
print(np.allclose(corr_mat, corr_mat.T)) #passes

pc and wmd-z

It seems that these functions do not produce the same results as the Matlab code.

LaTeX in documentation

  • Add sphinx to requirements.txt. Otherwise make html inside docs complains.
  • The docs are very complete but lack a bit in formatting. Using LaTeX for some of the rendering shouldn't be too difficult within sphinx. These changes could go well with the proposed style rewrite.

community_louvain with negative weights yields uncorrect calculation of modularity

Checking with the original code of community_louvain I've found a bug in the 'negative_asym' treatment of negative weights.

The original code of community_louvain reads:
W1 = -W * (W < 0)
when one is building the matrix of negative weights, while in the bctpy version the minus is forgotten:

W1 = W * (W < 0)

I suggest to fix this issue that results in wrong computation of the modularity.

get_components: length of a generator

The line

cptsizes=np.zeros(len(cpts))

in get_components() raises the following error

*** TypeError: object of type 'generator' has no len()

One way to get rid of it is using

cptsizes=np.zeros(len(list(cpts)))

instead.

incompatible Q values

first using
cIDs1, Q1 = bct.modularity_louvain_und(A)

with a binary, undirected, symmetric matrix A, and then

Q2 =bct.modularity_und(A,gamma=1,kci=cIDs1)[1]

Q1 and Q2 are significantly different (Q1=0.52 and Q2=-0.01)

when I use my code for the traditional formula of Q, I get the exact same value with Q2

Runtime warnings in distance.charpath

Where D is an array which includes infinity, D*(D != np.inf) returns an array which contains np.nan because inf * 0 = nan. This raises a runtime warning in the calculation of eccentricity (and causes the eccentricity to have lots of nans in it).

D = 1 / D raises a runtime warning wherever an element is 0 (i.e. on the first diagonal). This should either be suppressed or the first diagonal should be set to 1 before the operation (it is set to 0 after it).

bct.community_louvain adjmat must not contain negative weights

Hi,

I am having a problem with the comunity_louvain function, it says that the adjancency matrix cannot have negative values. If my understanding is correct, there shouldn't be a problem, plus the matlab version allows negative values (this is specified in the help) but the python one doesn't?

Cheers,
Vicente

modularity_dir can fail: removing self-loops fixes it

but not all self-looping graphs hit the bug.

  File "connectome_data/metrics.py", line 157, in metrics
    modules, modularity = modularity_dir(self.binarised)
  File "/home/barnesc/.pyenv/versions/elegans_connectome/lib/python3.7/site-packages/bct/algorithms/modularity.py", line 557, in modularity_dir
    recur(init_mod)
  File "/home/barnesc/.pyenv/versions/elegans_connectome/lib/python3.7/site-packages/bct/algorithms/modularity.py", line 551, in recur
    recur(mod2)
  File "/home/barnesc/.pyenv/versions/elegans_connectome/lib/python3.7/site-packages/bct/algorithms/modularity.py", line 550, in recur
    recur(mod1)
  File "/home/barnesc/.pyenv/versions/elegans_connectome/lib/python3.7/site-packages/bct/algorithms/modularity.py", line 551, in recur
    recur(mod2)
  File "/home/barnesc/.pyenv/versions/elegans_connectome/lib/python3.7/site-packages/bct/algorithms/modularity.py", line 539, in recur
    mod_asgn_iter[imax] *= -1
IndexError: invalid index to scalar variable.

Here is a CSV adjacency matrix (directed, unweighted) which triggers the issue: modularity_dir_example.txt

breadth function wrong results

Hi @aestrivex , @rudimeier,

I'm using your python library. I'm trying to obtain the distances between nodes in a undirected graph.
This is my adjacency matrix:

[[ 0. 1.]
[ 1. 0.]]

This is my code:

distance, branch = bct.breadth(oom_adj,0)
print distance
print branch

and finally these are my results:

for the distance
[ 2. 1.]

for the branch
[-1. 0.]

For the distances result array the value of the 0 index is not equal 0. ¿I'm doing something wrong?

Thanks

install on windows machine

Hello,
I'm trying to install bctpy on Windows 10 in Anaconda virtual environment but getting error when i try to import the package.

Python 3.5.4 |Anaconda, Inc.| (default, Nov 8 2017, 14:34:30) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

import bctpy
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named 'bctpy'

The installation seemed to worked fine did not get any errors.

(bctpy) C:\Users\Documents\bctpy>conda list
packages in environment at C:\Users\Anaconda3\envs\bctpy:
bctpy 0.5.0 pip
certifi 2017.11.5 py35h456c6ae_0
icc_rt 2017.0.4 h97af966_0
intel-openmp 2018.0.0 hd92c6cd_8
mkl 2018.0.1 h2108138_4
numpy 1.13.3 py35hb7e80fb_0
pip 9.0.1 py35h691316f_4
python 3.5.4 h1357f44_23
scipy 1.0.0 py35h75710e8_0
setuptools 36.5.0 py35h21a22e4_0
vc 14 h2379b0c_2
vs2015_runtime 14.0.25123 hd4c4e62_2
wheel 0.30.0 py35h38a90bc_1
wincertstore 0.2 py35hfebbdb8_0

Any idea what's going on?

'cannot convert float infinity to integer'

When using betweenness_bin() with a connected graph, line
L[L==0]=np.inf

raises error "OverflowError: 'cannot convert float infinity to integer'". This is due to L being an integer array while "np.inf" being defined as a float. You may either change the data type of L, or assign a huge value instead of np.inf.

betweenness failure

Hi Roan,

Good to meet you via Bug Reporting. I'm working on a new pipeline called PyNets (see my repo) that integrates bctpy with nilearn, and I'm testing the variety of functions that you ported over from the BCT into bctpy, so you may hear a lot from me in the coming months/years.

First order of business, and I'm probably just being amateur here, but any idea why I'm having trouble extracting betweenness measures?

In [28]: in_mat_wei
Out[28]: 
array([[ 0.        ,  0.68737646,  0.48285148, ...,  0.09148687,
         0.4492529 ,  0.48168688],
       [ 0.68737646,  0.        ,  0.3552837 , ...,  0.        ,
         0.25858502,  0.25389435],
       [ 0.48285148,  0.3552837 ,  0.        , ...,  0.        ,
         0.40509753,  0.33309095],
       ..., 
       [ 0.09148687,  0.        ,  0.        , ...,  0.        ,
         0.09396403,  0.45157042],
       [ 0.4492529 ,  0.25858502,  0.40509753, ...,  0.09396403,
         0.        ,  0.54513586],
       [ 0.48168688,  0.25389435,  0.33309095, ...,  0.45157042,
         0.54513586,  0.        ]])

In [37]: bct.betweenness_wei(in_mat_wei)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-37-8ce1fdf60561> in <module>()
----> 1 bct.betweenness_wei(in_mat_wei)

/work/04171/dpisner/stampede/lib/python2.7/site-packages/bct/algorithms/centrality.pyc in betweenness_wei(G)
    131         DP = np.zeros((n,))
    132         for w in Q[:n - 1]:
--> 133             BC[w] += DP[w]
    134             for v in np.where(P[w, :])[0]:
    135                 DP[v] += (1 + DP[w]) * NP[v] / NP[w]

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

[py3] very_long_tests.test_link_communities FAIL

======================================================================
ERROR: very_long_tests.test_link_communities
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python3.4/site-packages/nose/case.py", line 198, in runTest
    self.test(*self.arg)
  File "/home/brain/rpmbuild/BUILD/python3-python-bctpy-0.4.1-0.1gita266f25.fc24/test/very_long_tests.py", line 9, in test_link_communities
    M = bct.link_communities(x)
  File "/home/brain/rpmbuild/BUILD/python3-python-bctpy-0.4.1-0.1gita266f25.fc24/bct/algorithms/modularity.py", line 346, in link_communities
    u1, u2 = np.where(ES[np.ix_(U, U)] == np.max(ES[np.ix_(U, U)]))
  File "/home/brain/rpmbuild/BUILD/python3-python-bctpy-0.4.1-0.1gita266f25.fc24/bct/algorithms/modularity.py", line 346, in link_communities
    u1, u2 = np.where(ES[np.ix_(U, U)] == np.max(ES[np.ix_(U, U)]))
  File "/usr/lib64/python3.4/bdb.py", line 48, in trace_dispatch
    return self.dispatch_line(frame)
  File "/usr/lib64/python3.4/bdb.py", line 66, in dispatch_line
    self.user_line(frame)
  File "/usr/lib64/python3.4/pdb.py", line 259, in user_line
    self.interaction(frame, None)
  File "/usr/lib64/python3.4/pdb.py", line 346, in interaction
    self._cmdloop()
  File "/usr/lib64/python3.4/pdb.py", line 319, in _cmdloop
    self.cmdloop()
  File "/usr/lib64/python3.4/cmd.py", line 126, in cmdloop
    line = input(self.prompt)
nose.proxy.TypeError: bad argument type for built-in operation
-------------------- >> begin captured stdout << ---------------------
hierarchy 0
hierarchy 1
hierarchy 2
hierarchy 3
hierarchy 4
hierarchy 5
hierarchy 6
> /home/brain/rpmbuild/BUILD/python3-python-bctpy-0.4.1-0.1gita266f25.fc24/bct/algorithms/modularity.py(346)link_communities()
-> u1, u2 = np.where(ES[np.ix_(U, U)] == np.max(ES[np.ix_(U, U)]))

--------------------- >> end captured stdout << ----------------------

It works under python 2

broadcast error in community_louvain

HI,
I am trying to use bct.community_louvain on a 144*144 matrix using this command

[S,Q] = bct.community_louvain(B)

but it gives me this error:

File "/Users/Ralf/anaconda/lib/python2.7/site-packages/bct/algorithms/modularity.py", line 140, in community_louvain

Hnm[:, m - 1] = np.sum(B[:, ci == m], axis=1) # node to module degree

ValueError: could not broadcast input array from shape (144,1) into shape (144)

I tried to fix and change the shape manually, but then am hitting issues further below in the code, so I was wondering if I am missing something here or if that could be a glitch.
Thanks, R.

null_model_und_sign not supported between instances of 'tuple' and 'int'

Hello,

I tried to use null_model_und_sign but it gave me this error:
/opt/anaconda3/lib/python3.6/site-packages/bct/algorithms/reference.py in null_model_und_sign(W, bin_swaps, wei_freq)
978 if np.size(np.where(Ap.flat)) < (n * (n - 1)):
979 W_r = randmio_und_signed(W, bin_swaps)
--> 980 Ap_r = W_r > 0
981 An_r = W_r < 0
982 else:

TypeError: '>' not supported between instances of 'tuple' and 'int'

I used an undirected signed matrix of 416x416. Could you help with this issue?

Cheers,
Vicente

sparse matrices

Is there anyway you can modify the participation coefficient function to handle sparse matrices? I am trying to calculate it on some huge matrices and I think it would be quite helpful to use scipy.sparse matrices.

community_louvain infinite loop

I have been running into an issue when using the community_louvain function in modularity.py (the 'negative_sym’ method) in which I often receive the error message:

('Modularity infinite loop style G. ' 'Please contact the developer.’)

I have compared the code to the matlab version and haven’t been able to identify what is going on (I do not experience this issue with matlab)

Wondering if anyone has any ideas as to what may be going on?

Need for a new release

By installing bctpy through (for instance) pip I don't receive the last changes that are already corrected in master branch. For example, Issues #63 #64 are corrected but not included yet in a new release beyond 0.5.0 in pip.

You really should correct this, as probably most people just install bct through pip and don't have this and other bugs corrected.

Seed argument for functions with RNGs

Would (slightly) break compatibility with the reference implementation, so feel free to reject if it's against the design goals.

Determinism is good for replicability, and using random seeds local to each unit of randomness is convenient. Currently one can set np.random.seed() outside of the function call, but that changes the global state as well: setting the np.random.seed() inside the function would do the same. It would be nice for randomness inside the functions to use a local np.random.RandomState instance if a seed is explicitly passed in, and the global otherwise.

A workaround which may work in some situations is to use unittest.mock.patch() to patch over the global np.random with a seeded instance of np.random.RandomState:

from unittest import mock
from bct.algorithms.reference import randmio_dir_connected

def randomiser_wrapper(adj, itr, seed=None):
    with mock.patch('numpy.random', np.random.RandomState(seed)):
        return randmio_dir_connected(adj, itr)

non-broadcast error participation coefficient

I kept getting a ValueError: non-broadcastable output operand with shape (100,1) doesn't match the broadcast shape (100,100).
Similar to https://stackoverflow.com/questions/47493559/valueerror-non-broadcastable-output-operand-with-shape-3-1-doesnt-match-the

I am able to get it to work like this:

def participation_coef(W, ci, degree='undirected'):
'''
Participation coefficient is a measure of diversity of intermodular
connections of individual nodes.
Parameters
----------
W : NxN np.ndarray
binary/weighted directed/undirected connection matrix
ci : Nx1 np.ndarray
community affiliation vector
degree : str
Flag to describe nature of graph 'undirected': For undirected graphs
'in': Uses the in-degree
'out': Uses the out-degree
Returns
-------
P : Nx1 np.ndarray
participation coefficient
'''
if degree == 'in':
W = W.T

_, ci = np.unique(ci, return_inverse=True)
ci += 1

n = len(W)  # number of vertices
Ko = np.sum(W, axis=1)  # (out) degree
Gc = np.dot((W != 0), np.diag(ci))  # neighbor community affiliation
Kc2 = np.zeros((n,))  # community-specific neighbors

#changed here
for i in range(1, int(np.max(ci)) + 1):
Kc2 = Kc2 + np.square(np.sum(W * (Gc == i), axis=1))
#change ends
P = np.ones((n,)) - Kc2 / np.square(Ko)
# P=0 if for nodes with no (out) neighbors
P[np.where(np.logical_not(Ko))] = 0

return P

community_louvain error with negative weights

The community_louvain function in modularity.py throws an error if the input matrix contains negative weights, even if 'negative_sym' or 'negative_asym' specified via the modularity parameter.

Looks as though lines 110-111 should be made conditional on the user choosing one of the other modularity parameters?

if np.min(W) < -1e-10:
        raise BCTParamError('adjmat must not contain negative weights')

Problem with generative_model (generative.py)

Greetings,

I am running into an IndexError problem when using the evaluate_generative_model function.
To give you a better picture of what am I doing, I am using hyperopt to optimize the hyperparameters eta and gamma from the function evaluate_generative_model, and the parameter space for each is [-10, 10] in steps of 0.1, so for each trial, it selects two values from this space and send them to the function. Here is my code:

# Define the hyperparameters space
gamma = np.r_[-10:10:0.1]
eta = np.r_[-10:10:0.1]
hp_space = {
        'gamma': hp.choice('gamma', gamma),
        'eta': hp.choice('eta', eta)
}

# Setting up the number of evaluations and Trials object
n_evals = 2000
trials = Trials()

# Running the optimization
res_gnm = fmin(
            lambda hps: bct.evaluate_generative_model(seed_network,
                                                  target_network,
                                                  mean_dist,
                                                  [hps['eta']],
                                                  gamma=[hps['gamma']],
                                                  model_type='matching')[0],
            space=hp_space, algo=tpe.suggest, trials=trials, max_evals=n_evals,
            rstate=np.random.RandomState(42)
        )

Apparently, this problem disappears if I add this to the generative.py code (line 327)

...
for ii in range(mseed, m):
            C = np.append(0, np.cumsum(Ff[u,v]))
            r_n = np.sum(np.random.random()*C[-1] >= C) #storing the original index
            r = r_n - 1 #subtracting 1 from it
            uu = u[r]
            vv = v[r]
            A[uu,vv] = A[vv,uu] = 1
...

which is basically subtracting one from the index (I was assuming that it could be a 0-indexing problem, since the original code came from a matlab toolbox).

Even with this fix, though, the same error occurs when I use the evaluate_generative_model function passing eta and gamma as vectors.
gnm_eval = bct.evaluate_generative_model(seed_network, sc_dataset[0]['weights_bu'], mean_dist, eta, gamma=gamma)

Is there anything that I am doing wrong when using this function?

Thank you for your help.

P.S: I am attaching the dist, seed and target matrices as numpy files.
dist.txt
seed.txt
target.txt

subgraph_centrality 2

in subgraph_centrality,

the line
lambdas=np.diag(vals)
is a miss-translation from Matlab version. In Matlab, eig() function returns eigenvalues in a matrix, and this line is used to extract the diagonal as vector. In python eig() already returns a vector (not a matrix) for eigenvalues. Thus, using diag() generates a matrix now with diagonal filled with the eigenvalues. And the final result is therefore wrong (both the shape and values).

You should simply discard this line.

Typo in nbs.py

In line 201: pval[i]=np.size(np.where(null>=sz_links[i]))/k
but it must be pvals[i]

subgraph_centrality

In subgraph_centrality,

the line
Cs=np.real(np.dot(vecs*vecs),np.exp(lambdas))

should be
Cs=np.real(np.dot(vecs*vecs, np.exp(lambdas)))

currently it complains "TypeError: Required argument 'b' (pos 2) not found" since ending parenthesis of np.dot is misplaced.

Python 3 branch

Welcome to 2008! If you create a branch for py3 I can raise a pull request to merge my branch in. All I did was run 2to3 -w -n . but I didn't get any errors when importing the installed package so it may all be ok. I didn't know what framework you were using to run unit tests locally (maybe travisCI is something to look into) so couldn't check with them.

Cut down long-running unit tests

Several of the unit tests take several minutes to complete (at least one > 10 minutes). This means it won't ever get through travisCI - and more importantly, because travis just cancels the build, you never see the output from the tests. Is there any way these algorithms could be applied to a simpler problem to cut down the execution time? For the moment on my travis branch I've put a 5-minute timeout on each test (so that it just fails them and moves on, so we can hopefully see the output). Nope, turns out computer scientists get angry if you try to timeout unit tests.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.