Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatthewVita/5de7971e1adfb8724bdb989aa23317de to your computer and use it in GitHub Desktop.
Save MatthewVita/5de7971e1adfb8724bdb989aa23317de to your computer and use it in GitHub Desktop.
Early cTAKES Work
<?php
class C_ClinicalDocumentProcessing extends Controller {
var $test_data = array(
// Patient ID 1
"1" => array(
// A new patient note ID 32
"encounter_note_32" => array(
"RXNORM" => array(
array(
"text" => "Clotrimazole",
"code" => "2623"
),
array(
"text" => "Coumadin",
"code" => "202421"
)
),
"SNOMEDCT_US" => array(
array(
"text" => "Calcium Channel Blockers",
"code" => "48698004"
)
)
),
// A new patient note ID 43
"encounter_id_43" => array(
"RXNORM" => array(
array(
"text" => "Coumadin",
"code" => "202421"
)
),
"SNOMEDCT_US" => array()
)
),
// Patient ID 439
"439" => array(
// A new patient note ID 554
"encounter_id_554" => array(
"RXNORM" => array(
array(
"text" => "Coumadin",
"code" => "202421"
)
),
"SNOMEDCT_US" => array(
array(
"text" => "Clotrimazole",
"code" => "387325003"
),
array(
"text" => "Calcium Channel Blockers",
"code" => "48698004"
),
array(
"text" => "Calcium",
"code" => "5540006"
)
)
)
)
);
function __construct()
{
parent::__construct();
}
function default_action()
{
$this->check_action();
}
function check_action()
{
return \common\http\HttpResponseHelper::send(200, array("status" => "OK"), 'JSON');
}
function get_action($pid)
{
if (!in_array($pid, array("1", "439")) || empty($pid)) {
return \common\http\HttpResponseHelper::send(404, array("status" => "Not Found"), 'JSON');
}
// TODO: Call to a service that does the MySQL query here
$data = $this->test_data[$pid];
return \common\http\HttpResponseHelper::send(200, array("status" => $data), 'JSON');
}
function post_action($pid, $encounter_id, $content="")
{
if (!in_array($pid, array("1", "439")) || empty($pid)) {
return \common\http\HttpResponseHelper::send(404, array("status" => "Not Found"), 'JSON');
}
if (empty($encounter_id)) {
// TODO: Use real UUID library
$encounter_id = sprintf('%05d-%05d-%05d-%05d',
mt_rand(0, 99999),
mt_rand(0, 99999),
mt_rand(0, 99999),
mt_rand(0, 99999)
);
}
// TODO: Send to cTAKES here
return \common\http\HttpResponseHelper::send(201, array("status" => "Queued for Processing"), 'JSON');
}
function put_action($pid, $encounter_id)
{
// TODO: Logic here that marks a note as "read" so that it isn't returned in the get action...
return \common\http\HttpResponseHelper::send(200, array("status" => "OK"), 'JSON');
}
}
?>
import json
import xmltodict
import sys
def convert_xml_to_dict(file):
file = open(file, 'r')
data = xmltodict.parse(file.read())
file.close()
return data
def extract_codes(data):
raw_codes = data["xmi:XMI"]["refsem:UmlsConcept"]
formatted_codes = dict()
for code in raw_codes:
if not formatted_codes.has_key(code['@codingScheme']):
formatted_codes[code['@codingScheme']] = []
nullable_code_name = 'unknown'
if code.has_key('@code'):
nullable_code_name = code['@code']
formatted_codes[code['@codingScheme']].append({
'text': code['@preferredText'],
'code': nullable_code_name
})
return formatted_codes
def write_result_to_json_file(file, data):
file = open(file, 'w')
file.write(json.dumps(data))
file.close()
file_without_extension = sys.argv[1].rsplit('.', 1)[0]
data = convert_xml_to_dict(file_without_extension + '.xml')
codes = extract_codes(data)
write_result_to_json_file(file_without_extension + '_grouped_codes.json', codes)
xmltodict==0.11.0
@MatthewVita
Copy link
Author

http://localhost/openemr/controller.php?ClinicalDocumentProcessing&get&pid=1, for example :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment