endoreg-db 0.6.2__py3-none-any.whl → 0.6.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of endoreg-db might be problematic. Click here for more details.

Files changed (97) hide show
  1. endoreg_db/data/__init__.py +14 -0
  2. endoreg_db/data/disease_classification/chronic_kidney_disease.yaml +2 -2
  3. endoreg_db/data/disease_classification_choice/chronic_kidney_disease.yaml +6 -6
  4. endoreg_db/data/distribution/numeric/data.yaml +1 -1
  5. endoreg_db/data/examination/examinations/data.yaml +22 -21
  6. endoreg_db/data/examination/type/data.yaml +12 -0
  7. endoreg_db/data/examination_indication/endoscopy.yaml +417 -1
  8. endoreg_db/data/examination_indication_classification/endoscopy.yaml +157 -5
  9. endoreg_db/data/finding/data.yaml +18 -11
  10. endoreg_db/data/finding_intervention/endoscopy.yaml +26 -121
  11. endoreg_db/data/finding_intervention/endoscopy_colonoscopy.yaml +163 -0
  12. endoreg_db/data/finding_intervention/endoscopy_egd.yaml +128 -0
  13. endoreg_db/data/finding_intervention/endoscopy_ercp.yaml +32 -0
  14. endoreg_db/data/finding_intervention/endoscopy_eus_lower.yaml +9 -0
  15. endoreg_db/data/finding_intervention/endoscopy_eus_upper.yaml +36 -0
  16. endoreg_db/data/information_source/endoscopy_guidelines.yaml +7 -0
  17. endoreg_db/data/medication_indication/anticoagulation.yaml +4 -4
  18. endoreg_db/data/pdf_type/data.yaml +9 -16
  19. endoreg_db/data/requirement/colonoscopy_indications.yaml +56 -0
  20. endoreg_db/data/requirement/disease_cardiovascular.yaml +79 -0
  21. endoreg_db/data/requirement/disease_classification_choice_cardiovascular.yaml +38 -0
  22. endoreg_db/data/requirement/disease_hepatology.yaml +12 -0
  23. endoreg_db/data/requirement/disease_misc.yaml +12 -0
  24. endoreg_db/data/requirement/disease_renal.yaml +80 -0
  25. endoreg_db/data/requirement/event_cardiology.yaml +251 -0
  26. endoreg_db/data/requirement/lab_value.yaml +120 -0
  27. endoreg_db/data/requirement_operator/lab_operators.yaml +128 -0
  28. endoreg_db/data/requirement_operator/model_operators.yaml +90 -0
  29. endoreg_db/data/requirement_set/endoscopy_bleeding_risk.yaml +12 -0
  30. endoreg_db/data/requirement_set_type/data.yaml +20 -0
  31. endoreg_db/data/requirement_type/requirement_types.yaml +83 -0
  32. endoreg_db/data/risk/bleeding.yaml +26 -0
  33. endoreg_db/data/risk/thrombosis.yaml +37 -0
  34. endoreg_db/data/risk_type/data.yaml +27 -0
  35. endoreg_db/data/unit/time.yaml +36 -1
  36. endoreg_db/management/commands/load_base_db_data.py +14 -1
  37. endoreg_db/management/commands/load_center_data.py +46 -21
  38. endoreg_db/management/commands/load_examination_indication_data.py +49 -27
  39. endoreg_db/management/commands/load_requirement_data.py +156 -0
  40. endoreg_db/management/commands/load_risk_data.py +56 -0
  41. endoreg_db/mermaid/Overall_flow_patient_finding_intervention.md +10 -0
  42. endoreg_db/mermaid/anonymized_image_annotation.md +20 -0
  43. endoreg_db/mermaid/binary_classification_annotation.md +50 -0
  44. endoreg_db/mermaid/classification.md +8 -0
  45. endoreg_db/mermaid/examination.md +8 -0
  46. endoreg_db/mermaid/findings.md +7 -0
  47. endoreg_db/mermaid/image_classification.md +28 -0
  48. endoreg_db/mermaid/interventions.md +8 -0
  49. endoreg_db/mermaid/morphology.md +8 -0
  50. endoreg_db/mermaid/patient_creation.md +14 -0
  51. endoreg_db/mermaid/video_segmentation_annotation.md +17 -0
  52. endoreg_db/migrations/0009_requirementoperator_requirementsettype_and_more.py +154 -0
  53. endoreg_db/models/__init__.py +20 -0
  54. endoreg_db/models/ai_model/ai_model.py +0 -13
  55. endoreg_db/models/ai_model/model_meta.py +2 -12
  56. endoreg_db/models/data_file/base_classes/abstract_frame.py +0 -2
  57. endoreg_db/models/data_file/base_classes/abstract_pdf.py +0 -9
  58. endoreg_db/models/data_file/base_classes/abstract_video.py +7 -8
  59. endoreg_db/models/data_file/base_classes/utils.py +0 -22
  60. endoreg_db/models/data_file/frame.py +1 -1
  61. endoreg_db/models/data_file/import_classes/raw_pdf.py +5 -11
  62. endoreg_db/models/data_file/import_classes/raw_video.py +6 -4
  63. endoreg_db/models/data_file/video/video.py +3 -3
  64. endoreg_db/models/disease.py +88 -19
  65. endoreg_db/models/event.py +108 -21
  66. endoreg_db/models/examination/examination_indication.py +108 -29
  67. endoreg_db/models/examination/examination_type.py +20 -6
  68. endoreg_db/models/information_source.py +37 -1
  69. endoreg_db/models/laboratory/lab_value.py +83 -32
  70. endoreg_db/models/requirement/__init__.py +11 -0
  71. endoreg_db/models/requirement/requirement.py +325 -0
  72. endoreg_db/models/requirement/requirement_evaluation/__init__.py +134 -0
  73. endoreg_db/models/requirement/requirement_evaluation/requirement_type_parser.py +102 -0
  74. endoreg_db/models/requirement/requirement_operator.py +58 -0
  75. endoreg_db/models/requirement/requirement_set.py +127 -0
  76. endoreg_db/models/risk/__init__.py +7 -0
  77. endoreg_db/models/risk/risk.py +72 -0
  78. endoreg_db/models/risk/risk_type.py +55 -0
  79. endoreg_db/serializers/raw_pdf_anony_text_validation.py +137 -0
  80. endoreg_db/serializers/raw_pdf_meta_validation.py +223 -0
  81. endoreg_db/serializers/raw_video_meta_validation.py +163 -1
  82. endoreg_db/serializers/video_segmentation.py +208 -126
  83. endoreg_db/urls.py +127 -14
  84. endoreg_db/utils/__init__.py +43 -0
  85. endoreg_db/utils/dataloader.py +38 -19
  86. endoreg_db/utils/hashs.py +1 -0
  87. endoreg_db/utils/paths.py +86 -0
  88. endoreg_db/views/raw_pdf_anony_text_validation_views.py +95 -0
  89. endoreg_db/views/raw_pdf_meta_validation_views.py +111 -0
  90. endoreg_db/views/raw_video_meta_validation_views.py +128 -18
  91. endoreg_db/views/video_segmentation_views.py +28 -11
  92. endoreg_db/views/views.py +107 -0
  93. {endoreg_db-0.6.2.dist-info → endoreg_db-0.6.4.dist-info}/METADATA +1 -1
  94. {endoreg_db-0.6.2.dist-info → endoreg_db-0.6.4.dist-info}/RECORD +96 -46
  95. endoreg_db/management/commands/load_name_data.py +0 -37
  96. {endoreg_db-0.6.2.dist-info → endoreg_db-0.6.4.dist-info}/WHEEL +0 -0
  97. {endoreg_db-0.6.2.dist-info → endoreg_db-0.6.4.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,223 @@
1
+ from pathlib import Path
2
+ from rest_framework import serializers
3
+ from django.conf import settings
4
+ from ..models import RawPdfFile, SensitiveMeta
5
+
6
+
7
+ class PDFFileForMetaSerializer(serializers.ModelSerializer):
8
+ """
9
+ Serializer to fetch PDF metadata along with linked `SensitiveMeta` details.
10
+ Implements validation and ensures Vue.js can process errors easily.
11
+ """
12
+
13
+ # Fetch patient details from `SensitiveMeta`
14
+ patient_first_name = serializers.CharField(source="sensitive_meta.patient_first_name", read_only=True)
15
+ patient_last_name = serializers.CharField(source="sensitive_meta.patient_last_name", read_only=True)
16
+ patient_dob = serializers.CharField(source="sensitive_meta.patient_dob", read_only=True)
17
+ examination_date = serializers.CharField(source="sensitive_meta.examination_date", read_only=True)
18
+
19
+ # PDF file URL where Vue.js can fetch the document
20
+ pdf_url = serializers.SerializerMethodField()
21
+
22
+ # Full absolute path of the PDF file
23
+ full_pdf_path = serializers.SerializerMethodField()
24
+
25
+ # Direct file path from the database
26
+ file = serializers.SerializerMethodField()
27
+
28
+ class Meta:
29
+ model = RawPdfFile
30
+ fields = ['id', 'file', 'pdf_url', 'full_pdf_path',
31
+ 'sensitive_meta_id', 'patient_first_name',
32
+ 'patient_last_name', 'patient_dob', 'examination_date']
33
+
34
+ @staticmethod
35
+ def get_next_pdf(last_id=None):
36
+ """
37
+ Retrieves the first available PDF if `last_id` is NOT provided.
38
+ Otherwise, fetches the next available PDF where `id > last_id`.
39
+ """
40
+ query_filter = {} if last_id is None else {"id__gt": int(last_id)}
41
+
42
+ # Get the next available PDF
43
+ pdf_entry = RawPdfFile.objects.select_related("sensitive_meta").filter(**query_filter).order_by('id').first()
44
+
45
+ return pdf_entry # Returns a model instance or None
46
+
47
+ def get_pdf_url(self, obj):
48
+ """
49
+ Generates the full URL for Vue.js to fetch and display the PDF.
50
+ """
51
+ request = self.context.get('request')
52
+ print("---------------------here :",obj.file)
53
+ if request and obj.file:
54
+ return request.build_absolute_uri(f"/api/pdf/sensitivemeta/?id={obj.id}") # Constructs full API endpoint
55
+ return None # Return None if file is missing
56
+
57
+ def get_file(self, obj):
58
+ """
59
+ Returns the relative file path stored in the database.
60
+ """
61
+ if not obj.file:
62
+ return None # No file linked
63
+ return str(obj.file.name).strip() # Ensures clean output
64
+
65
+ def get_full_pdf_path(self, obj):
66
+ """
67
+ Constructs the full absolute file path using `settings.MEDIA_ROOT`.
68
+ """
69
+ if not obj.file:
70
+ return None # No file linked
71
+
72
+ pdf_relative_path = str(obj.file.name)
73
+
74
+ full_path = Path(settings.MEDIA_ROOT) / pdf_relative_path
75
+
76
+ return str(full_path) if full_path.exists() else None # Returns path or None if file is missing
77
+
78
+ def validate(self, data):
79
+ """
80
+ Ensures that the PDF file is valid and has required fields.
81
+ """
82
+ errors = {}
83
+
84
+ if 'file' in data and not data['file']:
85
+ errors['file'] = "A valid PDF file is required."
86
+
87
+ if 'sensitive_meta_id' in data and not SensitiveMeta.objects.filter(id=data['sensitive_meta_id']).exists():
88
+ errors['sensitive_meta_id'] = "The provided sensitive_meta_id does not exist."
89
+
90
+ if errors:
91
+ raise serializers.ValidationError(errors) # Returns structured error response
92
+
93
+ return data # Returns validated data
94
+
95
+
96
+ from rest_framework import serializers
97
+ from ..models import SensitiveMeta
98
+
99
+ class SensitiveMetaUpdateSerializer(serializers.ModelSerializer):
100
+ """
101
+ Serializer for updating patient details in the `SensitiveMeta` table.
102
+ Allows partial updates.
103
+ """
104
+
105
+ sensitive_meta_id = serializers.IntegerField(write_only=True) # Needed for lookup, not included in response
106
+
107
+ class Meta:
108
+ model = SensitiveMeta
109
+ fields = ['sensitive_meta_id', 'patient_first_name', 'patient_last_name', 'patient_dob', 'examination_date']
110
+
111
+ def validate(self, data):
112
+ """
113
+ Ensures valid input before updating.
114
+ """
115
+ errors = {}
116
+
117
+ if 'patient_first_name' in data and (not data['patient_first_name'].strip()):
118
+ errors['patient_first_name'] = "First name cannot be empty."
119
+
120
+ if 'patient_last_name' in data and (not data['patient_last_name'].strip()):
121
+ errors['patient_last_name'] = "Last name cannot be empty."
122
+
123
+ if 'patient_dob' in data and not data['patient_dob']:
124
+ errors['patient_dob'] = "Date of birth is required."
125
+
126
+ if 'examination_date' in data and not data['examination_date']:
127
+ errors['examination_date'] = "Examination date is required."
128
+
129
+ if errors:
130
+ raise serializers.ValidationError(errors)
131
+
132
+ return data
133
+
134
+ def update(self, instance, validated_data):
135
+ """
136
+ Updates only the provided fields dynamically.
137
+ """
138
+ validated_data.pop("sensitive_meta_id", None) # Remove ID before updating
139
+
140
+ for attr, value in validated_data.items():
141
+ setattr(instance, attr, value) # Dynamically update fields
142
+
143
+ instance.save()
144
+ return instance
145
+
146
+
147
+
148
+ """
149
+ await import('https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js');
150
+ const fetchPdfMeta = async (lastId = 1) => {
151
+ const url = lastId
152
+ ? `http://localhost:8000/api/pdf/sensitivemeta/?last_id=${lastId}`
153
+ : "http://localhost:8000/api/pdf/sensitivemeta/";
154
+
155
+ try {
156
+ const response = await axios.get(url);
157
+ console.log("PDF Metadata:", response.data);
158
+ } catch (error) {
159
+ console.error("Error fetching PDFs:", error.response?.data || error);
160
+ }
161
+ };
162
+
163
+ fetchPdfMeta(); // Fetch the first available PDF
164
+
165
+ """
166
+
167
+ """with header response"""
168
+ """
169
+ await import('https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js');
170
+
171
+ const fetchPdfMeta = async (lastId = 1) => {
172
+ const url = lastId
173
+ ? `http://localhost:8000/api/pdf/sensitivemeta/?last_id=${lastId}`
174
+ : "http://localhost:8000/api/pdf/sensitivemeta/";
175
+
176
+ try {
177
+ const response = await axios.get(url, {
178
+ headers: {
179
+ "Accept": "application/json"
180
+ }
181
+ });
182
+
183
+ console.log("PDF Metadata (JSON Expected):", response.data);
184
+ } catch (error) {
185
+ console.error("Error fetching PDFs:", error.response?.data || error);
186
+ }
187
+ };
188
+
189
+ fetchPdfMeta();
190
+ """
191
+
192
+
193
+ """
194
+ await import('https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js');
195
+
196
+ const updatePatientInfo = async () => {
197
+ const updatedData = {
198
+ sensitive_meta_id: 1,
199
+ patient_first_name: "dummy value",
200
+ patient_last_name: "dummy value",
201
+ patient_dob: "1994-06-15",
202
+ examination_date: "2024-06-15"
203
+ };
204
+
205
+ try {
206
+ const response = await axios.patch("http://localhost:8000/api/pdf/update_sensitivemeta/", updatedData, {
207
+ headers: { "Content-Type": "application/json" }
208
+ });
209
+
210
+ console.log("Update Success:", response.data);
211
+ alert("Patient information updated successfully!");
212
+
213
+ return response.data;
214
+ } catch (error) {
215
+ console.error("Update Error:", error.response?.data || error);
216
+ alert("Failed to update patient information.");
217
+ return error.response?.data || { error: "Unknown error" };
218
+ }
219
+ };
220
+
221
+ updatePatientInfo().then(response => console.log("Final Response:", response));
222
+
223
+ """
@@ -1,13 +1,175 @@
1
+ from pathlib import Path
1
2
  from rest_framework import serializers
3
+ from django.conf import settings
2
4
  from ..models import RawVideoFile, SensitiveMeta
5
+ import cv2
6
+ from datetime import datetime
7
+
3
8
 
4
9
  class VideoFileForMetaSerializer(serializers.ModelSerializer):
5
10
  """
6
11
  Serializer to fetch video metadata along with linked `SensitiveMeta` details.
12
+ Ensures that Vue.js can properly access and play videos.
7
13
  """
8
14
 
15
+ # Fetch patient name and DOB from `SensitiveMeta` table
9
16
  patient_first_name = serializers.CharField(source="sensitive_meta.patient_first_name", read_only=True)
17
+ patient_last_name = serializers.CharField(source="sensitive_meta.patient_last_name", read_only=True)
18
+ patient_dob = serializers.CharField(source="sensitive_meta.patient_dob", read_only=True)
19
+ examination_date = serializers.CharField(source="sensitive_meta.examination_date", read_only=True)
20
+
21
+ duration = serializers.SerializerMethodField()
22
+
23
+
24
+ # `video_url` generates the full URL where Vue.js can fetch & play the video
25
+ video_url = serializers.SerializerMethodField()
26
+
27
+ # `full_video_path` dynamically constructs the absolute path using MEDIA_ROOT
28
+ full_video_path = serializers.SerializerMethodField()
29
+
30
+ file = serializers.SerializerMethodField()
10
31
 
11
32
  class Meta:
12
33
  model = RawVideoFile
13
- fields = ['id', 'original_file_name', 'file', 'sensitive_meta_id', 'patient_first_name']
34
+ fields = ['id', 'original_file_name', 'file', 'video_url', 'full_video_path',
35
+ 'sensitive_meta_id', 'patient_first_name', 'patient_last_name', 'patient_dob', 'examination_date','duration']
36
+
37
+ @staticmethod
38
+ def get_next_video(last_id=None):
39
+ """
40
+ Fetches the first or next available video.
41
+ Returns a model instance if found, else returns None.
42
+ """
43
+ query_filter = {} if last_id is None else {"id__gt": int(last_id)}
44
+
45
+ # Get next available video
46
+ video_entry = RawVideoFile.objects.select_related("sensitive_meta").filter(**query_filter).order_by('id').first()
47
+
48
+ return video_entry # Always return model instance or None
49
+
50
+ def get_video_url(self, obj):
51
+ """
52
+ Generates the full URL where Vue.js can fetch and stream the video.
53
+ """
54
+ request = self.context.get('request')
55
+ if request and obj.file:
56
+ print("---------------------------:",obj.file)
57
+ return request.build_absolute_uri(f"/api/video/{obj.id}/") # Generates full URL
58
+ return None # Return None instead of an error dictionary
59
+
60
+ def get_file(self, obj):
61
+ """
62
+ Returns only the relative file path, without `/media/`.
63
+ """
64
+ if not obj.file:
65
+ return None # No file associated
66
+
67
+ video_relative_path = str(obj.file.name).strip()
68
+ return video_relative_path if video_relative_path else None # Avoids errors if the file path is empty
69
+
70
+ def get_full_video_path(self, obj):
71
+ """
72
+ Constructs the full absolute file path using `settings.MEDIA_ROOT`
73
+ and the `file` field from the database.
74
+ """
75
+ if not obj.file:
76
+ return None # No file associated
77
+
78
+ video_relative_path = str(obj.file.name).strip()
79
+ full_path = Path(settings.MEDIA_ROOT) / video_relative_path
80
+
81
+ return str(full_path) if full_path.exists() else None # Return path or None if not found
82
+
83
+ def get_duration(self, obj):
84
+ """
85
+ Returns the total duration of the video in seconds.
86
+ - If stored in the database, use it.
87
+ - Otherwise, calculate dynamically using OpenCV.
88
+ """
89
+
90
+ # Step 1: Check if the `duration` field is already stored in the database.
91
+ if hasattr(obj, "duration") and obj.duration:
92
+ return obj.duration # If duration exists in the database, return it immediately.
93
+
94
+ # Step 2: Get the actual video file path
95
+ video_path = obj.file.path if obj.file else None # Extracts the file path if it exists
96
+
97
+ # Step 3: Validate if the file exists
98
+ if not video_path or not Path(video_path).exists():
99
+ return None # Return None if the file path is invalid or missing
100
+
101
+ # Step 4: Open the video file using OpenCV
102
+ cap = cv2.VideoCapture(video_path) # Load the video file
103
+
104
+ if not cap.isOpened():
105
+ return None # If OpenCV fails to open the video, return None (invalid or corrupted file)
106
+
107
+ # Step 5: Extract FPS (Frames Per Second)
108
+ fps = cap.get(cv2.CAP_PROP_FPS) # Get the number of frames per second
109
+ print("here is the fps------------", fps) # Debugging print to check FPS value
110
+
111
+ # Step 6: Get Total Number of Frames
112
+ total_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT) # Get the total number of frames in the video
113
+
114
+ # Step 7: Release the video file from OpenCV memory
115
+ cap.release() # Free up system memory
116
+
117
+ # Step 8: Calculate and Return Video Duration
118
+ return round(total_frames / fps, 2) if fps > 0 else None # Divide frames by FPS to get duration (seconds)
119
+
120
+
121
+
122
+
123
+ class SensitiveMetaUpdateSerializer(serializers.ModelSerializer):
124
+ """
125
+ Serializer to update patient information in the `SensitiveMeta` table.
126
+ Handles validation and saves the data directly in the serializer.
127
+ """
128
+
129
+ sensitive_meta_id = serializers.IntegerField(write_only=True) # Needed for lookup but not included in response
130
+
131
+ class Meta:
132
+ model = SensitiveMeta
133
+ fields = ['sensitive_meta_id', 'patient_first_name', 'patient_last_name', 'patient_dob', 'examination_date']
134
+
135
+ def validate(self, data):
136
+ """
137
+ Validate input data before updating.
138
+ """
139
+ errors = {}
140
+
141
+ # Ensure the SensitiveMeta ID exists
142
+ sensitive_meta_id = data.get("sensitive_meta_id")
143
+ if not SensitiveMeta.objects.filter(id=sensitive_meta_id).exists():
144
+ raise serializers.ValidationError({"sensitive_meta_id": "SensitiveMeta entry not found."})
145
+
146
+ # Validate each field separately
147
+ if 'patient_first_name' in data and (data['patient_first_name'] is None or data['patient_first_name'].strip() == ""):
148
+ errors['patient_first_name'] = "First name cannot be empty."
149
+
150
+ if 'patient_last_name' in data and (data['patient_last_name'] is None or data['patient_last_name'].strip() == ""):
151
+ errors['patient_last_name'] = "Last name cannot be empty."
152
+
153
+ if 'patient_dob' in data and not data['patient_dob']:
154
+ errors['patient_dob'] = "Date of birth is required."
155
+
156
+ if 'examination_date' in data and not data['examination_date']:
157
+ errors['examination_date'] = "Examination date is required."
158
+
159
+ if errors:
160
+ raise serializers.ValidationError(errors) # Raise errors before saving
161
+
162
+ return data
163
+
164
+ def update(self, instance, validated_data):
165
+ """
166
+ Update the SensitiveMeta entry directly inside the serializer.
167
+ """
168
+ # Remove `sensitive_meta_id` before updating
169
+ validated_data.pop("sensitive_meta_id", None)
170
+
171
+ for attr, value in validated_data.items():
172
+ setattr(instance, attr, value) # Set attributes dynamically
173
+
174
+ instance.save() # Save changes
175
+ return instance # Return updated instance