ComScan package

Submodules

ComScan.clustering module

Author: Alexandre CARRE
Created on: Jan 14, 2021
class ComScan.clustering.KElbowVisualizer(estimator, ax=None, k=10, metric='distortion', timings=True, locate_elbow=True, n_jobs=None, verbose=0, pre_dispatch='2*n_jobs', **kwargs)[source]

Bases: yellowbrick.cluster.base.ClusteringScoreVisualizer

The K-Elbow Visualizer implements the “elbow” method of selecting the optimal number of clusters for K-means clustering. K-means is a simple unsupervised machine learning algorithm that groups data into a specified number (k) of clusters. Because the user must specify in advance what k to choose, the algorithm is somewhat naive – it assigns all members to k clusters even if that is not the right k for the dataset.

The elbow method runs k-means clustering on the dataset for a range of values for k (say from 1-10) and then for each value of k computes an average score for all clusters. By default, the distortion score is computed, the sum of square distances from each point to its assigned center. Other metrics can also be used such as the silhouette score, the mean silhouette coefficient for all samples or the calinski_harabasz score, which computes the ratio of dispersion between and within clusters.

When these overall metrics for each model are plotted, it is possible to visually determine the best value for k. If the line chart looks like an arm, then the “elbow” (the point of inflection on the curve) is the best value of k. The “arm” can be either up or down, but if there is a strong inflection point, it is a good indication that the underlying model fits best at that point.

Note

yellowbrick.cluster.elbow
Implements the elbow method for determining the optimal number of clusters.
Author: Benjamin Bengfort
Created: Thu Mar 23 22:36:31 2017 -0400
Copyright (C) 2016 The scikit-yb developers
For license information, see LICENSE.txt
ID: elbow.py [5a370c8] benjamin@bengfort.com
Parameters
  • estimator (a scikit-learn clusterer) – Should be an instance of an unfitted clusterer, specifically KMeans or MiniBatchKMeans. If it is not a clusterer, an exception is raised.

  • ax (matplotlib Axes, default: None) – The axes to plot the figure on. If None is passed in the current axes will be used (or generated if required).

  • k (integer, tuple, or iterable) – The k values to compute silhouette scores for. If a single integer is specified, then will compute the range (2,k). If a tuple of 2 integers is specified, then k will be in np.arange(k[0], k[1]). Otherwise, specify an iterable of integers to use as values for k.

  • metric (string, default: "distortion") –

    Select the scoring metric to evaluate the clusters. The default is the mean distortion, defined by the sum of squared distances between each observation and its closest centroid. Other metrics include:

    • distortion: mean sum of squared distances to centers

    • silhouette: mean ratio of intra-cluster and nearest-cluster distance

    • calinski_harabasz: ratio of within to between cluster dispersion

  • timings (bool, default: True) – Display the fitting time per k to evaluate the amount of time required to train the clustering model.

  • locate_elbow (bool, default: True) – Automatically find the “elbow” or “knee” which likely corresponds to the optimal value of k using the “knee point detection algorithm”. The knee point detection algorithm finds the point of maximum curvature, which in a well-behaved clustering problem also represents the pivot of the elbow curve. The point is labeled with a dashed line and annotated with the score and k values.

  • n_jobs (int, default: None) – Number of jobs to run in parallel. Training the estimator and computing the score are parallelized over the cross-validation splits. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors.

  • verbose (int, default: 0) – The verbosity level.

  • pre_dispatch (int or str, default: '2*n_jobs') – Controls the number of jobs that get dispatched during parallel execution. Reducing this number can be useful to avoid an explosion of memory consumption when more jobs get dispatched than CPUs can process. This parameter can be: - None, in which case all the jobs are immediately created and spawned. Use this for lightweight and fast-running jobs, to avoid delays due to on-demand spawning of the jobs - An int, giving the exact number of total jobs that are spawned - A str, giving an expression as a function of n_jobs, as in ‘2*n_jobs’

  • kwargs (dict) – Keyword arguments that are passed to the base class and may influence the visualization as defined in other Visualizers.

k_scores_

The silhouette score corresponding to each k value.

Type

array of shape (n,) where n is no. of k values

k_timers_

The time taken to fit n KMeans model corresponding to each k value.

Type

array of shape (n,) where n is no. of k values

elbow_value_

The optimal value of k.

Type

integer

elbow_score_

The silhouette score corresponding to the optimal value of k.

Type

float

estimators_

a scikit-learn fitted estimator

Type

BaseEstimator

Examples

