-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcodelist.rs
More file actions
executable file
·510 lines (452 loc) · 16.7 KB
/
codelist.rs
File metadata and controls
executable file
·510 lines (452 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#![allow(non_local_definitions)]
//! This file contains the python bindings for the codelist-rs library's
//! CodeList struct This should only contain the python bindings for the
//! CodeList struct.
use codelist_rs::{
codelist::{CodeList, TermManagement},
codelist_options::CodeListOptions,
metadata::{
CategorisationAndUsage, Metadata, Provenance, PurposeAndContext, Source,
ValidationAndReview,
},
types::CodeListType,
};
use codelist_validator_rs::validator::Validator;
use indexmap::IndexSet;
use pyo3::{
exceptions::PyValueError,
prelude::*,
types::{PyDict, PySet},
PyErr, PyResult,
};
use regex::Regex;
/// Python wrapper for the CodeList struct
///
/// This struct is a python wrapper for the CodeList struct in the codelist-rs
/// library. It allows us to create a new CodeList object from python and
/// interact with it.
#[pyclass(name = "CodeList")]
pub struct PyCodeList {
pub inner: CodeList,
}
/// Python methods for the PyCodeList struct
#[pymethods]
impl PyCodeList {
#[new]
#[pyo3(signature = (name, codelist_type, source, authors=None))]
fn new(
name: String,
codelist_type: &str,
source: &str,
authors: Option<Vec<String>>,
) -> PyResult<Self> {
// Convert string to CodeListType
let codelist_type = match codelist_type.to_uppercase().as_str() {
"ICD10" => CodeListType::ICD10,
"ICD" => CodeListType::ICD10,
"SNOMED" => CodeListType::SNOMED,
"SNOMEDCT" => CodeListType::SNOMED,
"OPCS" => CodeListType::OPCS,
_ => {
return Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(format!(
"Invalid codelist type: {codelist_type}"
)))
}
};
// Create metadata
let source = Source::from_string(source).map_err(|_| {
PyErr::new::<pyo3::exceptions::PyValueError, _>(format!("Invalid source: {source}"))
})?;
// convert authors vec to IndexSet
let authors_set = authors
.map(|authors| authors.into_iter().collect::<IndexSet<String>>())
.unwrap_or_default();
let provenance = Provenance::new(source, Some(authors_set));
let categorisation_and_usage = CategorisationAndUsage::new(None, None, None);
let purpose_and_context = PurposeAndContext::new(None, None, None);
let validation_and_review =
ValidationAndReview::new(Some(false), None, None, Some("started".to_string()), None);
let metadata = Metadata::new(
provenance,
categorisation_and_usage,
purpose_and_context,
validation_and_review,
);
// Parse CodeListOptions from PyDict
let codelist_options = CodeListOptions::default();
// Create codelist
let codelist = CodeList::new(name, codelist_type, metadata, Some(codelist_options));
Ok(PyCodeList { inner: codelist })
}
/// Get the name of the codelist
#[getter]
fn name(&self) -> String {
self.inner.name.to_string()
}
/// Add an entry to the codelist
#[pyo3(signature = (code, term=None, comment=None))]
fn add_entry(
&mut self,
code: String,
term: Option<String>,
comment: Option<String>,
) -> PyResult<()> {
let _ = self.inner.add_entry(code, term, comment);
Ok(())
}
/// Get all entries in the codelist
fn entries(&self) -> Vec<(String, Option<String>, Option<String>)> {
self.inner
.full_entries()
.iter()
.map(|(code, (term, comment))| (code.clone(), term.clone(), comment.clone()))
.collect()
}
/// Add a contributor to the codelist's provenance
fn add_contributor(&mut self, contributor: String) -> PyResult<()> {
self.inner.metadata.provenance.add_contributor(contributor);
Ok(())
}
/// Remove a contributor from the codelist's provenance
fn remove_contributor(&mut self, contributor: String) -> PyResult<()> {
self.inner
.metadata
.provenance
.remove_contributor(contributor)
.map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
#[getter]
fn contributors(&self, py: Python) -> PyResult<PyObject> {
let py_set = PySet::new(py, &[] as &[String])?;
for contributor in &self.inner.metadata.provenance.contributors {
py_set.add(contributor)?;
}
Ok(py_set.into())
}
/// Get date created and last modified date as dict
fn get_dates(&self, py: Python) -> PyResult<PyObject> {
let date_created = self.inner.metadata.provenance.created_date;
let last_modified_date = self.inner.metadata.provenance.last_modified_date;
let dict = PyDict::new(py);
dict.set_item("date_created", date_created.to_string())?;
dict.set_item("last_modified_date", last_modified_date.to_string())?;
Ok(dict.into())
}
/// Get tag information
fn get_tags(&self, py: Python) -> PyResult<PyObject> {
let tags = self.inner.metadata.categorisation_and_usage.tags.clone();
let py_set = PySet::new(py, &[] as &[String])?;
for tag in tags {
py_set.add(tag)?;
}
Ok(py_set.into())
}
/// Add a tag to the codelist
fn add_tag(&mut self, tag: String) -> PyResult<()> {
self.inner
.metadata
.categorisation_and_usage
.add_tag(tag)
.map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
/// Remove a tag from the codelist
fn remove_tag(&mut self, tag: String) -> PyResult<()> {
self.inner
.metadata
.categorisation_and_usage
.remove_tag(tag)
.map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
/// Get usage information
fn get_usage(&self, py: Python) -> PyResult<PyObject> {
let usage = self.inner.metadata.categorisation_and_usage.usage.clone();
let py_set = PySet::new(py, &[] as &[String])?;
for usage_item in usage {
py_set.add(usage_item)?;
}
Ok(py_set.into())
}
/// Add usage information to the codelist
fn add_usage(&mut self, usage: String) -> PyResult<()> {
self.inner.metadata.categorisation_and_usage.add_usage(usage);
Ok(())
}
/// Remove usage information from the codelist
fn remove_usage(&mut self, usage: String) -> PyResult<()> {
self.inner
.metadata
.categorisation_and_usage
.remove_usage(usage)
.map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
/// Get license information (with is a OPtional String)
fn get_license_info(&self) -> Option<String> {
self.inner.metadata.categorisation_and_usage.license.clone()
}
/// Add license information to the codelist
fn add_license(&mut self, license: String) -> PyResult<()> {
self.inner
.metadata
.categorisation_and_usage
.add_license(license)
.map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
/// Remove license information from the codelist
fn remove_license(&mut self) -> PyResult<()> {
self.inner
.metadata
.categorisation_and_usage
.remove_license()
.map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
/// Update the license information
fn update_license(&mut self, license: String) -> PyResult<()> {
self.inner
.metadata
.categorisation_and_usage
.update_license(license)
.map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
/// Get the purpose of the codelist
fn get_purpose(&self) -> Option<String> {
self.inner.metadata.purpose_and_context.purpose.clone()
}
/// Add a purpose to the codelist
fn add_purpose(&mut self, purpose: String) -> PyResult<()> {
self.inner
.metadata
.purpose_and_context
.add_purpose(purpose)
.map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
/// Update the purpose of the codelist
fn update_purpose(&mut self, purpose: String) -> PyResult<()> {
self.inner
.metadata
.purpose_and_context
.update_purpose(purpose)
.map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
/// Remove a purpose from the codelist
fn remove_purpose(&mut self) -> PyResult<()> {
self.inner
.metadata
.purpose_and_context
.remove_purpose()
.map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
/// Get the target audience of the codelist
fn get_audience(&self) -> Option<String> {
self.inner.metadata.purpose_and_context.target_audience.clone()
}
/// Add a target audience to the codelist
fn add_audience(&mut self, target_audience: String) -> PyResult<()> {
self.inner
.metadata
.purpose_and_context
.add_target_audience(target_audience)
.map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
/// Update the target audience of the codelist
fn update_audience(&mut self, target_audience: String) -> PyResult<()> {
self.inner
.metadata
.purpose_and_context
.update_target_audience(target_audience)
.map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
/// Remove a target audience from the codelist
fn remove_audience(&mut self) -> PyResult<()> {
self.inner
.metadata
.purpose_and_context
.remove_target_audience()
.map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
/// Get the use context of the codelist
fn get_use_context(&self) -> Option<String> {
self.inner.metadata.purpose_and_context.use_context.clone()
}
/// Add a use context to the codelist
fn add_use_context(&mut self, use_context: String) -> PyResult<()> {
self.inner
.metadata
.purpose_and_context
.add_use_context(use_context)
.map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
/// Update the use context of the codelist
fn update_use_context(&mut self, use_context: String) -> PyResult<()> {
self.inner
.metadata
.purpose_and_context
.update_use_context(use_context)
.map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
/// Remove a use context from the codelist
fn remove_use_context(&mut self) -> PyResult<()> {
self.inner
.metadata
.purpose_and_context
.remove_use_context()
.map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
/// Truncate to 3 digits if ICD10
fn truncate_to_3_digits(&mut self, term_management: String) -> PyResult<()> {
// Term management as string to TermManagement enum
let term_management = term_management
.parse::<TermManagement>()
.map_err(|e| PyValueError::new_err(e.to_string()))?;
self.inner
.truncate_to_3_digits(term_management)
.map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
/// Add X Codes to the codelist. This is a convenient way to add X to 3
/// digit ICD10 codes.
fn add_x_codes(&mut self) -> PyResult<()> {
self.inner.add_x_codes().map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
/// See if the codelist is validated
fn is_validated(&self) -> bool {
self.inner.metadata.validation_and_review.reviewed
}
/// Add Validation Information to the codelist
#[pyo3(signature = (reviewer, status=None, notes=None))]
fn add_validation_info(
&mut self,
reviewer: String,
status: Option<String>,
notes: Option<String>,
) -> PyResult<()> {
// Add reviewer
self.inner
.metadata
.validation_and_review
.add_reviewer(reviewer)
.map_err(|e| PyValueError::new_err(e.to_string()))?;
// Add review date // TODO: Sort out datetime with pyclass
self.inner
.metadata
.validation_and_review
.add_review_date(chrono::Utc::now())
.map_err(|e| PyValueError::new_err(e.to_string()))?;
// Add status
if let Some(status) = status {
self.inner
.metadata
.validation_and_review
.update_status(status)
.map_err(|e| PyValueError::new_err(e.to_string()))?;
}
// Add validation notes
if let Some(validation_notes) = notes {
// if existing notes, append to them, otherwise just set them
if let Some(_existing_notes) =
&self.inner.metadata.validation_and_review.get_validation_notes()
{
self.inner
.metadata
.validation_and_review
.update_validation_notes(validation_notes)
.map_err(|e| PyValueError::new_err(e.to_string()))?;
} else {
self.inner
.metadata
.validation_and_review
.add_validation_notes(validation_notes)
.map_err(|e| PyValueError::new_err(e.to_string()))?;
}
}
// Update reviewed status
self.inner.metadata.validation_and_review.update_reviewed(true);
Ok(())
}
/// Update the validaation notes
fn update_validation_notes(&mut self, notes: String) -> PyResult<()> {
self.inner
.metadata
.validation_and_review
.update_validation_notes(notes)
.map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
/// Get the validation status of the codelist
fn get_validation_status(&self) -> Option<String> {
self.inner.metadata.validation_and_review.status.clone()
}
/// Get the validation notes of the codelist
fn get_validation_notes(&self) -> Option<String> {
self.inner.metadata.validation_and_review.validation_notes.clone()
}
/// Get the reviewer of the codelist
fn get_reviewer(&self) -> Option<String> {
self.inner.metadata.validation_and_review.reviewer.clone()
}
/// Validate the codelist based on the codelist type
#[pyo3(signature = (custom_regex=None))]
fn validate_codes(&self, custom_regex: Option<String>) -> PyResult<()> {
match custom_regex {
Some(regex_str) => {
let regex = Regex::new(®ex_str)
.map_err(|e| PyValueError::new_err(format!("Invalid regex: {e}")))?;
self.inner
.validate_codes(Some(®ex))
.map_err(|e| PyValueError::new_err(e.to_string()))?
}
None => {
self.inner.validate_codes(None).map_err(|e| PyValueError::new_err(e.to_string()))?
}
}
Ok(())
}
/// Add a comment to the codelist
fn add_comment(&mut self, code: String, comment: String) -> PyResult<()> {
self.inner.add_comment(code, comment).map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
/// Update a comment in the codelist
fn update_comment(&mut self, code: String, comment: String) -> PyResult<()> {
self.inner
.update_comment(code, comment)
.map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
/// Remove a comment from the codelist
fn remove_comment(&mut self, code: String) -> PyResult<()> {
self.inner.remove_comment(code).map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
/// Add a term to the codelist
fn add_term(&mut self, code: String, term: String) -> PyResult<()> {
self.inner.add_term(code, term).map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
/// Update a term in the codelist
fn update_term(&mut self, code: String, term: String) -> PyResult<()> {
self.inner.update_term(code, term).map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
/// Remove a term from the codelist
fn remove_term(&mut self, code: String) -> PyResult<()> {
self.inner.remove_term(code).map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(())
}
}