Skip to content

Commit

Permalink
Update record view, dose calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-dvorak committed Apr 12, 2024
1 parent 3628b90 commit 722edb6
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 66 deletions.
18 changes: 18 additions & 0 deletions DOSPORTAL/migrations/0035_record_time_internal_start.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.11 on 2024-04-12 14:55

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('DOSPORTAL', '0034_alter_detector_data_alter_record_metadata'),
]

operations = [
migrations.AddField(
model_name='record',
name='time_internal_start',
field=models.FloatField(blank=True, default=0, help_text='System time of record start', null=True, verbose_name='Internal time start'),
),
]
9 changes: 8 additions & 1 deletion DOSPORTAL/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,6 @@ def user_directory_path_data(instance, extension='pk'):
blank=True
)


metadata_file = models.FileField(
verbose_name=_("Metadata file"),
help_text=_("Processed metadata file"),
Expand All @@ -574,6 +573,14 @@ def user_directory_path_data(instance, extension='pk'):
help_text=_("Tick this box if the record is dependent on absolute time. When you need align record to real time.")
)

time_internal_start = models.FloatField(
verbose_name = _("Internal time start"),
help_text=_("System time of record start"),
null=True,
blank=True,
default=0
)

time_start = models.DateTimeField(
verbose_name = _("Measurement beginning time"),
help_text=("When 'time is tracked', you can set start time of the record beginning. "),
Expand Down
7 changes: 0 additions & 7 deletions DOSPORTAL/serializers.py

This file was deleted.

6 changes: 4 additions & 2 deletions DOSPORTAL/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,17 @@ def save_record(sender, instance, created = None, **kwargs):


metadata['log_info'] = {}
metadata['log_info']['internat_time_min'] = df_spectrum['time'].min()
metadata['log_info']['internat_time_max'] = df_spectrum['time'].max()
metadata['log_info']['internal_time_min'] = df_spectrum['time'].min()
metadata['log_info']['internal_time_max'] = df_spectrum['time'].max()
metadata['log_info']['log_duration'] = float(duration)
metadata['log_info']['spectral_count'] = df_spectrum.shape[0]
metadata['log_info']['channels'] = df_spectrum.shape[1] - 1 # remove time column
metadata['log_info']['types'] = data_types

df_spectrum['time'] = df_spectrum['time'] - df_spectrum['time'].min()
instance.record_duration = datetime.timedelta(seconds=float(duration))

instance.time_internat_start = df_spectrum['time'].min()
instance.time_of_interest_start = 0
instance.time_of_interest_end = float(duration)

Expand Down
23 changes: 16 additions & 7 deletions DOSPORTAL/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,23 @@ def process_record_entry(pk):
record = Record.objects.filter(pk=pk)[0]

print(record)
df = pd.read_pickle(record.data_file.path).drop('time', axis=1).astype(float).to_numpy()

start_time = record.time_of_interest_start
end_time = record.time_of_interest_end
duration_hours = (end_time - start_time) / 3600


df = pd.read_pickle(record.data_file.path)
print(df)
df = df[(df['time'] >= start_time) & (df['time'] <= end_time)]
df = df.drop('time', axis=1).astype(float).to_numpy()

calib = record.calib

s = np.linspace(0, df.shape[1]-1, df.shape[1])
s = calib.coef0 + s * calib.coef1

energies_per_exposition = np.matmul(df, s)

dose_rate_per_exposition = ((1e6 * (1.602e-19 * energies_per_exposition)/0.1165e-3)/10) * 3600 # in uGy/h
dose_rate_per_exposition = (((1e6 * (1.602e-19 * np.matmul(df, s))/0.1165e-3)/10) * 3600) # in uGy/h

dose_rate = dose_rate_per_exposition.mean()

Expand All @@ -81,18 +88,20 @@ def process_record_entry(pk):
# si_mass = 0.1165e-3 # kg
# integration = 10 # s

#metadata = json.loads(record.metadata)
metadata = record.metadata

print('METADATA FILE', metadata)
print(type(metadata))

metadata = json.loads(metadata)
if type(metadata) == str:
metadata = json.loads(metadata)

if not 'outputs' in metadata:
metadata["outputs"] = {}

metadata["outputs"]["dose_rate_mean"] = dose_rate
metadata["outputs"]["dose_rate_mean"] = dose_rate_per_exposition.mean()
metadata["outputs"]["dose_rate_std"] = dose_rate_per_exposition.std()
metadata["outputs"]["dose_obtained"] = dose_rate_per_exposition.mean() * duration_hours

#record.metadata = json.dumps(metadata, indent=4)
record.metadata = metadata
Expand Down
31 changes: 14 additions & 17 deletions DOSPORTAL/templates/records/record_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{% load filters %}
{% block content %}


<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script charset="utf-8" src="https://d3js.org/d3.v7.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/echarts@5/dist/echarts.min.js"></script>
Expand Down Expand Up @@ -63,9 +64,9 @@
<tr>
<td class="w-auto">Log file:</td>
<td class="">{{record.log_original_filename}} ({{ record.log_file | filesize_mb }})</td>
{% if record.metadata|length > 1 %}<tr>
{% if record.description|length > 1 %}<tr>
<td class="w-auto">Description:</td>
<td class=""><div class="callout m-0 p-2">{{record.metadata | safe }}</div></td>
<td class=""><div class="callout m-0 p-2">{{record.description_formatted | safe }}</div></td>
</tr> {% endif %}
</tr>

Expand All @@ -87,13 +88,20 @@
<div class="card-header">
Outputs:
</div>
<div class="card-body">
<div class="row m-0">
<div class="card-body p-2">
<div class="row m-0 p-0">
{% if "dose_rate_mean" in outputs %}
<div class="card p-2" style="width: initial; text-align: center;" title="Mean dose rate from whole record">
<div class="card p-2 m-1" style="width: initial; text-align: center;" title="Mean dose rate from selected part of record">
<div class="fw-light">Dose rate</div>
<div class="fw-bold text-muted">{{outputs.dose_rate_mean|floatformat:2 }}</div>
<div class="fw-light">uS/h</div>
<div class="fw-light"> &mu;S/h</div>
</div>
{% endif %}
{% if "dose_obtained" in outputs %}
<div class="card p-2 m-1" style="width: initial; text-align: center;" title="Dose absorbed in silicone during the selected part of record.">
<div class="fw-light">Dose absorbed</div>
<div class="fw-bold text-muted">{{outputs.dose_obtained|floatformat:2 }}</div>
<div class="fw-light">&mu;S</div>
</div>
{% endif %}
</div>
Expand Down Expand Up @@ -134,17 +142,6 @@ <h3>Telemetry data:</h3>

</div>


<!--
<div class="card my-4">
<div class="card-header">
<h3>Energetic spectrogram:</h3>
</div>
<div class="card-body p-1">
<div id="graph2" style="width:100%;height:1000px;"></div>
</div>
</div> -->

</div>

<script>
Expand Down
44 changes: 17 additions & 27 deletions DOSPORTAL/views_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,27 +151,6 @@ def RecordNewView(request):
new_file_name = data.pk
new_file_path = os.path.join(settings.MEDIA_ROOT, 'user_records', str(data.pk) )

# Update the file name and path
#data.log_file.name = new_file_name
#data.log_file.path = new_file_path

# handle_uploaded_file(request.FILES['log_file'], new_file_path)
# try:
# metadata = obtain_parameters_from_log(new_file_path)
# except:
# metadata = None

# #handle_uploaded_file(request.FILES['log_file'], data.log_file.path)
# #metadata = obtain_parameters_from_log(data.log_file.path)
# print("Medatada z logu")
# print(metadata)
# if metadata:
# detector_pk = Detector.objects.get(sn=metadata['detector']['detector_sn'])
# if detector_pk:
# data.detector = detector_pk
# data.metadata = metadata
# data.record_duration = timedelta(seconds = metadata['record']['duration'])
# #data.measurement = measurement.objects.get(pk=pk)
pk = data.pk
data.author = request.user

Expand All @@ -194,7 +173,10 @@ def RecordView(request, pk):

#outputs = json.loads(rec.metadata).get('outputs', {})
print("metadata", type(rec.metadata))
outputs = rec.metadata.get('outputs', {})
if type(rec.metadata) == str:
outputs = eval(rec.metadata).get('outputs', {})
else:
outputs = rec.metadata.get('outputs', {})
return render(request, 'records/record_detail.html', context={'record': rec, 'outputs': outputs})


Expand Down Expand Up @@ -254,13 +236,17 @@ def GetEvolution(request, pk):


if not (minTime != 'nan' and maxTime != 'nan'):
minTime = (float(minTime)/1000-start_time)
maxTime = (float(maxTime)/1000-start_time)
minTime = (float(minTime)-start_time)
maxTime = (float(maxTime)-start_time)
df = df[(df['time'] >= minTime) & (df['time'] <= maxTime)]

total_time = df['time'].max()-df['time'].min()

time = df['time'].astype(float).mul(1000).add(start_time)
if record[0].time_tracked:
time = df['time'].astype(float).mul(1000).add(start_time)
else:
time = df['time'].astype(float)

sums = df.drop('time', axis=1).sum(axis=1).div(total_time)

if logarithm:
Expand All @@ -274,8 +260,12 @@ def GetEvolution(request, pk):
time_of_interest = None
if record[0].time_of_interest_start and record[0].time_of_interest_end:
time_of_interest = []
time_of_interest.append(record[0].time_of_interest_start*1000 + start_time)
time_of_interest.append(record[0].time_of_interest_end*1000 + start_time)
time_of_interest.append(record[0].time_of_interest_start)
time_of_interest.append(record[0].time_of_interest_end)

if record[0].time_tracked:
time_of_interest[0] += start_time
time_of_interest[1] += start_time

return JsonResponse({'evolution_values': data_list, 'time_tracked': record[0].time_tracked, 'time_of_interest': time_of_interest})

Expand Down
2 changes: 1 addition & 1 deletion api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class RecordSerializer(serializers.ModelSerializer):
#detector = DetectorSerializer(read_only = True, many=True)
class Meta:
model = Record
#fields = ['id', 'name', 'description', ]
fields = '__all__'


class MeasurementsSerializer(serializers.ModelSerializer):

records = RecordSerializer(read_only = True, many=True)
Expand Down
3 changes: 2 additions & 1 deletion api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@

urlpatterns = [
path('measurement/', views.MeasurementsGet),
path('measurement/add/', views.MeasurementsPost)
path('measurement/add/', views.MeasurementsPost),
path('record/', views.RecordGet),
]
13 changes: 10 additions & 3 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from rest_framework.permissions import AllowAny
from rest_framework.decorators import api_view, permission_classes

from DOSPORTAL.models import measurement
from .serializers import MeasurementsSerializer
from DOSPORTAL.models import measurement, Record
from .serializers import MeasurementsSerializer, RecordSerializer

@api_view(['GET'])
@permission_classes((AllowAny, ))
Expand All @@ -18,4 +18,11 @@ def MeasurementsPost(request):
serializer = MeasurementsSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.data)

@api_view(['GET'])
@permission_classes((AllowAny, ))
def RecordGet(request):
items = Record.objects.all()
serializer = RecordSerializer(items, many=True)
return Response(serializer.data)

0 comments on commit 722edb6

Please sign in to comment.