>>> from yellowbrick.cluster import KElbowVisualizer
>>> from sklearn.cluster import KMeans
>>> model = KElbowVisualizer(KMeans(), k=10)
>>> X = np.array([[1, 2], [1, 4], [1, 0],
...               [4, 2], [4, 4], [4, 0]])
>>> model.fit(X)
>>> model.show()

Notes

Modification from yellowbrick consist of get the best_estimator based on the finded elbow_value

If you get a visualizer that doesn’t have an elbow or inflection point, then this method may not be working. The elbow method does not work well if the data is not very clustered; in this case, you might see a smooth curve and the value of k is unclear. Other scoring methods, such as BIC or SSE, also can be used to explore if clustering is a correct choice.

For a discussion on the Elbow method, read more at Robert Gove’s Block website. For more on the knee point detection algorithm see the paper “Finding a “kneedle” in a Haystack”.

See also

The scikit-learn documentation for the silhouette_score and calinski_harabasz_score. The default, distortion_score, is implemented in yellowbrick.cluster.elbow.

draw()[source]

Draw the elbow curve for the specified scores and values of K.

finalize()[source]

Prepare the figure for rendering by setting the title as well as the X and Y axis labels and adding the legend.

fit(X, y=None, **kwargs)[source]

Fits n KMeans models where n is the length of self.k_values_, storing the silhouette scores in the self.k_scores_ attribute. The “elbow” and silhouette score corresponding to it are stored in self.elbow_value and self.elbow_score respectively. This method finishes up by calling draw to create the plot.

class ComScan.clustering.KMeansConstrainedMissing(n_clusters=8, size_min=None, size_max=None, em_iter=10, n_init=10, max_iter=300, features_reduction=None, n_components=2, tol=0.0001, verbose=False, random_state=None, copy_x=True, n_jobs=1)[source]

Bases: sklearn.base.TransformerMixin, sklearn.base.ClusterMixin, sklearn.base.BaseEstimator

K-Means clustering with minimum and maximum cluster size constraints with possible missing values

Parameters
  • n_clusters (int, optional, default: 8) – The number of clusters to form as well as the number of centroids to generate.

  • size_min (int, optional, default: None) – Constrain the label assignment so that each cluster has a minimum size of size_min. If None, no constrains will be applied

  • size_max (int, optional, default: None) – Constrain the label assignment so that each cluster has a maximum size of size_max. If None, no constrains will be applied

  • em_iter (int, default: 10) – expectation–maximization (EM) iteration for convergence of missing values. Use when no features reduction is applied and missing values.

  • n_init (int, default: 10) – Number of times the k-means algorithm will be run with different centroid seeds. The final results will be the best output of n_init consecutive runs in terms of inertia.

  • max_iter (int, default: 300) – Maximum number of iterations of the k-means algorithm for a single run.

  • features_reduction (str, default: None) – Method for reduction of the embedded space with n_components. Can be pca or umap.

  • n_components (int, default: 2) – Dimension of the embedded space for features reduction.

  • tol (float, default: 1e-4) – Relative tolerance with regards to inertia to declare convergence

  • verbose (int, default: 0) – Verbosity mode.

  • random_state (int, RandomState instance or None, optional, default: None) – If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by np.random.

  • copy_x (boolean, default: True) – When pre-computing distances it is more numerically accurate to center the data first. If copy_x is True, then the original data is not modified. If False, the original data is modified, and put back before the function returns, but small numerical differences may be introduced by subtracting and then adding the data mean.

  • n_jobs (int, default: 1) –

    The number of jobs to use for the computation. This works by computing each of the n_init runs in parallel.

    If -1 all CPUs are used. If 1 is given, no parallel computing code is used at all, which is useful for debugging. For n_jobs below -1, (n_cpus + 1 + n_jobs) are used. Thus for n_jobs = -2, all CPUs but one are used.

cluster_centers_

Coordinates of cluster centers

Type

array, [n_clusters, n_features]

labels_

Labels of each point

inertia_

Sum of squared distances of samples to their closest cluster center.

Type

float

cls_
Type

KMeansConstrained classifier object

cls_features_reduction_
Type

PCA or UMAP reduction object

centroids_

Centroids found at the last iteration of k-means.

Type

array

X_hat_

Copy of X with the missing values filled in.

Type

array

mu_
Type

Columns means

Examples

