Skip to content

Commit

Permalink
Merge branch 'master' of github.com:WhitakerLab/BrainNetworksInPython…
Browse files Browse the repository at this point in the history
… into sphinx
  • Loading branch information
Islast committed Mar 22, 2018
2 parents 747e581 + 5b0e68c commit 964c58f
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 17 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,11 @@ ENV/

# Rope project settings
.ropeproject

# files generated by jupyter demo
corrmat_file.txt
corrmat_picture.png
corrmat_picture_LowRes.png
network_analysis/


Binary file modified BrainNetworksInPython/figures/NetworkSummary.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BrainNetworksInPython/figures/NetworkSummary_LowRes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 18 additions & 2 deletions BrainNetworksInPython/scripts/make_corr_matrices.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,30 @@
import pandas as pd
import stats_functions

def get_non_numeric_cols(df):
numeric = np.fromiter((np.issubdtype(y, np.number) for y in df.dtypes),bool)
non_numeric_cols = np.array(df.columns)[~numeric]
return non_numeric_cols


def create_residuals_df(df, names, covars_list):
'''
Return residuals of names columns correcting for the columns in covars_list
* df is a pandas data frame with participants as rows.
* names is a list of the brain regions you wish to correlate.
* covars_list is a list of covariates (as df column headings) that
you choose to correct for before correlating the regions.
df should be numeric for the columns in names and covars_list
'''
# Raise TypeError if any of the relevant columns are nonnumeric
non_numeric_cols = get_non_numeric_cols(df[names+covars_list])
if non_numeric_cols:
raise TypeError('DataFrame columns {} are non numeric'.format(', '.join(non_numeric_cols)))

# Make a new data frame that will contain
# the residuals for each column after correcting for
# the covariates in covars
df_res = df.copy()
df_res = df[names+covars_list].copy()

