From 97525435e8d392d275961a6071f18e0c9b829315 Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Mon, 20 Jan 2025 10:48:27 +0100 Subject: [PATCH 1/2] Extract total proteome dialog --- src/ccompass/CCMPS.py | 6 +- src/ccompass/PPMS.py | 88 ----------------- .../total_proteome_parameters_dialog.py | 97 +++++++++++++++++++ 3 files changed, 100 insertions(+), 91 deletions(-) create mode 100644 src/ccompass/total_proteome_parameters_dialog.py diff --git a/src/ccompass/CCMPS.py b/src/ccompass/CCMPS.py index f63a7ba..1e16011 100644 --- a/src/ccompass/CCMPS.py +++ b/src/ccompass/CCMPS.py @@ -1523,9 +1523,9 @@ def run(self): self.model.tp_identifiers, ) elif event == "-tp_parameters-": - self.model.tp_preparams = PPMS.PPMS_exec_TP( - self.model.tp_preparams - ) + from .total_proteome_parameters_dialog import show_dialog + + self.model.tp_preparams = show_dialog(self.model.tp_preparams) elif event == "-tp_reset-": sure = sg.popup_yes_no( "Reset TotalProteome Pre-Processing? " diff --git a/src/ccompass/PPMS.py b/src/ccompass/PPMS.py index a808789..b7b8ee3 100644 --- a/src/ccompass/PPMS.py +++ b/src/ccompass/PPMS.py @@ -67,94 +67,6 @@ def NN_default(): return params_default -def PPMS_exec_TP(params_old): - tp_params = copy.deepcopy(params_old) - - if params_old["imputation"] == "normal": - is_normal = True - else: - is_normal = False - if params_old["imputation"] == "constant": - is_constant = True - else: - is_constant = False - - layout_TPPM = [ - [ - sg.Text("found in at least"), - sg.Spin( - values=(list(range(1, 10))), - size=(10, 2), - key="--tp_mincount--", - disabled=False, - enable_events=False, - readonly=True, - initial_value=params_old["minrep"], - text_color="black", - ), - ], - [ - sg.Text("Imputation of Missing Values:"), - sg.Checkbox( - "by normal distribution", - key="--normal--", - enable_events=True, - disabled=False, - default=is_normal, - ), - sg.Checkbox( - "by constant (0)", - key="--constant--", - enable_events=True, - disabled=False, - default=is_constant, - ), - ], - [ - sg.Button( - "Accept", - button_color="dark green", - key="--TPPM_accept--", - enable_events=True, - disabled=False, - ), - sg.Button( - "Cancel", - key="--TPPM_cancel--", - disabled=False, - enable_events=True, - button_color="black", - ), - ], - ] - - window_TPPM = sg.Window("TP Parameters", layout_TPPM, size=(500, 100)) - - while True: - event_TPPM, values_TPPM = window_TPPM.read() - - if event_TPPM == "--normal--": - tp_params["imputation"] = "normal" - window_TPPM["--normal--"].Update(value=True) - window_TPPM["--constant--"].Update(value=False) - if event_TPPM == "--constant--": - tp_params["imputation"] = "constant" - window_TPPM["--constant--"].Update(value=True) - window_TPPM["--normal--"].Update(value=False) - - if event_TPPM == sg.WIN_CLOSED or event_TPPM == "--TPPM_cancel--": - params = copy.deepcopy(params_old) - window_TPPM.close() - break - - if event_TPPM == "--TPPM_accept--": - params = copy.deepcopy(tp_params) - window_TPPM.close() - break - - return params - - def PPMS_exec_NN(params_old): NN_params = copy.deepcopy(params_old) diff --git a/src/ccompass/total_proteome_parameters_dialog.py b/src/ccompass/total_proteome_parameters_dialog.py new file mode 100644 index 0000000..a360678 --- /dev/null +++ b/src/ccompass/total_proteome_parameters_dialog.py @@ -0,0 +1,97 @@ +""" +The total proteome parameters dialog. + +This dialog allows the user to specify the parameters for the total proteome +data processing. +""" + +import copy + +import FreeSimpleGUI as sg + + +def _create_window(params_old) -> sg.Window: + """Create the total proteome parameters dialog window.""" + is_normal = params_old["imputation"] == "normal" + is_constant = params_old["imputation"] == "constant" + + layout_TPPM = [ + [ + sg.Text("found in at least"), + sg.Spin( + values=(list(range(1, 10))), + size=(10, 2), + key="--tp_mincount--", + disabled=False, + enable_events=False, + readonly=True, + initial_value=params_old["minrep"], + text_color="black", + ), + ], + [ + sg.Text("Imputation of Missing Values:"), + sg.Checkbox( + "by normal distribution", + key="--normal--", + enable_events=True, + disabled=False, + default=is_normal, + ), + sg.Checkbox( + "by constant (0)", + key="--constant--", + enable_events=True, + disabled=False, + default=is_constant, + ), + ], + [ + sg.Button( + "Accept", + button_color="dark green", + key="--TPPM_accept--", + enable_events=True, + disabled=False, + ), + sg.Button( + "Cancel", + key="--TPPM_cancel--", + disabled=False, + enable_events=True, + button_color="black", + ), + ], + ] + + return sg.Window("TP Parameters", layout_TPPM, size=(500, 100)) + + +def show_dialog(params_old) -> dict: + """ + Show the total proteome parameters dialog. + """ + tp_params = copy.deepcopy(params_old) + window_TPPM = _create_window(tp_params) + + while True: + event, values = window_TPPM.read() + + if event == sg.WIN_CLOSED or event == "--TPPM_cancel--": + tp_params = params_old + break + + if event == "--TPPM_accept--": + break + + if event == "--normal--": + tp_params["imputation"] = "normal" + window_TPPM["--normal--"].Update(value=True) + window_TPPM["--constant--"].Update(value=False) + elif event == "--constant--": + tp_params["imputation"] = "constant" + window_TPPM["--constant--"].Update(value=True) + window_TPPM["--normal--"].Update(value=False) + + window_TPPM.close() + return tp_params From f79dc5e65dbfc9c0c2bf7c3872c7921fff3680e4 Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Mon, 20 Jan 2025 10:59:24 +0100 Subject: [PATCH 2/2] Extract neural network parameters dialog --- src/ccompass/CCMPS.py | 4 +- src/ccompass/PPMS.py | 310 --------------------- src/ccompass/training_parameters_dialog.py | 288 +++++++++++++++++++ 3 files changed, 291 insertions(+), 311 deletions(-) create mode 100644 src/ccompass/training_parameters_dialog.py diff --git a/src/ccompass/CCMPS.py b/src/ccompass/CCMPS.py index 1e16011..e1c8222 100644 --- a/src/ccompass/CCMPS.py +++ b/src/ccompass/CCMPS.py @@ -1630,7 +1630,9 @@ def run(self): # window_CCMPS['-classification_SVM-'].Update(disabled = True) elif event == "-classification_parameters-": - self.model.NN_params = PPMS.PPMS_exec_NN(self.model.NN_params) + from .training_parameters_dialog import show_dialog + + self.model.NN_params = show_dialog(self.model.NN_params) elif event == "-classification_MOP-": self._handle_training(key=values["-marker_fractkey-"]) diff --git a/src/ccompass/PPMS.py b/src/ccompass/PPMS.py index b7b8ee3..8838857 100644 --- a/src/ccompass/PPMS.py +++ b/src/ccompass/PPMS.py @@ -1,9 +1,5 @@ """Preparameters""" -import copy - -import FreeSimpleGUI as sg - def fract_default(): params_default = { @@ -67,312 +63,6 @@ def NN_default(): return params_default -def PPMS_exec_NN(params_old): - NN_params = copy.deepcopy(params_old) - - if "adam" in params_old["optimizers"]: - isadam = True - else: - isadam = False - if "rmsprop" in params_old["optimizers"]: - isrms = True - else: - isrms = False - if "sgd" in params_old["optimizers"]: - issgd = True - else: - issgd = False - if params_old["class_loss"] == "mean_squared_error": - loss_default = "mean squared error" - elif params_old["class_loss"] == "binary_crossentropy": - loss_default = "binary cross-entropy" - - layout_NNP = [ - [ - sg.Checkbox( - "upsampling\t\t", - default=params_old["upsampling"], - key="--upsampling--", - disabled=False, - enable_events=True, - ), - sg.Combo( - ["noised", "average", "noisedaverage"], - default_value=params_old["upsampling_method"], - key="--upsampling_method--", - size=(23, 1), - disabled=not params_old["upsampling"], - enable_events=True, - readonly=True, - ), - ], - [ - sg.Text("\t\t\t\tnoise:\t"), - sg.Spin( - list(range(0, 6, 1)), - initial_value=params_old["upsampling_noise"], - size=(10, 1), - key="--noise--", - disabled=not params_old["upsampling"], - enable_events=True, - readonly=True, - text_color="black", - ), - sg.Text("* std"), - ], - [ - sg.Checkbox( - "SVM-filter", - default=params_old["svm_filter"], - key="--svm_filter--", - disabled=False, - enable_events=True, - ) - ], - [ - sg.Text("dividing-step for profile mixing:\t"), - sg.Spin( - ["-", 2, 4, 5, 10], - initial_value=params_old["mixed_part"], - size=(15, 1), - key="--mixstep--", - disabled=False, - enable_events=True, - readonly=True, - text_color="black", - ), - ], - [sg.HSep()], - [ - sg.Text("NN optimization method:\t\t"), - sg.Combo( - ["long", "short"], - default_value=params_old["NN_optimization"], - size=(25, 1), - key="--optimization_method--", - disabled=False, - enable_events=True, - readonly=True, - ), - ], - [ - sg.Text("dense layer activation:\t\t"), - sg.Combo( - ["relu", "leakyrelu"], - default_value=params_old["NN_activation"], - size=(25, 1), - key="--dense_activation--", - disabled=False, - enable_events=True, - readonly=True, - ), - ], - [ - sg.Text("output activation:\t\t\t"), - sg.Combo( - ["linear", "sigmoid", "softmax"], - default_value=params_old["class_activation"], - size=(25, 1), - key="--out_activation--", - disabled=False, - enable_events=True, - readonly=True, - ), - ], - [ - sg.Text("loss function:\t\t\t"), - sg.Combo( - ["mean squared error", "binary cross-entropy"], - default_value=loss_default, - size=(25, 1), - key="--loss--", - disabled=False, - enable_events=True, - readonly=True, - ), - ], - [ - sg.Text("optimizers:\t\t\t"), - sg.Checkbox( - "adam", - default=isadam, - key="--adam--", - disabled=False, - enable_events=True, - ), - sg.Checkbox( - "rmsprop", - default=isrms, - key="--rmsprop--", - disabled=False, - enable_events=True, - ), - sg.Checkbox( - "sgd", - default=issgd, - key="--sgd--", - disabled=False, - enable_events=True, - ), - ], - [ - sg.Text("max. training epochs:\t\t\t"), - sg.Spin( - list(range(10, 110, 10)), - initial_value=params_old["NN_epochs"], - size=(15, 1), - key="--epochs--", - disabled=False, - enable_events=True, - readonly=True, - text_color="black", - ), - ], - [ - sg.Text("tuning runs:\t\t\t"), - sg.Spin( - list(range(1, 11, 1)), - initial_value=params_old["rounds"], - size=(15, 1), - key="--rounds--", - disabled=False, - enable_events=True, - readonly=True, - text_color="black", - ), - ], - [ - sg.Text("ensemble runs:\t\t\t"), - sg.Spin( - list(range(5, 55, 5)), - initial_value=params_old["subrounds"], - size=(15, 1), - key="--subrounds--", - disabled=False, - enable_events=True, - readonly=True, - text_color="black", - ), - ], - [sg.HSep()], - [ - sg.Text("TP filter cut-off:\t\t\t"), - sg.Spin( - list(range(0, 100, 1)), - initial_value=params_old["reliability"], - size=(15, 1), - key="--filter_threshold--", - disabled=False, - enable_events=True, - readonly=True, - text_color="black", - ), - ], - [sg.HSep()], - [ - sg.Button( - "Accept", - key="--NNP_accept--", - disabled=False, - enable_events=True, - button_color="dark green", - ), - sg.Button( - "Cancel", - key="--NNP_cancel--", - disabled=False, - enable_events=True, - button_color="black", - ), - ], - ] - - window_NNP = sg.Window("NN Parameters", layout_NNP, size=(470, 420)) - - while True: - event_NNP, values_NNP = window_NNP.read() - - if event_NNP == "--upsampling--": - NN_params["upsampling"] = values_NNP["--upsampling--"] - window_NNP["--upsampling_method--"].Update( - disabled=not values_NNP["--upsampling--"] - ) - window_NNP["--noise--"].Update( - disabled=not values_NNP["--upsampling--"] - ) - - if event_NNP == "--upsampling_method--": - NN_params["upsampling_method"] = values_NNP[ - "--upsampling_method--" - ] - - if event_NNP == "--noise--": - NN_params["upsampling_noise"] = values_NNP["--noise--"] - - if event_NNP == "--svm_filter--": - NN_params["svm_filter"] = values_NNP["--svm_filter--"] - - if event_NNP == "--mixstep--": - NN_params["mixed_part"] = values_NNP["--mixstep--"] - - if event_NNP == "--optimization_method--": - NN_params["NN_optimization"] = values_NNP[ - "--optimization_method--" - ] - - if event_NNP == "--dense_activation--": - NN_params["NN_activation"] = values_NNP["--dense_activation--"] - - if event_NNP == "--out_activation--": - NN_params["class_activation"] = values_NNP["--out_activation--"] - - if event_NNP == "--loss--": - if values_NNP["--loss--"] == "mean squared error": - NN_params["class_loss"] = "mean_squared_error" - elif values_NNP["--loss--"] == "binary cross-entropy": - NN_params["class_loss"] = "binary_crossentropy" - - if event_NNP == "--adam--": - if values_NNP["--adam--"] == True: - NN_params["optimizers"].append("adam") - elif values_NNP["--adam--"] == False: - NN_params["optimizers"].remove("adam") - if event_NNP == "--rmsprop--": - if values_NNP["--rmsprop--"] == True: - NN_params["optimizers"].append("rmsprop") - elif values_NNP["--rmsprop--"] == False: - NN_params["optimizers"].remove("rmsprop") - if event_NNP == "--sgd--": - if values_NNP["--sgd--"] == True: - NN_params["optimizers"].append("sgd") - elif values_NNP["--sgd--"] == False: - NN_params["optimizers"].remove("sgd") - - if event_NNP == "--epochs--": - NN_params["NN_epochs"] = values_NNP["--epochs--"] - - if event_NNP == "--rounds--": - NN_params["rounds"] = values_NNP["--rounds--"] - - if event_NNP == "--subrounds--": - NN_params["subrounds"] = values_NNP["--subrounds--"] - - if event_NNP == "--filter_threshold--": - NN_params["reliability"] = values_NNP["--filter_threshold--"] - - if event_NNP == sg.WIN_CLOSED or event_NNP == "--NNP_cancel--": - params = copy.deepcopy(params_old) - window_NNP.close() - break - - if event_NNP == "--NNP_accept--": - params = copy.deepcopy(NN_params) - window_NNP.close() - break - - return params - - # def NN_default (): # params_default = { # 'upsampling' : True, diff --git a/src/ccompass/training_parameters_dialog.py b/src/ccompass/training_parameters_dialog.py new file mode 100644 index 0000000..d6b93f5 --- /dev/null +++ b/src/ccompass/training_parameters_dialog.py @@ -0,0 +1,288 @@ +"""The neural network training parameters dialog.""" + +import copy + +import FreeSimpleGUI as sg + + +def _create_window(nn_params: dict) -> sg.Window: + """Create the window for the neural network training parameters dialog.""" + isadam = "adam" in nn_params["optimizers"] + isrms = "rmsprop" in nn_params["optimizers"] + issgd = "sgd" in nn_params["optimizers"] + + if nn_params["class_loss"] == "mean_squared_error": + loss_default = "mean squared error" + elif nn_params["class_loss"] == "binary_crossentropy": + loss_default = "binary cross-entropy" + else: + raise ValueError(f"Unknown loss function: {nn_params['class_loss']}") + + layout = [ + [ + sg.Checkbox( + "upsampling\t\t", + default=nn_params["upsampling"], + key="--upsampling--", + disabled=False, + enable_events=True, + ), + sg.Combo( + ["noised", "average", "noisedaverage"], + default_value=nn_params["upsampling_method"], + key="--upsampling_method--", + size=(23, 1), + disabled=not nn_params["upsampling"], + enable_events=True, + readonly=True, + ), + ], + [ + sg.Text("\t\t\t\tnoise:\t"), + sg.Spin( + list(range(0, 6, 1)), + initial_value=nn_params["upsampling_noise"], + size=(10, 1), + key="--noise--", + disabled=not nn_params["upsampling"], + enable_events=True, + readonly=True, + text_color="black", + ), + sg.Text("* std"), + ], + [ + sg.Checkbox( + "SVM-filter", + default=nn_params["svm_filter"], + key="--svm_filter--", + disabled=False, + enable_events=True, + ) + ], + [ + sg.Text("dividing-step for profile mixing:\t"), + sg.Spin( + ["-", 2, 4, 5, 10], + initial_value=nn_params["mixed_part"], + size=(15, 1), + key="--mixstep--", + disabled=False, + enable_events=True, + readonly=True, + text_color="black", + ), + ], + [sg.HSep()], + [ + sg.Text("NN optimization method:\t\t"), + sg.Combo( + ["long", "short"], + default_value=nn_params["NN_optimization"], + size=(25, 1), + key="--optimization_method--", + disabled=False, + enable_events=True, + readonly=True, + ), + ], + [ + sg.Text("dense layer activation:\t\t"), + sg.Combo( + ["relu", "leakyrelu"], + default_value=nn_params["NN_activation"], + size=(25, 1), + key="--dense_activation--", + disabled=False, + enable_events=True, + readonly=True, + ), + ], + [ + sg.Text("output activation:\t\t\t"), + sg.Combo( + ["linear", "sigmoid", "softmax"], + default_value=nn_params["class_activation"], + size=(25, 1), + key="--out_activation--", + disabled=False, + enable_events=True, + readonly=True, + ), + ], + [ + sg.Text("loss function:\t\t\t"), + sg.Combo( + ["mean squared error", "binary cross-entropy"], + default_value=loss_default, + size=(25, 1), + key="--loss--", + disabled=False, + enable_events=True, + readonly=True, + ), + ], + [ + sg.Text("optimizers:\t\t\t"), + sg.Checkbox( + "adam", + default=isadam, + key="--adam--", + disabled=False, + enable_events=True, + ), + sg.Checkbox( + "rmsprop", + default=isrms, + key="--rmsprop--", + disabled=False, + enable_events=True, + ), + sg.Checkbox( + "sgd", + default=issgd, + key="--sgd--", + disabled=False, + enable_events=True, + ), + ], + [ + sg.Text("max. training epochs:\t\t\t"), + sg.Spin( + list(range(10, 110, 10)), + initial_value=nn_params["NN_epochs"], + size=(15, 1), + key="--epochs--", + disabled=False, + enable_events=True, + readonly=True, + text_color="black", + ), + ], + [ + sg.Text("tuning runs:\t\t\t"), + sg.Spin( + list(range(1, 11, 1)), + initial_value=nn_params["rounds"], + size=(15, 1), + key="--rounds--", + disabled=False, + enable_events=True, + readonly=True, + text_color="black", + ), + ], + [ + sg.Text("ensemble runs:\t\t\t"), + sg.Spin( + list(range(5, 55, 5)), + initial_value=nn_params["subrounds"], + size=(15, 1), + key="--subrounds--", + disabled=False, + enable_events=True, + readonly=True, + text_color="black", + ), + ], + [sg.HSep()], + [ + sg.Text("TP filter cut-off:\t\t\t"), + sg.Spin( + list(range(0, 100, 1)), + initial_value=nn_params["reliability"], + size=(15, 1), + key="--filter_threshold--", + disabled=False, + enable_events=True, + readonly=True, + text_color="black", + ), + ], + [sg.HSep()], + [ + sg.Button( + "Accept", + key="--NNP_accept--", + disabled=False, + enable_events=True, + button_color="dark green", + ), + sg.Button( + "Cancel", + key="--NNP_cancel--", + disabled=False, + enable_events=True, + button_color="black", + ), + ], + ] + + return sg.Window("NN Parameters", layout, size=(470, 420)) + + +def show_dialog(params_old: dict) -> dict: + nn_params = copy.deepcopy(params_old) + window = _create_window(nn_params) + + while True: + event, values = window.read() + + if event == sg.WIN_CLOSED or event == "--NNP_cancel--": + nn_params = params_old + break + + if event == "--NNP_accept--": + break + + if event == "--upsampling--": + nn_params["upsampling"] = values["--upsampling--"] + window["--upsampling_method--"].Update( + disabled=not values["--upsampling--"] + ) + window["--noise--"].Update(disabled=not values["--upsampling--"]) + elif event == "--upsampling_method--": + nn_params["upsampling_method"] = values["--upsampling_method--"] + elif event == "--noise--": + nn_params["upsampling_noise"] = values["--noise--"] + elif event == "--svm_filter--": + nn_params["svm_filter"] = values["--svm_filter--"] + elif event == "--mixstep--": + nn_params["mixed_part"] = values["--mixstep--"] + elif event == "--optimization_method--": + nn_params["NN_optimization"] = values["--optimization_method--"] + elif event == "--dense_activation--": + nn_params["NN_activation"] = values["--dense_activation--"] + elif event == "--out_activation--": + nn_params["class_activation"] = values["--out_activation--"] + elif event == "--loss--": + if values["--loss--"] == "mean squared error": + nn_params["class_loss"] = "mean_squared_error" + elif values["--loss--"] == "binary cross-entropy": + nn_params["class_loss"] = "binary_crossentropy" + elif event == "--adam--": + if values["--adam--"] == True: + nn_params["optimizers"].append("adam") + elif values["--adam--"] == False: + nn_params["optimizers"].remove("adam") + elif event == "--rmsprop--": + if values["--rmsprop--"] == True: + nn_params["optimizers"].append("rmsprop") + elif values["--rmsprop--"] == False: + nn_params["optimizers"].remove("rmsprop") + elif event == "--sgd--": + if values["--sgd--"] == True: + nn_params["optimizers"].append("sgd") + elif values["--sgd--"] == False: + nn_params["optimizers"].remove("sgd") + elif event == "--epochs--": + nn_params["NN_epochs"] = values["--epochs--"] + elif event == "--rounds--": + nn_params["rounds"] = values["--rounds--"] + elif event == "--subrounds--": + nn_params["subrounds"] = values["--subrounds--"] + elif event == "--filter_threshold--": + nn_params["reliability"] = values["--filter_threshold--"] + + window.close() + return nn_params