>>> X = np.array([[1, 2], [1, 4], [1, 0],
...                [4, 2], [4, 4], [4, 0]])
>>> clf = KMeansConstrainedMissing(
...     n_clusters=2,
...     size_min=2,
...     size_max=5,
...     random_state=0
... )
>>> clf.fit_predict(X)
array([0, 0, 0, 1, 1, 1], dtype=int32)
>>> clf.cluster_centers_
array([[ 1.,  2.],
       [ 4.,  2.]])
>>> clf.labels_
array([0, 0, 0, 1, 1, 1], dtype=int32)

Notes

K-means problem constrained with a minimum and/or maximum size for each cluster.

The constrained assignment is formulated as a Minimum Cost Flow (MCF) linear network optimisation problem. This is then solved using a cost-scaling push-relabel algorithm. The implementation used is Google’s Operations Research tools’s SimpleMinCostFlow.

Ref:

1. Bradley, P. S., K. P. Bennett, and Ayhan Demiriz. “Constrained k-means clustering.” Microsoft Research, Redmond (2000): 1-8. 2. Google’s SimpleMinCostFlow implementation: https://github.com/google/or-tools/blob/master/ortools/graph/min_cost_flow.h

fit(X, y=None)[source]

Compute k-means clustering with given constants.

Parameters
  • X (array-like, shape=(n_samples, n_features)) – Training instances to cluster.

  • y (Ignored) –

fit_predict(X, y=None)[source]

Compute cluster centers and predict cluster index for each sample.

Equivalent to calling fit(X) followed by predict(X) but also more efficient.

Parameters

X ({array-like, sparse matrix}, shape = [n_samples, n_features]) – New data to transform.

Returns

labels – Index of the cluster each sample belongs to.

Return type

array, shape [n_samples,]

predict(X, size_min='init', size_max='init')[source]

Predict the closest cluster each sample in X belongs to given the provided constraints. The constraints can be temporally overridden when determining which cluster each datapoint is assigned to.

Only computes the assignment step. It does not re-fit the cluster positions.

Parameters
  • X (array-like, shape = [n_samples, n_features]) – New data to predict.

  • size_min (int, optional, default: size_min provided with initialisation) – Constrain the label assignment so that each cluster has a minimum size of size_min. If None, no constrains will be applied. If ‘init’ the value provided during initialisation of the class will be used.

  • size_max (int, optional, default: size_max provided with initialisation) – Constrain the label assignment so that each cluster has a maximum size of size_max. If None, no constrains will be applied. If ‘init’ the value provided during initialisation of the class will be used.

Returns

labels – Index of the cluster each sample belongs to.

Return type

array, shape [n_samples,]

ComScan.clustering.optimal_clustering(X, size_min=10, metric='distortion', features_reduction=None, n_components=2, n_jobs=1, random_state=None, visualize=False)[source]

Function to find the optimal clustering using a constrained k means. Two method are available to find the optimal number of cluster silhouette or elbow.

Parameters
  • X (Union[DataFrame, ndarray]) – array-like or DataFrame of floats, shape (n_samples, n_features) The observations to cluster.

  • size_min (int) – Constrain the label assignment so that each cluster has a minimum size of size_min. If None, no constrains will be applied. default: None

  • metric (str) – Select the scoring metric to evaluate the clusters. The default is the mean distortion, defined by the sum of squared distances between each observation and its closest centroid. Other metrics include: - distortion: mean sum of squared distances to centers - silhouette: mean ratio of intra-cluster and nearest-cluster distance - calinski_harabasz: ratio of within to between cluster dispersion

  • features_reduction (Optional[str]) – Method for reduction of the embedded space with n_components. Can be pca or umap. Default: None

  • n_components (int) – Dimension of the embedded space for features reduction. Default 2.

  • n_jobs (int) – int The number of jobs to use for the computation. This works by computing each of the n_init runs in parallel. If -1 all CPUs are used. If 1 is given, no parallel computing code is used at all, which is useful for debugging. For n_jobs below -1, (n_cpus + 1 + n_jobs) are used. Thus for n_jobs = -2, all CPUs but one are used.

  • random_state (Optional[int]) – int, RandomState instance or None, optional, default: None If int, random_state is the seed used by the random number generator; If None, the random number generator is the RandomState instance used by np.random.

  • visualize (bool) – bool, default: False If True, calls show()

Return type

Tuple[KMeansConstrained, Union[UMAP, PCA], int, ndarray, int, Sequence[float], float, ndarray, ndarray, ndarray]

