Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
leoschwarz committed Jun 17, 2024
1 parent 8d515d0 commit 270d922
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,6 @@ def _find_local_maxima_indices(self, mz_arr: NDArray[float], int_arr: NDArray[fl
)
return local_maxima_indices


# TODO the interpolation could be much faster, if it were implemented in numba for our specific case of 3 points,
# since in general the scipy library will do everything much moe general than is actually required.
8 changes: 5 additions & 3 deletions src/depiction_targeted_preproc/example_compare/run_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ def prepare_tasks(input_imzml_path: Path, work_dir: Path) -> list[Path]:
requested_files = get_all_output_files(folders)

combined_dir = work_dir / input_imzml_path.stem
exp_files = [combined_dir / "exp_compare_cluster_stats.pdf",
combined_dir / "exp_plot_compare_peak_density.pdf",
combined_dir / "exp_plot_map_comparison.pdf"]
exp_files = [
combined_dir / "exp_compare_cluster_stats.pdf",
combined_dir / "exp_plot_compare_peak_density.pdf",
combined_dir / "exp_plot_map_comparison.pdf",
]
return requested_files + exp_files


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ def exp_plot_map_comparison(

plt.savefig(output_pdf_path, bbox_inches="tight")


if __name__ == "__main__":
typer.run(exp_plot_map_comparison)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np
import typer
import xarray
from hdbscan.flat import (HDBSCAN_flat)
from hdbscan.flat import HDBSCAN_flat
from loguru import logger
from sklearn.preprocessing import StandardScaler
from typer import Option
Expand All @@ -30,9 +30,8 @@ def cluster_dbscan(input_netcdf_path: Annotated[Path, Option()], output_netcdf_p
data_scaled = scaler.transform(reduced_data.values)

try:
clusterer = HDBSCAN_flat(data_scaled,
n_clusters=10)
#min_cluster_size=math.ceil(0.02 * data_scaled.shape[0]))
clusterer = HDBSCAN_flat(data_scaled, n_clusters=10)
# min_cluster_size=math.ceil(0.02 * data_scaled.shape[0]))
clusters = clusterer.labels_
except IndexError:
logger.error("No clusters found")
Expand Down
6 changes: 2 additions & 4 deletions src/depiction_targeted_preproc/workflow/proc/cluster_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def compute_metrics(cluster_data: np.ndarray, cluster_coords: np.ndarray) -> dic
"PAS": pas,
"Silhouette": silhouette,
"Davies-Bouldin": davies_bouldin,
"Calinski-Harabasz": calinski_harabasz
"Calinski-Harabasz": calinski_harabasz,
}


Expand All @@ -71,9 +71,7 @@ def cluster_stats(input_netcdf_path: Annotated[Path, Option()], output_csv_path:

metrics = compute_metrics(cluster_data, cluster_coords)

metrics_df = pl.DataFrame(
{"metric": list(metrics.keys()), "value": list(metrics.values())}
)
metrics_df = pl.DataFrame({"metric": list(metrics.keys()), "value": list(metrics.values())})
metrics_df.write_csv(output_csv_path)


Expand Down
2 changes: 1 addition & 1 deletion src/depiction_targeted_preproc/workflow/vis/clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def vis_clustering(input_netcdf_path: Annotated[Path, Option()], output_png_path
source_image.plot(cmap="tab10", ax=ax)
ax.set_aspect("equal")

#n_classes = len(set(source_image.values.ravel()))
# n_classes = len(set(source_image.values.ravel()))
n_classes = len(np.unique(source_image.values))
fig.suptitle(f"Clustering with {n_classes} classes")
fig.tight_layout()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ def vis_test_mass_shifts(

# compute the shifts
def compute_shifts(coef):
result = calibration.apply_spectrum_model(spectrum_mz_arr=test_masses, spectrum_int_arr=test_masses_int,
model_coef=xr.DataArray(coef, dims=["c"]))
result = calibration.apply_spectrum_model(
spectrum_mz_arr=test_masses, spectrum_int_arr=test_masses_int, model_coef=xr.DataArray(coef, dims=["c"])
)
return xr.DataArray(result[0] - test_masses, dims=["m"])

shifts = xr.apply_ufunc(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def test_get_distances_nearest_when_invalid(self) -> None:


if __name__ == "__main__":
unittest.main()
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def test_interpolate_max_mz_and_intensity_when_success_and_exact(self) -> None:
"""In this very simple case, interpolation should return the same value as the intensity on both sides is
symmetric."""
local_max_index = 2
mz_arr = np.array([1., 2, 3, 4, 5])
int_arr = np.array([0., 0, 10, 0, 0])
mz_arr = np.array([1.0, 2, 3, 4, 5])
int_arr = np.array([0.0, 0, 10, 0, 0])

interpolated_mz, interpolated_int = self.basic_interpolated_peak_picker._interpolate_max_mz_and_intensity(
local_max_index, mz_arr, int_arr
Expand All @@ -41,8 +41,8 @@ def test_interpolate_max_mz_and_intensity_when_success_and_exact(self) -> None:

def test_interpolate_max_mz_and_intensity_when_success_and_not_exact(self) -> None:
local_max_index = 2
mz_arr = np.array([1., 2, 3, 4, 5])
int_arr = np.array([0., 0, 10, 5, 0])
mz_arr = np.array([1.0, 2, 3, 4, 5])
int_arr = np.array([0.0, 0, 10, 5, 0])

interpolated_mz, interpolated_int = self.basic_interpolated_peak_picker._interpolate_max_mz_and_intensity(
local_max_index, mz_arr, int_arr
Expand All @@ -53,8 +53,8 @@ def test_interpolate_max_mz_and_intensity_when_success_and_not_exact(self) -> No

def test_interpolate_max_mz_and_intensity_when_failure(self) -> None:
local_max_index = 2
mz_arr = np.array([1., 2, 3, 4, 5])
int_arr = np.array([0., 10, 10, 10, 0])
mz_arr = np.array([1.0, 2, 3, 4, 5])
int_arr = np.array([0.0, 10, 10, 10, 0])

with Logot().capturing(capturer=LoguruCapturer) as logot:
interpolated_mz, interpolated_int = self.basic_interpolated_peak_picker._interpolate_max_mz_and_intensity(
Expand Down

0 comments on commit 270d922

Please sign in to comment.