feat: Add DICOM tag consistency analysis with dynamic loading in series viewer
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -0,0 +1,107 @@
|
|||||||
|
{% load django_htmx %}
|
||||||
|
<div id="series-tag-consistency-panel" class="d-flex flex-column gap-2">
|
||||||
|
<div class="d-flex flex-wrap gap-2 align-items-center">
|
||||||
|
<button type="button"
|
||||||
|
class="btn btn-outline-info btn-sm"
|
||||||
|
hx-get="{% url 'atlas:series_tag_consistency' pk=series.pk %}?analyze=1"
|
||||||
|
hx-target="#series-tag-consistency-panel"
|
||||||
|
hx-swap="outerHTML">
|
||||||
|
Analyze tag consistency
|
||||||
|
</button>
|
||||||
|
{% if analyze_requested %}
|
||||||
|
<span class="small text-muted">Scanned {{ image_count }} image{{ image_count|pluralize }}</span>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{% if analyze_requested %}
|
||||||
|
<div class="small d-flex flex-wrap gap-2">
|
||||||
|
<span class="badge text-bg-success">Same: {{ same_tags|length }}</span>
|
||||||
|
<span class="badge text-bg-warning">Different: {{ different_tags|length }}</span>
|
||||||
|
<span class="badge text-bg-secondary">Missing in some: {{ partial_tags|length }}</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<details class="styled-detail" open>
|
||||||
|
<summary>Different tags (highlighted)</summary>
|
||||||
|
<div class="table-responsive mt-2">
|
||||||
|
<table class="table table-sm table-striped align-middle mb-0">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Tag</th>
|
||||||
|
<th>Unique values</th>
|
||||||
|
<th>Sample values</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for row in different_tags %}
|
||||||
|
<tr class="table-warning">
|
||||||
|
<td>{{ row.tag_name }}</td>
|
||||||
|
<td>{{ row.unique_count }}</td>
|
||||||
|
<td>{{ row.sample_values|join:", " }}</td>
|
||||||
|
</tr>
|
||||||
|
{% empty %}
|
||||||
|
<tr>
|
||||||
|
<td colspan="3" class="text-muted">No tags vary across all images.</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<details class="styled-detail">
|
||||||
|
<summary>Same tags</summary>
|
||||||
|
<div class="table-responsive mt-2">
|
||||||
|
<table class="table table-sm table-striped align-middle mb-0">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Tag</th>
|
||||||
|
<th>Value</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for row in same_tags %}
|
||||||
|
<tr class="table-success">
|
||||||
|
<td>{{ row.tag_name }}</td>
|
||||||
|
<td>{{ row.sample_values.0 }}</td>
|
||||||
|
</tr>
|
||||||
|
{% empty %}
|
||||||
|
<tr>
|
||||||
|
<td colspan="2" class="text-muted">No uniform tags found across all images.</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<details class="styled-detail">
|
||||||
|
<summary>Missing in some images</summary>
|
||||||
|
<div class="table-responsive mt-2">
|
||||||
|
<table class="table table-sm table-striped align-middle mb-0">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Tag</th>
|
||||||
|
<th>Present in</th>
|
||||||
|
<th>Unique values</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for row in partial_tags %}
|
||||||
|
<tr class="table-secondary">
|
||||||
|
<td>{{ row.tag_name }}</td>
|
||||||
|
<td>{{ row.present_count }}/{{ image_count }}</td>
|
||||||
|
<td>{{ row.unique_count }}</td>
|
||||||
|
</tr>
|
||||||
|
{% empty %}
|
||||||
|
<tr>
|
||||||
|
<td colspan="3" class="text-muted">All scanned tags were present in every image.</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</details>
|
||||||
|
{% else %}
|
||||||
|
<p class="small text-muted mb-0">Run analysis to quickly highlight tags that are the same or different across the series.</p>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
@@ -195,6 +195,11 @@
|
|||||||
hx-swap="outerHTML">
|
hx-swap="outerHTML">
|
||||||
<span class="text-muted small">Loading split controls…</span>
|
<span class="text-muted small">Loading split controls…</span>
|
||||||
</div>
|
</div>
|
||||||
|
<div hx-get="{% url 'atlas:series_tag_consistency' pk=series.pk %}"
|
||||||
|
hx-trigger="load"
|
||||||
|
hx-swap="outerHTML">
|
||||||
|
<span class="text-muted small">Loading tag analysis controls...</span>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -619,6 +619,11 @@ urlpatterns = [
|
|||||||
views.series_split_by_tag,
|
views.series_split_by_tag,
|
||||||
name="series_split_by_tag",
|
name="series_split_by_tag",
|
||||||
),
|
),
|
||||||
|
path(
|
||||||
|
"series/<int:pk>/tag_consistency",
|
||||||
|
views.series_tag_consistency,
|
||||||
|
name="series_tag_consistency",
|
||||||
|
),
|
||||||
path(
|
path(
|
||||||
"series/<int:pk>/delete",
|
"series/<int:pk>/delete",
|
||||||
views.SeriesDelete.as_view(),
|
views.SeriesDelete.as_view(),
|
||||||
|
|||||||
@@ -3644,6 +3644,19 @@ def _normalise_dicom_split_value(value):
|
|||||||
return str(value)
|
return str(value)
|
||||||
|
|
||||||
|
|
||||||
|
def _summarise_dicom_value(value):
|
||||||
|
if isinstance(value, pydicom.multival.MultiValue):
|
||||||
|
value_text = ",".join(map(str, value))
|
||||||
|
elif isinstance(value, (bytes, bytearray)):
|
||||||
|
value_text = f"<binary:{len(value)} bytes>"
|
||||||
|
else:
|
||||||
|
value_text = str(value)
|
||||||
|
|
||||||
|
if len(value_text) > 120:
|
||||||
|
return f"{value_text[:117]}..."
|
||||||
|
return value_text
|
||||||
|
|
||||||
|
|
||||||
def _analyse_series_split_options(series):
|
def _analyse_series_split_options(series):
|
||||||
tags = [tag_value for tag_value, _ in SERIES_SPLIT_TAGS]
|
tags = [tag_value for tag_value, _ in SERIES_SPLIT_TAGS]
|
||||||
tag_value_sets = {tag: set() for tag in tags}
|
tag_value_sets = {tag: set() for tag in tags}
|
||||||
@@ -3675,6 +3688,81 @@ def _analyse_series_split_options(series):
|
|||||||
return split_options
|
return split_options
|
||||||
|
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
@user_is_author_or_atlas_series_checker_or_atlas_marker_or_open_access
|
||||||
|
def series_tag_consistency(request, pk):
|
||||||
|
"""Analyze DICOM tags across all images in a series.
|
||||||
|
|
||||||
|
Scope: Any user with view access to the series.
|
||||||
|
|
||||||
|
Functionality:
|
||||||
|
- GET: returns a panel with an optional on-demand analysis.
|
||||||
|
- GET ?analyze=1: scans all tags across images and groups them by:
|
||||||
|
1) same in all images, 2) different across images, 3) missing in some images.
|
||||||
|
"""
|
||||||
|
series = get_object_or_404(Series, pk=pk)
|
||||||
|
analyze_requested = request.GET.get("analyze") == "1"
|
||||||
|
|
||||||
|
same_tags = []
|
||||||
|
different_tags = []
|
||||||
|
partial_tags = []
|
||||||
|
image_count = 0
|
||||||
|
|
||||||
|
if analyze_requested:
|
||||||
|
tag_values = defaultdict(set)
|
||||||
|
tag_presence = defaultdict(int)
|
||||||
|
|
||||||
|
for image in series.images.iterator():
|
||||||
|
image_count += 1
|
||||||
|
ds = image.get_dicom_data()
|
||||||
|
seen_in_image = set()
|
||||||
|
|
||||||
|
for element in ds.iterall():
|
||||||
|
# PixelData is large and not useful for consistency checks.
|
||||||
|
if element.tag == (0x7FE0, 0x0010):
|
||||||
|
continue
|
||||||
|
|
||||||
|
tag_name = element.keyword or str(element.tag)
|
||||||
|
tag_values[tag_name].add(_summarise_dicom_value(element.value))
|
||||||
|
|
||||||
|
if tag_name not in seen_in_image:
|
||||||
|
tag_presence[tag_name] += 1
|
||||||
|
seen_in_image.add(tag_name)
|
||||||
|
|
||||||
|
for tag_name in sorted(tag_values.keys()):
|
||||||
|
present_count = tag_presence[tag_name]
|
||||||
|
unique_values = tag_values[tag_name]
|
||||||
|
unique_count = len(unique_values)
|
||||||
|
sample_values = sorted(unique_values)[:3]
|
||||||
|
|
||||||
|
row = {
|
||||||
|
"tag_name": tag_name,
|
||||||
|
"present_count": present_count,
|
||||||
|
"unique_count": unique_count,
|
||||||
|
"sample_values": sample_values,
|
||||||
|
}
|
||||||
|
|
||||||
|
if present_count == image_count and unique_count == 1:
|
||||||
|
same_tags.append(row)
|
||||||
|
elif present_count == image_count:
|
||||||
|
different_tags.append(row)
|
||||||
|
else:
|
||||||
|
partial_tags.append(row)
|
||||||
|
|
||||||
|
return render(
|
||||||
|
request,
|
||||||
|
"atlas/partials/_series_tag_consistency.html",
|
||||||
|
{
|
||||||
|
"series": series,
|
||||||
|
"analyze_requested": analyze_requested,
|
||||||
|
"image_count": image_count,
|
||||||
|
"same_tags": same_tags,
|
||||||
|
"different_tags": different_tags,
|
||||||
|
"partial_tags": partial_tags,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
@user_is_author_or_atlas_series_checker
|
@user_is_author_or_atlas_series_checker
|
||||||
def series_split_by_tag(request, pk):
|
def series_split_by_tag(request, pk):
|
||||||
|
|||||||
Reference in New Issue
Block a user