Returns

  • cls: KMeansConstrained classifier object

  • cls_features_reduction: PCA or UMAP reduction object

  • cluster_nb: optimal number of cluster

  • labels: label[i] is the code or index of the centroid the i’th observation is closest to.

  • ref_label: cluster label with the minimal within-cluster sum-of-squares.

  • wicss_clusters: within-cluster sum-of-squares for each cluster

  • best_wicss_cluster: minimal wicss.

  • centroid: Centroids found at the last iteration of k-means.

  • X_hat: Copy of X with the missing values filled in.

ComScan.neurocombat module

Author: Alexandre CARRE
Created on: Jan 14, 2021
class ComScan.neurocombat.AutoCombat(features, sites_features=None, sites=None, size_min=10, metric='distortion', use_ref_site=False, scaler_clustering=StandardScaler(), discrete_cluster_features=None, continuous_cluster_features=None, features_reduction=None, n_components=2, threshold_missing_sites_features=25, drop_site_columns=False, discrete_combat_covariates=None, continuous_combat_covariates=None, empirical_bayes=True, parametric=True, mean_only=False, return_only_features=False, n_jobs=1, random_state=123, copy=True)[source]

Bases: ComScan.neurocombat.Combat

Harmonize/normalize features using Combat’s parametric empirical Bayes framework.

Combat need to have well-known acquisition sites or scanner to harmonize features. It is sometimes difficult to define an imaging acquisition site if on two sites imaging parameters can be really similar. ComScan gives the possibility to automatically determine the number of sites and their association based on imaging features (e.g. dicom tags) by clustering. Thus ComScan can be used on data not seen during training because it can predict which imager best matches the one it has seen during training.

Parameters
  • features (Target features to be harmonized.) –

  • sites_features (Target variable for define (acquisition sites or scanner) by clustering.) –

  • sites (Target variable for ComScan problems (e.g. acquisition sites or scanner)) – This argument is Optional. If this argument is provided will run traditional ComBat else AutoCombat. In this case args: sites_features, size_min, method, scaler_clustering, discrete_cluster_features, continuous_cluster_features, threshold_missing_sites_features, drop_site_columns are unused.

  • size_min (Constraint of the minimum size of site for clustering.) –

  • metric ("distortion", "silhouette" or "calinski_harabasz".) – Metric to define the optimal number of cluster. Default: distortion.

  • use_ref_site (Use a ref site to be used as reference for batch adjustment. The ref site used is the cluster) – with the minimal inertia. i.e minimizing within-cluster sum-of-squares.

  • scaler_clustering (Scaler to use for continuous site features. Need to be a scikit learn scaler.) – Default is StandardScaler().

  • discrete_cluster_features (Target sites_features which are categorical to one-hot (e.g. ManufacturerModelName)) –

  • continuous_cluster_features (Target sites_features which are continuous to scale (e.g. EchoTime)) –

  • features_reduction (Method for reduction of the embedded space with n_components. Can be 'pca' or 'umap'.) – Default is None.

  • n_components (Dimension of the embedded space for features reduction.) – Default is 2.

  • threshold_missing_sites_features (Threshold of acceptable missing features for sites features clustering.) – 25 specify that 75% of all samples need to have this features. Default is 25.

  • drop_site_columns (Drop sites columns find by clustering in return.) – Default is False.

  • discrete_combat_covariates (Target covariates which are categorical (e.g. male or female)) –

  • continuous_combat_covariates (Target covariates which are continuous (e.g. age)) –

  • empirical_bayes (Performed empirical bayes.) – Default is True.

  • parametric (Performed parametric adjustements.) – Default is True.

  • mean_only (Adjust only the mean (no scaling)) – Default is False.

  • return_only_features (Return only features.) – Default is False.

  • n_jobs (The number of jobs to use for the computation. This works by computing each of the n_init runs in parallel.) – If -1 all CPUs are used. If 1 is given, no parallel computing code is used at all, which is useful for debugging. For n_jobs below -1, (n_cpus + 1 + n_jobs) are used. Thus for n_jobs = -2, all CPUs but one are used. Default is 1.

random_stateint, RandomState instance or None, optional, default: 123

If int, random_state is the seed used by the random number generator; If None, the random number generator is the RandomState instance used by np.random.

copySet to False to perform inplace row normalization and avoid a copy (if the input is already a numpy array).

Default is True.

cls_
Type

clustering classifier object

info_clustering_

wicss_clusters, best_wicss_cluster

Type

Dictionary that stores info of clustering from sites_features with cluster_nb, labels, ref_label

cls_feature_reduction_
Type

feature reduction object

clustering_data_features_mean_
Type

