Skip to content
Open
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
f46e88c
feat: add base and runner classes for generic post-processing framework
mohamedelabbas1996 Sep 18, 2025
d86ea4d
feat: add post-processing framework base post-processing task class
mohamedelabbas1996 Sep 30, 2025
2c0f78f
feat: add small size filter post-processing task class
mohamedelabbas1996 Sep 30, 2025
ffba709
feat: add post processing job type
mohamedelabbas1996 Sep 30, 2025
63cd84b
feat: trigger small size filter post processing task from admin page
mohamedelabbas1996 Sep 30, 2025
cab62bf
feat: add a new algorithm task type for post-processing
mohamedelabbas1996 Sep 30, 2025
6d0e284
chore: deleted runner.py
mohamedelabbas1996 Sep 30, 2025
4cfe2d8
feat: add migration for creating a new job type
mohamedelabbas1996 Sep 30, 2025
b42e069
fix: fix an import error with the AlgorithmTaskType
mohamedelabbas1996 Sep 30, 2025
cb7c83a
feat: update identification history of occurrences in SmallSizeFilter
mohamedelabbas1996 Oct 2, 2025
10103db
feat: add rank rollup
mohamedelabbas1996 Oct 6, 2025
2e81d90
feat: add class masking post processing task
mohamedelabbas1996 Oct 7, 2025
0baf8ce
feat: trigger class masking from admin page
mohamedelabbas1996 Oct 7, 2025
f3caa18
fix: modified log messages
mohamedelabbas1996 Oct 8, 2025
65d4fef
fix: set the classification algorithm to the rank rollup Algorithm w…
mohamedelabbas1996 Oct 8, 2025
e13afc1
feat: trigger rank rollup from admin page
mohamedelabbas1996 Oct 8, 2025
7ecc18c
Remove class_masking.py from framework branch
mohamedelabbas1996 Oct 14, 2025
f214025
fix: initialize post-processing tasks with job context and simplify r…
mohamedelabbas1996 Oct 14, 2025
20ff4b6
feat: add permission to run post-processing jobs
mohamedelabbas1996 Oct 14, 2025
5b66ae3
chore: remove class_masking import
mohamedelabbas1996 Oct 14, 2025
0419eff
refactor: redesign BasePostProcessingTask with job-aware logging, pro…
mohamedelabbas1996 Oct 14, 2025
1ad1e76
refactor: adapt RankRollupTask to new BasePostProcessingTask with sel…
mohamedelabbas1996 Oct 14, 2025
d97e8e0
refactor: update SmallSizeFilter to use BasePostProcessingTask loggin…
mohamedelabbas1996 Oct 14, 2025
2922c86
migrations: update Project options to include post-processing job per…
mohamedelabbas1996 Oct 14, 2025
9012d7f
migrations: update Algorithm.task_type choices to include post-proces…
mohamedelabbas1996 Oct 14, 2025
319bb3d
Merge branch 'main' into feat/postprocessing-framework
mohamedelabbas1996 Oct 14, 2025
787ac0b
migrations: merged migrations
mohamedelabbas1996 Oct 14, 2025
5e85b75
refactor: refactor job runner to initialize post-processing tasks wit…
mohamedelabbas1996 Oct 10, 2025
88ffba8
chore: rebase feat/postprocessing-class-masking onto feat/postprocess…
mohamedelabbas1996 Oct 14, 2025
9519600
chore: remove class masking trigger (moved to feat/postprocessing-cla…
mohamedelabbas1996 Oct 14, 2025
21e6648
feat: improved progress tracking
mohamedelabbas1996 Oct 14, 2025
7135e15
Merge branch 'feat/postprocessing-framework' into feat/postprocessing…
mohamedelabbas1996 Oct 14, 2025
6632c31
feat: add applied_to field to Classification to track source classifi…
mohamedelabbas1996 Oct 15, 2025
23f80fb
tests: added tests for small size filter and rank roll up post-proces…
mohamedelabbas1996 Oct 15, 2025
336636a
fix: create only terminal classifications and remove identification c…
mohamedelabbas1996 Oct 15, 2025
0d90cde
refactor: remove inner transaction.atomic for cleaner transaction man…
mohamedelabbas1996 Oct 15, 2025
23469e2
tests: fixed small size filter test
mohamedelabbas1996 Oct 15, 2025
001464e
Merge branch 'feat/postprocessing-framework' into feat/postprocessing…
mohamedelabbas1996 Oct 15, 2025
916d652
Merge branch 'main' of github.com:RolnickLab/antenna into feat/postpr…
mihow Oct 16, 2025
1b8700e
draft: work towards class masking in new framework
mihow Oct 16, 2025
e4639f6
Merge remote-tracking branch 'origin/main' into feat/postprocessing-c…
mihow Feb 18, 2026
a466a52
feat: add class masking tests, management command, and fix registry
mihow Feb 18, 2026
a107597
fix: address review feedback on class masking and rank rollup
mihow Feb 18, 2026
da9b081
feat: replace hardcoded admin action with dynamic class masking form
mihow Feb 18, 2026
fc3f9e1
docs: add class masking screenshots for PR review
mihow Feb 18, 2026
c96a865
fix: address review feedback — N+1 query, distinct, HTML, test ordering
mihow Feb 18, 2026
6be1239
feat: expose applied_to field in Classification API serializers
mihow Feb 18, 2026
c4311aa
feat: make applied_to a nested object with algorithm details
mihow Feb 18, 2026
daed538
fix: add prefetch for applied_to on occurrence detail endpoint
mihow Feb 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions ami/jobs/migrations/0018_alter_job_job_type_key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 4.2.10 on 2025-09-30 12:25

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("jobs", "0017_alter_job_logs_alter_job_progress"),
]

