Skip to content

Commit

Permalink
Remove broken upsampling method 'noised' (#186)
Browse files Browse the repository at this point in the history
This method was left from earlier testing and was not working anymore.
Removed here. Updated function signatures.

Closes #147.
  • Loading branch information
dweindl authored Feb 26, 2025
1 parent 86b16fb commit a4a4082
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 60 deletions.
37 changes: 2 additions & 35 deletions src/ccompass/MOP.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@


def upsample_condition(
stds: pd.DataFrame,
fract_full: pd.DataFrame,
fract_marker: pd.DataFrame,
method: Literal["none", "noised", "average", "noisedaverage"],
method: Literal["none", "average", "noisedaverage"],
noise_stds: float,
) -> tuple[pd.DataFrame, pd.DataFrame]:
"""Perform upsampling for the given condition.
Expand All @@ -67,30 +66,7 @@ def upsample_condition(
class_std_flat = class_std.values.flatten()

for i in range(class_difference):
if method == "noised":
sample = data_class.sample(n=1)
id_rnd = sample.index[0]
name_up = f"up_{k}_{id_rnd}"
k += 1

profile_rnd_flat = sample.values.flatten()
std_rnd = stds.loc[[id_rnd]]
std_rnd = std_rnd[~std_rnd.index.duplicated(keep="first")]
std_rnd_flat = std_rnd.values.flatten()
std_rnd_flat = np.tile(
std_rnd_flat,
int(profile_rnd_flat.size / std_rnd_flat.size),
)

nv = np.random.normal(
profile_rnd_flat,
noise_stds * std_rnd_flat,
size=sample.shape,
)
nv = np.where(nv > 1, 1, np.where(nv < 0, 0, nv))
profile_up = pd.DataFrame(nv, columns=sample.columns)

elif method == "average":
if method == "average":
sample = data_class.sample(n=3, replace=True)
name_up = f"up_{k}_{'_'.join(sample.index)}"
k += 1
Expand Down Expand Up @@ -147,7 +123,6 @@ def MOP_exec(
fract_full: dict[str, pd.DataFrame],
fract_marker: dict[str, pd.DataFrame],
fract_test: dict[str, pd.DataFrame],
stds: dict[str, pd.DataFrame],
nn_params: NeuralNetworkParametersModel,
max_processes: int = 1,
) -> dict[str, XYZ_Model]:
Expand Down Expand Up @@ -211,7 +186,6 @@ def update_progress(
fract_full,
fract_marker,
fract_test,
stds,
nn_params,
max_processes,
progress_queue,
Expand Down Expand Up @@ -243,7 +217,6 @@ def multi_organelle_prediction(
fract_full: dict[str, pd.DataFrame],
fract_marker: dict[str, pd.DataFrame],
fract_test: dict[str, pd.DataFrame],
stds: dict[str, pd.DataFrame],
nn_params: NeuralNetworkParametersModel,
max_processes: int = 1,
progress_queue: mp.Queue = None,
Expand Down Expand Up @@ -282,7 +255,6 @@ def multi_organelle_prediction(
fract_full[condition],
fract_marker[condition],
fract_test[condition],
stds.get(condition),
nn_params,
logger,
progress_queue,
Expand Down Expand Up @@ -327,7 +299,6 @@ def execute_round_wrapper(args):
fract_full,
fract_marker,
fract_test,
stds,
nn_params,
logger,
progress_queue,
Expand All @@ -345,7 +316,6 @@ def execute_round_wrapper(args):
fract_full,
fract_marker,
fract_test,
stds,
nn_params,
sub_logger,
round_id,
Expand All @@ -360,7 +330,6 @@ def execute_round(
fract_full: pd.DataFrame,
fract_marker: pd.DataFrame,
fract_test: pd.DataFrame,
stds: pd.DataFrame,
nn_params: NeuralNetworkParametersModel,
logger: logging.Logger,
round_id: str,
Expand All @@ -378,7 +347,6 @@ def execute_round(
if nn_params.upsampling:
logger.info("Upsampling")
fract_marker_up, fract_full_up = upsample_condition(
stds,
fract_full,
fract_marker,
method=nn_params.upsampling_method,
Expand Down Expand Up @@ -415,7 +383,6 @@ def execute_round(
]
logger.info("Upsampling after SVM-filtering...")
fract_marker_up, fract_full_up = upsample_condition(
stds,
fract_full,
fract_marker_filtered,
method=nn_params.upsampling_method,
Expand Down
1 change: 0 additions & 1 deletion src/ccompass/_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ def do_test_run(max_procs: int = None):
sess.fract_full,
sess.fract_marker,
sess.fract_test,
sess.fract_std,
sess.NN_params,
max_procs,
)
Expand Down
6 changes: 3 additions & 3 deletions src/ccompass/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ class NeuralNetworkParametersModel(BaseModel):
#: Perform upsampling?
upsampling: bool = True
#: Method for upsampling
upsampling_method: Literal[
"none", "noised", "average", "noisedaverage"
] = "noisedaverage"
upsampling_method: Literal["none", "average", "noisedaverage"] = (
"noisedaverage"
)
#: Noise level for upsampling (standard deviations)
upsampling_noise: float = 2
#: Perform SVM filtering?
Expand Down
19 changes: 0 additions & 19 deletions src/ccompass/main_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1443,32 +1443,13 @@ def _handle_match_markers(self, values: dict):

def _handle_training(self, key: str):
"""Handle click on "Train C-COMPASS!" button."""
# FIXME: `stds` is not in the format expected for upsampling
if key == IDENTIFIER:
stds = self.model.fract_std
else:
# Add the required column and set as index
conditions_std = [
x for x in self.model.fract_conditions if x != KEEP
]
stds = {}
for condition in conditions_std:
stds[condition] = pd.merge(
self.model.fract_std["class"][condition],
self.model.fract_info[key],
left_index=True,
right_index=True,
how="left",
).set_index(key)

with wait_cursor(self.main_window):
from .MOP import MOP_exec

self.model.learning_xyz = MOP_exec(
self.model.fract_full,
self.model.fract_marker,
self.model.fract_test,
stds,
self.model.NN_params,
max_processes=self.app_settings.max_processes,
)
Expand Down
2 changes: 1 addition & 1 deletion src/ccompass/training_parameters_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _create_window(nn_params: NeuralNetworkParametersModel) -> sg.Window:
enable_events=True,
),
sg.Combo(
["noised", "average", "noisedaverage"],
["average", "noisedaverage"],
default_value=nn_params.upsampling_method,
key="--upsampling_method--",
size=(23, 1),
Expand Down
1 change: 0 additions & 1 deletion tests/test_full_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ def test_full():
sess.fract_full,
sess.fract_marker,
sess.fract_test,
sess.fract_std,
sess.NN_params,
max_procs,
)
Expand Down

0 comments on commit a4a4082

Please sign in to comment.