|
| 1 | +# Python API |
| 2 | + |
| 3 | +Cardiotensor can be used as a Python library in addition to the CLI. |
| 4 | +The main workflow helpers are importable directly from `cardiotensor`, and |
| 5 | +subpackage imports are available for grouped functionality. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +```console |
| 10 | +$ pip install cardiotensor |
| 11 | +``` |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## Quick Start |
| 16 | + |
| 17 | +```python |
| 18 | +from cardiotensor import compute_orientation |
| 19 | + |
| 20 | +compute_orientation( |
| 21 | + volume_path="path/to/images/", |
| 22 | + mask_path="path/to/mask.tif", # optional |
| 23 | + output_dir="./output", |
| 24 | + sigma=1.0, |
| 25 | + rho=3.0, |
| 26 | + axis_points=[(0, 0, 0), (256, 256, 256), (512, 512, 512)], |
| 27 | +) |
| 28 | +``` |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +## Structure Tensor |
| 33 | + |
| 34 | +```python |
| 35 | +import numpy as np |
| 36 | +from cardiotensor import calculate_structure_tensor, compute_fraction_anisotropy |
| 37 | + |
| 38 | +volume = np.random.rand(32, 64, 64).astype(np.float32) |
| 39 | + |
| 40 | +# Compute eigenvalues and eigenvectors |
| 41 | +eigenvalues, eigenvectors = calculate_structure_tensor( |
| 42 | + volume, |
| 43 | + sigma=1.0, |
| 44 | + rho=3.0, |
| 45 | + use_gpu=False, |
| 46 | +) |
| 47 | +# eigenvectors shape: (3, Z, Y, X) |
| 48 | +# eigenvalues shape: (3, Z, Y, X) |
| 49 | + |
| 50 | +# Fractional Anisotropy map |
| 51 | +z_index = volume.shape[0] // 2 |
| 52 | +fa = compute_fraction_anisotropy(eigenvalues[:, z_index, :, :]) |
| 53 | +print(fa.shape) # (Y, X), values in [0, 1] |
| 54 | +``` |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +## Helix and Transverse Angles |
| 59 | + |
| 60 | +```python |
| 61 | +import numpy as np |
| 62 | +import matplotlib.pyplot as plt |
| 63 | +from cardiotensor import compute_helix_and_transverse_angles |
| 64 | +from cardiotensor.orientation import rotate_vectors_to_new_axis |
| 65 | + |
| 66 | +# Use one slice of the eigenvector field computed above |
| 67 | +vector_field_slice = eigenvectors[:, z_index, :, :] |
| 68 | + |
| 69 | +# Example long-axis direction used to rotate the slice before angle computation |
| 70 | +new_axis_vec = np.array([0.0, 1.0, 0.0], dtype=np.float32) |
| 71 | +rotated_vectors = rotate_vectors_to_new_axis(vector_field_slice, new_axis_vec) |
| 72 | + |
| 73 | +center_point = ( |
| 74 | + vector_field_slice.shape[2] // 2, |
| 75 | + vector_field_slice.shape[1] // 2, |
| 76 | + z_index, |
| 77 | +) |
| 78 | + |
| 79 | +helix_angle, intrusion_angle = compute_helix_and_transverse_angles( |
| 80 | + rotated_vectors, |
| 81 | + center_point, |
| 82 | +) |
| 83 | +# Both arrays shape: (Y, X), values in degrees |
| 84 | +# The second output corresponds to the transverse/intrusion companion angle map |
| 85 | + |
| 86 | +fig, axes = plt.subplots(1, 2, figsize=(10, 4), constrained_layout=True) |
| 87 | + |
| 88 | +im0 = axes[0].imshow(helix_angle, cmap="twilight", vmin=-90, vmax=90) |
| 89 | +axes[0].set_title("Helix angle") |
| 90 | +axes[0].axis("off") |
| 91 | +fig.colorbar(im0, ax=axes[0], label="degrees") |
| 92 | + |
| 93 | +im1 = axes[1].imshow(intrusion_angle, cmap="coolwarm", vmin=-90, vmax=90) |
| 94 | +axes[1].set_title("Intrusion angle") |
| 95 | +axes[1].axis("off") |
| 96 | +fig.colorbar(im1, ax=axes[1], label="degrees") |
| 97 | + |
| 98 | +plt.show() |
| 99 | +``` |
| 100 | + |
| 101 | +--- |
| 102 | + |
| 103 | +## Available Imports |
| 104 | + |
| 105 | +```python |
| 106 | +from cardiotensor import ( |
| 107 | + # Core |
| 108 | + DataReader, |
| 109 | + compute_orientation, |
| 110 | + read_conf_file, |
| 111 | + convert_to_8bit, |
| 112 | + # Orientation |
| 113 | + calculate_structure_tensor, |
| 114 | + compute_azimuth_and_elevation, |
| 115 | + compute_fraction_anisotropy, |
| 116 | + compute_helix_and_transverse_angles, |
| 117 | + # Tractography |
| 118 | + generate_streamlines_from_vector_field, |
| 119 | + generate_streamlines_from_params, |
| 120 | + # I/O |
| 121 | + load_npz_streamlines, |
| 122 | + load_trk_streamlines, |
| 123 | + write_spatialgraph_am, |
| 124 | + export_vector_field_to_vtk, |
| 125 | + # Analysis |
| 126 | + calculate_intensities, |
| 127 | + find_end_points, |
| 128 | + plot_intensity, |
| 129 | + save_intensity, |
| 130 | +) |
| 131 | +``` |
| 132 | + |
| 133 | +You can also use grouped imports: |
| 134 | + |
| 135 | +```python |
| 136 | +from cardiotensor.analysis import calculate_intensities, find_end_points |
| 137 | +from cardiotensor.orientation import ( |
| 138 | + calculate_structure_tensor, |
| 139 | + compute_orientation, |
| 140 | + rotate_vectors_to_new_axis, |
| 141 | +) |
| 142 | +from cardiotensor.tractography import generate_streamlines_from_params |
| 143 | +from cardiotensor.utils import DataReader, load_trk_streamlines, read_conf_file |
| 144 | +from cardiotensor.visualization import visualize_streamlines, visualize_vector_field |
| 145 | +``` |
| 146 | + |
| 147 | + |
| 148 | +!!! note |
| 149 | + |
| 150 | + For the full API reference with parameter descriptions, see the [API reference](../reference/api.md) page. |
0 commit comments