dict of mean for clustering data (use for imputation)

X_hat_
Type

array after fit

clustering_data_features_
Type

column features for clustering from train (after encoding + scaling)

clustering_data_discrete_features_
Type

column features for clustering after one-hot encoding

dict_cls_fitted
Type

dict of columns of fitted cls used for fitted clustering data

Examples

>>> data = pd.DataFrame([{"features_1": 0.97, "site_features_0": 2, "site_features_1": 0},
>>> {"features_1": 1.35, "site_features_0": 1.01, "site_features_1": 1},
>>> {"features_1": 1.43, "site_features_0": 1.09, "site_features_1": 1},
>>> {"features_1": 0.85, "site_features_0": 2.3, "site_features_1": 0}])
>>> auto_combat = AutoCombat(features=["features_1"], sites_features=["site_features_0", "site_features_1"],
>>> continuous_cluster_features=["site_features_0", "site_features_1"], size_min=2))
>>> print(auto_combat.fit(data))
AutoCombat(continuous_cluster_features=['site_features_0', 'site_features_1'],
       discrete_cluster_features=[], features=['features_1'],
       sites=['sites'],
       sites_features=['site_features_0', 'site_features_1'], size_min=2))

Notes

NaNs values are not treated.

Warning

Be sure to have the same sites features between fit and transform. The choice has not been to imposed an entry format to check a colum name or a slice.

fit(X, *y)[source]

Compute sites, ref_site using clustering. Then compute the stand mean, var pooled, gamma star, delta star to be used for later adjusted data from Combat.

Parameters
  • X (array-like or DataFrame of shape (n_samples, n_features)) – Requires the columns needed by the ComScan(). The data used to find adjustments.

  • *y (y in scikit learn: None) – Ignored.

Returns

self – Fitted ComScan estimator.

Return type

object

transform(X)[source]

Scale features of X according to combat estimator.

Parameters

X (array-like or DataFrame of shape (n_samples, n_features) Requires the columns needed by the Combat()) – Input data that will be transformed.

Returns

Xt – Transformed data.

Return type

array-like of shape (n_samples, n_features)

class ComScan.neurocombat.Combat(features, sites, discrete_covariates=None, continuous_covariates=None, ref_site=None, empirical_bayes=True, parametric=True, mean_only=False, return_only_features=False, raise_ref_site=True, copy=True)[source]

Bases: sklearn.base.BaseEstimator, sklearn.base.TransformerMixin

Harmonize/normalize features using Combat’s parametric empirical Bayes framework

Parameters
  • features (Target features to be harmonized) –

  • sites (Target variable for ComScan problems (e.g. acquisition sites or scanner)) –

  • discrete_covariates (Target covariates which are categorical (e.g. male or female)) –

  • continuous_covariates (Target covariates which are continuous (e.g. age)) –

  • ref_site (Variable value (acquisition sites or scanner) to be used as reference for batch adjustment.) – Default is False.

  • empirical_bayes (Performed empirical bayes.) – Default is True.

  • parametric (Performed parametric adjustements.) – Default is True.

  • mean_only (Adjust only the mean (no scaling)) – Default is False.

  • return_only_features (Return only features.) – Default is False.

  • raise_ref_site (raise when the reference site pass as arguments not exist, else set to no reference.) – Default is True.

  • copy (Set to False to perform inplace row normalization and avoid a copy (if the input is already a numpy array)) – Default is True.

info_dict_fit_

batch_levels, ref_level, n_batch, n_sample, sample_per_batch, batch_info

Type

dictionary that stores batch info of fitted data with:

stand_mean_

Standardized mean

Type

array-like

var_pooled_

Variance pooled

Type

array-like

mod_mean_

Mod mean

Type

array-like

gamma_star_

Adjustement gamma star

Type

array-like

delta_star_

Adjustement delta star

Type

array-like

info_dict_transform_

batch_levels, ref_level, n_batch, n_sample, sample_per_batch, batch_info

Type

dictionary that stores batch info of transformed data with

Examples

>>> data = pd.DataFrame([{"features_1": 0.97, "features_2": 2, "sites": 0},
>>> {"features_1": 1.35, "features_2": 1.01, "sites": 1}, {"features_1": 1.43, "features_2": 1.09, "sites": 1},
>>> {"features_1": 0.85, "features_2": 2.3, "sites": 0}])
>>> combat = Combat(features=["features_1", "features_2"], sites=["sites"], ref_site=1)
>>> print(combat.fit(data))
Combat(continuous_covariates=[], discrete_covariates=[],
   features=['features_1', 'features_2'], ref_site=1, sites=['sites'])
