-
Notifications
You must be signed in to change notification settings - Fork 0
Cleanup Scanning Page #70
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
98293b8
replace selectors for record type / modality with image type
aspiringLich 8d15530
removed unused fields in scan state: aiImageTypeCode, aiPatientOrient…
aspiringLich 746dda8
review pass
aspiringLich 5f8e7b5
Merge branch 'develop' into 64-missing-frontal-pa-type
zgypa 363cdb4
Initial implementation of FHIR CWRU valueset, importing from external…
zgypa 2fb9966
feat: implement FHIR ValueSet import functionality and update initial…
zgypa 201db35
fix: update import command to load all valuesets and enhance document…
zgypa 0e0d14d
fix: update BFD9020_BASE_URL to use localhost and enhance docker-comp…
zgypa 1b36382
fix: update modality column to display code with tooltip for full term
zgypa 09c5f1b
Fix: robust valueset import error handling, FHIR UTF-8 decoding, safe…
zgypa fe8e922
fix: repair broken if/else block in handleAIFill causing SyntaxError
zgypa 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 hidden or 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
34 changes: 34 additions & 0 deletions
34
bfd9000_web/archive/management/commands/import_valuesets.py
This file contains hidden or 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,34 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
| from django.core.management.base import BaseCommand, CommandError | ||
| from archive.management.importers.valuesets import import_valueset | ||
| from archive.constants import VALUESET_EXPAND_URLS | ||
|
|
||
| class Command(BaseCommand): | ||
| help = "Import FHIR ValueSet expansions. Use --all or provide --slug and --expand-url." | ||
|
|
||
| def add_arguments(self, parser) -> None: | ||
| parser.add_argument('--slug', type=str, help='Internal ValueSet slug') | ||
| parser.add_argument('--expand-url', type=str, help='FHIR $expand URL') | ||
| parser.add_argument('--all', action='store_true', help='Import all valuesets from constants mapping') | ||
|
|
||
| def handle(self, *args: Any, **options: Any) -> None: | ||
| if options.get('all'): | ||
| if not VALUESET_EXPAND_URLS: | ||
| raise CommandError('No valuesets configured in VALUESET_EXPAND_URLS.') | ||
| for slug, expand_url in VALUESET_EXPAND_URLS.items(): | ||
| count = import_valueset(expand_url=expand_url, slug=slug) | ||
| self.stdout.write(self.style.SUCCESS( | ||
| f"Imported {count} codings into ValueSet '{slug}'." | ||
| )) | ||
| return | ||
|
|
||
| slug = options.get('slug') | ||
| expand_url = options.get('expand_url') | ||
| if not slug or not expand_url: | ||
| raise CommandError('Use --all or provide both --slug and --expand-url.') | ||
| count = import_valueset(expand_url=expand_url, slug=slug) | ||
| self.stdout.write(self.style.SUCCESS( | ||
| f"Imported {count} codings into ValueSet '{slug}'." | ||
| )) |
This file contains hidden or 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
This file contains hidden or 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,110 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from typing import Any, Dict, List | ||
| from urllib.request import urlopen | ||
|
|
||
| from archive.models import Coding, ValueSet, ValueSetConcept | ||
|
|
||
| def import_valueset(expand_url: str, slug: str) -> int: | ||
| """ | ||
| Import a FHIR ValueSet via $expand, upsert ValueSet and Coding rows, | ||
| and sync ValueSetConcept join links. Returns count of codings. | ||
| """ | ||
| payload = _fetch_valueset(expand_url) | ||
| valueset = _upsert_valueset(payload, slug) | ||
| codings = _upsert_codings(valueset, payload) | ||
| _sync_valueset_links(valueset, codings) | ||
| return len(codings) | ||
|
|
||
| def _fetch_valueset(url: str) -> Dict[str, Any]: | ||
| with urlopen(url) as response: | ||
| raw = response.read().decode("utf-8") | ||
| data: Dict[str, Any] = json.loads(raw) | ||
| return data | ||
|
|
||
| def _upsert_valueset(payload: Dict[str, Any], slug: str) -> ValueSet: | ||
| compose = payload.get("compose") or {} | ||
| include = list(compose.get("include") or []) | ||
| code_system_url = None | ||
| if include: | ||
| code_system_url = include[0].get("system") | ||
| expansion = payload.get("expansion") or {} | ||
| contains = list(expansion.get("contains") or []) | ||
| if contains and not code_system_url: | ||
| code_system_url = contains[0].get("system") | ||
|
|
||
| valueset, created = ValueSet.objects.get_or_create( | ||
| slug=slug, | ||
| defaults={ | ||
| "url": payload.get("url", ""), | ||
| "name": payload.get("name", slug), | ||
| "title": payload.get("title", ""), | ||
| "description": payload.get("description", ""), | ||
| "version": payload.get("version", ""), | ||
| "status": payload.get("status", ""), | ||
| "publisher": payload.get("publisher", ""), | ||
| "code_system_url": code_system_url or "", | ||
| }, | ||
| ) | ||
|
|
||
| if not created: | ||
| updates: Dict[str, str] = { | ||
| "url": payload.get("url", ""), | ||
| "name": payload.get("name", slug), | ||
| "title": payload.get("title", ""), | ||
| "description": payload.get("description", ""), | ||
| "version": payload.get("version", ""), | ||
| "status": payload.get("status", ""), | ||
| "publisher": payload.get("publisher", ""), | ||
| "code_system_url": code_system_url or "", | ||
| } | ||
| changed_fields: List[str] = [] | ||
| for field, value in updates.items(): | ||
| if getattr(valueset, field) != value: | ||
| setattr(valueset, field, value) | ||
| changed_fields.append(field) | ||
| if changed_fields: | ||
| valueset.save(update_fields=changed_fields) | ||
|
|
||
| return valueset | ||
|
|
||
| def _upsert_codings(valueset: ValueSet, payload: Dict[str, Any]) -> List[Coding]: | ||
| expansion = payload.get("expansion") or {} | ||
| contains = expansion.get("contains") or [] | ||
| codings: List[Coding] = [] | ||
|
|
||
| for concept in contains: | ||
| system = str(concept.get("system") or "").strip() | ||
| code = str(concept.get("code") or "").strip() | ||
| display = str(concept.get("display") or "").strip() | ||
| definition = str(concept.get("definition") or "").strip() | ||
|
|
||
| if not system or not code: | ||
| continue | ||
|
|
||
| coding, _ = Coding.objects.get_or_create( | ||
| system=system, | ||
| version="", | ||
| code=code, | ||
| defaults={"display": display, "meaning": definition}, | ||
| ) | ||
| updates: List[str] = [] | ||
| if display and coding.display != display: | ||
| coding.display = display | ||
| updates.append("display") | ||
| if definition and coding.meaning != definition: | ||
| coding.meaning = definition | ||
| updates.append("meaning") | ||
| if updates: | ||
| coding.save(update_fields=updates) | ||
| codings.append(coding) | ||
|
|
||
| return codings | ||
|
|
||
| def _sync_valueset_links(valueset: ValueSet, codings: List[Coding]) -> None: | ||
| for coding in codings: | ||
| ValueSetConcept.objects.get_or_create(valueset=valueset, coding=coding) | ||
|
|
||
| coding_ids = [coding.id for coding in codings] | ||
| ValueSetConcept.objects.filter(valueset=valueset).exclude(coding_id__in=coding_ids).delete() |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.