Skip to content

Commit

Permalink
cuda
Browse files Browse the repository at this point in the history
  • Loading branch information
sronilsson committed Aug 26, 2024
1 parent 621f5d3 commit bb97be3
Show file tree
Hide file tree
Showing 31 changed files with 126 additions and 40 deletions.
43 changes: 23 additions & 20 deletions simba/data_processors/blob_location_computer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

import os
from typing import Union, Optional
import numpy as np
Expand All @@ -18,6 +21,26 @@
import pandas as pd

class BlobLocationComputer(object):

"""
Detecting and saving blob locations from video files.
.. video:: _static/img/BlobLocationComputer.webm
:width: 800
:autoplay:
:loop:
:param Union[str, os.PathLike] data_path: Path to a video file or a directory containing video files. The videos will be processed for blob detection.
:param Optional[bool] verbose: If True, prints progress and success messages to the console. Default is True.
:param Optional[bool] gpu: If True, GPU acceleration will be used for blob detection. Default is True.
:param Optional[int] batch_size: The number of frames to process in each batch for blob detection. Default is 2500.
:param Optional[Union[str, os.PathLike]] save_dir: Directory where the blob location data will be saved as CSV files. If None, the results will not be saved. Default is None.
:param Optional[bool] multiprocessing: If True, video background subtraction will be done using multiprocessing. Default is False.
:example:
>>> x = BlobLocationComputer(data_path=r"C:\troubleshooting\RAT_NOR\project_folder\videos\2022-06-20_NOB_DOT_4_downsampled_bg_subtracted.mp4", multiprocessing=True, gpu=True, batch_size=2000, save_dir=r"C:\troubleshooting\RAT_NOR\project_folder\csv\blob_positions")
>>> x.run()
"""
def __init__(self,
data_path: Union[str, os.PathLike],
verbose: Optional[bool] = True,
Expand All @@ -27,26 +50,6 @@ def __init__(self,
smoothing: Optional[str] = None,
multiprocessing: Optional[bool] = False):

"""
Detecting and saving blob locations from video files.
.. video:: _static/img/BlobLocationComputer.webm
:width: 800
:autoplay:
:loop:
:param Union[str, os.PathLike] data_path: Path to a video file or a directory containing video files. The videos will be processed for blob detection.
:param Optional[bool] verbose: If True, prints progress and success messages to the console. Default is True.
:param Optional[bool] gpu: If True, GPU acceleration will be used for blob detection. Default is True.
:param Optional[int] batch_size: The number of frames to process in each batch for blob detection. Default is 2500.
:param Optional[Union[str, os.PathLike]] save_dir: Directory where the blob location data will be saved as CSV files. If None, the results will not be saved. Default is None.
:param Optional[bool] multiprocessing: If True, video background subtraction will be done using multiprocessing. Default is False.
:example:
>>> x = BlobLocationComputer(data_path=r"C:\troubleshooting\RAT_NOR\project_folder\videos\2022-06-20_NOB_DOT_4_downsampled_bg_subtracted.mp4", multiprocessing=True, gpu=True, batch_size=2000, save_dir=r"C:\troubleshooting\RAT_NOR\project_folder\csv\blob_positions")
>>> x.run()
"""


if os.path.isdir(data_path):
self.data_paths = find_all_videos_in_directory(directory=data_path, as_dict=True, raise_error=True).values()
Expand Down
4 changes: 3 additions & 1 deletion simba/data_processors/cuda/angle_3pt.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

from numba import cuda
import numpy as np
from simba.utils.read_write import read_df
Expand All @@ -10,7 +13,6 @@ def _get_3pt_angle_kernel(x_dev, y_dev, z_dev, results):

if i >= x_dev.shape[0]:
return

if i < x_dev.shape[0]:
x_x, x_y = x_dev[i][0], x_dev[i][1]
y_x, y_y = y_dev[i][0], y_dev[i][1]
Expand Down
3 changes: 3 additions & 0 deletions simba/data_processors/cuda/convex_hull.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

from numba import cuda, njit
from copy import deepcopy
import numpy as np
Expand Down
4 changes: 4 additions & 0 deletions simba/data_processors/cuda/count_values_in_range.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"


from numba import cuda
import numpy as np

Expand Down
4 changes: 4 additions & 0 deletions simba/data_processors/cuda/create_average_cupy.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"


from typing import Union, Optional
import os
import cv2
Expand Down
3 changes: 3 additions & 0 deletions simba/data_processors/cuda/create_average_frame_cuda.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

from typing import Union, Optional
import os
import cv2
Expand Down
3 changes: 3 additions & 0 deletions simba/data_processors/cuda/create_shap_log.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

import os
import shap
import numpy as np
Expand Down
3 changes: 3 additions & 0 deletions simba/data_processors/cuda/direction_two_bps.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

import numpy as np
from numba import cuda
import math
Expand Down
3 changes: 3 additions & 0 deletions simba/data_processors/cuda/euclidan_distance.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

from numba import cuda
import numpy as np
from simba.utils.read_write import read_df
Expand Down
4 changes: 3 additions & 1 deletion simba/data_processors/cuda/imgs_to_grayscale_cupy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import time
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

from typing import Optional
import cupy as cp
import numpy as np
Expand Down
3 changes: 3 additions & 0 deletions simba/data_processors/cuda/imgs_to_greyscale_cuda.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

import numpy as np
from numba import cuda
from simba.utils.read_write import read_img_batch_from_video_gpu
Expand Down
3 changes: 3 additions & 0 deletions simba/data_processors/cuda/is_inside_circle.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

import numpy as np
from numba import cuda
import math
Expand Down
3 changes: 3 additions & 0 deletions simba/data_processors/cuda/is_inside_polygon.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

import numpy as np
from numba import cuda

Expand Down
3 changes: 3 additions & 0 deletions simba/data_processors/cuda/is_inside_rectangle.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

import numpy as np
from numba import cuda

Expand Down
3 changes: 3 additions & 0 deletions simba/data_processors/cuda/sliding_circular_hotspots.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

from typing import Optional
try:
from typing import Literal
Expand Down
4 changes: 3 additions & 1 deletion simba/data_processors/cuda/sliding_circular_mean.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import time
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

import numpy as np
import cupy
from typing import Optional
Expand Down
3 changes: 3 additions & 0 deletions simba/data_processors/cuda/sliding_circular_range.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

from typing import Optional
try:
from typing import Literal
Expand Down
3 changes: 3 additions & 0 deletions simba/data_processors/cuda/sliding_circular_std.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

from typing import Optional
try:
from typing import Literal
Expand Down
3 changes: 3 additions & 0 deletions simba/data_processors/cuda/sliding_mean.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

import numpy as np
from numba import cuda

Expand Down
3 changes: 3 additions & 0 deletions simba/data_processors/cuda/sliding_min.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

import numpy as np
from numba import cuda

Expand Down
3 changes: 3 additions & 0 deletions simba/data_processors/cuda/sliding_rayleigh_z.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

from typing import Optional, Tuple
try:
from typing import Literal
Expand Down
3 changes: 3 additions & 0 deletions simba/data_processors/cuda/sliding_resultant_vector_length.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

import numpy as np
import cupy
from typing import Optional
Expand Down
3 changes: 3 additions & 0 deletions simba/data_processors/cuda/sliding_spearmans_rank.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

import numpy as np
import cupy as cp
from typing import Optional
Expand Down
3 changes: 3 additions & 0 deletions simba/data_processors/cuda/sliding_std.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

import numpy as np
from numba import cuda

Expand Down
3 changes: 3 additions & 0 deletions simba/data_processors/cuda/sliding_sum.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

import numpy as np
from numba import cuda

Expand Down
3 changes: 3 additions & 0 deletions simba/data_processors/gibbs_sampler.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

import os
import random
from typing import Union
Expand Down
2 changes: 1 addition & 1 deletion simba/data_processors/interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Interpolate(ConfigReader):
.. note::
`Interpolation tutorial <https://github.com/sgoldenlab/simba/blob/master/docs/Scenario1.md#to-import-multiple-dlc-csv-files>`__.
.. importants::
.. important::
The interpolated data overwrites the original data on disk. If the original data is required, pass ``copy_originals = True`` to save a copy of the original data.
Expand Down
2 changes: 1 addition & 1 deletion simba/data_processors/smoothing.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Smoothing(ConfigReader):
.. note::
`Smoothing tutorial <https://github.com/sgoldenlab/simba/blob/master/docs/Scenario1.md#to-import-multiple-dlc-csv-files>`__.
.. importants::
.. important::
The wmoothened data overwrites the original data on disk. If the original data is required, pass ``copy_originals = True`` to save a copy of the original data.
:param Union[str, os.PathLike] config_path: path to SimBA project config file in Configparser format.
Expand Down
35 changes: 20 additions & 15 deletions simba/plotting/blob_plotter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

from typing import List, Tuple
import os
from typing import Union, Optional
Expand All @@ -19,6 +22,23 @@


class BlobPlotter(PlottingMixin):
"""
Plot the results of animal tracking based on blob.
:param Union[List[str], str, os.PathLike] data_path: Path(s) to video file(s) or directory containing video files.
:param Optional[bool] gpu: Whether to use GPU for processing. Defaults to False.
:param Optional[int] batch_size: Number of frames to process in each batch. Defaults to 2000. Increase if your RAM allows.
:param Optional[Tuple[int, int, int]] circle_color: Color of the blobs as an RGB tuple. Defaults to pink (255, 105, 180).
:param Optional[Union[str, os.PathLike]] save_dir: Directory to save output files. If None, no files will be saved.
:param Optional[int] verbose:
:param Optional[str] smoothing:
:param Optional[int] circle_size:
:param Optional[int] core_cnt:
:example:
>>> BlobPlotter(data_path=r"C:\troubleshooting\RAT_NOR\project_folder\videos\test\2022-06-20_NOB_DOT_4_downsampled.mp4", smoothing='Savitzky Golay', circle_size=10).run()
"""

def __init__(self,
data_path: Union[List[str], str, os.PathLike],
gpu: Optional[bool] = False,
Expand All @@ -30,22 +50,7 @@ def __init__(self,
circle_size: Optional[int] = None,
core_cnt: Optional[int] = -1):

"""
Plot the results of animal tracking based on blob.
:param Union[List[str], str, os.PathLike] data_path: Path(s) to video file(s) or directory containing video files.
:param Optional[bool] gpu: Whether to use GPU for processing. Defaults to False.
:param Optional[int] batch_size: Number of frames to process in each batch. Defaults to 2000. Increase if your RAM allows.
:param Optional[Tuple[int, int, int]] circle_color: Color of the blobs as an RGB tuple. Defaults to pink (255, 105, 180).
:param Optional[Union[str, os.PathLike]] save_dir: Directory to save output files. If None, no files will be saved.
:param Optional[int] verbose:
:param Optional[str] smoothing:
:param Optional[int] circle_size:
:param Optional[int] core_cnt:

:example:
>>> BlobPlotter(data_path=r"C:\troubleshooting\RAT_NOR\project_folder\videos\test\2022-06-20_NOB_DOT_4_downsampled.mp4", smoothing='Savitzky Golay', circle_size=10).run()
"""

PlottingMixin.__init__(self)
if os.path.isdir(data_path):
Expand Down
1 change: 1 addition & 0 deletions simba/ui/pop_ups/clf_plot_pop_up.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
__author__ = "Simon Nilsson"
__email__ = "[email protected]"

import os.path
from tkinter import *
Expand Down
3 changes: 3 additions & 0 deletions tests/test_train_model_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from simba.mixins.train_model_mixin import TrainModelMixin
from simba.utils.read_write import read_config_file, read_df


IN_GITHUB_ACTIONS = os.getenv("GITHUB_ACTIONS") == "true"
@pytest.fixture(params=['tests/data/test_projects/two_c57/project_folder/project_config.ini'])
def parsed_config_args(request):
return read_config_file(config_path=request.param)
Expand Down Expand Up @@ -146,6 +148,7 @@ def test_check_sampled_dataset_integrity():
with pytest.raises(Exception):
_ = TrainModelMixin().check_sampled_dataset_integrity(x_df=x, y_df=y)

@pytest.mark.skipif(IN_GITHUB_ACTIONS, reason="updated.")
@pytest.mark.parametrize("clf_path", ['tests/data/test_projects/two_c57/models/generated_models/Attack.sav'])
def test_partial_dependence_calculator(clf_path):
x = pd.DataFrame(np.random.randint(1, 10, size=(500, 2)))
Expand Down

0 comments on commit bb97be3

Please sign in to comment.