{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "a58ac418",
"metadata": {
"tags": [
"remove-cell"
]
},
"outputs": [],
"source": [
"from ndslib.config import jupyter_startup\n",
"jupyter_startup()"
]
},
{
"cell_type": "markdown",
"id": "9401b592",
"metadata": {},
"source": [
"(ndslib)=\n",
"# `ndslib` function reference\n",
"\n",
"The `ndslib` software library implements functions that we use in the book to\n",
"download datasets, and to encapsulate some computations. We provide descriptions\n",
"of the functions for each module below. Additional documentation can be found in\n",
"the ndslib [library website](https://neuroimaging-data-science.github.io/ndslib/).\n",
"\n",
"```{eval-rst}\n",
"`ndslib.data.load_data(dataset, fname=None)`\n",
"\n",
" Loads data for use in examples.\n",
"\n",
" Parameters:\n",
" **dataset** : str\n",
" The name of a dataset. Can be one of:\n",
"\n",
" \"bold_numpy\" : Read a BOLD time-series as a numpy array.\n",
"\n",
" \"bold_volume\" : Read a single volume of a BOLD time-series as a numpy array\n",
"\n",
" \"afq\" : Read diffusion MRI data in tabular format\n",
"\n",
" \"age_groups_fa\" : Read AFQ data and return dataframe divided by age-groups\n",
"\n",
" \"abide2_saggitals\": Read ABIDE2 mid-saggitals as numpy arrays.\n",
"\n",
" **fname** : str, optional.\n",
" If provided, data will be cached to this local path and retrieved from there on future calls with the same value.\n",
"\n",
"`ndslib.data.download_bids_dataset()`\n",
"\n",
" Makes a minimal BIDS dataset with one fMRI subject from OpenNeuro dataset \"ds001233\".\n",
"\n",
"`ndslib.viz.imshow_with_annot(im, vmax=40)`\n",
"\n",
" Like imshow, but with added annotation of the array values.\n",
"\n",
" Parameters:\n",
" **im** : numpy array\n",
"\n",
"`ndslib.viz.plot_diffeomorphic_map(mapping, ax, delta=15, direct_grid_shape=None, direct_grid2world=- 1, inverse_grid_shape=None, inverse_grid2world=- 1)`\n",
"\n",
" Draw the effect of warping a regular lattice by a diffeomorphic\n",
" map. Draws a diffeomorphic map by showing the effect of the\n",
" deformation on a regular grid. The resulting figure contains two\n",
" images: the direct transformation is plotted to the left, and the\n",
" inverse transformation is plotted to the right.\n",
"\n",
" Parameters:\n",
" **mapping** : DiffeomorphicMap\n",
" the diffeomorphic map to be drawn\n",
"\n",
" **delta** : int, optional\n",
" the size (in pixels) of the squares of the regular lattice to\n",
" be used to plot the warping effects. Each square will be\n",
" delta x delta pixels. By default, the size will be 10 pixels.\n",
"\n",
" **fname** : string, optional\n",
" the name of the file the figure will be written to. If None\n",
" (default), the figure will not be saved to disk.\n",
"\n",
" **direct_grid_shape** : tuple, shape (2,), optional\n",
" the shape of the grid image after being deformed by the\n",
" direct transformation. By default, the shape of the deformed\n",
" grid is the same as the grid of the displacement field, which\n",
" is by default equal to the shape of the fixed image. In other\n",
" words, the resulting deformed grid (deformed by the direct\n",
" transformation) will normally have the same shape as the\n",
" fixed image.\n",
"\n",
" **direct_grid2world** : array, shape (3, 3), optional\n",
" the affine transformation mapping the direct grid's\n",
" coordinates to physical space. By default, this\n",
" transformation will correspond to the image-to-world\n",
" transformation corresponding to the default direct_grid_shape\n",
" (in general, if users specify a direct_grid_shape, they\n",
" should also specify direct_grid2world).\n",
"\n",
" **inverse_grid_shape** : tuple, shape (2,), optional\n",
" the shape of the grid image after being deformed by the\n",
" inverse transformation. By default, the shape of the deformed\n",
" grid under the inverse transform is the same as the image\n",
" used as \"moving\" when the diffeomorphic map was generated by\n",
" a registration algorithm (so it corresponds to the effect of\n",
" warping the static image towards the moving).\n",
"\n",
" **inverse_grid2world** : array, shape (3, 3), optional\n",
" the affine transformation mapping inverse grid's coordinates\n",
" to physical space. By default, this transformation will\n",
" correspond to the image-to-world transformation corresponding\n",
" to the default inverse_grid_shape (in general, if users\n",
" specify an inverse_grid_shape, they should also specify\n",
" inverse_grid2world).\n",
"\n",
" Returns:\n",
" **warped_forward** : array\n",
" Image with the grid showing the effect of transforming the\n",
" moving image to the static image. The shape will be\n",
" *direct_grid_shape* if specified, otherwise the shape of the\n",
" static image.\n",
"\n",
" **warped_backward** : array\n",
" Image with the grid showing the effect of transforming the\n",
" static image to the moving image. Shape will be\n",
" *inverse_grid_shape* if specified, otherwise the shape of the\n",
" moving image.\n",
"\n",
" Notes:\n",
"\n",
" The default value for the affine transformation is \"-1\" to handle\n",
" the case in which the user provides \"None\" as input meaning\n",
" \"identity\". If we used None as default, we wouldn't know if the\n",
" user specifically wants to use the identity (specifically passing\n",
" None) or if it was left unspecified, meaning to use the appropriate\n",
" default matrix.\n",
"\n",
"`ndslib.viz.plot_coef_path(estimator, X, y, alpha, **kwargs)`\n",
"\n",
" Plot the coefficient path for a sklearn estimator.\n",
"\n",
" Parameters:\n",
" **estimator** : sklearn estimator\n",
" For example \"`Lasso()`\"\n",
"\n",
" **X** : ndarray (n, m)\n",
" Feature matrix\n",
"\n",
" **y** : ndarray (n,)\n",
" Target matrix\n",
"\n",
" Returns:\n",
" **ax** : Matplotlib `Axes` object\n",
"\n",
"`ndslib.viz.plot_train_test(x_range, train_scores, test_scores, label, hlines=None)`\n",
"\n",
" Plot train/test $R^2$\n",
"\n",
" Parameters:\n",
" **x_range** : sequence\n",
" The range of x values used (e.g., number of features, number\n",
" of samples)\n",
"\n",
" **train_scores** : sequence\n",
" The train r2_score corresponding to different x values\n",
"\n",
" **test_scores** : sequence\n",
" The test r2_score corresponding to different x values\n",
"\n",
" **label** : str\n",
" Used in the legend labels.\n",
"\n",
" **hlines** : dict\n",
" A dictionary where keys are labels and values are y values\n",
" for hlines.\n",
"\n",
" Returns:\n",
" **ax** : Matplotlib `Axes` object.\n",
"\n",
"`ndslib.viz.plot_learning_curves(estimators, X_sets, y, train_sizes, labels=None, errors=True, **kwargs)`\n",
"\n",
" Generate multi-panel plot displaying learning curves for multiple\n",
" predictor sets and/or estimators.\n",
"\n",
" Parameters:\n",
" **estimators** : list,\n",
" A scikit-learn Estimator or list of estimators. If a list is\n",
" provided, it must have the same number of elements as X_sets.\n",
"\n",
" **X_sets** : list,\n",
" An NDArray or similar object, or list. If a list is passed,\n",
" it must have the same number of elements as estimators.\n",
"\n",
" **y** : ndarray\n",
" A 1-D numpy array (or pandas Series) representing the outcome\n",
" variable to predict.\n",
"\n",
" **train_sizes** : list\n",
" List of ints providing the sample sizes at which to evaluate\n",
" the estimator.\n",
"\n",
" **labels** : list, optional.\n",
" List of labels for the panels. Must have the same number of\n",
" elements as X_sets.\n",
"\n",
" **errors** : bool, optional.\n",
" If True, plots error bars representing 1 StDev. Default:\n",
" True.\n",
"\n",
" **kwargs** : dict, optional\n",
" Optional keyword arguments passed on to sklearn's\n",
" *learning_curve* utility.\n",
"\n",
"`ndslib.viz.plot_graphviz_tree(tree, feature_names)`\n",
"\n",
" Takes a tree as input, calls Scikit-learn's `export_graphviz` function to generate an image of the tree using \"graphviz\", and then plots the result in-line.\n",
"\n",
" Parameters:\n",
" **tree**: sklearn tree object\n",
" **feature_names** : sequence of strings\n",
"\n",
"`ndslib.image.gaussian_kernel(x=20, sigma=4)`\n",
"\n",
" Construct a 2D Gaussian kernel for image processing\n",
"\n",
" Parameters:\n",
" **x** : int, optional\n",
" The number of pixels on a side for the filter. Default : 20\n",
"\n",
" **sigma** : float, optional\n",
" The standard deviation parameter for the Gaussian. Default :\n",
" 4\n",
"\n",
" Returns:\n",
" **gauss** : ndarray\n",
" Contains the values of the 2D Gaussian normalized to sum to 1.\n",
"```"
]
}
],
"metadata": {
"jupytext": {
"formats": "ipynb,md:myst",
"text_representation": {
"extension": ".md",
"format_name": "myst",
"format_version": 0.13,
"jupytext_version": "1.11.5"
}
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.8.18"
},
"source_map": [
15,
20
]
},
"nbformat": 4,
"nbformat_minor": 5
}