>>> print(combat.gamma_star_)
[[-11.85476756  27.30493785]
[  0.           0.        ]]
>>> print(combat.transform(data))
[[1.40593957 1.01395564 0.        ]
[1.35       1.01       1.        ]
[1.43       1.09       1.        ]
[1.37064296 1.08999992 0.        ]]

Notes

NaNs values are not treated.

fit(X, *y)[source]

Compute the stand mean, var pooled, gamma star, delta star to be used for later adjusted data.

Parameters
  • X (array-like or DataFrame of shape (n_samples, n_features) Requires the columns needed by the Combat()) – The data used to find adjustments.

  • *y (y in scikit learn: None) – Ignored.

Returns

self – Fitted combat estimator.

Return type

object

load_fit(filepath)[source]

load a fitted model attribute info_dict_fit_, stand_mean_, var_pooled_, gamma_star_, delta_star_

Parameters

filepath (str) – filepath of the pkl file to load

Return type

None

save_fit(filepath)[source]

save a fitted model attribute info_dict_fit_, stand_mean_, var_pooled_, gamma_star_, delta_star_

Parameters

filepath (str) – filepath were to save. if no extension .pkl will add it

Return type

None

transform(X)[source]

Scale features of X according to combat estimator.

Parameters

X (array-like or DataFrame of shape (n_samples, n_features) Requires the columns needed by the Combat()) – Input data that will be transformed.

Returns

Xt – Transformed data.

Return type

array-like of shape (n_samples, n_features)

class ComScan.neurocombat.ImageCombat(image_path, sites_features=None, sites=None, save_path_fit='fit_data', save_path_transform='transform_data', size_min=10, method='silhouette', use_ref_site=False, scaler_clustering=StandardScaler(), discrete_cluster_features=None, continuous_cluster_features=None, features_reduction=None, n_components=2, threshold_missing_sites_features=25, drop_site_columns=True, discrete_combat_covariates=None, continuous_combat_covariates=None, empirical_bayes=True, parametric=True, mean_only=False, random_state=123, flattened_dtype=<class 'numpy.float16'>, output_dtype=<class 'numpy.float32'>, copy=True)[source]

Bases: ComScan.neurocombat.AutoCombat

Harmonize/normalize features using Combat’s parametric empirical Bayes framework directly on image.

ImageCombat allow the possibility to Harmonize/normalize a set of NIFTI images. All images must have the same dimensions and orientation. A common mask is created based on an heuristic proposed by T.Nichols. Images are then vectorizing for ComScan. ImageCombat allows the possibily to use Combat (well-defined site) or AutoCombat (clustering for sites finding)

Parameters
  • image_path (image_path of nifti files.) –

  • sites_features (Target variable for define (acquisition sites or scanner) by clustering.) –

  • sites (Target variable for ComScan problems (e.g. acquisition sites or scanner)) – This argument is Optional. If this argument is provided will run traditional ComBat. In this case args: sites_features, size_min, method, scaler_clustering, discrete_cluster_features, continuous_cluster_features, threshold_missing_sites_features, drop_site_columns are unused.

  • size_min (Constraint of the minimum size of site for clustering.) –

  • method ("silhouette" or "elbow". Method to define the optimal number of cluster.) – Default is silhouette.

  • use_ref_site (Use a ref site to be used as reference for batch adjustment. The ref site used is the cluster) – with the minimal inertia. i.e minimizing within-cluster sum-of-squares. Default is False.

  • scaler_clustering (Scaler to use for continuous site features. Need to be a scikit learn scaler.) – Default is StandardScaler().

  • discrete_cluster_features (Target sites_features which are categorical to one-hot (e.g. ManufacturerModelName)) –

  • continuous_cluster_features (Target sites_features which are continuous to scale (e.g. EchoTime)) –

  • features_reduction (Method for reduction of the embedded space with n_components. Can be 'pca' or 'umap'.) – Default is None.

  • n_components (Dimension of the embedded space for features reduction.) – Default is 2.

  • threshold_missing_sites_features (Threshold of acceptable missing features for sites features clustering.) – 25 specify that 75% of all samples need to have this features. Default is 25.

  • drop_site_columns (Drop sites columns find by clustering in return.) –

  • discrete_combat_covariates (Target covariates which are categorical (e.g. male or female)) –

  • continuous_combat_covariates (Target covariates which are continuous (e.g. age)) –

  • empirical_bayes (Performed empirical bayes.) – Default is True.

  • parametric (Performed parametric adjustements.) – Default is True.

  • mean_only (Adjust only the mean (no scaling)) – Default is False.

  • random_state (int, RandomState instance or None, optional, default: 123) – If int, random_state is the seed used by the random number generator; If None, the random number generator is the RandomState instance used by np.random.

  • copy (Set to False to perform inplace row normalization and avoid a copy (if the input is already a numpy array)) – Default is True.