operations = [
migrations.AlterField(
model_name="job",
name="job_type_key",
field=models.CharField(
choices=[
("ml", "ML pipeline"),
("populate_captures_collection", "Populate captures collection"),
("data_storage_sync", "Data storage sync"),
("unknown", "Unknown"),
("data_export", "Data Export"),
("post_processing", "Post Processing"),
],
default="unknown",
max_length=255,
verbose_name="Job Type",
),
),
]
43 changes: 42 additions & 1 deletion ami/jobs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,40 @@ def run(cls, job: "Job"):
job.update_status(JobState.SUCCESS, save=True)


class PostProcessingJob(JobType):
name = "Post Processing"
key = "post_processing"

@classmethod
def run(cls, job: "Job"):
import ami.ml.post_processing # noqa F401
from ami.ml.post_processing.base import get_postprocessing_task

job.progress.add_stage(cls.name, key=cls.key)
job.update_status(JobState.STARTED)
job.started_at = datetime.datetime.now()
job.save()

params = job.params or {}
task_key: str = params.get("task", "")
config = params.get("config", {})
job.logger.info(f"Post-processing task: {task_key} with params: {config}")
# Get the registered task class
task_cls = get_postprocessing_task(task_key)
if not task_cls:
raise ValueError(f"Unknown post-processing task '{task_key}'")

# Instantiate the task with job context and config
task = task_cls(job=job, **config)

# Run the task
task.run()
job.progress.update_stage(cls.key, status=JobState.SUCCESS, progress=1)
job.finished_at = datetime.datetime.now()
job.update_status(JobState.SUCCESS)
job.save()


class UnknownJobType(JobType):
name = "Unknown"
key = "unknown"
Expand All @@ -654,7 +688,14 @@ def run(cls, job: "Job"):
raise ValueError(f"Unknown job type '{job.job_type()}'")


VALID_JOB_TYPES = [MLJob, SourceImageCollectionPopulateJob, DataStorageSyncJob, UnknownJobType, DataExportJob]
VALID_JOB_TYPES = [
MLJob,
SourceImageCollectionPopulateJob,
DataStorageSyncJob,
UnknownJobType,
DataExportJob,
PostProcessingJob,
]


def get_job_type_by_key(key: str) -> type[JobType] | None:
Expand Down
50 changes: 49 additions & 1 deletion ami/main/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import ami.utils
from ami import tasks
from ami.jobs.models import Job
from ami.ml.models.project_pipeline_config import ProjectPipelineConfig
from ami.ml.tasks import remove_duplicate_classifications

