Last active
August 29, 2015 14:01
-
-
Save czarrar/d395b9d798978dbc817c to your computer and use it in GitHub Desktop.
Calculates degree centrality using CPAC code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
import os | |
from os import path | |
from CPAC.network_centrality import calc_centrality | |
### | |
# Load command-line argument | |
### | |
parser = argparse.ArgumentParser(description='Compute centrality for a given timeseries.') | |
# Inputs | |
parser.add_argument('-i', '--input', help='Input timeseries data file', | |
required=True) | |
parser.add_argument('-m', '--mask', help='Brain mask (by default the program will create a mask where voxels have non-zero variance regardeless of the user specified mask).') | |
# Option: Method | |
parser.add_argument('--degree', help='Calculate degree centrality', | |
action="store_true") | |
parser.add_argument('--eigen', help='Calculate eigenvector centrality', | |
action="store_true") | |
parser.add_argument('--lfcd', help='Calculate lFCD (local functional connectivity density)', | |
action="store_true") | |
# Option: Outputs | |
parser.add_argument('--binarize', action="store_true", | |
help='For a given voxel, save the number of connections that pass a threshold') | |
parser.add_argument('--weighted', action="store_true", | |
help='For a given voxel, save the sum of all connection weights that pass a threshold.') | |
# Option: Threshold | |
# TODO: explicitly check that only one is specified. | |
parser.add_argument('--no-threshold', action='store_true', | |
help="The raw correlations will be used and the approach will be very fast and low memory usage.") | |
parser.add_argument('--sparsity', type=float, | |
help="Sparsity based threshold. (Only one threshold option can be specified.)") | |
parser.add_argument('--pvalue', type=float, | |
help='P-value threshold for each connection. (Only one threshold option can be specified.)') | |
parser.add_argument('--rho', type=float, | |
help="Regular correlation threshold. (Only one threshold option can be specified.)") | |
# Option: Memory | |
parser.add_argument('--memlimit', type=float, | |
help="Memory limit that should be set.") | |
# Option: Threads | |
parser.add_argument('--nthreads', type=int, default=1, | |
help="Number of threads to parallel processes for Intel MKL") | |
# Output | |
parser.add_argument('-o', '--outdir', default=os.getcwd(), help="Output directory") | |
### | |
# Parse and Read User Args | |
### | |
args = parser.parse_args() | |
try: | |
import mkl | |
mkl.set_num_threads(args.nthreads) | |
except ImportError: | |
pass | |
method_options = [args.degree, args.eigen, args.lfcd] | |
if method_options.count(True) != 1: | |
raise SystemExit("either --degree, --eigen, or --lfcd must be specified") | |
method_option = [ i for i,opt in enumerate(method_options) if opt is True ][0] # 0=degree, 1=eigen, 2=lfcd | |
if not args.binarize and not args.weighted: | |
raise SystemExit("--binarize and/or --weighted must be specified") | |
weight_options = [args.binarize, args.weighted] | |
# 0 = probability p-value | |
# 1 = sparsity threshold | |
# 2 = actual threshold value | |
# 3 = no threshold and fast approach | |
if args.pvalue is not None: | |
option = 0 | |
threshold = args.pvalue | |
elif args.sparsity is not None: | |
option = 1 | |
threshold = args.sparsity | |
elif args.rho is not None: | |
option = 2 | |
threshold = args.rho | |
elif args.no_threshold: | |
option = 3 | |
threshold = None | |
else: | |
raise SystemExit("You must specify one threshold option: --pvalue, --sparsity, --rho, or --no-threshold.") | |
outdir = os.path.abspath(args.outdir) | |
infile = os.path.abspath(args.input) | |
maskfile = os.path.abspath(args.mask) | |
### | |
# Call on the Big Guy/Gal (CPAC) | |
### | |
curdir = os.getcwd() | |
#args.outdir = os.path.abspath(args.outdir) | |
os.chdir(outdir) | |
res = calc_centrality(infile, maskfile, method_option, option, threshold, | |
weight_options, args.memlimit) | |
print(res) | |
os.chdir(curdir) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Sample run using the script above. | |
func_file="/data/Projects/ABIDE_Initiative/CCS/processed/Pitt_50003/func/rest_1/rest.sm0.mni152.nii.gz" | |
mask_file="/home2/data/Projects/ABIDE_Initiative/CPAC/abide/templates/masks/mask_ccs_90percent_gm.nii.gz" | |
./cpac_centrality.py \ | |
-i ${func_file} \ | |
-m ${mask_file} \ | |
--degree \ | |
--binarize --weighted \ | |
--pvalue 0.001 \ | |
--memlimit 8 \ | |
-o $(pwd)/tmp2 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment