Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

Commit

Permalink
enh: finishing the transforms module
Browse files Browse the repository at this point in the history
Adds a bunch of notebooks about coordinates, affines and nonlinear
transforms to the jupyter book.
  • Loading branch information
oesteban committed Mar 1, 2022
1 parent 0dcbf5c commit 7bf2377
Show file tree
Hide file tree
Showing 15 changed files with 231 additions and 203 deletions.
105 changes: 56 additions & 49 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,86 +22,93 @@ exclude_patterns:
# string_formatting.Rmd.
# https://github.com/executablebooks/meta/discussions/105
# https://myst-nb.readthedocs.io/en/latest/use/orphaned_nb.html#orphaned-nb
- README.md
- todo.md
- _scripts
# OE: I have sorted the pages with a tentative sectioning
#
# Python and Numpy
- pages/allclose.Rmd
- pages/anterior_cingulate.md
- pages/arange.Rmd
- pages/array_reductions.md
- pages/bibliography.md
- pages/boolean_indexing.Rmd
- pages/choosing_editor.md
- pages/classes.md
- pages/classes_and_labs.md
- pages/coding_style.md
- pages/array_reductions.md
- pages/comparing_arrays.Rmd
- pages/comparing_floats.md
- pages/diag_inverse.md
- pages/diagnostics_project.md
- pages/dipy_registration.Rmd
- pages/docstrings.md
- pages/dot_and_outer.md
- pages/dot_outer.Rmd
- pages/ds114_sub009_highres.nii
- pages/example_data.md
- pages/exercises.md
- pages/floating_in_text.md
- pages/index_reshape.Rmd
- pages/keyword_arguments.Rmd
- pages/list_comprehensions.Rmd
- pages/methods_vs_functions.Rmd
- pages/nans.md
- pages/newaxis.Rmd
- pages/numpy_diag.Rmd
- pages/numpy_meshgrid.Rmd
- pages/numpy_random.Rmd
- pages/numpy_squeeze.Rmd
- pages/numpy_transpose.Rmd
- pages/on_loops.Rmd
- pages/packages_namespaces.Rmd
- pages/printing_floating.Rmd
- pages/string_literals.md
- pages/subtract_mean_math.md
- pages/subtract_means.Rmd
- pages/two_dunders.md
# Images and code and images
- pages/saving_images.Rmd
- pages/plot_lines.Rmd
- pages/subplots.Rmd
# Coordinate systems and spatial transforms
# [all transferred already]
# Into the fourth dimension
- pages/reshape_and_4d.Rmd
- pages/tr_and_headers.Rmd
# Statistics
- pages/whole_image_statistics.Rmd
- pages/pearson_functions.md
- pages/validate_against_scipy.Rmd
# Hemodynamic response modeling
- pages/anterior_cingulate.md
- pages/otsu_threshold.Rmd
- pages/introducing_nipype.Rmd
# Code organization and collaboration
- pages/choosing_editor.md
- pages/coding_style.md
- pages/full_scripting.md
- pages/git_videos.md
- pages/git_walk_through.Rmd
- pages/git_workflow_exercises.md
- pages/github_dummies_homework.md
- pages/github_glm_homework.md
- pages/github_pca_homework.md
- pages/glossary.md
- pages/image_header_and_affine.Rmd
- pages/images_and_affines.Rmd
- pages/index.md
- pages/index_reshape.Rmd
- pages/installation_on_linux.md
- pages/installation_on_mac.md
- pages/installation_on_windows.md
- pages/introducing_nipype.Rmd
- pages/keyword_arguments.Rmd
- pages/list_comprehensions.Rmd
# Meta-course
- README.md
- todo.md
- _scripts
- pages/bibliography.md
- pages/classes.md
- pages/classes_and_labs.md
- pages/diagnostics_project.md
- pages/ds114_sub009_highres.nii
- pages/example_data.md
- pages/exercises.md
- pages/glossary.md
- pages/index.md
- pages/logistics.md
- pages/markdown.md
- pages/mentors.md
- pages/methods_vs_functions.Rmd
- pages/multi_model_homework.md
- pages/nans.md
- pages/newaxis.Rmd
- pages/numpy_diag.Rmd
- pages/numpy_meshgrid.Rmd
- pages/numpy_random.Rmd
- pages/numpy_squeeze.Rmd
- pages/numpy_transpose.Rmd
- pages/on_loops.Rmd
- pages/otsu_threshold.Rmd
- pages/packages_namespaces.Rmd
- pages/participate.md
- pages/participate_grading.md
- pages/pearson_functions.md
- pages/plot_lines.Rmd
- pages/preparation.md
- pages/printing_floating.Rmd
- pages/project_grading.md
- pages/projects.md
- pages/resampling_with_ndimage.Rmd
- pages/reshape_and_4d.Rmd
- pages/rotation_2d_3d.md
- pages/saving_images.Rmd
- pages/string_literals.md
- pages/subplots.Rmd
- pages/subtract_mean_math.md
- pages/subtract_means.Rmd
- pages/topics.md
- pages/tr_and_headers.Rmd
- pages/two_dunders.md
# Drop?
- pages/using_pythonpath.Rmd
- pages/validate_against_scipy.Rmd
- pages/whole_image_statistics.Rmd

