### Install DeepTrack Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/2-examples/DTEx231B_LodeSTAR_tracking_particles_of_various_shapes.ipynb Installs the DeepTrack library. This is a prerequisite for running the examples. ```python %matplotlib inline !pip install deeptrack ``` -------------------------------- ### Install DeepTrack and DeepPlay Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/2-examples/DTEx231A_LodeSTAR_autotracker_template.ipynb Install the necessary libraries if you are running on a Colab or Kaggle environment. This is a one-time setup. ```python # !pip install deeptrack deeplay # Uncomment if running on colab/kaggle. ``` -------------------------------- ### Install DeepTrack and Optuna Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/1-getting-started/DTGS127_characterizing_aberrations_optuna.ipynb Uncomment and run this command to install necessary libraries if you are using Google Colab or Kaggle. ```python # !pip install deeptrack optuna # Uncomment if running on Colab/Kaggle. ``` -------------------------------- ### Install DeepTrack and DeepPlay Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/1-getting-started/DTGS126_characterizing_aberrations_cnn.ipynb Uncomment and run this command if you are using Google Colab or Kaggle to install the necessary libraries. ```python # !pip install deeplay deeptrack # Uncomment if running on Colab/Kaggle. ``` -------------------------------- ### Install DeepTrack and DeepPlay Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/1-getting-started/DTGS151_unsupervised_object_detection_with_lodestar.ipynb Uncomment this line to install the necessary libraries if running on Colab or Kaggle. ```python # !pip install deeplay deeptrack # Uncomment if running on Colab/Kaggle. ``` -------------------------------- ### Install Deeplay Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/1-getting-started/DTGS101_intro.ipynb Install the 'deeplay' library, which is required for building and training deep-learning models with DeepTrack2. Uncomment the line if running in an environment like Colab or Kaggle. ```python # !pip install deeplay # Uncomment if running on Colab/Kaggle. ``` -------------------------------- ### Install DeepTrack Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/1-getting-started/DTGS161_torch_fitting.ipynb Uncomment this line to install DeepTrack if running on Colab or Kaggle. ```python # !pip install deeptrack # Uncomment if running on Colab/Kaggle. ``` -------------------------------- ### Install Imagecodecs Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/1-getting-started/DTGS151_unsupervised_object_detection_with_lodestar.ipynb Uncomment this line to install the imagecodecs package if it is not already installed. This may be required for image loading. ```python # Uncomment the line below if the package is not installed # !pip install imagecodecs ``` -------------------------------- ### Instantiate Feature with String Property Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT301_features.ipynb Example of instantiating the `Foo` feature with a string value for `my_property` and observing the output. ```python foo2 = Foo(my_property="bar") foo2.update() output_image = foo2() output_image ``` -------------------------------- ### Plot Example Images Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/1-getting-started/DTGS131_tracking_multiple_particles_unet.ipynb Generates and displays four example images by updating the random features (particle count, positions, noise) and then plotting the resulting image. ```python for i in range(4): image_features.update() output_image = image_features.plot(cmap="gray") plt.show() ``` -------------------------------- ### Setup Fluorescence Microscope Optics Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT321_scatterers.ipynb Create a fluorescence microscope instance with specified numerical aperture, wavelength, resolution, magnification, and output region. ```python from deeptrack.optics import Fluorescence optics = Fluorescence( NA=0.7, wavelength=680e-9, resolution=1e-6, magnification=10, output_region=(0, 0, 64, 64), ) ``` -------------------------------- ### Plotting Example Images Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/1-getting-started/DTGS141_distinguishing_particles_in_brightfield.ipynb Generates and displays multiple example images of particles. Call `.update()` to refresh random features and `.plot()` to visualize. Requires `matplotlib.pyplot` to be imported as `plt`. ```python for _ in range(8): image_of_particles.update() image_of_particles.plot(cmap="gray") plt.show() ``` -------------------------------- ### Setup Imports and Parameters Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/2-examples/DTEx205_inline_holography_3d_tracking.ipynb Imports necessary libraries and defines constants for image size and physical scales. This code block is essential for setting up the simulation environment. ```python import numpy as np import matplotlib.pyplot as plt import scipy.io import skimage import skimage.measure import deeptrack as dt from deeptrack.extras import datasets datasets.load("3DTracking") video = scipy.io.loadmat(r"./datasets/3DTracking/1Every1_2_movie_1.aviProcessedField0.mat") traces = scipy.io.loadmat(r"./datasets/3DTracking/1Every1_2_movie_1.aviCompletedTraces") mapping = scipy.io.loadmat(r"./datasets/3DTracking/mapping.mat")["Mapping"].T IMAGE_SIZE = 128 MIN_Z = 2e-6 MAX_Z = 30e-6 Z_SCALE = 3.45e-7 PIXEL_SCALE = 3.45e-7 ``` -------------------------------- ### Plot Example Training Images Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/2-examples/DTEx211_MNIST.ipynb Generates and displays a grid of example MNIST images from the training dataset, along with their corresponding digit labels. ```python fig, axs = plt.subplots(10, 6, figsize=(9, 18)) for i, ax in enumerate(axs.flatten()): image, digit = train_dataset[i * 1_000] ax.imshow(image.numpy().squeeze(), cmap="gray") ax.set_title(digit) ax.axis("off") ``` -------------------------------- ### DeepTrack2 Installation Output Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/2-examples/DTEx231J_LodeSTAR_measure_mass_cell.ipynb This output shows the successful installation of DeepTrack2 and its dependencies, including TensorFlow, NumPy, SciPy, and others. ```text Requirement already satisfied: deeptrack in c:\users\gu\deeptrack\deeptrack-2.0 (1.2.0) Requirement already satisfied: tensorflow in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from deeptrack) (2.9.1) Requirement already satisfied: tensorflow-probability in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from deeptrack) (0.17.0) Requirement already satisfied: numpy in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from deeptrack) (1.23.0) Requirement already satisfied: scipy in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from deeptrack) (1.8.1) Requirement already satisfied: pint in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from deeptrack) (0.19.2) Requirement already satisfied: scikit-image>=0.18.0 in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from deeptrack) (0.19.3) Requirement already satisfied: pydeepimagej in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from deeptrack) (1.1.0) Requirement already satisfied: more_itertools in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from deeptrack) (8.13.0) Requirement already satisfied: tensorflow_addons in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from deeptrack) (0.17.1) Requirement already satisfied: imageio>=2.4.1 in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from scikit-image>=0.18.0->deeptrack) (2.19.3) Requirement already satisfied: networkx>=2.2 in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from scikit-image>=0.18.0->deeptrack) (2.8.4) Requirement already satisfied: pillow!=7.1.0,!=7.1.1,!=8.3.0,>=6.1.0 in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from scikit-image>=0.18.0->deeptrack) (9.1.1) Requirement already satisfied: PyWavelets>=1.1.1 in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from scikit-image>=0.18.0->deeptrack) (1.3.0) Requirement already satisfied: packaging>=20.0 in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from scikit-image>=0.18.0->deeptrack) (21.3) Requirement already satisfied: tifffile>=2019.7.26 in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from scikit-image>=0.18.0->deeptrack) (2022.5.4) Requirement already satisfied: gast<=0.4.0,>=0.2.1 in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from tensorflow->deeptrack) (0.4.0) Requirement already satisfied: flatbuffers<2,>=1.12 in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from tensorflow->deeptrack) (1.12) Requirement already satisfied: typing-extensions>=3.6.6 in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from tensorflow->deeptrack) (4.2.0) Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from tensorflow->deeptrack) (0.26.0) Requirement already satisfied: grpcio<2.0,>=1.24.3 in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from tensorflow->deeptrack) (1.47.0) Requirement already satisfied: termcolor>=1.1.0 in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from tensorflow->deeptrack) (1.1.0) Requirement already satisfied: tensorboard<2.10,>=2.9 in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from tensorflow->deeptrack) (2.9.1) Requirement already satisfied: keras<2.10.0,>=2.9.0rc0 in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from tensorflow->deeptrack) (2.9.0) Requirement already satisfied: protobuf<3.20,>=3.9.2 in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from tensorflow->deeptrack) (3.19.4) Requirement already satisfied: six>=1.12.0 in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from tensorflow->deeptrack) (1.16.0) Requirement already satisfied: h5py>=2.9.0 in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from tensorflow->deeptrack) (3.7.0) Requirement already satisfied: astunparse>=1.6.0 in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from tensorflow->deeptrack) (1.6.3) Requirement already satisfied: wrapt>=1.11.0 in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from tensorflow->deeptrack) (1.14.1) Requirement already satisfied: libclang>=13.0.0 in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from tensorflow->deeptrack) (14.0.1) Requirement already satisfied: absl-py>=1.0.0 in c:\users\gu\appdata\local\programs\python\python310\lib\site-packages (from tensorflow->deeptrack) (1.1.0) ``` -------------------------------- ### Switch Computational Backend Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT399F_backend._config.ipynb Change the active computational backend to 'torch' or 'numpy'. Ensure the selected backend is installed. ```python config.set_backend("torch") print(config.get_backend()) ``` ```python config.set_backend("numpy") print(config.get_backend()) ``` -------------------------------- ### Install DeepTrack2 using pip Source: https://github.com/deeptrackai/deeptrack2/blob/develop/README.md Use this command to install DeepTrack2 and its dependencies. Ensure you have Python 3.9 or higher. ```bash pip install deeptrack ``` -------------------------------- ### Binary Classifier Setup and Testing Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/1-getting-started/DTGS111_datafiles.ipynb Configure a binary classifier with F1 score and confusion matrix metrics. Use this for evaluating model performance on test data. ```python f1 = tm.F1Score(task="binary") confusion_matrix = tm.ConfusionMatrix(task="binary") ROC = tm.ROC(task="binary") results = model.test( test, metrics={"f1": f1, "confusion_matrix": confusion_matrix, "roc": ROC}, ) print(f"Test f1 score: {results['f1'] * 100:.2f}%") ``` -------------------------------- ### Instantiate and Update a Simple Feature Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT301_features.ipynb Create an instance of a custom feature, update its internal state, and then call the instance to get the output image. Demonstrates passing properties during instantiation. ```python foo1 = Foo(my_property=1) foo1.update() output_image = foo1() output_image ``` -------------------------------- ### Get Current Backend and Device Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT399F_backend._config.ipynb Check the currently active computational backend and device. Defaults to NumPy and CPU. ```python print(config.get_backend()) ``` ```python print(config.get_device()) ``` -------------------------------- ### Check Deeplay Availability Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT399F_backend._config.ipynb Import and check the `DEEPLAY_AVAILABLE` boolean flag to determine if Deeplay is installed and accessible. ```python from deeptrack.backend import DEEPLAY_AVAILABLE print(DEEPLAY_AVAILABLE) ``` -------------------------------- ### Start UNet Training Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/2-examples/DTEx215_cell_counting.ipynb Initiates the training process for the UNet model using the provided training data loader. After training, it plots the training history and sets the model to evaluation mode. ```python unet_trainer.fit(unet_regressor, train_loader) unet_trainer.history.plot() unet_regressor.eval() ``` -------------------------------- ### Train UNet Model with DeepTrack Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/1-getting-started/DTGS131_tracking_multiple_particles_unet.ipynb Call the `.fit()` method on your model to start training. Configure training parameters such as `max_epochs`, `steps_per_epoch`, and `val_steps_per_epoch`. Use `accelerator='auto'` to let DeepTrack determine the best hardware accelerator. ```python h = model.fit( train_data=data_pipeline, val_data=data_pipeline, max_epochs=100, steps_per_epoch=25, val_steps_per_epoch=5, accelerator="auto", ) ``` -------------------------------- ### Compatibility with Image and Properties Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/4-developers/DTDV421_backends.ipynb Illustrates that DeepTrack's `xp` and `Image` objects are compatible, and that properties are preserved throughout feature chaining and backend execution. This example uses `Zeros` and `Sum` features. ```python feature = Zeros((100, 100)) >> Sum() feature.store_properties() feature.torch() output = feature() print(output.properties) print(type(output._value)) ``` -------------------------------- ### Initialize Particle and Unaberrated Optics Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT325_aberrations.ipynb Sets up a point particle and defines the parameters for unaberrated optical system. Imports necessary modules from deeptrack. ```python from deeptrack import Arguments, Fluorescence, PointParticle from deeptrack import units_registry as u arguments = Arguments(z=0) particle = PointParticle( position=(32, 32), position_unit="pixel", intensity=1, z=arguments.z, ) unaberrated_optics = Fluorescence( NA=0.3, resolution=1 * u.um, magnification=10, wavelength=530 * u.nm, output_region=(0, 0, 64, 64), padding=(128, 128, 128, 128), ) unaberrated_particle = unaberrated_optics(particle).bind_arguments(arguments) ``` -------------------------------- ### Install DeepTrack and Dependencies Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/2-examples/DTEx215_cell_counting.ipynb Installs the DeepTrack library and its required dependencies using pip. Ensure you have a compatible Python environment. ```python %matplotlib inline !pip install deeptrack deeplay ``` -------------------------------- ### Create ImageFolder Dataset Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT391B_sources.folder.ipynb Initializes an ImageFolder dataset from a specified root directory. This is the first step before any splitting or manipulation. ```python dataset = ImageFolder("dummy_dataset") ``` -------------------------------- ### Install DeepTrack2 using python -m pip Source: https://github.com/deeptrackai/deeptrack2/blob/develop/README.md An alternative command to install DeepTrack2, useful in specific Python environments. Python 3.9+ is required. ```bash python -m pip install deeptrack ``` -------------------------------- ### Using DispatchExample with NumPy Backend Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/4-developers/DTDV421_backends.ipynb Shows how to use the `DispatchExample` feature with a NumPy array. The `feature.numpy()` call directs the execution to the NumPy-specific code path. ```python feature = Zeros((100, 100)) >> DispatchExample() feature.numpy() out = feature(zeros) ``` -------------------------------- ### Using DispatchExample with PyTorch Backend Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/4-developers/DTDV421_backends.ipynb Demonstrates how to apply the `DispatchExample` feature to a PyTorch tensor. The `feature.torch()` call ensures that the PyTorch-specific code path within `DispatchExample` is executed. ```python feature = Zeros((100, 100)) >> DispatchExample() feature.torch() output = feature(zeros) ``` -------------------------------- ### Runtime Warning Example Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/2-examples/DTEx203_particle_sizing.ipynb This example shows a potential runtime warning related to invalid values encountered during square root calculations in the optics module. ```python c:\users\gu\deeptrack\deeptrack-2.0\deeptrack\optics.py:272: RuntimeWarning: invalid value encountered in sqrt * np.sqrt(1 - (NA / refractive_index_medium) ** 2 * RHO), ``` -------------------------------- ### Initialize Brightfield Microscope Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT341_sequences.ipynb Instantiate a Brightfield microscope with specified optical parameters including initial wavelength and output region. ```python optics = Brightfield( NA=0.6, magnification=10, resolution=1e-6, wavelength=400e-9, # Initial wavelength at time step 0 output_region=(0, 0, 128, 128), ) ``` -------------------------------- ### Create a Pipeline with a Custom Scatterer Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/1-getting-started/DTGS171A_custom_scatterers.ipynb Set up an optical system (e.g., Fluorescence microscope) and apply the custom scatterer to it to form a simulation pipeline. This pipeline can then be resolved to generate an image. ```python optics = dt.Fluorescence() pip = optics(cylinder) ``` -------------------------------- ### Implement `get` method for Custom Cylinder Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/1-getting-started/DTGS171A_custom_scatterers.ipynb Implement the `get` method to generate the voxel representation of the custom cylinder. This involves creating a meshgrid, rotating it, and applying a mask based on cylinder geometry. ```python def get( self, *ignore, # catches unused positional arguments radius, length, rotation, voxel_size, **kwargs ): # Determine grid size rad_ceil = np.ceil(np.max(radius) / np.min(voxel_size[:2])) len_ceil = np.ceil(length * 0.5 / voxel_size[2]) ceil = int(max(rad_ceil, len_ceil)) # Create grid x = np.arange(-ceil, ceil) * voxel_size[0] y = np.arange(-ceil, ceil) * voxel_size[1] z = np.arange(-ceil, ceil) * voxel_size[2] Y, X, Z = np.meshgrid(y, x, z) # Rotate the grid cos = np.cos(rotation) sin = np.sin(rotation) XR = ( cos[0] * cos[1] * X + (cos[0] * sin[1] * sin[2] - sin[0] * cos[2]) * Y + (cos[0] * sin[1] * cos[2] + sin[0] * sin[2]) * Z ) YR = ( sin[0] * cos[1] * X + (sin[0] * sin[1] * sin[2] + cos[0] * cos[2]) * Y + (sin[0] * sin[1] * cos[2] - cos[0] * sin[2]) * Z ) ZR = (-sin[1] * X) + cos[1] * sin[2] * Y + cos[1] * cos[2] * Z # Cylinder mask mask = ((XR**2 / radius[0] ** 2 + ZR**2 / radius[1] ** 2) < 1) & ( np.abs(YR) < length / 2 ) return mask.astype(float) ``` -------------------------------- ### Import necessary libraries and load dataset Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/2-examples/DTEx203_particle_sizing.ipynb Imports required libraries and loads the 'ParticleSizing' dataset. Ensure the dataset is downloaded and available. ```python import numpy as np import matplotlib.pyplot as plt import deeptrack as dt from deeptrack.extras import datasets datasets.load('ParticleSizing') experimental_data = np.load('datasets/ParticleSizing/sizing_150nm_227nm_PSL.npy', allow_pickle=True) import scipy.io as IO IMAGE_SIZE = 64 ``` -------------------------------- ### Python Module Docstring Example Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/4-developers/DTDV411_style.ipynb This snippet demonstrates the standard format for a Python module docstring, including an overview, key features, module structure, and examples. Ensure the closing triple quotes are on a line by themselves. ```python """Docstring for the example.py module. Module names should be short and all lowercase. The module name may have underscores if this improves readability. Every module should have a docstring at the very top of the file. The module's docstring may extend over multiple lines. If your docstring does extend over multiple lines, the closing three quotation marks must be on a line by itself, preferably preceded by a blank line. It is a good idea to provide an overview of the module here so that someone who reads the module's docstring does not have to read the entire file to understand what the module does. Key Features ------------ - **First Specific Feature** Brief description of the first module feature. This description can be on multiple lines. - **Second Specific Feature** Brief description of the second module feature. This description can be on multiple lines. Module Structure ---------------- Classes: - `MyClass`: One-line description. Possibly more extensive description of this class, which can also be on multiple lines. Functions: - `my_function(param1, param2) -> list[int]` Short description of the function. Attributes: - `attr1`: int Short desctiption of the attribute. - `attr2`: int Short desctiption of the attribute. Examples -------- Provide some simple examples of the use of this module. """ pass ``` -------------------------------- ### Get Number of Images in Source Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT391B_sources.folder.ipynb Returns the total number of images indexed by the `ImageFolder` source. ```python len(train_source) ``` -------------------------------- ### Import Libraries for DeepTrack and Optuna Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/1-getting-started/DTGS127_characterizing_aberrations_optuna.ipynb Imports required libraries for the tutorial, including DeepTrack for optical simulations, Matplotlib for plotting, and Optuna for hyperparameter optimization. ```python import random import deeptrack as dt from matplotlib import pyplot as plt import optuna ``` -------------------------------- ### Create ImageFolder Source Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT393B_pytorch.data.ipynb Initializes an `ImageFolder` source to provide metadata about images in a directory. It prints the total number of images and the details of the first file. ```python files = dt.sources.ImageFolder(root=os.path.join(dataset_path, "cell_images")) print(f"Dataset contains {len(files)} images") print(f"There are {len(files)} files.") print(f"The first file is {files[0]}.") ``` -------------------------------- ### Get Cell Trajectories Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/2-examples/DTEx241A_MAGIK_cell_migration_analysis.ipynb Extracts cell trajectories from a DataFrame of edges. Requires the DeepTrack library. ```python traj = dt.models.gnns.get_traj(edges_df, th = 8) ``` -------------------------------- ### Check OpenCV Availability Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT399F_backend._config.ipynb Import and check the `OPENCV_AVAILABLE` boolean flag to determine if OpenCV is installed and accessible. ```python from deeptrack.backend import OPENCV_AVAILABLE print(OPENCV_AVAILABLE) ``` -------------------------------- ### Initialize Propagation Matrices and Load Data Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/2-examples/DTEx231J_LodeSTAR_measure_mass_cell.ipynb Sets up propagation matrices for forward and backward propagation and loads experimental cell data. Ensure the 'datasets/CellData/shifted_cell_data.npy' file exists. ```python import numpy as np import matplotlib.pyplot as plt crop_size = 64 padding = 32 wavelength = 633e-9 pixel_size = 114e-9 crop_propagation_forward = dt.get_propagation_matrix((crop_size + 2 * padding, ) * 2, 1e-7, pixel_size * 2, wavelength) crop_propagation_backward = dt.get_propagation_matrix((crop_size + 2 * padding, ) * 2, -1e-7, pixel_size * 2, wavelength) experimental_data = np.load(r'datasets/CellData/shifted_cell_data.npy', allow_pickle=True) ``` -------------------------------- ### Check PyTorch Availability Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT399F_backend._config.ipynb Import and check the `TORCH_AVAILABLE` boolean flag to determine if PyTorch is installed and accessible. ```python from deeptrack.backend import TORCH_AVAILABLE print(TORCH_AVAILABLE) ``` -------------------------------- ### Iterate and Evaluate SequentialProperty Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT306_properties.ipynb Demonstrates how to iterate through a SequentialProperty, evaluate it at each step, store the results, and print the sequence of evaluated values. ```python for index in range(seq_prop.sequence_length()): # 1. Set the current index of the sequence seq_prop.set_current_index(index) # 2. Evaluate feature current_value = seq_prop.sample() # 3. Store the current value seq_prop.store(current_value) # 4. Print all the stored values so far print(f"{index}: {seq_prop.previous()}") ``` -------------------------------- ### Define Fluorescence Optics Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT341_sequences.ipynb Initializes a Fluorescence optical device with specified parameters. This is a common setup for imaging. ```python from deeptrack.optics import Fluorescence optics = Fluorescence( NA=0.6, magnification=10, resolution=1e-6, wavelength=633e-9, output_region=(0, 0, 32, 32), ) ``` -------------------------------- ### Define Simulation Parameters and Optics Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/2-examples/DTEx231I_LodeSTAR_measure_mass_simulated.ipynb Sets up simulation parameters such as crop size, padding, wavelength, and pixel size. It also defines the propagation matrices and a simulated particle using Mie theory. ```python import numpy as np import matplotlib.pyplot as plt crop_size = 40 padding = 32 wavelength = 633e-9 pixel_size = 114e-9 crop_propagation_forward = dt.get_propagation_matrix((crop_size + 2 * padding, ) * 2, 1e-7, pixel_size * 2, wavelength) crop_propagation_backward = dt.get_propagation_matrix((crop_size + 2 * padding, ) * 2, -1e-7, pixel_size * 2, wavelength) particle = dt.MieSphere( radius=lambda: 228e-9, refractive_index=lambda: 1.58, z=lambda: 0, position=(crop_size / 2,) * 2, L=100, ) optics = dt.Brightfield( NA=1.3, magnification=1, wavelength=633e-9, resolution=2*1.14e-7, illumination=dt.Gaussian(sigma=0.005) >> dt.Gaussian(sigma=0.005j), return_field=True, output_region=(0, 0, 40, 40) ) imaged_particle = optics(particle) training_data = (imaged_particle >> dt.Real()) & (imaged_particle >> dt.Imag()) training_data = training_data >> dt.Merge(lambda: lambda x: np.concatenate( [np.array(_x) for _x in x], axis=-1 )) ``` -------------------------------- ### Import Libraries Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/2-examples/DTEx211_MNIST.ipynb Imports the required libraries for the DeepTrack MNIST example, including DeepTrack, NumPy, Matplotlib, and PyTorch. ```python import deeptrack as dt import numpy as np import matplotlib.pyplot as plt import torch ``` -------------------------------- ### Create and Trigger SourceItem with Callback Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT391A_sources.base.ipynb Instantiate a `SourceItem` with the defined callback and some data. Calling the `SourceItem` triggers the registered callback, demonstrating its execution. ```python from deeptrack.sources.base import SourceItem item = SourceItem([callback], a=5, b=10) item(); ``` -------------------------------- ### Access Feature Properties Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/1-getting-started/DTGS101_intro.ipynb Retrieve properties of a feature by accessing 'feature.{propertyname}'. This example extracts the position of a particle. ```python noisy_image_particle_position = dt.Value(particle.position) image = noisy_image() position = noisy_image_particle_position() plt.imshow(np.squeeze(image), cmap="gray") plt.scatter(position[1], position[0], c="r"); ``` -------------------------------- ### Train LodeSTAR Model Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/2-examples/DTEx231E_LodeSTAR_track_Fluo-C2DL-Huh7.ipynb Initializes the trainer and starts the training process for the LodeSTAR model using the defined dataloader. ```python # Start training. trainer_lodestar = dl.Trainer(max_epochs=30, accelerator="cuda") trainer_lodestar.fit(model, dataloader) ``` -------------------------------- ### Load Datasets and Data Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/2-examples/DTEx231H_LodeSTAR_track_3D_holography.ipynb Loads necessary datasets and pre-processed experimental data for 3D tracking. Ensure the '3DTracking' and 'ParticleSizing' datasets are downloaded. ```python import random import deeptrack as dt import deeptrack.extras import numpy as np import skimage.color import matplotlib.pyplot as plt import tensorflow as tf import scipy.io deetrack.extras.datasets.load("3DTracking") deetrack.extras.datasets.load("ParticleSizing") video = scipy.io.loadmat(r"./datasets/3DTracking/1Every1_2_movie_1.aviProcessedField0.mat") holotraces = scipy.io.loadmat(r"./datasets/3DTracking/1Every1_2_movie_1.aviCompletedTraces") mapping = scipy.io.loadmat(r"./datasets/3DTracking/mapping.mat")["Mapping"] ``` -------------------------------- ### Declare Class with PropertyLike Type Hint Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT387_types.ipynb Example of a DeepTrack2 class with type hints for initialization parameters, including PropertyLike. ```python from __future__ import annotations import deeptrack as dt from deeptrack.types import PropertyLike class Augmentation(dt.Feature): def __init__( self: Augmentation, time_consistent: PropertyLike[bool] = False, **kwargs: Any, ) -> None: super().__init__(time_consistent=time_consistent, **kwargs) def _image_wrapped_process_and_get( self: Augmentation, image_list: list[Image], time_consistent: bool, **kwargs ) -> list[Image]: pass ``` -------------------------------- ### Import Backend-Agnostic Functions Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/1-getting-started/DTGS161_torch_fitting.ipynb Import `xp` for backend-agnostic numpy-like functions, such as `torch.exp` or `np.exp`. ```python from deeptrack.backend import xp ``` -------------------------------- ### Declare Function with ArrayLike Type Hint Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT387_types.ipynb Example of declaring a function that accepts and returns ArrayLike types for float arrays. ```python from deeptrack.types import ArrayLike def matrix_add( input: ArrayLike[float] ) -> ArrayLike[float]: """Function docsting.""" return input + input ``` -------------------------------- ### Apply Functions to Features Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/1-getting-started/DTGS101_intro.ipynb Features can be piped into standard functions like 'np.sqrt'. This example calculates the square root of an image. ```python # Function. sqrt_of_particle = imaged_particle >> np.sqrt sqrt_of_particle.plot(cmap="gray"); ``` -------------------------------- ### Create Dataset and DataLoader Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/2-examples/DTEx231F_LodeSTAR_track_PhC-C2DL-PSC.ipynb Creates a PyTorch dataset from the defined training pipeline with a specified length and no replacement, then sets up a DataLoader for batching and shuffling the training data. ```python train_dataset = dt.pytorch.Dataset( training_pipeline, length=128, replace=False, ) dataloader = dl.DataLoader( train_dataset, batch_size=4, shuffle=True, ) ``` -------------------------------- ### Instantiate Feature with Default Property Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT301_features.ipynb Demonstrates instantiating the `Foo` feature without explicitly setting `my_property`, showing that it defaults to `None`. ```python foo3 = Foo() foo3.update() output_image = foo3() output_image ``` -------------------------------- ### Get Available Classes from Source Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT391B_sources.folder.ipynb Retrieves a list of unique class names that the `ImageFolder` source has identified from the directory structure. ```python train_source.classes ``` -------------------------------- ### Complete Cylinder Scatterer Class Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/1-getting-started/DTGS171A_custom_scatterers.ipynb Assemble the `Cylinder` class by combining the `__init__`, `_process_properties`, and `get` methods, along with the `__conversion_table__`. ```python class Cylinder(dt.Scatterer): __conversion_table__ = ConversionTable( radius=(u.meter, u.meter), length=(u.meter, u.meter), rotation=(u.radian, u.radian), ) def __init__( self, radius=1e-6, length=2e-6, rotation=(0, 0, 0), **kwargs ): super().__init__( radius=radius, length=length, rotation=rotation, **kwargs ) def _process_properties(self, properties): properties = super()._process_properties(properties) # Ensure radius is of length 2 radius = np.array(properties["radius"]) if radius.ndim == 0: radius = np.array((properties["radius"], properties["radius"])) elif radius.size == 1: radius = np.array((*radius,) * 2) properties["radius"] = radius # Ensure length is scalar properties["length"] = float(properties["length"]) # Ensure rotation is 3 values rotation = np.array(properties["rotation"]) if rotation.ndim == 0: rotation = [rotation, 0, 0] elif rotation.size == 1: rotation = [rotation[0], 0, 0] elif rotation.size == 2: rotation = [rotation[0], rotation[1], 0] properties["rotation"] = tuple(rotation) return properties ``` -------------------------------- ### Initialize BinaryClassifier Model Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/1-getting-started/DTGS111_datafiles.ipynb Instantiate a BinaryClassifier with a specified model architecture. This sets up the loss function and metrics for binary classification tasks. ```python model = dl.BinaryClassifier(model) print(model) ``` -------------------------------- ### Train LodeSTAR Model Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/2-examples/DTEx231G_LodeSTAR_track_plankton.ipynb Initializes a DeepPlay Trainer and starts the training process for the LodeSTAR model using the prepared dataloader. ```python # Start training. trainer_lodestar = dl.Trainer(max_epochs=100, accelerator="auto") trainer_lodestar.fit(model, dataloader) ``` -------------------------------- ### Create Dummy Dataset Directory Structure Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT391B_sources.folder.ipynb This code creates a dummy directory structure for an image dataset, including train/test splits and class categories, along with placeholder image files. ```python import os import shutil # Define root directory dataset_path = "dummy_dataset" # Remove existing directory if exists if os.path.exists(dataset_path): shutil.rmtree(dataset_path) # Define splits and class names splits = ["train", "test"] classes = ["cat", "dog", "bird"] # Create directory structure and add dummy files for split in splits: for class_name in classes: class_dir = os.path.join(dataset_path, split, class_name) os.makedirs(class_dir, exist_ok=True) for i in range(3): with open( os.path.join(class_dir, f"{split}_image_{i}.jpg"), "w" ) as f: f.write("") ``` -------------------------------- ### Apply Arithmetic Operators to Features Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/1-getting-started/DTGS101_intro.ipynb Features can interact with standard arithmetic operators. This example inverts an image using '1 - imaged_particle'. ```python # Arithmetic operator. inverted_particle = 1 - imaged_particle inverted_particle.plot(cmap="gray"); ``` -------------------------------- ### Initialize ImageFolder Source Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT391B_sources.folder.ipynb Initializes an `ImageFolder` object from a specified directory path. This object will manage the image dataset. ```python from deeptrack.sources.folder import ImageFolder train_source = ImageFolder(os.path.join(dataset_path, "train")) ``` -------------------------------- ### Declare Function with Custom DoubleLike Type Hint Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT387_types.ipynb Example of a function that uses the custom 'DoubleLike' type hint for its parameters and return value. ```python def double_add( input: ArrayLike[DoubleLike], number: DoubleLike ) -> ArrayLike[DoubleLike]: """Function docsting.""" return input + number ``` -------------------------------- ### Create DataLoaders for Experimental Videos Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/2-examples/DTEx212_single_particle_tracking.ipynb Instantiate PyTorch DataLoaders for low-noise and high-noise experimental videos using the custom dataset. This prepares the data for batch processing during model inference. ```python video_low_noise_dataloader = dl.DataLoader( ExperimantalDataset(data_images=video_low_noise), batch_size=32, ) video_high_noise_dataloader = dl.DataLoader( ExperimantalDataset(data_images=video_high_noise), batch_size=32, ) ``` -------------------------------- ### Apply Indexing Operators to Features Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/1-getting-started/DTGS101_intro.ipynb Features support standard indexing operations. This example downsamples an image by taking every second pixel. ```python # Indexing operator. downsampled_particle = imaged_particle[::2, ::2] downsampled_particle.plot(cmap="gray"); ``` -------------------------------- ### Get Kwarg Names for Function Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT383_utils.ipynb Use `get_kwarg_names` to retrieve a list of keyword argument names for a given function, including those from *args and **kwargs. ```python from deeptrack.utils import get_kwarg_names # Function with *args and **kwargs def k(*args, alpha=0.1, beta=0.2, **kwargs): pass print(get_kwarg_names(k)) ``` ```python # Built-in function print(get_kwarg_names(len)) ``` ```python # Lambda function l = lambda x, y=5: x + y get_kwarg_names(l) ``` ```python # Method class MyClass: def method(self, a, b=2): pass get_kwarg_names(MyClass.method) ``` -------------------------------- ### Configure Data Generation Arguments and Optics Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/2-examples/DTEx231B_LodeSTAR_tracking_particles_of_various_shapes.ipynb Sets up arguments for particle properties like shape, position, and SNR, and defines the optical system's output region. ```python arguments = dt.Arguments( particle_shape = shape_to_train_on, position=lambda: np.random.uniform(27, 37, 2), snr=lambda: np.random.uniform(2, 20) ) optics = dt.Fluorescence(output_region=(0, 0, 64, 64)) ``` -------------------------------- ### Define Data Loading and Label Pipelines Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT393B_pytorch.data.ipynb Sets up data pipelines for loading images and extracting labels. Images are resized to 32x32 and divided by 255. Labels are provided as constant values. ```python loader = dt.LoadImage(path=files.path) label_pipeline = dt.Value(value=files.label) loader_pipeline = ( loader >> dt.Resize((32, 32)) >> dt.Divide(255) ) data_pipeline = loader_pipeline & label_pipeline ``` -------------------------------- ### Initialize Trace Detection Loop Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/2-examples/DTEx231H_LodeSTAR_track_3D_holography.ipynb Initializes an empty list to store traces and begins a loop to process detected objects for tracing. ```python traces = [] for idx in range(len(dets)): connections = [] connected_observations = [] # Trace doroplets ``` -------------------------------- ### Train LodeSTAR Model Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/2-examples/DTEx231D_LodeSTAR_track_BF-C2DL-HSC.ipynb Initializes the trainer with a specified number of epochs and accelerator (CUDA), then starts the training process using the model and dataloader. ```python # Start training. trainer_lodestar = dl.Trainer(max_epochs=100, accelerator="cuda") trainer_lodestar.fit(model, dataloader) ``` -------------------------------- ### Download file from Google Drive Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/2-examples/DTEx213_multi_particle_tracking.ipynb A utility function to download files from Google Drive using their file ID. Requires the 'gdown' library to be installed. ```python def download_file_from_google_drive( file_id: str, root, ): try: import gdown except ModuleNotFoundError: raise RuntimeError( "To download files from GDrive, 'gdown' is required. You can install it with 'pip install gdown'." ) root = os.path.expanduser(root) filename = file_id + ".zip" fpath = os.fspath(os.path.join(root, filename)) os.makedirs(root, exist_ok=True) gdown.download(id=file_id, output=fpath, quiet=False) return fpath ``` -------------------------------- ### Example Module Structure in Python Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/4-developers/DTDV401_overview.ipynb Illustrates a typical Python module structure within DeepTrack2, including abstract base classes and concrete implementations. ```python """Example module. ... """ from deeptrack.backend import BackendClass class AbstractClass(BackendClass): """Define abstract class that inherits from some backbone module. """ def __init__(...): super().__init__(...) def process(): self.get() class AdditionCase(AbstractClass): """Define concrete implementation of the abstract class. """ def __init__(...): super().__init__(...) # Define abstract method. def get(...): return 1 + 1 ``` -------------------------------- ### Set Backend to PyTorch and Perform Operation Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/4-developers/DTDV421_backends.ipynb Switches the computational backend to PyTorch using `config.set_backend()` and performs an array operation with `xp`. Verifies the output type is a PyTorch tensor. ```python config.set_backend("torch") output_with_torch = xp.sum(xp.random.randn(100, 100), keepdims=True) print(f"type(output_with_torch): {type(output_with_torch)}") ``` -------------------------------- ### Apply Reuse with Augmentation Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT329_augmentations.ipynb Create a pipeline using `Reuse` with a slow feature and apply `FlipLR` augmentation. This demonstrates efficient generation of augmented images from a cached feature output. ```python slow_feature = DummyFeature() pipeline = Reuse(slow_feature, uses=3) >> FlipLR(p=0.5) output_3 = pipeline.resolve() plt.imshow(output_3[..., 0]) plt.title("Flipped feature output") plt.show() ``` -------------------------------- ### Dynamic Evaluation with Features Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT391A_sources.base.ipynb Source attributes can be passed directly to features and dynamically evaluated using `SourceItem`. This example combines two source attributes. ```python import deeptrack as dt feature = dt.Value(source.a) + dt.Value(source.b) feature(source[2]) ``` -------------------------------- ### Poisson Noise Implementation Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT327_noises.ipynb Introduces Poisson-distributed noise to an image. Initialize with a signal-to-noise ratio (snr). This example chains an Offset noise with Poisson noise. ```python from deeptrack.noises import Poisson poisson_noise = offset >> Poisson(snr=20) poisson_noise.plot(input_image=np.zeros((32, 32, 1))) plt.show() ``` -------------------------------- ### Create and Generate Dataset Images Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/2-examples/DTEx205_inline_holography_3d_tracking.ipynb Combines the optical system, particles, noise, gradient, and normalization to create the final dataset. It then generates and displays sample images from this dataset. ```python dataset = optics(particles) >> noise >> gradient >> normalization ``` ```python for _ in range(4): image = dataset.update().resolve() plt.imshow(image[:, :, 0], cmap="gray") plt.colorbar() plt.show() ``` -------------------------------- ### Instantiate Feature with Random Choice Property Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT301_features.ipynb Example of using a lambda function with `np.random.choice` to assign a random value from a list to a feature's property. ```python # Pick one random element from the list. foo5 = Foo(my_property=lambda: np.random.choice(["bar", "baz"])) foo5.update() output_image = foo5() output_image ``` -------------------------------- ### Combine Image and Properties with '&' Operator Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/1-getting-started/DTGS101_intro.ipynb Use the '&' operator to stack features, returning multiple outputs. This example retrieves both the image and particle positions simultaneously. ```python position_feature = dt.TakeProperties(experimental_image, "position") image_and_position = experimental_image & position_feature output_image, positions = image_and_position.update()() plt.imshow(np.squeeze(output_image), cmap="gray") plt.scatter(positions[:, 1], positions[:, 0], c="r", s=100, linewidths=4, marker="x") plt.show() ``` -------------------------------- ### Create Dataset and DataLoader Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/2-examples/DTEx231D_LodeSTAR_track_BF-C2DL-HSC.ipynb Creates a PyTorch dataset from the defined training pipeline and initializes a DataLoader for batching and shuffling the training data. ```python train_dataset = dt.pytorch.Dataset( training_pipeline, length=256, replace=False, ) dataloader = dl.DataLoader( train_dataset, batch_size=8, shuffle=True, ) ``` -------------------------------- ### Get Non-existent Property with Default Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT311_image.ipynb Demonstrates the behavior of `.get_property()` when a requested property does not exist. It returns the specified default value instead of raising an error. ```python output_image.get_property("a_property_that_does_not_exist", default="Not found!") ``` -------------------------------- ### Get All Position Property Instances Source: https://github.com/deeptrackai/deeptrack2/blob/develop/tutorials/3-advanced-topics/DTAT311_image.ipynb Retrieves all 'position' properties from an `Image` object. This allows for the analysis of multiple feature instances that may have contributed to the image. ```python output_image.get_property("position", get_one=False) ```