Skip to content

Commit a362fe4

Browse files
committed
fix: add arguments and fix export commands made for SEC-SEQ Symposium.
1 parent 2c8249b commit a362fe4

File tree

6 files changed

+106
-26
lines changed

6 files changed

+106
-26
lines changed

ami/exports/base.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import csv
22
import logging
3+
import typing
34

45
from django.core.files.storage import default_storage
56
from django.db import models
@@ -28,7 +29,11 @@ class BaseExportView(APIView):
2829
pass
2930

3031

31-
def get_data_in_batches(QuerySet: models.QuerySet, Serializer: type[serializers.Serializer], batch_size=1000):
32+
def get_data_in_batches(
33+
QuerySet: models.QuerySet,
34+
Serializer: type[serializers.Serializer],
35+
batch_size: int = 1000,
36+
) -> typing.Iterator[list[dict]]:
3237
items = QuerySet.iterator(chunk_size=batch_size)
3338
batch = []
3439
for i, item in enumerate(items):
@@ -55,7 +60,11 @@ def get_data_in_batches(QuerySet: models.QuerySet, Serializer: type[serializers.
5560
yield batch
5661

5762

58-
def write_export(report_name, Serializer: type[serializers.Serializer], QuerySet: models.QuerySet):
63+
def write_export(
64+
report_name: str,
65+
Serializer: type[serializers.Serializer],
66+
QuerySet: models.QuerySet,
67+
) -> str:
5968
timestamp = timezone.now().strftime("%Y%m%d-%H%M%S")
6069
file_name = f"{slugify(report_name)}-{timestamp}.csv"
6170
file_path = file_name

ami/exports/by_capture.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,15 @@ class DetectionsByDeterminationAndCaptureTabularSerializer(serializers.Serialize
4040

4141
def to_representation(self, instance: typing.Any) -> dict[str, typing.Any]:
4242
data = super().to_representation(instance)
43-
taxon: Taxon = Taxon.objects.get(id=data["taxon_id"])
44-
45-
for taxon_rank in taxon.parents_json:
46-
field_name = f"taxon_{taxon_rank.rank.name.lower()}"
47-
data[field_name] = taxon_rank.name
43+
try:
44+
taxon: Taxon = Taxon.objects.get(id=data["taxon_id"])
45+
except Taxon.DoesNotExist:
46+
logger.warning(f"Taxon with ID '{data['taxon_id']}' not found")
47+
pass
48+
else:
49+
for taxon_rank in taxon.parents_json:
50+
field_name = f"taxon_{taxon_rank.rank.name.lower()}"
51+
data[field_name] = taxon_rank.name
4852

4953
return data
5054

ami/exports/management/commands/export_by_capture.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
"""
55

66
import logging
7+
import typing
78

89
from django.core.management.base import BaseCommand
10+
from django.db import models
911

1012
from ami.exports import by_capture
1113
from ami.exports.base import write_export
@@ -16,18 +18,34 @@
1618
class Command(BaseCommand):
1719
help = "Export data by capture"
1820

19-
def handle(self, *args, **options):
20-
# for i, batch in enumerate(by_capture.get_data_in_batches())
21-
# # print(f"Processing batch {batch}")
22-
# print(f"Processing batch {i}")
21+
def add_arguments(self, parser) -> None:
22+
parser.add_argument(
23+
"--project-id",
24+
type=int,
25+
required=True,
26+
help="Project ID to export data from",
27+
)
28+
parser.add_argument(
29+
"--collection-ids",
30+
type=int,
31+
nargs="+",
32+
required=False,
33+
default=[],
34+
help="Collection IDs to export data from (space-separated list)",
35+
)
36+
37+
def handle(self, *args, **options) -> None:
38+
project_id: int = options["project_id"]
39+
collection_ids: list[int] = options["collection_ids"]
40+
41+
qs = by_capture.get_queryset().filter(occurrence__project=project_id)
42+
if collection_ids:
43+
qs = qs.filter(source_image__collections__in=collection_ids)
2344

2445
fname = write_export(
2546
"detections_by_determination_and_capture",
2647
Serializer=by_capture.DetectionsByDeterminationAndCaptureTabularSerializer,
27-
QuerySet=by_capture.get_queryset()
28-
.filter(occurrence__project=85)
29-
.filter(source_image__collections__in=[82, 79]),
30-
# .filter(source_image__collections__in=[82]),
48+
QuerySet=typing.cast(models.QuerySet, qs),
3149
)
3250
# get full path to the file
3351
print(f"Exported to {fname}")

ami/exports/management/commands/export_by_detection.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,42 @@
1414

1515

1616
class Command(BaseCommand):
17-
help = "Export data by capture"
17+
help = "Export data by detection and determination"
18+
19+
def add_arguments(self, parser) -> None:
20+
parser.add_argument(
21+
"--project-id",
22+
type=int,
23+
required=True,
24+
help="Project ID to export data from",
25+
)
26+
parser.add_argument(
27+
"--collection-ids",
28+
type=int,
29+
nargs="+",
30+
required=False,
31+
default=[],
32+
help="Collection IDs to export data from (space-separated list)",
33+
)
1834

1935
def handle(self, *args, **options):
2036
# for i, batch in enumerate(by_capture.get_data_in_batches())
2137
# # print(f"Processing batch {batch}")
2238
# print(f"Processing batch {i}")
39+
project_id: int = options["project_id"]
40+
collection_ids: list[int] = options["collection_ids"]
41+
42+
qs = by_detection.get_queryset().filter(occurrence__project=project_id)
43+
if collection_ids:
44+
qs = qs.filter(source_image__collections__in=collection_ids)
2345

2446
fname = write_export(
2547
"detections",
2648
Serializer=by_detection.DetectionsTabularSerializer,
27-
QuerySet=by_detection.get_queryset()
28-
.filter(occurrence__project=85)
29-
.filter(source_image__collections__in=[82, 79]),
30-
# .filter(source_image__collections__in=[82]),
49+
QuerySet=qs,
3150
)
3251
# get full path to the file
3352
print(f"Exported to {fname}")
3453

35-
logger.info("Export by capture completed")
36-
self.stdout.write(self.style.SUCCESS("Export by capture completed"))
54+
logger.info("Export by detection completed")
55+
self.stdout.write(self.style.SUCCESS("Export by detection completed"))

ami/exports/management/commands/export_captures.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,37 @@
1616
class Command(BaseCommand):
1717
help = "Export data by capture"
1818

19+
def add_arguments(self, parser) -> None:
20+
parser.add_argument(
21+
"--project-id",
22+
type=int,
23+
required=True,
24+
help="Project ID to export data from",
25+
)
26+
parser.add_argument(
27+
"--collection-ids",
28+
type=int,
29+
nargs="+",
30+
required=False,
31+
default=[],
32+
help="Collection IDs to export data from (space-separated list)",
33+
)
34+
1935
def handle(self, *args, **options):
2036
# for i, batch in enumerate(by_capture.get_data_in_batches())
2137
# # print(f"Processing batch {batch}")
2238
# print(f"Processing batch {i}")
39+
project_id: int = options["project_id"]
40+
collection_ids: list[int] = options["collection_ids"]
41+
42+
qs = all_captures.get_queryset().filter(project=project_id)
43+
if collection_ids:
44+
qs = qs.filter(collections__in=collection_ids)
2345

2446
fname = write_export(
2547
"captures",
2648
Serializer=all_captures.CapturesTabularSerializer,
27-
QuerySet=all_captures.get_queryset().filter(project=85).filter(collections__in=[82, 79]),
28-
# .filter(collections__in=[82]),
49+
QuerySet=qs,
2950
)
3051
# get full path to the file
3152
print(f"Exported to {fname}")

ami/exports/management/commands/export_sessions.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import logging
77

8-
from django.core.management.base import BaseCommand
8+
from django.core.management.base import BaseCommand, CommandParser
99

1010
from ami.exports import all_sessions
1111
from ami.exports.base import write_export
@@ -16,15 +16,24 @@
1616
class Command(BaseCommand):
1717
help = "Export data by capture"
1818

19+
def add_arguments(self, parser: CommandParser) -> None:
20+
parser.add_argument(
21+
"--project-id",
22+
type=int,
23+
required=True,
24+
help="Project ID to export data from",
25+
)
26+
1927
def handle(self, *args, **options):
2028
# for i, batch in enumerate(by_capture.get_data_in_batches())
2129
# # print(f"Processing batch {batch}")
2230
# print(f"Processing batch {i}")
31+
project_id: int = options["project_id"]
2332

2433
fname = write_export(
2534
"sessions",
2635
Serializer=all_sessions.SessionsTabularSerializer,
27-
QuerySet=all_sessions.get_queryset().filter(project=85),
36+
QuerySet=all_sessions.get_queryset().filter(project=project_id),
2837
)
2938
# get full path to the file
3039
print(f"Exported to {fname}")

0 commit comments

Comments
 (0)