-
Notifications
You must be signed in to change notification settings - Fork 0
/
mycbr_py_api.py
1058 lines (757 loc) · 32.9 KB
/
mycbr_py_api.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
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import datetime as datetime
import random
import requests
import json
from typing import Any
from typing import List
from typing import Tuple
from typing import Dict
from typing import Mapping
from typing import NoReturn
class _Constant:
BASE_URL = 'http://localhost:8080'
CASE_ID = 'caseID'
SIMILARITY = 'similarity'
class MyCBRRestApi:
__base_url = None
__conceptID = None
__casebaseID = None
__amalgamationFunctionID = None
__columnNames = None
def __init__ (self, base_url=None):
if base_url is None:
base_url = _Constant.BASE_URL
self.__base_url = base_url
self.__conceptID = self.getAllConcepts()[0]
self._setColumnNamesForConcept( self.__conceptID)
def _getCurrentBaseURL(self):
return self.__base_url
def _getCurrentConceptID(self):
return self.__conceptID
def _getCurrentCasebaseID(self):
return self.__casebaseID
def _getCurrentAmalgamationFunctionID(self):
return self.__amalgamationFunctionID
def _getCurrentColumnNames (self) -> List[str]:
"""
Get the column names that is set in the current instance.
Returns
-------
List[str] : column names, including 'caseID' and 'similarity'.
"""
return self.__columnNames
def _setCurrentConceptID (self, conceptID:str = None) -> bool:
"""
To set the default concept name.
Parameters
----------
:param conceptID : Name of the concept (default: None)
Returns
-------
Boolean : True is concept name is set, else flase.
"""
flag = False
if (conceptID is not None) and (conceptID is not ''):
self.__conceptID = conceptID
if conceptID is self.__conceptID:
flag = True
self.setColumnNames(conceptID)
return flag
def _setCurrentCasebaseID (self, casebaseID:str = None) -> bool:
"""
To set the default casebase name.
Parameters
----------
:param casebaseID : Name of the casebase (default: None)
Returns
-------
Boolean : True is casebase name is set, else flase.
"""
flag = False
if (casebaseID is not None) and (casebaseID is not ''):
self.__casebaseID = casebaseID
if casebaseID is self.__casebaseID:
flag = True
return flag
def _setCurrentAmalgamationFunctionID (self, amalgamationFunctionID:str = None) -> bool:
"""
To set the default amalgamation function.
Parameters
----------
:param amalgamationFunctionID : Name of the amalgamation function (default: None)
Returns
-------
Boolean : True is amalgamation function is set, else flase.
"""
flag = False
if (amalgamationFunctionID is not None) and (amalgamationFunctionID is not ''):
self.__amalgamationFunctionID = amalgamationFunctionID
if amalgamationFunctionID is self.__amalgamationFunctionID:
flag = True
return flag
def _setColumnNamesForConcept (self, conceptID:str = None) -> List[str]:
"""
Set all the possible column names for the given conceptID.
Parameters
----------
:param conceptID : Name of the concept (default: self.__conceptID)
Returns
-------
Boolean : True is amalgamation function is set, else flase.
"""
flag = False
self.__columnNames = self.getColumnNames(conceptID)
if self.__columnNames is not None:
flag = True
return flag
def getColumnNames (self, conceptID:str = None) -> List[str]:
"""
Get all the possible column names for the given conceptID.
Parameters
----------
:param conceptID : Name of the concept (default: self.__conceptID)
Returns
-------
List[str] : column names, including 'caseID' and 'similarity'.
"""
if conceptID is None:
conceptID = self.__conceptID
default_columns = [ _Constant.CASE_ID, _Constant.SIMILARITY ]
attributes = list (self.getAllAttributes( conceptID=conceptID).keys())
#attributes = pd.DataFrame( self.getAllAttributes( conceptID=conceptID)).index.values.tolist()
column_list = default_columns + attributes
return column_list
def __rest_response_to_dataframe (self, response:requests.Response ) -> pd.DataFrame:
"""
Helper function: convert the request response to pandas DataFrame.
Parameters
----------
:param response : response from a REST API call
Returns
-------
DataFrame : To be done.
"""
response_json = response.json()
# The below try:, except:, and else: are used to determine if a programme variable is defined or not!
try:
column_list
except NameError:
# print('column_list is not defined!!!')
df = pd.DataFrame(response_json)
else:
df = pd.DataFrame(response_json, columns= column_list)
df.replace('_unknown_', np.nan, inplace=True)
# print(df_response_gai)
if ( df.empty):
print("The response from myCBR is empty! Kindly have a look!")
print("The Dataframe is : ", df)
return df
def show_ordered_ssm (
self,
df:pd.DataFrame,
name:str = 'NotProvided',
ticks_interval:int =10,
figsize:Tuple[int,int] =(10,9),
isAnnot:bool=False
) -> pd.DataFrame:
"""
Get the Self-Similarity Matrix in an ordered form, where the first column has the highest sum.
Parameters
----------
:param df : The Self-Similarity Matrix in pandas DataFrame format, where indexs and columns are same i.e. caseIDs.
:param name : The name to be shown on the plot title. (default: NotProvided).
:param ticks_interval : The interval of ticks for the Self-Similarity heatmap.
:param figsize : Figure size of the Heatmap plot (default: (10,10)).
:param isAnnot : True, will annotate each cell with its similarity value (default: False).
Plots
-----
Heatmap : heatmap of the ordered Self-Similarity Matrix.
Returns
-------
DataFrame : Ordered Self-Similarity Matrix. NaN represents that a caseID was not compared for the similarity.
"""
ordered_series = df.sum( axis=1).sort_values( ascending=False)
lis = ordered_series.index.tolist()
df_1 = df[lis]
df_temp = df_1.reindex(lis)
plt.figure( figsize=figsize)
ax = sns.heatmap(
df_temp,
cmap='viridis',
xticklabels=ticks_interval,
yticklabels=ticks_interval,
fmt='g',
annot=isAnnot,
annot_kws={'size': 9}
)
ax.invert_xaxis()
plt.yticks(rotation=0)
plt.title('Self-Similarity Matrix (ordered) for : '+name)
return df_temp
# ****************** myCBR-rest API Calls **************************
def getAllConcepts (self) -> List[str]:
"""
Get all the concepts for the given myCBR project.
* Sample URL: ~/concepts
Parameters
----------
Returns
-------
List : list of concept IDs.
"""
final_url = self.__base_url + '/concepts'
#print(final_url)
response = requests.get( url= final_url)
concept_list = response.json()
return concept_list
def getCaseBaseIDs (self) -> List[str]:
"""
Get all the casebeses.
* Sample URL: ~/casebases
Parameters
----------
Returns
-------
List : list of casebase IDs.
"""
final_url = self.__base_url + '/casebases'
# print(final_url)
response = requests.get( url= final_url)
casebases = response.json()
return casebases
def getAllAmalgamationFunctions (self, conceptID:str = None) -> List[str]:
"""
Get all the amalgamation functions for the given conceptID.
* Sample URL: ~/concepts/patient/amalgamationFunctions
Parameters
----------
:param conceptID : Name of the concept (default: 'self.__conceptID')
Returns
-------
List : list of amalgamation function IDs.
"""
if conceptID is None:
conceptID = self.__conceptID
final_url = self.__base_url + '/concepts/' + conceptID + '/amalgamationFunctions'
#print(final_url)
response = requests.get( url= final_url)
#print(response.text)
amalgamation_list = response.json()
return amalgamation_list
def getAllAttributes (self, conceptID:str = None) -> Dict[str,str]:
"""
Get all the attributes for the given conceptID.
* Sample URL: ~/concepts/patient/attributes
Parameters
----------
:param conceptID : Name of the concept (default: self.__conceptID)
Returns
-------
Dict[str1, str2] :
str1: The attribute name.
str2: The attribute datatype.
"""
if conceptID is None:
conceptID = self.__conceptID
final_url = self.__base_url + '/concepts/' + conceptID + '/attributes'
#print(final_url)
response = requests.get( url= final_url)
#print(response.text)
attribute_list = response.json()
return attribute_list
def getAttributeByID (self, attributeID:str, conceptID:str = None) -> json:
"""
Get an attribute by its attributeID for a given conceptID.
* Sample URL: ~/concepts/patient/attributes/body_main
Parameters
----------
:param attributeID : Name of the attribute (default: None)
:param conceptID : Name of the concept (default: self.__conceptID)
Returns
-------
attribute : json respresentation of the attribute content.
"""
if conceptID is None:
conceptID = self.__conceptID
final_url = self.__base_url + '/concepts/' + conceptID + '/attributes/' + attributeID
#print(final_url)
response = requests.get( url= final_url)
attributes = response.json()
return attributes
def getAllAttributeSimilarityFunctions (self, attributeID:str, conceptID:str = None) -> json:
"""
Get an attribute similarityFunctions by its attributeID for a given conceptID.
* Sample URL: ~/concepts/patient/attributes/body_main/similarityFunctions
Parameters
----------
:param attributeID : Name of the attribute
:param conceptID : Name of the concept (default: self.__conceptID)
Returns
-------
similarityFunctions : json respresentation of the attribute similarityFunctions.
"""
if conceptID is None:
conceptID = self.__conceptID
final_url = self.__base_url + '/concepts/' + conceptID + '/attributes/' + attributeID + '/similarityFunctions'
#print(final_url)
response = requests.get( url= final_url)
attributes = response.json()
return attributes
def getCaseBaseIDs (self) -> List[str]:
"""
Get all the casebeses.
* Sample URL: ~/casebases
Parameters
----------
Returns
-------
List : list of casebase IDs.
"""
final_url = self.__base_url + '/casebases'
response = requests.get( url= final_url)
casebases = response.json()
return casebases
def addCaseBaseID (self, casebaseID:str) -> bool:
"""
Add a casebaseID.
* Sample URL : ~/casebases/test_casebase
Parameters
----------
:param casebaseID : Name of the case base
Returns
-------
bool : True (casebaseID added), False (casebaseID not added)
"""
final_url = self.__base_url + '/casebases/' + casebaseID
# print(final_url)
response = requests.put( url= final_url)
return response.json()
def deleteCaseBaseID (self, casebaseID:str) -> bool:
"""
Add a casebaseID.
* Sample URL : ~/casebases/test_casebase
Parameters
----------
:param casebaseID : Name of the case base
Returns
-------
bool : True (casebaseID deleted), False (casebaseID not deleted)
"""
final_url = self.__base_url + '/casebases/' + casebaseID
# print(final_url)
response = requests.delete( url= final_url)
return response.json()
def getAllCasesFromCaseBase (self, conceptID:str = None, casebaseID:str = None) -> pd.DataFrame:
"""
Get all the cases for a given conceptID and casebaseID.
Sample URL: ~/concepts/patient/casebases/casebase/cases
Parameters
----------
:param conceptID : Name of the concept (default: self.__conceptID)
:param casebaseID : Name of the case base (default: self.__casebaseID)
Returns
-------
Dataframe: pandas Dataframe, where rows are the cases with unique "caseID" and columns are the concept attributes.
Note
----
NaN : The placeholder for empty values
"""
if conceptID is None:
conceptID = self.__conceptID
if casebaseID is None:
casebaseID = self.__casebaseID
final_url = self.__base_url + '/concepts/' + conceptID + '/casebases/' + casebaseID + '/cases'
#print(final_url)
response = requests.get( url= final_url)
df = self.__rest_response_to_dataframe(response)
return df
def getCaseByCaseID (self, caseID:str=None, conceptID:str = None, casebaseID:str = None) -> pd.DataFrame :
"""
Get the cases for the given conceptID, casebaseID, and caseID.
* Sample URL: ~/concepts/patient/casebases/casebase/cases/patient0
Parameters
----------
:param caseID : Unique ID of the case from the CBR system
:param conceptID : Name of the concept (default: self.__conceptID)
:param casebaseID : Name of the case base (default: self.__casebaseID)
Returns
-------
DataFrame : The row is the case and columns are it's concept attributes.
Note
----
_unknown_ : is the placeholder for empty values
"""
if conceptID is None:
conceptID = self.__conceptID
if casebaseID is None:
casebaseID = self.__casebaseID
final_url = self.__base_url + '/concepts/' + conceptID + '/casebases/' + casebaseID + '/cases/' + caseID
#print(final_url)
response = requests.get( url= final_url)
df = pd.DataFrame(pd.Series(response.json()))
df = df.transpose()
return df
def getAllCases (self, conceptID:str = None) -> pd.DataFrame :
"""
Get the cases for the given conceptID.
* Sample URL: ~/concepts/patient/cases
Parameters
----------
:param conceptID : Name of the concept (default: self.__conceptID)
Returns
-------
DataFrame : The row is the case with unique "caseID" and columns are the concept attributes.
Note
----
_unknown_ : is the placeholder for empty values
"""
if conceptID is None:
conceptID = self.__conceptID
final_url = self.__base_url + '/concepts/' + conceptID + '/cases'
#print(final_url)
response = requests.get( url= final_url)
df = pd.DataFrame(response.json())
return df
def getSimilarCasesFromEphemeralCaseBaseWithContent (
self,
caseID:str,
ephemeralCaseIDs:List[str],
amalgamationFunctionID:str,
conceptID:str = None,
casebaseID:str = None,
k:int = None,
deci_precision:int=3
) -> pd.DataFrame:
"""
Get the cases for the given conceptID.
* Sample URL: ~/ephemeral/concepts/patient/casebases/casebase/amalgamationFunctions/LCA_var_no_lca_sim/retrievalByCaseIDWithContent?caseID=patient0&k=-1
Parameters
----------
:param caseID : Name of the concept
:param ephemeralCaseIDs : List of cases' IDs to be included in the ephemeral casebase
:param amalgamationFunctionID : Name of the amalgamation function
:param conceptID : Name of the concept (default: self.__conceptID)
:param casebaseID : Name of the casebase (default: self.__casebaseID)
:param k : Name of the retrieved cases (default: np.size(ephemeralCaseIDs))
:param deci_precision : The numeric precision value for similarity (default: 3)
Returns
-------
DataFrame : The row is the case with unique "caseID" and columns are the concept attributes.
Note
----
_unknown_ : is the placeholder for empty values
"""
if conceptID is None:
conceptID = self.__conceptID
if casebaseID is None:
casebaseID = self.__casebaseID
if k is None:
k = np.size(ephemeralCaseIDs)
final_url = self.__base_url \
+ '/ephemeral/concepts/' + conceptID \
+ '/casebases/' + casebaseID \
+ '/amalgamationFunctions/' + amalgamationFunctionID \
+ '/retrievalByCaseIDWithContent?caseID=' + caseID \
+ '&k=' + (k).__str__()
#print( final_url)
payload = ephemeralCaseIDs
response = requests.post( url= final_url, json=payload)
df = self.__rest_response_to_dataframe(response)
df.similarity = pd.to_numeric(df.similarity, errors='ignore')
df.similarity = df.similarity.round( decimals=deci_precision)
df = df.sort_values( by= _Constant.SIMILARITY, ascending=False)
return df
def getSimilarCasesFromEphemeralCaseBase (
self,
queryIDs:List[str],
ephemeralCaseIDs:List[str],
amalgamationFunctionID:str,
conceptID:str = None,
casebaseID:str = None,
k:int = None,
deci_precision:int=3
) -> pd.DataFrame:
"""
Get the cases for the given conceptID.
* Sample URL: ~/ephemeral/concepts/patient/casebases/casebase/amalgamationFunctions/LCA_variables/retrievalByCaseIDs?k=-1
* Body : "{ \"query_case_id_list\": [ \"patient0\", \"patient3\" ], \"casebase_case_id_list\": [ \"patient1\", \"patient1\" ]}"
Parameters
----------
:param queryIDs : The list of query cases' IDs
:param ephemeralCaseIDs : List of cases' IDs to be included in the ephemeral casebase
:param amalgamationFunctionID : Name of the amalgamation function
:param conceptID : Name of the concept (default: self.__conceptID)
:param casebaseID : Name of the casebase (default: self.__casebaseID)
:param k : Name of the retrieved cases (default: np.size(casebase_list))
:param deci_precision : The numeric precision value for similarity (default: 3)
Returns
-------
DataFrame : The rows are the cases from the ephemeral casebase with unique "caseID" and columns are are the query cases' IDs with similarity values.
"""
if conceptID is None:
conceptID = self.__conceptID
if casebaseID is None:
casebaseID = self.__casebaseID
if k is None:
k = np.size(ephemeralCaseIDs)
final_url = self.__base_url \
+ '/ephemeral/concepts/' + conceptID \
+ '/casebases/' + casebaseID \
+ '/amalgamationFunctions/' + amalgamationFunctionID \
+ '/retrievalByCaseIDs?k=' + (k).__str__()
#print( final_url)
payload = dict()
payload.update([('query_case_id_list', queryIDs), ('casebase_case_id_list', ephemeralCaseIDs)])
response = requests.post( url= final_url, json=payload)
df = pd.DataFrame(response.json()).round( deci_precision)
return df
def getEphemeralCaseBaseSelfSimilarity (
self,
ephemeralCaseIDs:List[str],
amalgamationFunctionID:str,
conceptID:str = None,
casebaseID:str = None,
k:int = None,
deci_precision:int=3
) -> pd.DataFrame:
"""
Get the Self-Similarity Matrix for an ephemeral casebase.
* Sample URL: ~/ephemeral/concepts/patient/casebases/casebase/amalgamationFunctions/LCA_variables/computeSelfSimlarity?k=-1
* Body : "[ \"patient0\", \"patient3\"]"
Parameters
----------
:param ephemeralCaseIDs : List of cases' IDs to be included in the ephemeral casebase.
:param amalgamationFunctionID : Name of the amalgamation function.
:param conceptID : Name of the concept (default: self.__conceptID)
:param casebaseID : Name of the casebase (default: self.__casebaseID)
:param k : Name of the retrieved cases (default: np.size(casebase_list))
:param deci_precision : The numeric precision value for similarity (default: 3)
Returns
-------
DataFrame : The rows are the cases from the ephemeral casebase with unique "caseID" and columns are are the query cases' IDs. NaN represents that a caseID was not compared for the similarity.
"""
if conceptID is None:
conceptID = self.__conceptID
if casebaseID is None:
casebaseID = self.__casebaseID
if k is None:
k = np.size(ephemeralCaseIDs)
final_url = self.__base_url \
+ '/ephemeral/concepts/' + conceptID \
+ '/casebases/' + casebaseID \
+ '/amalgamationFunctions/' + amalgamationFunctionID \
+ '/computeSelfSimlarity?k=' + (k).__str__()
#print( final_url)
payload = ephemeralCaseIDs
response = requests.post( url= final_url, json=payload)
df = pd.DataFrame(response.json()).round( deci_precision)
return df
def getCaseBaseSelfSimilarity (
self,
amalgamationFunctionID:str,
conceptID:str = None,
casebaseID:str = None,
k:int = -1,
deci_precision:int = 3
) -> pd.DataFrame:
"""
Get the Self-Similarity Matrix for the given casebase.
* Sample URL: ~/concepts/patient/casebases/casebase/computeSelfSimlarity?amalgamationFunctionID=LCA_variables&k=-1
Parameters
----------
:param amalgamationFunctionID : Name of the amalgamation function.
:param conceptID : Name of the concept (default: self.__conceptID)
:param casebaseID : Name of the casebase (default: self.__casebaseID)
:param k : Name of the retrieved cases (default: -1)
:param deci_precision : The numeric precision value for similarity (default: 3)
Returns
-------
DataFrame : The rows are the cases from the casebase with unique "caseID" and columns are are the query cases' IDs.
"""
if conceptID is None:
conceptID = self.__conceptID
if casebaseID is None:
casebaseID = self.__casebaseID
final_url = self.__base_url \
+ '/concepts/' + conceptID \
+ '/casebases/' + casebaseID \
+ '/computeSelfSimlarity?amalgamationFunctionID=' + amalgamationFunctionID \
+ '&k=' + (k).__str__()
#print( final_url)
response = requests.get( url= final_url)
df = pd.DataFrame(response.json()).round( deci_precision)
df = df[df.columns.sort_values()] # To rearrange colomns in the ascening order
return df
def getSimilarCasesByAttribute (
self,
amalgamationFunctionID:str,
attributeID:str,
value:Any,
conceptID:str = None,
casebaseID:str = None,
k:int =-1,
deci_precision:int=3
) -> pd.DataFrame:
"""
Retrieve similar cases by attributeID.
* Sample URL: ~/concepts/patient/casebases/casebase/amalgamationFunctions/LCA_variables/retrievalByAttribute?Symbol%20attribute%20name=body_main&k=-1&value=back
Parameters
----------
:param amalgamationFunctionID : Name of the amalgamation function
:param attributeID : Name of the attribute
:param value : Value of the attribute
:param conceptID : Name of the concept (default: self.__conceptID)
:param casebaseID : Name of the casebase (default: self.__casebaseID)
:param k : Name of the retrieved cases (default: -1, where -1 means all)
:param deci_precision : The numeric precision value for similarity (default: 3)
Returns
-------
DataFrame : The rows are the cases from the casebase with unique "caseID" and column is the similarity value.
"""
if conceptID is None:
conceptID = self.__conceptID
if casebaseID is None:
casebaseID = self.__casebaseID
final_url = self.__base_url \
+ '/concepts/'+conceptID \
+ '/casebases/'+casebaseID \
+ '/amalgamationFunctions/'+ amalgamationFunctionID \
+ '/retrievalByAttribute?Symbol%20attribute%20name='+attributeID \
+ '&k='+(k).__str__() \
+ '&value=' + value
#print( final_url)
response = requests.get( url= final_url)
df = pd.DataFrame(response.json()).round( deci_precision)
df = df.sort_values( by='similarCases', ascending=False)
df.index.name = _Constant.CASE_ID
df.columns = [_Constant.SIMILARITY]
return df
def getSimilarCasesByCaseID(
self,
caseID:str,
amalgamationFunctionID:str,
conceptID:str = None,
casebaseID:str = None,
k:int =-1,
deci_precision:int=3
) -> pd.DataFrame:
"""
Retrieve similar cases by caseID.
* Sample URL: ~/concepts/patient/casebases/casebase/amalgamationFunctions/LCA_variables/retrievalByCaseID?caseID=patient0&k=-1
Parameters
----------
:param caseID : The caseID of the queried case
:param amalgamationFunctionID : Name of the amalgamation function
:param conceptID : Name of the concept (default: self.__conceptID)
:param casebaseID : Name of the casebase (default: self.__casebaseID)
:param k : Name of the retrieved cases (default: -1, where -1 means all)
:param deci_precision : The numeric precision value for similarity (default: 3)
Returns
-------
DataFrame : The rows are the cases from the casebase with unique "caseID" and column is the similarity value.
"""
if conceptID is None:
conceptID = self.__conceptID
if casebaseID is None:
casebaseID = self.__casebaseID
final_url = self.__base_url \
+ '/concepts/'+conceptID \
+ '/casebases/'+casebaseID \
+ '/amalgamationFunctions/'+ amalgamationFunctionID \
+ '/retrievalByCaseID?caseID='+caseID \
+ '&k='+(k).__str__()
#print( final_url)
response = requests.get( url= final_url)
response_json = response.json()
df = pd.DataFrame(list(response_json.values()), index=response_json.keys())
df.index.name = _Constant.CASE_ID
df.columns = [_Constant.SIMILARITY]
df = df.round( deci_precision)
df = df.sort_values( by=_Constant.SIMILARITY, ascending=False)
return df
def getSimilarCasesByMultipleCaseIDs (
self,
caseIDs:List[str],
amalgamationFunctionID:str,
conceptID:str = None,
casebaseID:str = None,
k:int =-1,
deci_precision:int=3
) -> pd.DataFrame:
"""
Retrieve similar cases for multiple caseIDs.
* Sample URL: ~/concepts/patient/casebases/casebase/amalgamationFunctions/LCA_variables/retrievalByMultipleCaseIDs?k=1
Parameters
----------
:param caseIDs : The list of caseIDs for retrieval
:param amalgamationFunctionID : Name of the amalgamation function
:param conceptID : Name of the concept (default: self.__conceptID)
:param casebaseID : Name of the casebase (default: self.__casebaseID)
:param k : Name of the retrieved cases (default: -1, where -1 means all)
:param deci_precision : The numeric precision value for similarity (default: 3)
Returns
-------
DataFrame : The rows are the cases from the casebase with unique caseID, and the column are the queried caseIDs with their similarity values.
"""
if conceptID is None:
conceptID = self.__conceptID
if casebaseID is None:
casebaseID = self.__casebaseID
final_url = self.__base_url \
+ '/concepts/'+ conceptID \
+ '/casebases/'+ casebaseID \
+ '/amalgamationFunctions/'+ amalgamationFunctionID \
+ '/retrievalByMultipleCaseIDs?k=' + (k).__str__()
#print( final_url)
payload = caseIDs
response = requests.post( url= final_url, json=payload)