Skip to content

Commit

Permalink
Merge branch 'issue279' into issue275
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlinLiao committed Dec 17, 2023
2 parents 588a3bd + 9720bdb commit 2b46c77
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 16 deletions.
16 changes: 13 additions & 3 deletions spatialprofilingtoolbox/cggnn/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
)
from numpy.random import seed as np_seed
from numpy.typing import NDArray
from scipy.sparse import spmatrix
from scipy.sparse import spmatrix, save_npz, load_npz # type: ignore

SETS = ('train', 'validation', 'test')
SETS_type = Literal['train', 'validation', 'test']
Expand Down Expand Up @@ -47,19 +47,29 @@ class GraphData(NamedTuple):


def save_hs_graphs(graphs_data: list[GraphData], output_directory: str) -> None:
"""Save histological structure graphs to a directory."""
"""Save histological structure graphs to a directory.
Saves the adjacency graph separately from the rest of the graph data for compatibility.
"""
makedirs(output_directory, exist_ok=True)
for i, gd in enumerate(graphs_data):
save_npz(join(output_directory, f'graph_{i}_adj.npz'), gd.graph.adj)
with open(join(output_directory, 'graphs.pkl'), 'wb') as f:
for gd in graphs_data:
gd.graph.adj = None # type: ignore
dump(graphs_data, f)


def load_hs_graphs(graph_directory: str) -> tuple[list[GraphData], list[str]]:
"""Load histological structure graphs from a directory.
Assumes directory contains the files `graphs.pkl` and `feature_names.txt`.
Assumes directory contains the files `graphs.pkl`, `feature_names.txt`, and a sparse array for
every graph in `graphs.pkl`.
"""
with open(join(graph_directory, 'graphs.pkl'), 'rb') as f:
graphs_data: list[GraphData] = load(f)
for i, gd in enumerate(graphs_data):
gd.graph.adj = load_npz(join(graph_directory, f'graph_{i}_adj.npz'))
feature_names: list[str] = loadtxt(
join(graph_directory, 'feature_names.txt'),
dtype=str,
Expand Down
15 changes: 3 additions & 12 deletions spatialprofilingtoolbox/workflow/assets/cggnn.nf
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ process finalize_graphs {

output:
path "results/", emit: working_directory
path "results/feature_names.txt", emit: feature_names_file
path "results/graphs.pkl", emit: graphs_file

script:
"""
Expand Down Expand Up @@ -153,8 +151,7 @@ process train {

output:
path "${working_directory}/importances.csv", emit: importances_csv_path
path "${working_directory}/model/", emit: model_directory
path "${working_directory}/graphs.pkl", optional: true
path "${working_directory}/", emit: working_directory

script:
"""
Expand Down Expand Up @@ -293,12 +290,6 @@ workflow {
finalize_out.working_directory
.set{ working_directory_ch }

finalize_out.feature_names_file
.set{ feature_names_ch }

finalize_out.graphs_file
.set{ graphs_file_ch }

train(
working_directory_ch,
in_ram_ch,
Expand All @@ -315,8 +306,8 @@ workflow {
train_out.importances_csv_path
.set{ importances_csv_path_ch }

train_out.model_directory
.set{ model_directory_ch }
train_out.working_directory
.set{ working_directory_ch }

upload_importance_scores(
upload_importances_ch,
Expand Down
File renamed without changes.
Binary file added test/cggnn/unit_tests/graphs/graph_0_adj.npz
Binary file not shown.
Binary file added test/cggnn/unit_tests/graphs/graph_1_adj.npz
Binary file not shown.
Binary file added test/cggnn/unit_tests/graphs/graph_2_adj.npz
Binary file not shown.
Binary file added test/cggnn/unit_tests/graphs/graph_3_adj.npz
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion test/cggnn/unit_tests/test_plotting.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
spt cggnn plot-interactives \
--cg_path unit_tests/ \
--cg_path unit_tests/graphs/ \
--output_directory . \
--merge_rois
plotting_ran="$?"
Expand Down

0 comments on commit 2b46c77

Please sign in to comment.