-
Notifications
You must be signed in to change notification settings - Fork 0
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
shravani-whoi
wants to merge
4
commits into
main
Choose a base branch
from
changeLabels
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Change labels #229
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
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.") | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.