# Create your covariates array
if len(covars_list) > 1:
Expand All @@ -48,7 +59,12 @@ def create_corrmat(df_residuals, names, method='pearson'):
* names is a list of the brain regions you wish to correlate.
* method is the method of correlation fed to pandas.DataFram.corr
'''
return df_residuals.loc[:, names].corr(method=method)
# Raise TypeError if any of the relevant columns are nonnumeric
non_numeric_cols = get_non_numeric_cols(df_residuals)
if non_numeric_cols:
raise TypeError('DataFrame columns {} are non numeric'.format(', '.join(non_numeric_cols)))

return df_residuals.loc[:, names].astype(float).corr(method=method)

def save_mat(M, M_text_name):
'''
Expand Down
6 changes: 3 additions & 3 deletions BrainNetworksInPython/scripts/make_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def calculate_global_measures(G, R_list=None, n_rand=10, nodal_partition=None, R
# graphs as it is expecting then calculate a random
# graph here
if R_list is None:
R_list, R_nodal_partition_list = make_random_list(n_rand)
R_list, R_nodal_partition_list = make_random_list(G, n_rand)
else:
n = len(R_list)

Expand Down Expand Up @@ -201,7 +201,7 @@ def calculate_nodal_measures(G, centroids, aparc_names, nodal_partition=None, na
* 34_name (Desikan Killiany atlas region)
* 68_name (Desikan Killiany atlas region with hemisphere)
'''

import numpy as np
import networkx as nx

Expand Down Expand Up @@ -507,7 +507,7 @@ def assign_nodal_distance(G, centroids):
def shortest_path(G):
import networkx as nx
import numpy as np


shortestpl_dict_dict = dict(nx.shortest_path_length(G))

Expand Down
112 changes: 112 additions & 0 deletions README.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# BrainNetworksInPython  \n",
"[![Join the chat at https://gitter.im/WhitakerLab/BrainNetworksInPython](https://badges.gitter.im/WhitakerLab/BrainNetworksInPython.svg)](https://gitter.im/WhitakerLab/BrainNetworksInPython?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Build Status](https://travis-ci.org/WhitakerLab/BrainNetworksInPython.svg?branch=master)](https://travis-ci.org/WhitakerLab/BrainNetworksInPython)\n",
"[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/WhitakerLab/BrainNetworksInPython/blob/master/LICENSE)\n",
"\n",
"Welcome to the BrainNetworksInPython GitHub repository!:sparkles: <img align=\"right\" src=\"https://media.giphy.com/media/3ohhwwL4kj5z1Id6uI/giphy.gif\">\n",
"\n",
"## Getting Started\n",
"\n",
"### Prerequisites\n",
"\n",
"BrainNetworksInPython requires Python3 with the following Python modules installed\n",
"\n",
"* [numpy](https://pypi.python.org/pypi/numpy/1.13.0rc1)\n",
"* [pandas](https://pypi.python.org/pypi/pandas/0.20.1)\n",
"* [matplotlib](https://matplotlib.org/users/installing.html)\n",
"* [community/python-louvain](https://pypi.python.org/pypi/python-louvain/0.6) (pip install python-louvain if you use pip)\n",
"* [networkx](https://pypi.python.org/pypi/networkx/2.20)\n",
"* [seaborn](https://pypi.python.org/pypi/seaborn/)\n",
"\n",
"### Installing\n",
"\n",
"Clone the repository using either\n",
"```\n",
"git clone [email protected]:WhitakerLab/BrainNetworksInPython.git\n",
"``` \n",
"or \n",
"```\n",
"git clone https://github.com/WhitakerLab/BrainNetworksInPython.git\n",
"```\n",
"### demo\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'regionalmeasures_file' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-3-831028b2ab08>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mwrappers\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcorrmat_from_regionalmeasures\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mcfrm\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mcfrm\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcorrmat_from_regionalmeasures\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mregionalmeasures_file\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnames_file\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcorrmat_file\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnames_308_style\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'regionalmeasures_file' is not defined"
]
}
],
"source": [
"import wrappers.corrmat_from_regionalmeasures as cfrm\n",
"cfrm.corrmat_from_regionalmeasures(regionalmeasures_file, names_file, corrmat_file, names_308_style=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## What are we doing?\n",
"BrainNetworksInPython is a toolkit to analyse structural covariance brain networks using python. \n",
"\n",
"BrainNetworksInPython takes regional cortical thickness data obtained from structural MRI and generates a matrix of correlations between regions over a cohort of subjects. The correlation matrix is used alongside the [networkx package](https://networkx.github.io/) to generate a variety of networks and network measures.\n",
"\n",
"The BrainNetworksInPython codebase was first developed by Dr Kirstie Whitaker for the Neuroscience in Psychiatry Network publication \"Adolescence is associated with genomically patterned consolidation of the hubs of the human brain connectome\" published in PNAS in 2016 [(Whitaker*, Vertes* et al, 2016](http://dx.doi.org/10.1073/pnas.1601745113)). This project is to take the existing codebase (accessible at https://github.com/KirstieJane/NSPN_WhitakerVertes_PNAS2016) and turn it into a documented, tested python package that is easy to use and re-use.\n",
"\n",
"Check out our [tutorial](jupyter_demo.ipynb) to get started.\n",
"\n",
"## Want to get involved?\n",
"BrainNetworksInPython is openly developed and welcomes contributers.\n",
"\n",
"If you're thinking about contributing (:green_heart: you are loved), our [roadmap](https://github.com/WhitakerLab/BrainNetworksInPython/issues/12) and our [contributing guidelines](https://github.com/WhitakerLab/BrainNetworksInPython/blob/master/CONTRIBUTING.md) are a good place to start. You don't need advanced skills or knowledge to help out. Newbies to Python, neuroscience, git and GitHub are all welcome.\n",
"\n",
"If you have questions or want to get in touch you can join our [gitter lobby](https://gitter.im/WhitakerLab/BrainNetworksInPython?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge), tweet @Whitaker\\_Lab or email Isla at islastaden __*at*__ gmail __*dot*__ com.\n",
"\n",
"## Other Stuff\n",
"\n",
"To view our (successful) Mozilla Mini-Grant application, head [here](https://github.com/WhitakerLab/WhitakerLabProjectManagement/blob/master/FUNDING_APPLICATIONS/MozillaScienceLabMiniGrant_June2017.md).\n",
"\n",
"In October 2017 BrainNetworksInPython ran a MozFest [session](https://github.com/MozillaFoundation/mozfest-program-2017/issues/724)\n",
"\n",
"We are still choosing a name for our python package, vote here https://poll.ly/#/Gx4yMMY7"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
Welcome to the BrainNetworksInPython GitHub repository!:sparkles: <img align="right" src="https://media.giphy.com/media/3ohhwwL4kj5z1Id6uI/giphy.gif">





## What are we doing?
BrainNetworksInPython is a toolkit to analyse structural covariance brain networks using python.

BrainNetworksInPython takes regional cortical thickness data obtained from structural MRI and generates a matrix of correlations between regions over a cohort of subjects. The correlation matrix is used alongside the [networkx package](https://networkx.github.io/) to generate a variety of networks and network measures.

The BrainNetworksInPython codebase was first developed by Dr Kirstie Whitaker for the Neuroscience in Psychiatry Network publication "Adolescence is associated with genomically patterned consolidation of the hubs of the human brain connectome" published in PNAS in 2016 [(Whitaker*, Vertes* et al, 2016](http://dx.doi.org/10.1073/pnas.1601745113)). This project is to take the existing codebase (accessible at https://github.com/KirstieJane/NSPN_WhitakerVertes_PNAS2016) and turn it into a documented, tested python package that is easy to use and re-use.

Check out our [tutorial](https://github.com/WhitakerLab/BrainNetworksInPython/blob/master/Example_JupyterNotebook.ipynb) to get started.
Check out our [tutorial](jupyter_demo.ipynb) to get started.

## Want to get involved?
BrainNetworksInPython is openly developed and welcomes contributers.
Expand All @@ -22,8 +25,9 @@ If you're thinking about contributing (:green_heart: you are loved), our [roadma
If you have questions or want to get in touch you can join our [gitter lobby](https://gitter.im/WhitakerLab/BrainNetworksInPython?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge), tweet @Whitaker\_Lab or email Isla at islastaden __*at*__ gmail __*dot*__ com.

## Other Stuff
In October 2017 BrainNetworksInPython will be running a MozFest [session](https://github.com/MozillaFoundation/mozfest-program-2017/issues/724)

To view our (successful) Mozilla Mini-Grant application, head [here](https://github.com/WhitakerLab/WhitakerLabProjectManagement/blob/master/FUNDING_APPLICATIONS/MozillaScienceLabMiniGrant_June2017.md).

Help us choose a name for our python package by voting https://poll.ly/#/Gx4yMMY7
In October 2017 BrainNetworksInPython ran a MozFest [session](https://github.com/MozillaFoundation/mozfest-program-2017/issues/724)

We are still choosing a name for our python package, vote here https://poll.ly/#/Gx4yMMY7
24 changes: 15 additions & 9 deletions tutorials/jupyter_demo.ipynb

Large diffs are not rendered by default.

0 comments on commit 964c58f

Please sign in to comment.