mask_
Type

array-like of the common brain mask

flattened_array_
Type

flattened array of all the training set

Notes

NaNs values are not treated.

fit(X, *y)[source]

Compute sites, ref_site using clustering. Then compute the stand mean, var pooled, gamma star, delta star to be used for later adjusted data from Combat.

Parameters
  • X (array-like or DataFrame of shape (n_samples, n_features)) – Requires the columns needed by the ComScan(). The data used to find adjustments.

  • *y (y in scikit learn: None) – Ignored.

Returns

self – Fitted ComScan estimator.

Return type

object

transform(X)[source]

Scale image according to combat estimator and save it.

Parameters

X (array-like or DataFrame of shape (n_samples, n_features) Requires the columns needed by the ImageCombat()) – Input data that will be transformed.

Returns

Return type

None, save transformed image

ComScan.nifti module

Author: Alexandre CARRE
Created on: Jan 14, 2021
ComScan.nifti.flatten_nifti_files(input_path, mask, output_flattened_array_path='flattened_array', dtype=<class 'numpy.float16'>, save=True, compress_save=True)[source]

Flattened list of nifti files to a flattened array [n_images, n_masked_voxels] and save to .npy or .npz if compressed

Parameters
  • input_path (List[str]) – List of nifti files path

  • mask (Union[str, ndarray]) – path of mask or array

  • output_flattened_array_path (str) – path of the output flattened array. No extension is needed. Will be save as .npy if no compression, else .npz

  • save (bool) – save the flattened array

  • dtype ([<class 'numpy.dtype'>, typing.Callable]) – dtype of the output flattened array. Default is float 16 to save memory

  • compress_save (bool) – If true compress the numpy array into .npz

Returns

flattened array [n_images, n_masked_voxels]

ComScan.utils module

Author: Alexandre CARRE
Created on: Jan 14, 2021
ComScan.utils.check_exist_vars(df, _vars)[source]

Check that a list of columns name exist in a DataFrame.

Parameters
  • df (DataFrame) – a DataFrame

  • _vars (List) – List of columns name to check

Return type

ndarray

Returns

index of columns name

Raise

ValueError if missing features

ComScan.utils.check_is_nii_exist(input_file_path)[source]

Check if a directory exist.

Parameters

input_file_path (str) – string of the path of the nii or nii.gz.

Return type

str

Returns

string if exist, else raise Error.

Raise

FileNotFoundError or FileExistsError

ComScan.utils.column_var_dtype(df, identify_dtypes=('object'))[source]

identify type of columns in DataFrame

Parameters
  • df (DataFrame) – input dataframe

  • identify_dtypes (Sequence[str]) – pandas dtype

Return type

DataFrame

Returns

summary df with col index and col name for all identify_dtypes vars

ComScan.utils.fix_columns(df, columns, inplace=False, extra_nans=False)[source]

Fix columns for the test set. When the train was encoded with pd.get_dummies.

Parameters
  • df (DataFrame) – input dataframe

  • columns (List[str]) – columns of the original dataframe

  • inplace (bool) – If False, return a copy. Otherwise, do operation inplace and return None

  • extra_nans (bool) – put extra columns as nans based on one hot encoding columns

Return type

DataFrame

Returns

the corrected version of DataFrame for test set

ComScan.utils.get_column_index(df, query_cols)[source]

Get columns index from columns name

Parameters
  • df (DataFrame) – input dataframe

  • query_cols (List[str]) – List name of colunns

Return type

ndarray

Returns

array of column index

ComScan.utils.load_nifty_volume_as_array(input_path_file)[source]

Load nifty image into numpy array [z,y,x] axis order. The output array shape is like [Depth, Height, Width].

Parameters

input_path_file (str) – input path file, should be ‘.nii’ or ‘.nii.gz’

Return type

Tuple[ndarray, Tuple[Tuple, Tuple, Tuple]]

Returns

a numpy data array, (with header)

ComScan.utils.mat_to_bytes(nrows, ncols, dtype=32, out='GB')[source]

Calculate the size of a numpy array in bytes.

