Commit e0db8975 by Junhao WEN

Merge remote-tracking branch 'origin/master'

parents 42908403 ae89332a
Pipeline #1055 passed with stages
in 2 minutes 18 seconds
......@@ -109,6 +109,7 @@ unit:mac:
- cd test
- ln -s /Volumes/builds/Clinica/data_ci ./data
- pytest -v
- module purge
- source deactivate
......
......@@ -2,7 +2,7 @@
Welcome to the [**Clinica**](http://clinica.run) software! All the information can be found on the [wiki](http://clinica.run/doc) section!
[![pipeline status](https://gitlab.icm-institute.org/aramislab/clinica/badges/master/pipeline.svg)](https://gitlab.icm-institute.org/aramislab/clinica/commits/master)
[![pipeline status](https://gitlab.icm-institute.org/aramislab/clinica/badges/dev/pipeline.svg)](https://gitlab.icm-institute.org/aramislab/clinica/commits/dev)
[![conda install](https://anaconda.org/aramislab/clinica/badges/installer/conda.svg)](http://clinica.run/doc/Installation/)
[![platform](https://anaconda.org/aramislab/clinica/badges/platforms.svg)](http://clinica.run/doc/Installation/)
[![license](https://anaconda.org/aramislab/clinica/badges/license.svg)](http://clinica.run/doc/Installation/)
......@@ -11,10 +11,11 @@ from os.path import expanduser
from colorama import Fore
PIPELINE_CATEGORIES = {
'CLINICA_COMPULSORY': '%sClinica mandatory arguments%s' % (Fore.YELLOW, Fore.RESET),
'OPTIONAL': '%sPipeline options%s' % (Fore.YELLOW, Fore.RESET),
'CLINICA_OPTIONAL': '%sClinica standard options%s' % (Fore.YELLOW, Fore.RESET),
'ADVANCED': '%sPipeline advanced options%s' % (Fore.YELLOW, Fore.RESET),
'CLINICA_COMPULSORY': '%sClinica mandatory arguments%s' % (Fore.BLUE, Fore.RESET),
'OPTIONAL': '%sPipeline options%s' % (Fore.BLUE, Fore.RESET),
'CLINICA_OPTIONAL': '%sClinica standard options%s' % (Fore.BLUE, Fore.RESET),
'ADVANCED': '%sPipelines advanced options%s' % (Fore.BLUE, Fore.RESET),
'IOTOOLS_OPTIONS': '%sOptional arguments%s' % (Fore.BLUE, Fore.RESET)
}
......@@ -42,15 +43,16 @@ class CmdParser:
self._name = None
def set_content(self):
self._args._positionals.title = '%sMandatory arguments%s' % (Fore.YELLOW, Fore.RESET)
self._args._optionals.title = '%sOptional arguments%s' % (Fore.YELLOW, Fore.RESET)
from colorama import Fore
self._args._positionals.title = '%sMandatory arguments%s' % (Fore.BLUE, Fore.RESET)
self._args._optionals.title = '%sOptional arguments%s' % (Fore.BLUE, Fore.RESET)
if self._description is None:
self._description = self._name
self._args.description = '%sIf you are not familiar with Clinica, see: http://clinica.run/doc/InteractingWithClinica/%s' % \
(Fore.CYAN, Fore.RESET)
(Fore.GREEN, Fore.RESET)
else:
self._args.description = '%s%s\n\nIf you are not familiar with Clinica, see: http://clinica.run/doc/InteractingWithClinica/%s' % \
(Fore.CYAN, self._description, Fore.RESET)
(Fore.GREEN, self._description, Fore.RESET)
@property
def options(self): return self._args
......@@ -144,3 +146,57 @@ def get_cmdparser_names(objects=None):
objects = get_cmdparser_objects()
for x in objects:
yield x.name
# class CmdParserInsightToBids(CmdParser):
#
# def define_name(self):
# self._name = 'insight-to-bids'
#
# def define_options(self):
# self._args.add_argument("dataset_directory",
# help='Path of the unorganized INSIGHT directory.')
# self._args.add_argument("bids_directory",
# help='Path to the BIDS directory.')
# self._args.add_argument("-co", type=bool, default=False,
# help='(Optional) Given an already existing BIDS output folder, convert only the clinical data.')
#
# def run_command(self, args):
# from clinica.bids import insight_to_bids
# insight_to_bids.convert(args.dataset_directory, args.bids_directory)
# class CmdParserPrevDemAlsToBids(CmdParser):
#
# def define_name(self):
# self._name = 'prevdemals-to-bids'
#
# def define_options(self):
# self._args.add_argument("dataset_directory",
# help='Path of the unorganized INSIGHT directory.')
# self._args.add_argument("bids_directory",
# help='Path to the BIDS directory.')
# self._args.add_argument("-co", type=bool, default=False,
# help='(Optional) Given an already existing BIDS output folder, convert only the clinical data.')
#
# def run_command(self, args):
# from clinica.bids import prevdemals_to_bids
# prevdemals_to_bids.convert(args.dataset_directory, args.bids_directory)
# class CmdParserHmtcToBids(CmdParser):
#
# def define_name(self):
# self._name = 'hmtc-to-bids'
#
# def define_options(self):
# self._args.add_argument("dataset_directory",
# help='Path of the unorganized HMTC directory.')
# self._args.add_argument("bids_directory",
# help='Path to the BIDS directory.')
#
# def run_command(self, args):
# from clinica.iotools import hmtc_to_bids
# hmtc_to_bids.convert(args.dataset_directory, args.bids_directory)
......@@ -40,16 +40,41 @@ class CmdParserMergeTsv(ce.CmdParser):
self._description = 'Merge TSV files containing clinical data of a BIDS dataset into a single TSV file.'
def define_options(self):
self._args.add_argument("bids_directory",
help='Path to the BIDS dataset directory.')
self._args.add_argument("out_directory",
help='Path to the output directory.')
self._args.add_argument("-tf", '--true_false_mode', type=bool, default=False,
help='(Optional) Convert all the field with binary meaning into True and False values.')
from clinica.engine.cmdparser import PIPELINE_CATEGORIES
# Clinica compulsory arguments (e.g. BIDS, out_tsv)
clinica_comp = self._args.add_argument_group(PIPELINE_CATEGORIES['CLINICA_COMPULSORY'])
clinica_comp.add_argument("bids_directory",
help='Path to the BIDS dataset directory.')
clinica_comp.add_argument("out_tsv",
help='Path to the output file.')
# Optional arguments
iotools_options = self._args.add_argument_group(PIPELINE_CATEGORIES['IOTOOLS_OPTIONS'])
iotools_options.add_argument('-caps', "--caps_directory", type=str, default=None,
help='(Optional) path to a CAPS directory.')
iotools_options.add_argument('-p', "--pipelines", nargs="*", type=str, default=None,
help='(Optional) pipelines which will be merged to the .tsv file. \n'
'Default: all pipeline are merged')
iotools_options.add_argument('-atlas', "--atlas_selection", nargs="*", type=str, default=None,
help='(Optional) atlas that will be merged. \n'
'Default: all atlases are merged')
iotools_options.add_argument('-pvc', "--pvc_restriction", type=int, default=None,
help='(Optional) indicates restriction on the label [_pvc-rbv]\n'
'Default: all atlases are merged\n'
'0: atlases without the label only are merged\n'
'1: atlases with the label only are merged')
iotools_options.add_argument('-group', '--group_selection', nargs="*", type=str, default=None,
help='(Optional) groups that will be merged. \n'
'Default: all groups are merged')
iotools_options.add_argument("-tsv", "--subjects_sessions_tsv",
help='TSV file containing the subjects with their sessions.')
def run_command(self, args):
from clinica.iotools.utils import data_handling as dt
dt.create_merge_file(args.bids_directory, args.out_directory, args.true_false_mode)
dt.create_merge_file(args.bids_directory, args.out_tsv,
caps_dir=args.caps_directory, pipelines=args.pipelines,
atlas_selection=args.atlas_selection, pvc_restriction=args.pvc_restriction,
tsv_file=args.subjects_sessions_tsv, group_selection=args.group_selection)
class CmdParserMissingModalities(ce.CmdParser):
......@@ -58,7 +83,7 @@ class CmdParserMissingModalities(ce.CmdParser):
self._name = 'check-missing-modalities'
def define_description(self):
self._description = 'Check missing modalities in a BIDS directory.'
self._description = 'Check missing modalities in a BIDS directory'
def define_options(self):
self._args.add_argument("bids_directory",
......
......@@ -11,6 +11,7 @@ from clinica.pipelines.machine_learning import base
import clinica.pipelines.machine_learning.voxel_based_io as vbio
import clinica.pipelines.machine_learning.vertex_based_io as vtxbio
import clinica.pipelines.machine_learning.region_based_io as rbio
import clinica.pipelines.machine_learning.tsv_based_io as tbio
import clinica.pipelines.machine_learning.svm_utils as utils
......@@ -394,3 +395,96 @@ class CAPSVertexBasedInput(CAPSInput):
def save_weights_as_nifti(self, weights, output_dir):
pass
class CAPSTSVBasedInput(CAPSInput):
def __init__(self, caps_directory, subjects_visits_tsv, diagnoses_tsv, group_id, image_type, atlas, dataset,
pvc=None, precomputed_kernel=None):
"""
Args:
caps_directory:
subjects_visits_tsv:
diagnoses_tsv:
group_id:
image_type: 'T1', 'fdg', 'av45', 'pib' or 'flute'
atlas:
precomputed_kernel:
"""
super(CAPSTSVBasedInput, self).__init__(caps_directory, subjects_visits_tsv, diagnoses_tsv, group_id,
image_type, precomputed_kernel)
self._atlas = atlas
self._pvc = pvc
self._dataset = dataset
self._orig_shape = None
self._data_mask = None
if atlas not in ['AAL2', 'Neuromorphometrics', 'AICHA', 'LPBA40', 'Hammers']:
raise Exception("Incorrect atlas name. It must be one of the values 'AAL2', 'Neuromorphometrics', 'AICHA', 'LPBA40', 'Hammers' ")
def get_images(self):
"""
Returns: string
"""
#import pandas as pd
pass
#if self._images is not None:
# return self._images
#print self._group_id
#print self._atlas
#print self._image_type
#if self._image_type == 'T1':
# self._images = str('group-' + self._group_id + '_T1w_space-' + self._atlas + '_map-graymatter')
### to implement for PET
#return self._images
def get_x(self):
"""
Returns: a numpy 2d-array.
"""
#if self._x is not None:
# return self._x
print 'Loading TSV subjects'
string = str('group-' + self._group_id + '_T1w_space-' + self._atlas + '_map-graymatter')
self._x = tbio.load_data(string, self._caps_directory, self._subjects, self._sessions, self._dataset)
print 'Subjects loaded'
return self._x
def save_weights_as_nifti(self, weights, output_dir):
"""
Args:
weights:
output_dir:
Returns:
"""
#output_filename = path.join(output_dir, 'weights.nii.gz')
#rbio.weights_to_nifti(weights, self._atlas, output_filename)
pass
\ No newline at end of file
......@@ -288,7 +288,7 @@ class RB_RepHoldOut_DualSVM(base.MLWorkflow):
class RB_RepHoldOut_LogisticRegression(base.MLWorkflow):
def __init__(self, caps_directory, subjects_visits_tsv, diagnoses_tsv, group_id, image_type, atlas,
output_dir, pvc=None, n_threads=15, n_iterations=100, test_size=0.3,
grid_search_folds=10, balanced=True, c_range=np.logspace(-6, 2, 17), splits_indices=None):
......@@ -305,20 +305,20 @@ class RB_RepHoldOut_LogisticRegression(base.MLWorkflow):
image_type, atlas, pvc)
self._validation = None
self._algorithm = None
def run(self):
x = self._input.get_x()
y = self._input.get_y()
self._algorithm = algorithm.LogisticReg(x, y, balanced=self._balanced,
grid_search_folds=self._grid_search_folds,
c_range=self._c_range,
n_threads=self._n_threads)
self._validation = validation.RepeatedHoldOut(self._algorithm, n_iterations=self._n_iterations, test_size=self._test_size)
classifier, best_params, results = self._validation.validate(y, n_threads=self._n_threads, splits_indices=self._splits_indices)
classifier_dir = os.path.join(self._output_dir, 'classifier')
if not path.exists(classifier_dir):
os.makedirs(classifier_dir)
......@@ -507,7 +507,7 @@ class VB_LearningCurveRepHoldOut_DualSVM(base.MLWorkflow):
class RB_RepKFold_DualSVM(base.MLWorkflow):
def __init__(self, caps_directory, subjects_visits_tsv, diagnoses_tsv, group_id, image_type, atlas,
output_dir, pvc=None, n_threads=15, n_iterations=100, test_size=0.3,
output_dir, pvc=None, n_threads=15, n_iterations=100, test_size=0.3,n_folds = 10,
grid_search_folds=10, balanced=True, c_range=np.logspace(-6, 2, 17), splits_indices=None):
self._output_dir = output_dir
self._n_threads = n_threads
......@@ -516,6 +516,7 @@ class RB_RepKFold_DualSVM(base.MLWorkflow):
self._grid_search_folds = grid_search_folds
self._balanced = balanced
self._c_range = c_range
self._n_folds = n_folds
self._splits_indices = splits_indices
self._input = input.CAPSRegionBasedInput(caps_directory, subjects_visits_tsv, diagnoses_tsv, group_id,
......@@ -553,3 +554,105 @@ class RB_RepKFold_DualSVM(base.MLWorkflow):
self._validation.save_results(self._output_dir)
self._input.save_weights_as_nifti(weights, classifier_dir)
class TB_RepHoldOut_DualSVM(base.MLWorkflow):
def __init__(self, caps_directory, subjects_visits_tsv, diagnoses_tsv, group_id, image_type, atlas, dataset,
output_dir, pvc=None, n_threads=15, n_iterations=100, test_size=0.3,
grid_search_folds=10, balanced=True, c_range=np.logspace(-6, 2, 17), splits_indices=None):
self._output_dir = output_dir
self._n_threads = n_threads
self._n_iterations = n_iterations
self._test_size = test_size
self._grid_search_folds = grid_search_folds
self._balanced = balanced
self._c_range = c_range
self._splits_indices = splits_indices
self._input = input.CAPSTSVBasedInput(caps_directory, subjects_visits_tsv, diagnoses_tsv, group_id, image_type,
atlas, dataset, pvc)
self._validation = None
self._algorithm = None
def run(self):
x = self._input.get_x()
y = self._input.get_y()
kernel = self._input.get_kernel()
self._algorithm = algorithm.DualSVMAlgorithm(kernel,
y,
balanced=self._balanced,
grid_search_folds=self._grid_search_folds,
c_range=self._c_range,
n_threads=self._n_threads)
self._validation = validation.RepeatedHoldOut(self._algorithm, n_iterations=self._n_iterations, test_size=self._test_size)
classifier, best_params, results = self._validation.validate(y, n_threads=self._n_threads, splits_indices=self._splits_indices)
classifier_dir = path.join(self._output_dir, 'classifier')
if not path.exists(classifier_dir):
os.makedirs(classifier_dir)
self._algorithm.save_classifier(classifier, classifier_dir)
self._algorithm.save_parameters(best_params, classifier_dir)
weights = self._algorithm.save_weights(classifier, x, classifier_dir)
#self._input.save_weights_as_nifti(weights, classifier_dir)
self._validation.save_results(self._output_dir)
class TB_RepHoldOut_RandomForest(base.MLWorkflow):
def __init__(self, caps_directory, subjects_visits_tsv, diagnoses_tsv, group_id, image_type, atlas, dataset,
output_dir, pvc=None, n_threads=15, n_iterations=100, test_size=0.3,
grid_search_folds=10, balanced=True, n_estimators_range=(100, 200, 400),
max_depth_range=[None], min_samples_split_range=[2],
max_features_range=('auto', 0.25, 0.5), splits_indices=None):
self._output_dir = output_dir
self._n_threads = n_threads
self._n_iterations = n_iterations
self._test_size = test_size
self._grid_search_folds = grid_search_folds
self._balanced = balanced
self._n_estimators_range = n_estimators_range
self._max_depth_range = max_depth_range
self._min_samples_split_range = min_samples_split_range
self._max_features_range = max_features_range
self._splits_indices = splits_indices
self._input = input.CAPSTSVBasedInput(caps_directory, subjects_visits_tsv, diagnoses_tsv, group_id,
image_type, atlas, dataset, pvc)
self._validation = None
self._algorithm = None
def run(self):
x = self._input.get_x()
y = self._input.get_y()
self._algorithm = algorithm.RandomForest(x, y, balanced=self._balanced,
grid_search_folds=self._grid_search_folds,
n_estimators_range=self._n_estimators_range,
max_depth_range=self._max_depth_range,
min_samples_split_range=self._min_samples_split_range,
max_features_range=self._max_features_range,
n_threads=self._n_threads)
self._validation = validation.RepeatedHoldOut(self._algorithm, n_iterations=self._n_iterations, test_size=self._test_size)
classifier, best_params, results = self._validation.validate(y, n_threads=self._n_threads, splits_indices=self._splits_indices)
classifier_dir = os.path.join(self._output_dir, 'classifier')
if not path.exists(classifier_dir):
os.makedirs(classifier_dir)
self._algorithm.save_classifier(classifier, classifier_dir)
self._algorithm.save_parameters(best_params, classifier_dir)
weights = self._algorithm.save_weights(classifier, classifier_dir)
#self._input.save_weights_as_nifti(weights, classifier_dir)
#self._validation.save_results(self._output_dir)
# coding: utf8
import numpy as np
import pandas as pd
import nibabel as nib
import os
__author__ = "Simona Bottani"
__copyright__ = "Copyright 2016-2018, The Aramis Lab Team"
__credits__ = ["Simona Bottani"]
__license__ = "See LICENSE.txt file"
__version__ = "0.1.0"
__maintainer__ = "Arnaud Marcoux"
__email__ = "simona.bottani@icm-institute.com"
__status__ = "Development"
def load_data(images, caps_directory, subjects, sessions, dataset):
"""
Args:
images:
caps_directory:
subjects_visits_tsv:
dataset:
Returns:
np 2D array
"""
df = pd.io.parsers.read_csv(os.path.join(caps_directory), sep='\t')
all_vector = np.array([])
participant_id = subjects
print len(participant_id)
session_id = sessions
for i in xrange(len(participant_id)):
df_sub = df[df.participant_id == participant_id[i]]
df_analysis = df_sub[[col for col in df_sub.columns if images in col]]
all_vector = np.append(all_vector, df_analysis.values)
data = np.zeros((len(participant_id), df_analysis.shape[1]))
data_temp = np.split(all_vector, len(participant_id))
for i in xrange(len(participant_id)):
for j in xrange(df_analysis.shape[1]):
data[i][j] = data_temp[i][j]
return data
......@@ -136,8 +136,8 @@ class StatisticsSurfaceCLI(ce.CmdParser):
raise Exception('You must specify a --feature_label when using the --custom_files flag')
# Check if the group label has been existed, if yes, give an error to the users
if os.path.exists(os.path.join(os.path.abspath(self.absolute_path(args.caps_directory)), 'groups', 'group-' + args.group_label)):
error_message = 'group_id : ' + args.group_label + ' already exists, please choose another one or delete the existing folder and also the working directory and rerun the pipelines'
if os.path.exists(os.path.join(os.path.abspath(self.absolute_path(args.caps_directory)), 'groups', 'group-' + args.group_id)):
error_message = 'group_id : ' + args.group_id + ' already exists, please choose another one or delete the existing folder and also the working directory and rerun the pipelines'
raise Exception(error_message)
pipeline = StatisticsSurface(
......
......@@ -16,7 +16,7 @@ setup(
description='Software platform for clinical neuroimaging studies',
long_description=open('README.md').read(),
author='ARAMIS Lab',
maintainer='Clinica developpers',
maintainer='Clinica developers',
maintainer_email='clinica-user@googlegroups.com',
license='MIT license',
packages=find_packages(exclude=('tests', 'tests.*')),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment