Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor DicomNet class #556

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 62 additions & 51 deletions invesalius/net/dicom.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,59 @@
from typing import Any
import gdcm
import invesalius.utils as utils
from dataclasses import dataclass

@dataclass
class DicomNet:
"""
Class for managing DICOM network configuration and to make standard Requests
"""
address: str = ''
port: int = 0
aetitle_call: str = ''
aetitle: str = ''
search_word: str = ''
search_type: str = 'patient'

def __init__(self):
self.address = ''
self.port = ''
self.aetitle_call = ''
self.aetitle = ''
self.search_word = ''
self.search_type = 'patient'

def __call__(self):
return self

def SetHost(self, address):
def set_host(self, address: str):
self.address = address

def SetPort(self, port):
def set_port(self, port: int):
self.port = port

def SetAETitleCall(self, name):
def set_aetitle_call(self, name: str):
self.aetitle_call = name

def SetAETitle(self, ae_title):
def set_aetitle(self, ae_title: str):
self.aetitle = ae_title

def SetSearchWord(self, word):
def set_search_word(self, word: str):
self.search_word = word

def SetSearchType(self, stype):
def set_search_type(self, stype: str):
self.search_type = stype

def GetValueFromDICOM(self, ret, tag):
value = str(ret.GetDataElement(gdcm.Tag(tag[0],\
tag[1])).GetValue())

def get_value_from_dicom(self, ret:Any, tag: tuple) -> str:
value = str(ret.GetDataElement(gdcm.Tag(tag[0], tag[1])).GetValue())
if value == 'None' and tag != (0x0008,0x103E):
value = ''
return value




def RunCEcho(self):
def run_c_echo(self) -> bool:
cnf = gdcm.CompositeNetworkFunctions()
return cnf.CEcho(self.address, int(self.port),\
self.aetitle, self.aetitle_call)
try:
res = cnf.CEcho(self.address, self.port, \
self.aetitle, self.aetitle_call)
except TimeoutError:
print('TIMEOUT ERROR')
except Exception as e:
print(f"ERROR: {e}")

def RunCFind(self):
return res

def run_c_find(self) -> dict:

tags = [(0x0010, 0x0010), (0x0010, 0x1010), (0x0010,0x0040), (0x0008,0x1030),\
(0x0008,0x0060), (0x0008,0x0022), (0x0008,0x0080), (0x0010,0x0030),\
Expand Down Expand Up @@ -73,10 +81,10 @@ def RunCFind(self):


cnf = gdcm.CompositeNetworkFunctions()
theQuery = cnf.ConstructQuery(gdcm.ePatientRootType, gdcm.eImageOrFrame, ds)
theQuery = cnf.ConstructQuery(gdcm.ePatientRootType, gdcm.eImage, ds)
ret = gdcm.DataSetArrayType()

cnf.CFind(self.address, int(self.port), theQuery, ret, self.aetitle,\
cnf.CFind(self.address, self.port, theQuery, ret, self.aetitle,\
self.aetitle_call)

patients = {}
Expand All @@ -95,20 +103,20 @@ def RunCFind(self):

rt = ret[i]

name = self.GetValueFromDICOM(rt, (0x0010, 0x0010))
age = self.GetValueFromDICOM(rt, (0x0010, 0x1010))
gender = self.GetValueFromDICOM(rt, (0x0010,0x0040))
study_description = self.GetValueFromDICOM(rt, (0x0008,0x1030))
modality = self.GetValueFromDICOM(rt, (0x0008,0x0060))
institution = self.GetValueFromDICOM(rt, (0x0008,0x0080))
date_of_birth = utils.format_date(self.GetValueFromDICOM(rt, (0x0010,0x0030)))
acession_number = self.GetValueFromDICOM(rt, (0x0008,0x0050))
ref_physician = self.GetValueFromDICOM(rt, (0x0008,0x0090))
serie_description = self.GetValueFromDICOM(rt, (0x0008,0x103E))
acquisition_time = utils.format_time(self.GetValueFromDICOM(rt, (0x0008,0x0032)))
acquisition_date = utils.format_date(self.GetValueFromDICOM(rt, (0x0008,0x0022)))

teste = self.GetValueFromDICOM(rt, (0x0020,0x000d))
name = self.get_value_from_dicom(rt, (0x0010, 0x0010))
age = self.get_value_from_dicom(rt, (0x0010, 0x1010))
gender = self.get_value_from_dicom(rt, (0x0010,0x0040))
study_description = self.get_value_from_dicom(rt, (0x0008,0x1030))
modality = self.get_value_from_dicom(rt, (0x0008,0x0060))
institution = self.get_value_from_dicom(rt, (0x0008,0x0080))
date_of_birth = utils.format_date(self.get_value_from_dicom(rt, (0x0010,0x0030)))
acession_number = self.get_value_from_dicom(rt, (0x0008,0x0050))
ref_physician = self.get_value_from_dicom(rt, (0x0008,0x0090))
serie_description = self.get_value_from_dicom(rt, (0x0008,0x103E))
acquisition_time = utils.format_time(self.get_value_from_dicom(rt, (0x0008,0x0032)))
acquisition_date = utils.format_date(self.get_value_from_dicom(rt, (0x0008,0x0022)))

teste = self.get_value_from_dicom(rt, (0x0020,0x000d))

patients[patient_id][serie_id] = {'name':name, 'age':age, 'gender':gender,\
'study_description':study_description,\
Expand All @@ -128,7 +136,7 @@ def RunCFind(self):
return patients


def RunCMove(self, values):
def run_c_move(self, values:list) -> None:

ds = gdcm.DataSet()

Expand All @@ -152,7 +160,8 @@ def RunCMove(self, values):


cnf = gdcm.CompositeNetworkFunctions()
theQuery = cnf.ConstructQuery(gdcm.ePatientRootType, gdcm.eImageOrFrame, ds)
theQuery = cnf.ConstructQuery(gdcm.ePatientRootType, gdcm.eImage, ds)

#ret = gdcm.DataSetArrayType()

"""
Expand All @@ -169,8 +178,8 @@ def RunCMove(self, values):
self.aetitle_call, "/home/phamorim/Desktop/output/")


cnf.CMove(self.address, int(self.port), theQuery, 11112, self.aetitle,\
self.aetitle_call, "/home/phamorim/Desktop/")
cnf.CMove(self.address, self.port, theQuery, 11112, self.aetitle,\
self.aetitle_call, "D:\Opensource\Invesaliusproject\dicomfiles")

print("BAIXOUUUUUUUU")
#ret = gdcm.DataSetArrayType()
Expand All @@ -186,3 +195,5 @@ def RunCMove(self, values):
#for r in ret:
# print r
# print "\n"


Empty file added invesalius/tests/__init__.py
Empty file.
76 changes: 76 additions & 0 deletions invesalius/tests/test_dicom_net.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import unittest
from unittest.mock import MagicMock
from invesalius.net.dicom import DicomNet, gdcm


class TestDicomNet(unittest.TestCase):

def test_set_host(self):
dicom_net = DicomNet()
dicom_net.set_host('127.0.0.1')
self.assertEqual(dicom_net.address, '127.0.0.1')

def test_set_port(self):
dicom_net = DicomNet()
dicom_net.set_port(1234)
self.assertEqual(dicom_net.port, 1234)

def test_set_aetitle_call(self):
dicom_net = DicomNet()
dicom_net.set_aetitle_call('my_call_ae')
self.assertEqual(dicom_net.aetitle_call, 'my_call_ae')

def test_set_aetitle(self):
dicom_net = DicomNet()
dicom_net.set_aetitle('my_ae')
self.assertEqual(dicom_net.aetitle, 'my_ae')

def test_set_search_word(self):
dicom_net = DicomNet()
dicom_net.set_search_word('my_word')
self.assertEqual(dicom_net.search_word, 'my_word')

def test_set_search_type(self):
dicom_net = DicomNet()
dicom_net.set_search_type('study')
self.assertEqual(dicom_net.search_type, 'study')
dicom_net.set_search_type('patient')
self.assertEqual(dicom_net.search_type, 'patient')

def test_get_value_from_dicom(self):
# Create a mock object for the `gdcm.DataSet` class
ret_mock = MagicMock(spec=gdcm.DataSet)

# Define a function to mock the behavior of `GetValue` method
def get_value_mock():
return {
(0x0010, 0x0010): "John Doe",
(0x0010, 0x1010): "30",
(0x0010, 0x0040): "M",
(0x0008, 0x1030): "CT scan",
}.get(tag, "")

def get_data_element_mock(tag):
elem_mock = MagicMock(spec=gdcm.DataElement)
elem_mock.GetValue.return_value = get_value_mock()
return elem_mock

ret_mock.GetDataElement.side_effect = get_data_element_mock

tag = (0x0010, 0x0010)
obj = DicomNet()
value = obj.get_value_from_dicom(ret_mock, tag)
self.assertEqual(value, "John Doe")

tag = (0x0010, 0x1000)
value = obj.get_value_from_dicom(ret_mock, tag)
self.assertEqual(value, "")

# Test with (0x0008,0x103E) tag
tag = (0x0008, 0x103E)
value = obj.get_value_from_dicom(ret_mock, tag)
self.assertEqual(value, "")


if __name__ == '__main__':
unittest.main()