forked from bioinformatics-ua/dicoogle-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dicoogle.py
113 lines (100 loc) · 3.48 KB
/
dicoogle.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__title__ = 'dicoogle'
__version__ = '3.0.0' # semver
__author__ = 'Luís Bastião Silva'
__original_authors__ = 'Luís A. Bastião Silva, Tiago Marques Godinho'
__license__ = 'GPL v3'
__copyright__ = 'Copyright 2020, BMD software'
__url__ = 'https://www.dicoogle.com'
__maintainer__ = 'Luís Bastião Silva'
__email__ = '[email protected]'
__all__ = ()
import requests
class Dicoogle:
"""The client"""
def __init__(self, url='demo.dicoogle.com'):
self.ENDPOINT = "http://" + url
def search_dim(self, query, provider=None, keyword=True):
"""Search with DIM levels
Parameters
----------
query : str
The query using Dicoogle query supported syntax
Forward to the query plugin/provider
provider : str, optional
The provider that the query will be forward
keyword : bool, optional
if a keyword query, this option should be true
"""
url = self.ENDPOINT + "/searchDIM"
data = {"query": query, "keyword": keyword}
if provider:
data["provider"] = provider
r = requests.get(url, params=data)
return r
def export_csv(self, query, fields=[], providers=[], file=None):
"""Export the results for a CSV file
Parameters
----------
query : str
The query using Dicoogle query supported syntax
Forward to the query plugin/provider
fields: list of strings
a list of the fields that should be exported
providers : list of strings (providers)
A list of providers
file : string, optional
exported file name
"""
url = self.ENDPOINT + "/export"
data = {"query": query, "fields": fields, "providers": providers}
r = requests.get(url, params=data, stream=True)
if file:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
file.write(chunk)
file.flush()
return r
def index(self, uri, provider=None):
"""Index DICOM objects
Parameters
----------
uri : str
The uri that will be indexed (e.g file:///C:/files/abc or mongodb://collect1/dicom/a)
provider : str
Provider
"""
url = self.ENDPOINT + "/management/tasks/index"
data = {"uri":uri}
if provider:
data["provider"] = provider
r = requests.post(url,data=data)
return r
def search(self, query, provider, keyword=False):
"""Search with any levels
Parameters
----------
query : str
The query using Dicoogle query supported syntax
Forward to the query plugin/provider
provider : str
The provider that the query will be forward
keyword : bool, optional
if a keyword query, this option should be true
"""
url = self.ENDPOINT + "/search"
data = {"query": query, "provider": provider, "keyword": keyword}
r = requests.get(url, params=data);
return r
def dump(self, uid):
"""Dump the metadata for a SOPInstanceUID
Parameters
----------
uid : str
SOPInstanceUID that it is expected to get a metadata dump
"""
url = self.ENDPOINT + "/dump"
data = {"uid": uid}
r = requests.get(url, params=data)
return r