Expand Down Expand Up @@ -619,7 +620,54 @@ def populate_collection_async(self, request: HttpRequest, queryset: QuerySet[Sou
f"Populating {len(queued_tasks)} collection(s) background tasks: {queued_tasks}.",
)

actions = [populate_collection, populate_collection_async]
@admin.action(description="Run Small Size Filter post-processing task (async)")
def run_small_size_filter(self, request: HttpRequest, queryset: QuerySet[SourceImageCollection]) -> None:
jobs = []
for collection in queryset:
job = Job.objects.create(
name=f"Post-processing: SmallSizeFilter on Collection {collection.pk}",
project=collection.project,
job_type_key="post_processing",
params={
"task": "small_size_filter",
"config": {
"size_threshold": 0.01, # default threshold
"source_image_collection_id": collection.pk,
},
},
)
job.enqueue()
jobs.append(job.pk)

self.message_user(request, f"Queued Small Size Filter for {queryset.count()} collection(s). Jobs: {jobs}")

@admin.action(description="Run Rank Rollup post-processing task (async)")
def run_rank_rollup(self, request: HttpRequest, queryset: QuerySet[SourceImageCollection]) -> None:
"""Trigger the Rank Rollup post-processing job asynchronously."""
jobs = []
DEFAULT_THRESHOLDS = {"species": 0.8, "genus": 0.6, "family": 0.4}

for collection in queryset:
job = Job.objects.create(
name=f"Post-processing: RankRollup on Collection {collection.pk}",
project=collection.project,
job_type_key="post_processing",
params={
"task": "rank_rollup",
"config": {"source_image_collection_id": collection.pk, "thresholds": DEFAULT_THRESHOLDS},
},
)
job.enqueue()
jobs.append(job.pk)

self.message_user(request, f"Queued Rank Rollup for {queryset.count()} collection(s). Jobs: {jobs}")

actions = [
populate_collection,
populate_collection_async,
run_small_size_filter,
run_rank_rollup,
]

# Hide images many-to-many field from form. This would list all source images in the database.
exclude = ("images",)
Expand Down
59 changes: 59 additions & 0 deletions ami/main/migrations/0075_alter_project_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Generated by Django 4.2.10 on 2025-10-14 05:01

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("main", "0074_taxon_cover_image_credit_taxon_cover_image_url_and_more"),
]

operations = [
migrations.AlterModelOptions(
name="project",
options={
"ordering": ["-priority", "created_at"],
"permissions": [
("create_identification", "Can create identifications"),
("update_identification", "Can update identifications"),
("delete_identification", "Can delete identifications"),
("create_job", "Can create a job"),
("update_job", "Can update a job"),
("run_ml_job", "Can run/retry/cancel ML jobs"),
("run_populate_captures_collection_job", "Can run/retry/cancel Populate Collection jobs"),
("run_data_storage_sync_job", "Can run/retry/cancel Data Storage Sync jobs"),
("run_data_export_job", "Can run/retry/cancel Data Export jobs"),
("run_single_image_ml_job", "Can process a single capture"),
("run_post_processing_job", "Can run/retry/cancel Post-Processing jobs"),
("delete_job", "Can delete a job"),
("create_deployment", "Can create a deployment"),
("delete_deployment", "Can delete a deployment"),
("update_deployment", "Can update a deployment"),
("sync_deployment", "Can sync images to a deployment"),
("create_sourceimagecollection", "Can create a collection"),
("update_sourceimagecollection", "Can update a collection"),
("delete_sourceimagecollection", "Can delete a collection"),
("populate_sourceimagecollection", "Can populate a collection"),
("create_sourceimage", "Can create a source image"),
("update_sourceimage", "Can update a source image"),
("delete_sourceimage", "Can delete a source image"),
("star_sourceimage", "Can star a source image"),
("create_sourceimageupload", "Can create a source image upload"),
("update_sourceimageupload", "Can update a source image upload"),
("delete_sourceimageupload", "Can delete a source image upload"),
("create_s3storagesource", "Can create storage"),
("delete_s3storagesource", "Can delete storage"),
("update_s3storagesource", "Can update storage"),
("test_s3storagesource", "Can test storage connection"),
("create_site", "Can create a site"),
("delete_site", "Can delete a site"),
("update_site", "Can update a site"),
("create_device", "Can create a device"),
("delete_device", "Can delete a device"),
("update_device", "Can update a device"),
("view_private_data", "Can view private data"),
("trigger_exports", "Can trigger data exports"),
],
},
),
]
12 changes: 12 additions & 0 deletions ami/main/migrations/0077_merge_20251014_1426.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Generated by Django 4.2.10 on 2025-10-14 14:26

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("main", "0075_alter_project_options"),
("main", "0076_add_occurrence_composite_indexes"),
]

operations = []
2 changes: 2 additions & 0 deletions ami/main/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ class Permissions:
RUN_POPULATE_CAPTURES_COLLECTION_JOB = "run_populate_captures_collection_job"
RUN_DATA_STORAGE_SYNC_JOB = "run_data_storage_sync_job"
RUN_DATA_EXPORT_JOB = "run_data_export_job"
RUN_POST_PROCESSING_JOB = "run_post_processing_job"
DELETE_JOB = "delete_job"