Parameters
  • nrows (int) – the number of rows of the matrix.

  • ncols (int) – the number of columns of the matrix.

  • dtype (int) – the size of each element in the matrix. Defaults to 32bits.

  • out (str) – the output unit. Defaults to gigabytes (GB)

Return type

float

Returns

the size of the matrix in the given unit

ComScan.utils.one_hot_encoder(df, columns, drop_column=True, dummy_na=False, add_nan_columns=False, inplace=False)[source]

Encoding categorical feature in the dataframe, allow possibility to keep NaN. The categorical feature index and name are from cat_var function. These columns need to be “object” dtypes.

Parameters
  • df (DataFrame) – input dataframe

  • columns (List[str]) – List of columns to encode

  • drop_column (bool) – Set to True to drop the original column after encoding. Default to True.

  • dummy_na (bool) – Add a column to indicate NaNs, if False NaNs are ignored.

  • add_nan_columns (bool) – Add a empty nan columns if not create (can be used are other categories)

  • inplace (bool) – If False, return a copy. Otherwise, do operation inplace and return None

Return type

DataFrame

Returns

new dataframe where columns are one hot encoded

ComScan.utils.save_to_nii(im, header, output_dir, filename, mode='image', gzip=True)[source]

Save numpy array to nii.gz format to submit.

Parameters
  • im (ndarray) – array numpy

  • header ((<class 'tuple'>, <class 'tuple'>, <class 'tuple'>)) – header metadata (origin, spacing, direction).

  • output_dir (str) – Output directory.

  • filename (str) – Filename of the output file.

  • mode (str) – save as ‘image’ or ‘label’

  • gzip (bool) – zip nii (ie, nii.gz)

Return type

None

ComScan.utils.scaler_encoder(df, columns, scaler=StandardScaler(), inplace=False)[source]

Apply sklearn scaler to columns.

Parameters
  • df (DataFrame) – input dataframe

  • columns (List[str]) – List of columns to encode

  • scaler – scaler object from sklearn

  • inplace (bool) – If False, return a copy. Otherwise, do operation inplace and return None

Return type

DataFrame

Returns

  • df: DataFrame scaled

  • dict_cls_fitted: dict by col of fitted cls

ComScan.utils.split_filename(file_name)[source]

Split file_name into folder path name, basename, and extension name.

Parameters

file_name (str) – full path

Return type

Tuple[str, str, str]

Returns

path name, basename, extension name

ComScan.utils.tsne(df, columns, n_components=2, random_state=123, n_jobs=- 1)[source]

t-distributed Stochastic Neighbor Embedding.

t-SNE is a tool to visualize high-dimensional data. It converts similarities between data points to joint probabilities and tries to minimize the Kullback-Leibler divergence between the joint probabilities of the low-dimensional embedding and the high-dimensional data. t-SNE has a cost function that is not convex, i.e. with different initializations we can get different results.

Parameters
  • df (DataFrame) – input dataframe

  • columns (List[str]) – List of columns to use

  • n_components (int) – Dimension of the embedded space. Default 2.

  • random_state (Optional[int]) – int, RandomState instance or None, optional, default: 123 If int, random_state is the seed used by the random number generator; If None, the random number generator is the RandomState instance used by np.random.

  • n_jobs (Optional[int]) – default=-1 The number of parallel jobs to run for neighbors search. This parameter has no impact when metric="precomputed" or (metric="euclidean" and method="exact"). None means 1 unless in a joblib.parallel_backend context. -1 means using all processors.

Returns

array-like with projections

ComScan.utils.u_map(df, columns, n_components=2, random_state=123, n_jobs=- 1)[source]

Just like t-SNE, UMAP is a dimensionality reduction specifically designed for visualizing complex data in low dimensions (2D or 3D). As the number of data points increase, UMAP becomes more time efficient compared to TSNE.

Parameters
  • df (DataFrame) – input dataframe

  • columns (List[str]) – List of columns to use

  • n_components (int) – Dimension of the embedded space. Default 2.

  • random_state (Optional[int]) – int, RandomState instance or None, optional, default: 123 If int, random_state is the seed used by the random number generator; If None, the random number generator is the RandomState instance used by np.random.

  • n_jobs (Optional[int]) – default=-1 The number of parallel jobs to run for neighbors search. This parameter has no impact when metric="precomputed" or (metric="euclidean" and method="exact"). None means 1 unless in a joblib.parallel_backend context. -1 means using all processors.

Returns

array-like with projections

Module contents