# Define the name of the latex output file for PDF builds
latex:
Expand Down
5 changes: 5 additions & 0 deletions _toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ parts:
- file: pages/nibabel_affines
- file: pages/nibabel_apply_affine
- file: pages/map_coordinates
- file: pages/image_header_and_affine
- file: pages/images_and_affines
- file: pages/rotation_2d_3d
- file: pages/resampling_with_ndimage
- file: pages/dipy_registration
- caption: Statistics
chapters:
- file: pages/mean_test_example
Expand Down
66 changes: 66 additions & 0 deletions code/rotations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
""" Rotation matrices for rotations around x, y, z axes
See: https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations
"""

import numpy as np


def x_rotmat(theta):
""" Rotation matrix for rotation of `theta` radians around x axis
Parameters
----------
theta : scalar
Rotation angle in radians
Returns
-------
M : shape (3, 3) array
Rotation matrix
"""
cos_t = np.cos(theta)
sin_t = np.sin(theta)
return np.array([[1, 0, 0],
[0, cos_t, -sin_t],
[0, sin_t, cos_t]])


def y_rotmat(theta):
""" Rotation matrix for rotation of `theta` radians around y axis
Parameters
----------
theta : scalar
Rotation angle in radians
Returns
-------
M : shape (3, 3) array
Rotation matrix
"""
cos_t = np.cos(theta)
sin_t = np.sin(theta)
return np.array([[cos_t, 0, sin_t],
[0, 1, 0],
[-sin_t, 0, cos_t]])


def z_rotmat(theta):
""" Rotation matrix for rotation of `theta` radians around z axis
Parameters
----------
theta : scalar
Rotation angle in radians
Returns
-------
M : shape (3, 3) array
Rotation matrix
"""
cos_t = np.cos(theta)
sin_t = np.sin(theta)
return np.array([[cos_t, -sin_t, 0],
[sin_t, cos_t, 0],
[0, 0, 1]])
40 changes: 7 additions & 33 deletions pages/dipy_registration.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ jupyter:
format_name: rmarkdown
format_version: '1.2'
jupytext_version: 1.11.5
kernelspec:
display_name: Python 3 (ipykernel)
language: python
name: python3
---

