API Documentation
Here is an API documentation of the MacTrack2 package.
MacTrack2
Analysis
- distance(n)[source]
This function calculates the distance of each segmented macrophage to the right edge of the image. It processes images in the specified folder, extracts the distance for each segmented macrophage, and saves the results in an Excel file.
- Parameters:
n (int) – The number of segmented macrophages to process.
- Returns:
The path to the output Excel file containing the distance data.
- Return type:
str
- distance_to_right_edge(image_path)[source]
Calculate the distance from the center of the largest contour in an image to the right edge of the image.
- Parameters:
image_path (str) – Path to the input image.
- Returns:
Distance from the center of the largest contour to the right edge of the image.
- Return type:
int
- Raises:
ValueError – If the image cannot be loaded or if no contours are found.
- graph_distance()[source]
Generates a plot of the distance of individuals over time. Reads data from an Excel file, processes it, and saves the plot as a PNG image.
- calculate_ratio(image_path_contour, image_a, image_f0)[source]
Calculate the ratio of the difference between two images (
image_a
andimage_f0
) within the contour of a third image (image_path_contour
). The ratio is calculated as :\[ratio = \frac{f - f_0}{f_0}\]where f is the pixel value in
image_a
andf_0
is the pixel value inimage_f0
.- Parameters:
image_path_contour (str) – Path to the contour image.
image_a (numpy.ndarray) – The first image (green channel) to compare.
image_f0 (numpy.ndarray) – The second image (green channel) to compare.
- Returns:
The mean ratio calculated from the pixels within the contour.
- Return type:
float
- graph_intensity()[source]
Generates a plot of the intensity of individuals over time (frames). Reads data from an Excel file, processes it, and saves the plot as a PNG image.
- graphmed_intensity()[source]
Generates a plot of the median intensity of individuals over time (frames). Reads data from an Excel file, processes it, and saves the plot as a PNG image.
- intensity(n, frame, input_folder)[source]
This function calculates the intensity of segmented macrophages in images. It processes images in the specified folder, extracts the intensity for each segmented macrophage, and saves the results in an Excel file.
- Parameters:
n (int) – The number of segmented macrophages to process.
frame (object) – An object containing the frames of the images.
input_folder (str) – The folder containing the input images.
- Returns:
The path to the output Excel file containing the intensity data.
- Return type:
str
- intensitymed(n, frame, input_folder)[source]
This function calculates the median intensity of segmented macrophages in images. It processes images in the specified folder, extracts the median intensity for each segmented macrophage, and saves the results in an Excel file.
- Parameters:
n (int) – The number of segmented macrophages to process.
frame (object) – An object containing the frames of the images.
input_folder (str) – The folder containing the input images.
- Returns:
The path to the output Excel file containing the median intensity data.
- Return type:
str
- graphmed_perimeter()[source]
Generates a plot of the perimeter of individuals over time (frames). Reads data from an Excel file, processes it, and saves the plot as a PNG image.
- object_size(image_path)[source]
Calculate the perimeter of a contour in an image.
- Parameters:
image_path (str) – Path to the input image.
- Returns:
Perimeter of the largest contour in the image.
- Return type:
float
- Raises:
ValueError – If the image cannot be loaded or if no contours are found.
- perimeter(n)[source]
Calculate the perimeter of objects in images and save the results to an Excel file. The images are expected to be organized in folders named ‘macrophage_<number>’. Each folder contains images named ‘<number>_<index>.png’. The results are saved in an Excel file named ‘perimeter.xlsx’.
- Parameters:
n (int) – The number of images to process in each folder.
- Returns:
The path to the output Excel file.
- Return type:
str
- Raises:
ValueError – If the output file does not have an .xlsx extension.
- aggregate(distance_file, intensity_file, size_file, perimeter_file)[source]
Aggregates data from multiple files and saves the results to an Excel file.
- Parameters:
distance_file (str) – Path to the distance data file.
intensity_file (str) – Path to the intensity data file.
size_file (str) – Path to the size data file.
perimeter_file (str) – Path to the perimeter data file.
- calculate_intensity_features(intensity_data)[source]
Calculates intensity features such as the number of peaks, mean prominence, and mean distance between peaks for each row in the intensity data.
- Parameters:
intensity_data (pd.DataFrame) – DataFrame containing intensity data, where each row represents a signal.
- Returns:
num_peaks (list) – Number of peaks for each row.
mean_prominence (list) – Mean prominence of peaks for each row.
mean_distance (list) – Mean distance between peaks for each row.
- calculate_mean(data)[source]
Calculates the mean of each row in the given DataFrame.
- Parameters:
data (pd.DataFrame) – The input DataFrame to analyze.
- Returns:
A series containing the mean of each row.
- Return type:
pd.Series
- count_valid_entries(data)[source]
Counts the number of valid (non-NaN) entries in each row of the given DataFrame.
- Parameters:
data (pd.DataFrame) – The input DataFrame to analyze.
- Returns:
A series containing the count of valid entries for each row.
- Return type:
pd.Series
- load_data(file_path)[source]
Loads data from an Excel file.
- Parameters:
file_path (str) – Path to the Excel file.
- Returns:
Data loaded from the Excel file.
- Return type:
pd.DataFrame
- plot_intensity_curves(intensity_data, valid_entry_counts, threshold=10)[source]
Plots intensity curves for each row in the DataFrame and saves them as PNG files.
- Parameters:
intensity_data (pd.DataFrame) – The DataFrame containing intensity data.
valid_entry_counts (pd.Series) – A series containing the count of valid entries for each row.
threshold (int) – The minimum number of valid entries required to plot the curve.
- object_size(image_path)[source]
Calculate the size of an object in an image.
- Parameters:
image_path (str) – Path to the input image.
- Returns:
Area of the detected object in pixels.
- Return type:
object_area (float)
- size(n)[source]
Calculate the size of objects in images and save the results to an Excel file. Uses the object_size function to calculate the size of each object in the images. Uses the graph_size function to plot the size of individuals over time.
- Parameters:
n (int) – Number of images to process.
See also
object_size: Function to calculate the size of an object in an image. graph_size: Function to plot the size of individuals over time.
Locate
- calculate_iou(image1, image2)[source]
Calculate the Intersection over Union (IoU) between two binary images.
\[IoU = \frac{A \cap B}{A \cup B}\]where \(A\) and \(B\) are the two binary images.
- Parameters:
image1 (numpy array) – First binary image.
image2 (numpy array) – Second binary image.
- Returns:
IoU value between 0 and 1.
- Return type:
float
- defuse(n, image_storage)[source]
Splits the images by processing them and saving the results.
- Parameters:
n (int) – Number of images to process.
image_storage (ImageStorage) – The image storage object.
- Returns:
The updated image storage object.
- Return type:
image_storage (ImageStorage)
- extract_and_save_objects(image_c, image, heatmap, object, image_storage, min_object_size=100)[source]
Extracts and saves objects from the image based on contours.
- Parameters:
image_c (numpy array) – The original image (BGR or grayscale).
image (numpy array) – The image with contours.
heatmap (int) – The heatmap number.
object (int) – The object number.
image_storage (ImageStorage) – The image storage object.
min_object_size (int) – Minimum size of the object to be extracted. Default is 100.
- Returns:
The updated image storage object.
- Return type:
image_storage (ImageStorage)
- invdefuse(n, image_storage)[source]
Inverse splits the images by processing them and saving the results.
- Parameters:
n (int) – Number of images to process.
image_storage (ImageStorage) – The image storage object.
- Returns:
The updated image storage object.
- Return type:
image_storage (ImageStorage)
- paint_black_area_from_mask(image_c, mask)[source]
Paints the black area of the image based on the mask.
- Parameters:
image_c (numpy array) – The original image (BGR or grayscale).
mask (numpy array) – The mask to use for painting the black area.
- Returns:
The image with the black area painted.
- Return type:
image_c_black (numpy array)
- process_images(list, image_c, heatmap, object, image_storage, max_line_length=50)[source]
Process images by tracing lines between contours and painting the black area.
- Parameters:
list (list) – List of images (numpy arrays) to process.
image_c (numpy array) – The original image (BGR or grayscale).
heatmap (int) – The heatmap number.
object (int) – The object number.
image_storage (ImageStorage) – The image storage object.
max_line_length (int) – Maximum length of the line to be drawn. Default is 50.
- Returns:
The updated image storage object.
- Return type:
image_storage (ImageStorage)
- save_segmentation_images(segmentation_instance, output_folder)[source]
Save the segmented images to the specified output folder.
- Parameters:
segmentation_instance (Segmentation) – The segmentation instance containing the images.
output_folder (str) – The folder where the images will be saved.
- trace_lines_between_contours(images, distance_threshold=50)[source]
Trace lines between contours in a list of images based on a distance threshold. The contours are obtained from the binary images by the
findContours
function from the OpenCV library.- Parameters:
images (list) – List of images (numpy arrays) to process.
distance_threshold (int) – Maximum distance between contours to draw a line.
- Returns:
Image with traced lines between contours.
- Return type:
traced_lines_image (numpy array)
- class segmentation(root_folder)[source]
Bases:
object
Class to handle the loading and processing of images for segmentation.
- add_image(heatmap_test_name, object_name, image)[source]
Add a new image to the specified heatmap test directory.
- Parameters:
heatmap_test_name (str) – Name of the heatmap test directory.
object_name (str) – Name of the object image file.
image (numpy.ndarray) – Image to be added.
- get_image(heatmap_test_name, object_name)[source]
Get a specific image from the loaded images.
- Parameters:
heatmap_test_name (str) – Name of the heatmap test directory.
object_name (str) – Name of the object image file.
- Returns:
Sparse matrix representation of the image.
- Return type:
sparse_image (scipy.sparse.csr_matrix)
- get_list(heatmap_test_name)[source]
Get a list of images from a specific heatmap test directory.
- Parameters:
heatmap_test_name (str) – Name of the heatmap test directory.
- Returns:
List of sparse matrix representations of images.
- Return type:
list
- load_images()[source]
Load images from the root folder and store them in a list. The images are expected to be in subdirectories named “heatmap_test_<number>”. Each subdirectory should contain images named “object_<number>.png”.
- replace_image(heatmap_test_name, object_name, new_image)[source]
Replace an existing image in the specified heatmap test directory.
- Parameters:
heatmap_test_name (str) – Name of the heatmap test directory.
object_name (str) – Name of the object image file.
new_image (numpy.ndarray) – New image to replace the existing one.
- extract_objects(image, output_dir, filename)[source]
Extract objects from a binary image and save them as separate images.
- Parameters:
image (numpy.ndarray) – Input binary image.
output_dir (str) – Directory to save the extracted objects.
filename (str) – Name of the input image file.
- Returns:
Number of objects extracted from the image.
- Return type:
object_count (int)
- filter_small_shapes(image, min_size)[source]
Filter out small shapes in a binary image.
- Parameters:
image (numpy.ndarray) – Input binary image.
min_size (int) – Minimum size of shapes to keep.
- Returns:
Image with small shapes removed.
- Return type:
image (numpy.ndarray)
- locate(input_folder)[source]
Locate the objects in the video and save the results in a folder named ‘output’ in the same directory as the script.
- Parameters:
input_folder (str) – Path to the folder containing the dataset and models.
- Returns:
the prediction on the input video.
- Return type:
p_test (list)
- locate_frame(input_image_path, model_path, output_name)[source]
Same function as locate but for a single frame. Used for model building or to test the performences of the model.
- Parameters:
input_image_path (str) – Path to the input image.
model_path (str) – Path to the model you want to use. Should contain a ‘models’ folder with the model in it and a dataset folder with the dataset used to train the model.
output_name (str) – Name of the output folder where the results will be saved.
- Returns:
the prediction on the input frame
- Return type:
p_test (list)
- create_temporary_dataset(imput_image_path, model_path)[source]
Creates a temporary dataset for structural purposes in the execution of the locate_frame function.
- Parameters:
imput_image_path (str) – Path to the input image.
model_path (str) – Path to the model you want to use. Should contain a ‘models’ folder with the model in it and a dataset folder with the dataset used to train the model.
Track
- supprimer_petit(p)[source]
Deletes subdirectories containing fewer than p PNG files in the specified directory, meaning the macrophages were tracked in less than .. math:p frames.
- Parameters:
p (int) – The threshold number of PNG files.
- calculate_iou(image1, image2)[source]
Calculate the Intersection over Union (IoU) between two binary images.
\[IoU = \frac{A \cap B}{A \cup B}\]where \(A\) and \(B\) are the two binary images.
- Parameters:
image1 (numpy array) – First binary image.
image2 (numpy array) – Second binary image.
- Returns:
IoU value between 0 and 1.
- Return type:
float
- find_containing_folder(image_path, root_folder)[source]
Find the folder containing the specified image.
- Parameters:
image_path (str) – Path to the image.
root_folder (str) – Path to the root folder containing subfolders.
- Returns:
Path to the folder containing the image, or None if not found.
- Return type:
folder_path (str)
- track(n, threshold_iou, image_storage)[source]
Track objects across frames based on IoU thresholding.
- Parameters:
n (int) – Number of frames to process.
threshold_iou (float) – IoU threshold for tracking.
image_storage (segmentation) – Instance of the segmentation class containing images.
Video
- class VideoFrames[source]
Bases:
object
A class to hold video frames and their corresponding visualizations.
- Parameters:
frames (list) – A list to store the video frames.
frames_v (list) – A list to store the visualizations of the video frames.
- create_average_green_image(folder_path, output_path)[source]
Create an average green intensity image from images in a folder.
- Parameters:
folder_path (str) – Path to the folder containing images.
output_path (str) – Path to save the average green intensity image.
- inputconfig(input_folder)[source]
Process a video file and its corresponding green channel video file.
- Parameters:
input_folder (str) – Path to the folder containing the video files.
- Returns:
An object containing the processed video frames.
- Return type:
video_frames (VideoFrames)
- median_green_intensity(input_folder, output_path)[source]
Calculate the median green intensity from images in a folder and save the result.
- Parameters:
input_folder (str) – Path to the folder containing images.
output_path (str) – Path to save the median green intensity image.
- draw_contours(image, contours, number, colors)[source]
Draw contours on the image with a specific color based on the number.
- Parameters:
image (numpy.ndarray) – The image on which to draw the contours.
contours (list) – List of contours to draw.
number (int) – The number used to determine the color.
colors (list) – List of colors to choose from.
- generate_random_rgb_color()[source]
Generate a random RGB color.
- Returns:
A tuple representing a random RGB color.
- Return type:
tuple
Visualisation
- build_global_mask(roi_dict, shape)[source]
Build a global mask from a dictionary of ROIs. The mask will have the same shape as the image, with all ROIs filled with 1s.
- Parameters:
roi_dict (dict) – A dictionary of ROIs, where each key is a ROI name and the value is a dict with ‘x’ and ‘y’ coordinates.
shape (tuple) – The shape of the image (height, width).
- Returns:
A binary mask with the same shape as the image.
- Return type:
mask (numpy.ndarray)
- compute_global_iou(zip1_path, zip2_path, shape)[source]
Compute the Intersection over Union (IoU) between two sets of ROIs.
- Parameters:
zip1_path (str) – Path to the first zip file containing ROIs.
zip2_path (str) – Path to the second zip file containing ROIs.
shape (tuple) – The shape of the image (height, width).
- Returns:
The IoU value between the two sets of ROIs.
- Return type:
iou (float)
- get_folder_shapes(folder_path)[source]
Gets the dimensions (height and width) of all images in a specified folder.
Warning
The folder must only contain image files. If any non-image files are present or an image cannot be read, the function will raise a ValueError.
- Parameters:
folder_path (str) – The path to the folder containing the images.
- Raises:
ValueError – If the folder contains non-image files or if an image cannot be read.
- Returns:
A list of tuples representing the dimensions (height, width) of each image in the folder.
- Return type:
list of tuple
- get_image_shape(image_path)[source]
Gets the dimensions (height and width) of an image. :param image_path: The path to the image file. :type image_path: str
- Returns:
A tuple representing the dimensions (height, width) of the image.
- Return type:
tuple
- mean_global_iou(zip_pred_folder, zip_gt_folder, image_folder)[source]
Compute the mean global Intersection over Union (IoU) between two folders of zip files.
The zip files are expected to contain Regions of Interest (ROIs) in ImageJ format. The function ensures that the number of zip files in the two folders is the same and validates that all files have the .zip extension. It also retrieves image shapes from the specified image folder to compute the IoU.
- Parameters:
zip_pred_folder (str) – Path to the folder containing predicted zip files.
zip_gt_folder (str) – Path to the folder containing ground truth zip files.
image_folder (str) – Path to the folder containing images to retrieve their shapes.
- Raises:
ValueError – If the number of zip files in the two folders is not the same.
ValueError – If any file in the folders does not have a .zip extension.
- Returns:
mean_iou (float) (The mean IoU value across all zip files.)
ious (list) (A list of IoU values for each pair of zip files.)
- roi_to_mask(shape, roi)[source]
Convert a single ROI to a binary mask. The mask will have the same shape as the image, with the ROI filled with 1s.
- Parameters:
shape (tuple) – The shape of the image (height, width).
roi (dict) – The ROI dictionary containing ‘x’ and ‘y’ coordinates.
- Returns:
A binary mask with the same shape as the image.
- Return type:
mask (numpy.ndarray)
- comp_model(model_path, train=False)[source]
Compare the model predictions with the ground truth.
This function takes a test or training dataset and a model path, then compares the model’s predictions with the ground truth data. The comparison results are stored in an output directory.
- Parameters:
model_path (str) – The path to the directory containing the model and dataset.
train (bool, optional) – If True, the training dataset is used for comparison. If False, the test dataset is used. Default is False.
- Raises:
FileNotFoundError – If the specified dataset or model path does not exist.
PermissionError – If there are insufficient permissions to access or modify the output directory.
Note
The function assumes a specific directory structure under
model_path
:dataset/train/train_x
anddataset/train/train_y
for training data.dataset/test/test_x
anddataset/test/test_y
for test data.
The output directory is cleared and recreated for each function call.
See also
comp_model_frame
Function used to compare individual frames.
- comp_model_frame(frame, model_path, roi_frame)[source]
Create an image comparison between the model’s predictions and the ground truth.
This function processes a given frame using a specified model, generates predicted ROIs (Regions of Interest), and visualizes them alongside the ground truth ROIs. It is designed to compare the model’s predictions with the ground truth after the model has been run. Note that if there are multiple models in the ‘models’ folder, the function will process all of them, resulting in multiple red outlines. It is recommended to keep only one model in the ‘models’ folder for accurate comparison.
- Parameters:
frame (str) – Path to the frame image.
model_path (str) – Path to the model folder. The folder must include the ‘models’ and ‘dataset’ subfolders, as well as the ‘dataset.csv’ and ‘META.json’ files.
roi_frame (str) – Path to the ground truth ROI frame. This must be a .zip file containing the ROIs or a .roi file.
- Returns:
The function does not return any value. It generates visualizations and saves
them in the appropriate directories.
Note
The function creates several temporary directories for intermediate outputs, which are cleaned up after processing.
The final outputs, including predicted ROIs, visualizations, and masks, are saved in the ‘test_def’ directory within the ‘model_output’ folder.
Ensure that the input paths are valid and the required files are present in the specified directories.
- create_polygon_roi(points, **kwargs)[source]
Create an ImagejRoi object with the type set to POLYGON.
Parameters:
- pointslist
List of (x, y) coordinates for the polygon.
- kwargsdict
Additional arguments to pass to ImagejRoi.frompoints.
Returns:
- ImagejRoi
An ImagejRoi object with the type set to POLYGON.
- pred_to_rois(pred, min_length)[source]
Convert model predictions into Regions of Interest (ROIs).
This function processes the output of the ‘locate_frame’ function, extracts contours from the masks, and generates ROIs for each contour. Each ROI is represented as a polygon with its coordinates.
- Parameters:
pred (list) – A list of predictions from the model on a frame. Each element in the list is a tuple containing a list of masks and a score. The masks are expected to be binary arrays.
min_length (int) – The minimum length of the contour to be considered a valid ROI. This helps filter out small artefacts or noise.
- Returns:
A dictionary where each key is a unique name for the ROI, and the value is a dictionary containing the following information:
type (str): The type of the ROI (e.g., “polygon”).
x (list of float): The x-coordinates of the ROI’s contour points.
y (list of float): The y-coordinates of the ROI’s contour points.
n (int): The number of points in the contour.
width (int): Placeholder for width (currently set to 0).
name (str): The unique name of the ROI.
position (int): Placeholder for position (currently set to 0).
- Return type:
dict
- save_rois_to_folder(rois, folder_path, frame_name)[source]
Save ROIs for each objects as .roi files in a zip file.
- Parameters:
rois (dict) – Dictionary containing ROIs.
dictionary (Each key is a unique name for the ROI and the value is a)
folder_path (str) – Path to the folder where the ROIs will be saved.
frame_name (str) – Name of the frame. This will be used to name the zip file.
- Returns:
None
- visualize_roi(image_path, roi_path, output_folder, color='red')[source]
Visualize the region of interest (ROI) on an image without white borders.
This function overlays the specified ROI on the given image and saves the resulting visualization to the specified output folder.
- Parameters:
image_path (str) – Path to the image file.
roi_path (str) – Path to the ROI file. Supported formats are .zip and .roi.
output_folder (str) – Path to the folder where the output image will be saved.
color (str) – Outline color for the ROI. Defaults to “red”.
- Raises:
ValueError – If the ROI file format is not supported.
ValueError – If the ROI type is not recognized.
- Returns:
None
Set-up
Structure
- copy_zip_files(input_folder, n)[source]
Copy zip files from the train_y folder to the test_y folder. The number of zip files copied is determined by the parameter n. If there are fewer zip files than n, the last zip file is copied multiple times.
- Parameters:
input_folder (str) – Path to the input folder containing the dataset.
n (int) – Number of zip files to copy.
Convert
- convert_all_avi_in_folder(folder_path)[source]
Converts all AVI files in a folder and its subfolders to MP4 format.
- Parameters:
folder_path (str) – Path to the folder containing AVI files.
Count
Dataset_csv
Empty_zip
- empty_dataset_testy_zip(testy_folder, video_path)[source]
Function that creates empty zip files in the
test_y
folder. The number of zip files created matched the number of frmaes of the video file you provided. :param testy_folder: Path to the testy folder. :type testy_folder: str :param video_path: Path to the video file. :type video_path: str
- empty_dataset_testy_zip_single_frame(testy_folder, frame_path)[source]
Function that creates empty zip file in the
test_y
folder for a unique frame. The zip file created matched the name of the frame you provided.- Parameters:
testy_folder (str) – Path to the testy folder.
frame_path (str) – Path to the frame file.
Explain_model
- class SummaryModel(params, fitness, endpoint, mode, nodes, sequence, functions, model_path)[source]
Bases:
object
This class summarizes the model parameters, fitness, endpoint, mode, nodes, sequence, functions, and model path.
- get_function(sequence, functions)[source]
Extracts the function names from the sequence based on their IDs.
The functions are from OpenCV and are used to create the model using Kartezio (not all OpenCV functions are used). The function ID is the index of the function in the list of functions chosen in the Kartezio package.
- Parameters:
sequence (list) –
The sequence of the model, following the Kartezio architecture:
functions (list) – The list of functions available in the Kartezio package.
- Returns:
The list of function names corresponding to the
function_id
in the sequence.- Return type:
function_names (list)
- summary_model(model_folder_path)[source]
This function loads models from all subdirectories in a specified folder and creates summaries.
- Parameters:
model_folder (str) – Path to the folder containing the models. The function will look for subdirectories within this folder.
- Returns:
A list of SummaryModel objects, each representing a model in the subdirectories.
- Return type:
summaries (list)
Extract_frames
- extract_frames(video_path, output_folder, fps=10)[source]
Extract frames from an MP4 video and save them as PNG images with a fixed resolution. Ensures uniform frame size by resizing and padding if needed.
- Parameters:
video_path – Path to the input MP4 video file.
output_folder – Path to the folder where frames will be saved.
fps – Frames per second to extract.
width – Output frame width.
height – Output frame height.
Train_model
1# Source code for model training
2# ########################################################
3
4from kartezio.apps.segmentation import create_segmentation_model
5from kartezio.endpoint import EndpointThreshold
6from kartezio.dataset import read_dataset
7from kartezio.training import train_model
8
9
10DATASET = "./dataset"
11OUTPUT = "./models"
12
13
14generations = 1000
15_lambda = 5
16frequency = 5
17rate = 0.1
18print(rate)
19model = create_segmentation_model(
20 generations,
21 _lambda,
22 inputs=3,
23 nodes=30,
24 node_mutation_rate=rate,
25 output_mutation_rate=rate,
26 outputs=1,
27 fitness="IOU",
28 endpoint=EndpointThreshold(threshold=4),
29)
30
31dataset = read_dataset(DATASET)
32elite, a = train_model(model, dataset, OUTPUT, callback_frequency=frequency)
Eval_models
1# Source code for model testing
2# ########################################################
3
4import numpy as np
5import pandas as pd
6
7from kartezio.easy import print_stats
8from kartezio.dataset import read_dataset
9from kartezio.fitness import FitnessIOU
10from kartezio.inference import ModelPool
11
12scores_all = {}
13pool = ModelPool(f"./models", FitnessIOU(), regex="*/elite.json").to_ensemble()
14dataset = read_dataset(f"./dataset", counting=True)
15annotations_test = 0
16annotations_training = 0
17roi_pixel_areas = []
18for y_true in dataset.train_y:
19 n_annotations = y_true[1]
20 annotations_training += n_annotations
21for y_true in dataset.test_y:
22 annotations = y_true[0]
23 n_annotations = y_true[1]
24 annotations_test += n_annotations
25 for i in range(1, n_annotations + 1):
26 roi_pixel_areas.append(np.count_nonzero(annotations[annotations == i]))
27print(f"Total annotations for training set: {annotations_training}")
28print(f"Total annotations for test set: {annotations_test}")
29print(f"Mean pixel area for test set: {np.mean(roi_pixel_areas)}")
30
31
32scores_test = []
33scores_training = []
34for i, model in enumerate(pool.models):
35 # Test set
36 _, fitness, _ = model.eval(dataset, subset="test")
37 scores_test.append(1.0 - fitness)
38
39 # Training set
40 _, fitness, _ = model.eval(dataset, subset="train")
41 scores_training.append(1.0 - fitness)
42
43
44scores_all[f"training"] = scores_training
45scores_all[f"test"] = scores_test
46print_stats(scores_training, "AP50", "training set")
47print_stats(scores_test, "AP50", "test set")
48
49pd.DataFrame(scores_all).to_csv("./results/results.csv", index=False)