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
| import highdicom as hd
import pandas as pd
from tkinter import Tk, Button, N, W, E, S, StringVar
from tkinter import ttk
import tkinter as tk
global df_s
mesure_choisie = []
valeur = []
cb = []
cb_v = []
opt = []
# Structured report file
repertoire = "C:\\Dfi_5012\\Dcm\\catherine"
img = "I000000024.dcm" # SR
path = repertoire + '\\' + img
def convert_sr(path):
global mesure
global df_s
global vari
# Read the file with Highdicom
sr = hd.sr.srread(path)
# Temp file to rescue only the name, measure and unity
outputfile = repertoire + '\\text.txt'
outputfile_mod = repertoire + '\\textmod.txt'
outputfile_final = repertoire + '\\textfinal.txt'
# Put the SR into a txt file
with open(outputfile, 'w+') as f:
f.write('%s\n' % str(sr))
# supress lines without interesting information
fichier_source = open(outputfile, "r")
fichier_destination = open(outputfile_mod, "w")
for ligne in fichier_source:
temp = str(ligne).lstrip()
if temp[:12] == "(0008, 0104)":
fichier_destination.write(temp)
elif temp[:12] == "(0040, a30a)":
fichier_destination.write(temp)
fichier_source.close()
fichier_destination.close()
# Format datas to be human being readable
fichier_source = open(outputfile_mod, "r")
fichier_destination = open(outputfile_final, "w")
lines = fichier_source.readlines()
for i in range(1, len(lines)):
if lines[i][:12] == "(0040, a30a)":
tmp = lines[i-2].split("'")
fichier_destination.write(tmp[1])
fichier_destination.write(",")
tmp = lines[i].split("'")
fichier_destination.write(tmp[1])
fichier_destination.write(",")
tmp = lines[i-1].split("'")
fichier_destination.write(tmp[1])
fichier_destination.write("\n")
fichier_source.close()
fichier_destination.close()
df = pd.read_table(outputfile_final, sep=",", header=None)
df = df.drop_duplicates()
df_s = df.sort_values([0])
df_s.reset_index(inplace=True, drop=True)
# GUI Tkinter
mesure = Tk()
mesure.title("Choix des mesures à incorporer dans le pdf")
mainframe = ttk.Frame(mesure, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mesure.columnconfigure(0, weight=1)
mesure.rowconfigure(0, weight=1)
# show text results with modulo 4 in order organize by 4 columns
col = 1
lig = 0
# color = "green"
sti = (N, W, E, S)
for i in range(0, len(df)):
mod = i % 4
if mod == 0:
col = 1
lig = lig + 1
if mod == 1:
col = 2
if mod == 2:
col = 3
if mod == 3:
col = 4
temp = str(df_s.loc[i, 0:2].to_string(index=False))
cb_v.append(tk.StringVar())
off_value = ""
cb.append(tk.Checkbutton(mainframe, text=temp, onvalue=temp,
offvalue=off_value, variable=cb_v[i],
command=chkbox_checked))
opt.append(off_value)
cb[i].grid(column=col, row=lig, sticky=sti)
btn_valide = Button(mainframe, text="Valider les mesures",
command=lambda: ecrit_mesure(repertoire),
bg='red', height=5, width=30)
btn_valide.grid(column=4, row=lig+1, sticky=sti)
for child in mainframe.winfo_children():
child.grid_configure(padx=5, pady=5, ipadx=5, ipady=5)
mesure.mainloop()
def chkbox_checked():
for i, item in enumerate(cb):
opt[i] = (cb_v[i].get())
print(opt)
def ecrit_mesure(chemin):
for itemp in opt:
itempo = itemp.split(',')
for itempoo in itempo:
itempo1 = itempoo.split('\n')
try:
itempo2 = itempo1[0] + ' ' + itempo1[1].lstrip() + ' ' + itempo1[2].lstrip()
mesure_choisie.append(itempo2)
except:
print("")
fichier_mesure = chemin + "\\mesureselect.txt"
with open(fichier_mesure, 'w+')as mf:
for j in range(0, len(mesure_choisie)):
temp = mesure_choisie[j]
mf.write('%s\n' % temp)
mesure.destroy()
convert_sr(path) |
Partager