Skip to content

Commit

Permalink
Refactor scans_tagger.py for improved readability and maintainability
Browse files Browse the repository at this point in the history
  • Loading branch information
jomasator2 committed Apr 19, 2024
1 parent f52608f commit 5dc51b3
Showing 1 changed file with 42 additions and 32 deletions.
74 changes: 42 additions & 32 deletions xnat2mids/protocols/scans_tagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,83 +24,93 @@ def classification_by_min_max(self, dict_atrubutes):
# targetScan = pd.DataFrame.from_dict([dict_atrubutes])
# Verify to which scan group corresponds
Manufacturer = dict_atrubutes["Manufacturer"]
Manufacturers_model_name = dict_atrubutes["ManufacturerModelName"]
scaning_sequence = dict_atrubutes["ScanningSequence"]
sequence_variant = dict_atrubutes["SequenceVariant"]
scan_options = dict_atrubutes["ScanOptions"]
image_type = dict_atrubutes["ImageType"]
#image_type = dict_atrubutes["ImageType"]

series_description = dict_atrubutes["SeriesDescription"]
if series_description == ".": raise EmptyDataError
print(f"{json.dumps(Manufacturer)}")
if Manufacturer == "Agfa": raise EmptyDataError

print(f"{json.dumps(Manufacturers_model_name)}")
print(f"{json.dumps(scaning_sequence)}")
print(f"{json.dumps(sequence_variant)}")
print(f"{json.dumps(scan_options)}")
print(f"{json.dumps(image_type)}")
print(f'"{json.dumps(image_type)}"')
#scaning_sequence = scaning_sequence if type(scaning_sequence) is str else "\\".join(scaning_sequence)


table_protocol_M = self.table_protocols[[
any([True for s in json.loads(l) if s == Manufacturer])
for l in list(self.table_protocols["Manufacturer"])
]]
print("#" * 40, "table_protocol_M", "#" * 40)
print(table_protocol_M[["Protocol", "acq", "Manufacturer"]])
# print("#" * 40, "table_protocol_M", "#" * 40)
# print(table_protocol_M[["Protocol", "acq", "Manufacturer"]])

table_protocol_M_MMN = table_protocol_M[[
any([True for s in json.loads(l) if s == Manufacturers_model_name])
for l in list(table_protocol_M["ManufacturerModelName"])
]]
# print("#" * 40, "table_protocol_M_MMN", "#" * 40)
# print(table_protocol_M_MMN[["Protocol", "acq", "Manufacturer","ManufacturerModelName"]])

if table_protocol_M.empty: raise EmptyDataError
for l in list(table_protocol_M["ScanningSequence"]):
print(l)
print(json.loads(l))
table_protocol_M_SS = table_protocol_M[[
if table_protocol_M_MMN.empty: raise EmptyDataError

table_protocol_M_MMN_SS = table_protocol_M_MMN[[
any([True for s in json.loads(l) if s == scaning_sequence])
for l in list(table_protocol_M["ScanningSequence"])
for l in list(table_protocol_M_MMN["ScanningSequence"])
]]

print("#" * 40, "table_protocol_M_SS", "#" * 40)
print(table_protocol_M_SS[["Protocol", "acq", "Manufacturer", "ScanningSequence"]])
# print("#" * 40, "table_protocol_M_MMN_SS", "#" * 40)
# print(table_protocol_M_MMN_SS[["Protocol", "acq", "Manufacturer", "ManufacturerModelName"]])

table_protocol_M_SS_SV = table_protocol_M_SS[[
table_protocol_M_MMN_SS_SV = table_protocol_M_MMN_SS[[
any([True for s in json.loads(l) if s == sequence_variant])
for l in list(table_protocol_M_SS["SequenceVariant"])
for l in list(table_protocol_M_MMN_SS["SequenceVariant"])
]]

print("#" * 40, "table_protocol_M_SS_SV", "#" * 40)
print(table_protocol_M_SS_SV[["Protocol", "acq", "Manufacturer", "ScanningSequence"]])
# print("#" * 40, "table_protocol_M_MMN_SS_SV", "#" * 40)
# print(table_protocol_M_MMN_SS_SV[["Protocol", "acq", "Manufacturer", "ManufacturerModelName"]])

table_protocol_M_SS_SV_SO = table_protocol_M_SS_SV[[
table_protocol_M_MMN_SS_SV_SO = table_protocol_M_MMN_SS_SV[[
any([True for s in json.loads(l) if s == scan_options])
for l in list(table_protocol_M_SS_SV["ScanOptions"])
for l in list(table_protocol_M_MMN_SS_SV["ScanOptions"])
]]


print("#" * 40, "table_protocol_M_SS_VS_SO", "#" * 40)
print(table_protocol_M_SS_SV_SO[["Protocol", "acq", "Manufacturer", "ScanningSequence"]])
# print("#" * 40, "table_protocol_M_MMN_SS_VS_SO", "#" * 40)
# print(table_protocol_M_MMN_SS_SV_SO[["Protocol", "acq", "Manufacturer", "ManufacturerModelName"]])

for l in list(table_protocol_M_SS_SV_SO["ImageType"]):
print(l)
print(json.loads(l))
#for l in list(table_protocol_M_MMN_SS_SV_SO["ImageType"]):
#print(l)
#print(json.loads(l))

table_protocol_M_SS_SV_SO_IT = table_protocol_M_SS_SV_SO[[
table_protocol_M_MMN_SS_SV_SO_IT = table_protocol_M_MMN_SS_SV_SO[[
any([True for s in json.loads(l) if s == image_type])
for l in list(table_protocol_M_SS_SV_SO["ImageType"])
for l in list(table_protocol_M_MMN_SS_SV_SO["ImageType"])
]]

print("#" * 40, "table_protocol_M_SS_VS_SO_IT", "#" * 40)
print(table_protocol_M_SS_SV_SO_IT[["Protocol", "acq", "Manufacturer", "ScanningSequence"]])
# print("#" * 40, "table_protocol_M_MMN_SS_VS_SO_IT", "#" * 40)
# print(table_protocol_M_MMN_SS_SV_SO_IT[["Protocol", "acq", "Manufacturer"]])

table_protocols = table_protocol_M_SS_SV_SO_IT
table_protocols = table_protocol_M_MMN_SS_SV_SO_IT
# print("#"*40, "table_protocol_SS", "#"*40)
# print(table_protocol_SS)

if table_protocols.empty:
return ["n/a", "n/a", "n/a", "n/a", "n/a", "n/a", "n/a", "n/a"]

# print(f"{dict_atrubutes=}")
matrix = []
# print(list(dict_atrubutes.keys())[-5:])
adquisition_param_keys = list(dict_atrubutes.keys())[-5:-1]
adquisition_param_keys = list(dict_atrubutes.keys())[-6:-2]
for p in adquisition_param_keys:
distance = []
# print("p_value", dict_atrubutes[p], type(dict_atrubutes[p]))

p_value = float(dict_atrubutes[p]) # if dict_atrubutes[p] !="nan" else -1
for list_values in table_protocols[p]:
print(list_values)
min_ = np.amin(eval(list_values))
max_ = np.amax(eval(list_values))
if p_value >= min_ and p_value <= max_:
Expand Down

0 comments on commit 5dc51b3

Please sign in to comment.