Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change labels #229

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
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
44 changes: 44 additions & 0 deletions core/management/commands/changelabels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import csv
import os

from django.core.management.base import BaseCommand, CommandError
from core.models import Annotation, ROI, Label
from django.db import connection
from django.db.models import Count

class Command(BaseCommand):
help = 'change labels'

def add_arguments(self, parser):
parser.add_argument('-m','--mapping', type=str, help='Path to csv of mapping of old and new label names')

def handle(self, *args, **options):
# handle arguments
mapping_csv = options['mapping']
# validate arguments
if not mapping_csv:
raise CommandError('Input mapping file not specified')
if not os.path.exists(mapping_csv):
raise CommandError('specified file does not exist')

# Array of label names
labels = []
with open(mapping_csv,'r') as csvin:
reader = csv.reader(csvin)
row = next(reader)

for row in reader:
res = 0
res = Label.objects.filter(name=row[0]).update(name=row[1])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add error handling; in the case that the provided source label does not exist, this should be treated as a failure and reported to the user instead of silently taking no action.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is the reason I am generating an output file. The update() calls returns a 0, not an error when provided source label doesn't exist. And I just thought filtering through rows containing 0 in the UpdateRowCount column is easier to track than going through multiple error logs in the output (Might miss seeing some). This way, we go through all keys, instead of interrupting when one error occurs in the middle. But happy to change if this is an overkill.
Screen Shot 2022-06-09 at 12 39 18 PM

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the above image, none of the labels matched.

if res == 0:
print("Error: Source Label, " + row[0] + " not found!")
continue
# Number of ROIs affected with this particular Label change
i = Annotation.objects.filter(label__name__contains=row[1]).count()
print(row[0] + " -> " + row[1] + ", affected ROIs: " + str(i))
labels.append(row[1])

roi_count = ROI.objects.filter(annotations__label__name__in=labels).count()
print("Number of ROIs affected by all label changes: " + str(roi_count))
print("Done.")