$\newcommand{L}[1]{\| #1 \|}\newcommand{VL}[1]{\L{ \vec{#1} }}\newcommand{R}[1]{\operatorname{Re}\,(#1)}\newcommand{I}[1]{\operatorname{Im}\, (#1)}$

## Registration with dipy
# Registration with dipy

[Dipy](http://nipy.org/dipy) is a Python package for diffusion imaging.

Expand Down Expand Up @@ -86,9 +88,9 @@ Dipy works on the image data arrays. It also needs the affine arrays of each
of the images:

```{python}
moving_data = moving_img.get_data()
moving_data = moving_img.get_fdata()
moving_affine = moving_img.affine
template_data = template_img.get_data()
template_data = template_img.get_fdata()
template_affine = template_img.affine
```

Expand Down Expand Up @@ -262,31 +264,3 @@ regtools.overlay_slices(template_data, warped_moving, None, 1,
regtools.overlay_slices(template_data, warped_moving, None, 2,
"Template", "Transformed")
```

<!-- vim:ft=rst -->
<!-- Course -->
<!-- BIC -->
<!-- Python distributions -->
<!-- Version control -->
<!-- Editors -->
<!-- Python and common libraries -->
<!-- IPython -->
<!-- Virtualenv and helpers -->
<!-- Pypi and packaging -->
<!-- Mac development -->
<!-- Windows development -->
<!-- Nipy and friends -->
<!-- FMRI datasets -->
<!-- Languages -->
<!-- Imaging software -->
<!-- Installation -->
<!-- Tutorials -->
<!-- MB tutorials -->
<!-- Ideas -->
<!-- Psych-214 -->
<!-- People -->
<!-- Licenses -->
<!-- Neuroimaging stuff -->
<!-- OpenFMRI projects -->
<!-- Unix -->
<!-- Substitutions -->
1 change: 1 addition & 0 deletions pages/ds107_sub012_highres.nii
1 change: 1 addition & 0 deletions pages/ds114_sub009_highres_brain_222.nii
1 change: 1 addition & 0 deletions pages/ds114_sub009_highres_moved.hdr
1 change: 1 addition & 0 deletions pages/ds114_sub009_highres_moved.img
43 changes: 5 additions & 38 deletions pages/image_header_and_affine.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,20 @@ jupyter:
format_name: rmarkdown
format_version: '1.2'
jupytext_version: 1.11.5
kernelspec:
display_name: Python 3 (ipykernel)
language: python
name: python3
---

$\newcommand{L}[1]{\| #1 \|}\newcommand{VL}[1]{\L{ \vec{#1} }}\newcommand{R}[1]{\operatorname{Re}\,(#1)}\newcommand{I}[1]{\operatorname{Im}\, (#1)}$

## The image header and affine
# The image header and affine

See: [coordinate systems and affine transforms](http://nipy.org/nibabel/coordinate_systems.html) for an introduction.

```{python}
# import common modules
import numpy as np
np.set_printoptions(precision=4, suppress=True) # print arrays to 4DP
import matplotlib.pyplot as plt
```

```{python}
#: gray colormap and nearest neighbor interpolation by default
plt.rcParams['image.cmap'] = 'gray'
plt.rcParams['image.interpolation'] = 'nearest'
```

## The image affine
Expand Down Expand Up @@ -87,31 +82,3 @@ may still find them used. They have only one advantage, which is that, if some
software wants to change only the header information, it only has to rewrite a
small `.hdr` file, rather than the whole `.nii` file containing the image
data and the header.

<!-- vim:ft=rst -->
<!-- Course -->
<!-- BIC -->
<!-- Python distributions -->
<!-- Version control -->
<!-- Editors -->
<!-- Python and common libraries -->
<!-- IPython -->
<!-- Virtualenv and helpers -->
<!-- Pypi and packaging -->
<!-- Mac development -->
<!-- Windows development -->
<!-- Nipy and friends -->
<!-- FMRI datasets -->
<!-- Languages -->
<!-- Imaging software -->
<!-- Installation -->
<!-- Tutorials -->
<!-- MB tutorials -->
<!-- Ideas -->
<!-- Psych-214 -->
<!-- People -->
<!-- Licenses -->
<!-- Neuroimaging stuff -->
<!-- OpenFMRI projects -->
<!-- Unix -->
<!-- Substitutions -->
Loading

0 comments on commit 7bf2377

Please sign in to comment.