# Deployment permissions
Expand Down Expand Up @@ -394,6 +395,7 @@ class Meta:
("run_data_storage_sync_job", "Can run/retry/cancel Data Storage Sync jobs"),
("run_data_export_job", "Can run/retry/cancel Data Export jobs"),
("run_single_image_ml_job", "Can process a single capture"),
("run_post_processing_job", "Can run/retry/cancel Post-Processing jobs"),
("delete_job", "Can delete a job"),
# Deployment permissions
("create_deployment", "Can create a deployment"),
Expand Down
42 changes: 42 additions & 0 deletions ami/ml/migrations/0025_alter_algorithm_task_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Generated by Django 4.2.10 on 2025-10-14 05:01

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("ml", "0024_fix_classifications_missing_category_maps"),
]

operations = [
migrations.AlterField(
model_name="algorithm",
name="task_type",
field=models.CharField(
choices=[
("detection", "Detection"),
("localization", "Localization"),
("segmentation", "Segmentation"),
("classification", "Classification"),
("embedding", "Embedding"),
("tracking", "Tracking"),
("tagging", "Tagging"),
("regression", "Regression"),
("captioning", "Captioning"),
("generation", "Generation"),
("translation", "Translation"),
("summarization", "Summarization"),
("question_answering", "Question Answering"),
("depth_estimation", "Depth Estimation"),
("pose_estimation", "Pose Estimation"),
("size_estimation", "Size Estimation"),
("post_processing", "Post Processing"),
("other", "Other"),
("unknown", "Unknown"),
],
default="unknown",
max_length=255,
null=True,
),
),
]
1 change: 1 addition & 0 deletions ami/ml/models/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ class AlgorithmTaskType(str, enum.Enum):
DEPTH_ESTIMATION = "depth_estimation"
POSE_ESTIMATION = "pose_estimation"
SIZE_ESTIMATION = "size_estimation"
POST_PROCESSING = "post_processing"
OTHER = "other"
UNKNOWN = "unknown"

Expand Down
1 change: 1 addition & 0 deletions ami/ml/post_processing/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import class_masking, rank_rollup, small_size_filter # noqa: F401
92 changes: 92 additions & 0 deletions ami/ml/post_processing/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# ami/ml/post_processing/base.py

import abc
import logging
from typing import Any

from ami.jobs.models import Job
from ami.ml.models import Algorithm
from ami.ml.models.algorithm import AlgorithmTaskType

# Registry of available post-processing tasks
POSTPROCESSING_TASKS: dict[str, type["BasePostProcessingTask"]] = {}


def register_postprocessing_task(task_cls: type["BasePostProcessingTask"]):
"""
Decorator to register a post-processing task in the global registry.
Each task must define a unique `key`.
Ensures an Algorithm entry exists for this task.
"""
if not hasattr(task_cls, "key") or not task_cls.key:
raise ValueError(f"Task {task_cls.__name__} missing required 'key' attribute")

# Register the task
POSTPROCESSING_TASKS[task_cls.key] = task_cls
return task_cls


def get_postprocessing_task(name: str) -> type["BasePostProcessingTask"] | None:
"""
Get a task class by its registry key.
Returns None if not found.
"""
return POSTPROCESSING_TASKS.get(name)


class BasePostProcessingTask(abc.ABC):
"""
Abstract base class for all post-processing tasks.
"""

# Each task must override these
key: str = ""
name: str = ""

def __init__(
self,
job: Job | None = None,
logger: logging.Logger | None = None,
**config: Any,
):
self.job = job
self.config = config
# Choose the right logger
if logger is not None:
self.logger = logger
elif job is not None:
self.logger = job.logger
else:
self.logger = logging.getLogger(f"ami.post_processing.{self.key}")

algorithm, _ = Algorithm.objects.get_or_create(
name=self.__class__.__name__,
defaults={
"description": f"Post-processing task: {self.key}",
"task_type": AlgorithmTaskType.POST_PROCESSING.value,
},
)
self.algorithm: Algorithm = algorithm

self.logger.info(f"Initialized {self.__class__.__name__} with config={self.config}, job={job}")

def update_progress(self, progress: float):
"""
Update progress if job is present, otherwise just log.
"""

if self.job:
self.job.progress.update_stage(self.job.job_type_key, progress=progress)
self.job.save(update_fields=["progress"])

else:
# No job object — fallback to plain logging
self.logger.info(f"[{self.name}] Progress {progress:.0%}")

@abc.abstractmethod
def run(self) -> None:
"""
Run the task logic.
Must be implemented by subclasses.
"""
raise NotImplementedError("BasePostProcessingTask subclasses must implement run()")
Loading