napistu 0.3.1__py3-none-any.whl → 0.3.2.dev1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
napistu/__init__.py CHANGED
@@ -3,10 +3,8 @@ from __future__ import annotations
3
3
  from importlib.metadata import PackageNotFoundError
4
4
  from importlib.metadata import version
5
5
 
6
- __version__ = "0.2.2"
7
-
8
6
  try:
9
- __version__ = version("calicolabs-cpr")
7
+ __version__ = version("napistu")
10
8
  except PackageNotFoundError:
11
9
  # package is not installed
12
10
  pass
napistu/__main__.py CHANGED
@@ -753,7 +753,7 @@ def export_precomputed_distances(
753
753
  weights_vars=weights_vars_list,
754
754
  )
755
755
 
756
- utils.save_json(output_uri, precomputed_distances.to_json())
756
+ precompute.save_precomputed_distances(precomputed_distances, output_uri)
757
757
 
758
758
 
759
759
  @exporter.command(name="export_smbl_dfs_tables")
napistu/mcp/constants.py CHANGED
@@ -104,7 +104,7 @@ REPOS_WITH_WIKI = [PACKAGE_DEFS.GITHUB_PROJECT_REPO]
104
104
  TUTORIAL_URLS = {
105
105
  "adding_data_to_graphs": "https://raw.githubusercontent.com/napistu/napistu/refs/heads/main/tutorials/adding_data_to_graphs.ipynb",
106
106
  "downloading_pathway_data": "https://raw.githubusercontent.com/napistu/napistu/refs/heads/main/tutorials/downloading_pathway_data.ipynb",
107
- "formatting_sbml_dfs_as_napistu_graphs": "https://raw.githubusercontent.com/napistu/napistu/refs/heads/main/tutorials/formatting_sbml_dfs_as_napistu_graphs.ipynb",
107
+ "creating_a_napistu_graph": "https://raw.githubusercontent.com/napistu/napistu/refs/heads/main/tutorials/creating_a_napistu_graph.ipynb",
108
108
  "merging_models_into_a_consensus": "https://raw.githubusercontent.com/napistu/napistu/refs/heads/main/tutorials/merging_models_into_a_consensus.ipynb",
109
109
  "r_based_network_visualization": "https://raw.githubusercontent.com/napistu/napistu/refs/heads/main/tutorials/r_based_network_visualization.ipynb",
110
110
  "suggesting_mechanisms_with_networks": "https://raw.githubusercontent.com/napistu/napistu/refs/heads/main/tutorials/suggesting_mechanisms_with_networks.ipynb",
@@ -2,12 +2,16 @@ from __future__ import annotations
2
2
 
3
3
  import logging
4
4
  import math
5
+ from pathlib import Path
6
+ from typing import Union
5
7
 
6
8
  import numpy as np
7
9
  import pandas as pd
10
+ from fs.errors import ResourceNotFound
8
11
 
9
12
  from napistu.network.napistu_graph_core import NapistuGraph
10
13
  from napistu.network.ig_utils import validate_edge_attributes
14
+ from napistu.utils import load_json, save_json
11
15
 
12
16
  logger = logging.getLogger(__name__)
13
17
 
@@ -75,21 +79,17 @@ def precompute_distances(
75
79
  # interate through all partitions of "from" nodes and find their shortest and lowest weighted paths
76
80
  unique_partitions = vs_to_partition.index.unique().tolist()
77
81
 
78
- precomputed_distances = (
79
- pd.concat(
80
- [
81
- _calculate_distances_subset(
82
- napistu_graph,
83
- vs_to_partition,
84
- vs_to_partition.loc[uq_part],
85
- weights_vars=weights_vars,
86
- )
87
- for uq_part in unique_partitions
88
- ]
89
- )
90
- .reset_index(drop=True)
91
- .query("sc_id_origin != sc_id_dest")
92
- )
82
+ precomputed_distances = pd.concat(
83
+ [
84
+ _calculate_distances_subset(
85
+ napistu_graph,
86
+ vs_to_partition,
87
+ vs_to_partition.loc[uq_part],
88
+ weights_vars=weights_vars,
89
+ )
90
+ for uq_part in unique_partitions
91
+ ]
92
+ ).query("sc_id_origin != sc_id_dest")
93
93
 
94
94
  # filter by path length and/or weight
95
95
  filtered_precomputed_distances = _filter_precomputed_distances(
@@ -97,11 +97,59 @@ def precompute_distances(
97
97
  max_steps=max_steps,
98
98
  max_score_q=max_score_q,
99
99
  path_weights_vars=["path_" + w for w in weights_vars],
100
- )
100
+ ).reset_index(drop=True)
101
101
 
102
102
  return filtered_precomputed_distances
103
103
 
104
104
 
105
+ def save_precomputed_distances(
106
+ precomputed_distances: pd.DataFrame, uri: Union[str, Path]
107
+ ) -> None:
108
+ """
109
+ Save a precomputed distances DataFrame to a JSON file.
110
+
111
+ Parameters
112
+ ----------
113
+ precomputed_distances : pd.DataFrame
114
+ The precomputed distances DataFrame to save
115
+ uri : Union[str, Path]
116
+ Path where to save the JSON file. Can be a local path or a GCS URI.
117
+
118
+ Raises
119
+ ------
120
+ OSError
121
+ If the file cannot be written to (permission issues, etc.)
122
+ """
123
+ save_json(str(uri), precomputed_distances.to_dict(orient="index"))
124
+
125
+
126
+ def load_precomputed_distances(uri: Union[str, Path]) -> pd.DataFrame:
127
+ """
128
+ Load a precomputed distances DataFrame from a JSON file.
129
+
130
+ Parameters
131
+ ----------
132
+ uri : Union[str, Path]
133
+ Path to the JSON file to load
134
+
135
+ Returns
136
+ -------
137
+ pd.DataFrame
138
+ The reconstructed precomputed distances DataFrame
139
+
140
+ Raises
141
+ ------
142
+ FileNotFoundError
143
+ If the specified file does not exist
144
+ """
145
+ try:
146
+ data_dict = load_json(str(uri))
147
+ except ResourceNotFound as e:
148
+ raise FileNotFoundError(f"File not found: {uri}") from e
149
+
150
+ return pd.DataFrame.from_dict(data_dict, orient="index").rename(index=int)
151
+
152
+
105
153
  def _calculate_distances_subset(
106
154
  napistu_graph: NapistuGraph,
107
155
  vs_to_partition: pd.DataFrame,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: napistu
3
- Version: 0.3.1
3
+ Version: 0.3.2.dev1
4
4
  Summary: Connecting high-dimensional data to curated pathways
5
5
  Home-page: https://github.com/napistu/napistu-py
6
6
  Author: Sean Hackett
@@ -1,5 +1,5 @@
1
- napistu/__init__.py,sha256=HEXcB6w8PCTD-tm4rq7AoFi7ufZNDuoG3EoM0kz9aPY,269
2
- napistu/__main__.py,sha256=1O_ZyCqNeHcyuL4rbbz6R7_jnPOAamKtTNXZ68gSA7U,28624
1
+ napistu/__init__.py,sha256=dFXAhIqlTLJMwowS4BUDT08-Vy3Q0u1L0CMCErSZT1Y,239
2
+ napistu/__main__.py,sha256=PbzIsFAoFHNQuSyi-ql-D7tQLEOuqgmTcgk0PY-OGeU,28636
3
3
  napistu/consensus.py,sha256=UbKKSLP1O46e3Rk8d_aqNlhRHeR3sZRztAgIm7-XK6Y,69960
4
4
  napistu/constants.py,sha256=Wb1eir8DKSAjukNPknNkKTS4kQSqZjTcTjFaOYNyrC8,15317
5
5
  napistu/identifiers.py,sha256=wsVriQdvPllA5uvh5CiREklA2tYW2MIB14dV7CPaMVU,34003
@@ -41,7 +41,7 @@ napistu/mcp/codebase.py,sha256=q3L76xzpIalcw1ZBy2Zt59G22rX1knila__QdQMh-OA,7950
41
41
  napistu/mcp/codebase_utils.py,sha256=r1nbDmGzq-NaH9cT11jC53mEjszQpwQ0uZUJkMHvgVs,10567
42
42
  napistu/mcp/component_base.py,sha256=nTa8RMW2rRBpFXfhUSHMgaA0sUS2Vy-ERZe__OB-R6Y,4461
43
43
  napistu/mcp/config.py,sha256=2CMUlcYhS0TMziAHFfmwQXpR1jx1qxSnZoP7UcNKYBk,7149
44
- napistu/mcp/constants.py,sha256=XlU5P9uY3LfRPjpSbe9xU35LkcBShP-oRNk_iQ2CqQk,3945
44
+ napistu/mcp/constants.py,sha256=XJuWS-ORCOFU0xi80lRfWo0-MO4CIWYvmrUM7UwwGCs,3919
45
45
  napistu/mcp/documentation.py,sha256=BduopA2YiJQ43Gz_EtK3JGAE2HvFhuknj_pB1KaLBnQ,10738
46
46
  napistu/mcp/documentation_utils.py,sha256=dxzyu2pNxqlvCWJ2Au87_Q1BlmKrVnOo3qyTctoSogc,5873
47
47
  napistu/mcp/execution.py,sha256=ATwMNhrZTZHQCvWPAwmC95B-Yj9QjMyVR_1kqop5dak,16049
@@ -67,7 +67,7 @@ napistu/network/net_create.py,sha256=2N5ocGmibdBxIUVtv3H36iFWwkbys9ECCERFRlByhLc
67
67
  napistu/network/net_propagation.py,sha256=89ZR4p2mGpkCCIemofZ53XbUjQsuNABxIc6UmF8A5n8,4935
68
68
  napistu/network/ng_utils.py,sha256=ijWDa5MTuULJpdV6dcVFGmLmtB_xy87jaUG7F5nvC_k,15240
69
69
  napistu/network/paths.py,sha256=S4ZaV0yVmI-o8sXfom5eXA3yy2IEbleYUyXEvnmVw98,17468
70
- napistu/network/precompute.py,sha256=aEynTDdKK_WJbEqBd6Q8iihXzuk3rheurkPcnh9bYqQ,7651
70
+ napistu/network/precompute.py,sha256=xDIHWxGWwDyEw1sF1bQKHVbunI8qmeJvo3Iv7wADUys,8960
71
71
  napistu/ontologies/__init__.py,sha256=dFXAhIqlTLJMwowS4BUDT08-Vy3Q0u1L0CMCErSZT1Y,239
72
72
  napistu/ontologies/constants.py,sha256=GyOFvezSxDK1VigATcruTKtNhjcYaid1ggulEf_HEtQ,4345
73
73
  napistu/ontologies/dogma.py,sha256=jGZS-J3d29AoUOow-HVjfVZQJ87lnqO5L1aozieN1ec,8825
@@ -81,7 +81,7 @@ napistu/rpy2/rids.py,sha256=AfXLTfTdonfspgAHYO0Ph7jSUWv8YuyT8x3fyLfAqc8,3413
81
81
  napistu/scverse/__init__.py,sha256=Lgxr3iMQAkTzXE9BNz93CndNP5djzerLvmHM-D0PU3I,357
82
82
  napistu/scverse/constants.py,sha256=0iAkhyJUIeFGHdLLU3fCaEU1O3Oix4qAsxr3CxGTjVs,653
83
83
  napistu/scverse/loading.py,sha256=jqiE71XB-wdV50GyZrauFNY0Lai4bX9Fm2Gv80VR8t8,27016
84
- napistu-0.3.1.dist-info/licenses/LICENSE,sha256=kW8wVT__JWoHjl2BbbJDAZInWa9AxzJeR_uv6-i5x1g,1063
84
+ napistu-0.3.2.dev1.dist-info/licenses/LICENSE,sha256=kW8wVT__JWoHjl2BbbJDAZInWa9AxzJeR_uv6-i5x1g,1063
85
85
  tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
86
86
  tests/conftest.py,sha256=XVkd0tQywhnf2cgab7fIjBo3NlaTVX3cO8HaRS2jIwM,3190
87
87
  tests/test_consensus.py,sha256=3dJvvPsPG7bHbw_FY4Pm647N_Gt_Ud9157OKYfPCUd4,9502
@@ -101,14 +101,14 @@ tests/test_matching_species.py,sha256=OuUWp0-X3WYXkc-g51XyOqhp4MgO8cJvUSqt8ZvqRa
101
101
  tests/test_mcp_config.py,sha256=GTu9vywqAHTYkolywdYS_BEIW3gBzs4A4qcneMSPpRk,7007
102
102
  tests/test_mcp_documentation_utils.py,sha256=OW0N2N_2IOktbYTcCWhhWz4bANi8IB60l1q3DJi8Ra4,810
103
103
  tests/test_mcp_server.py,sha256=bP3PWVQsEfX6-lAgXKP32njdg__o65n2WuLvkxTTHkQ,11215
104
- tests/test_net_propagation.py,sha256=9pKkUdduWejH4iKNCJXKFzAkdNpCfrMbiUWySgI_LH4,3244
105
104
  tests/test_network_data_handling.py,sha256=oBSZuB3IRG9bwmD6n8FY-UZLe2UqGzXpNSxVtkHRSvE,12605
106
105
  tests/test_network_ig_utils.py,sha256=uojDLtL7oT9S9NJrXL8kBEHHFq5DB1GnJQT0v-gHEBE,632
107
106
  tests/test_network_neighborhoods.py,sha256=8BV17m5X1OUd5FwasTTYUOkNYUHDPUkxOKH_VZCsyBE,631
108
107
  tests/test_network_net_create.py,sha256=VNFZTwQawAZQPDnVk_qFevgZErx5KyQZ24bMoZF4T4w,16462
108
+ tests/test_network_net_propagation.py,sha256=9pKkUdduWejH4iKNCJXKFzAkdNpCfrMbiUWySgI_LH4,3244
109
109
  tests/test_network_ng_utils.py,sha256=CwDw4MKTPhVZXz2HA2XU2QjjBv8CXc1_yQ0drvkBkFw,724
110
110
  tests/test_network_paths.py,sha256=TWZnxY5bF3m6gahcxcYJGrBIawh2-_vUcec1LyPmXV8,1686
111
- tests/test_network_precompute.py,sha256=iyqvxdlt_LQdAoFr8x-NZUbq5VH4Xo64fTg3gyXiWEg,7245
111
+ tests/test_network_precompute.py,sha256=W1tuHM-dd90nk0vUUNP_xZ7EhCKSjigI5ndm8oq8l0c,8869
112
112
  tests/test_ontologies_genodexito.py,sha256=hBlunyEPiKskqagjWKW5Z6DJwKvpueYHJLwbfyeeAdo,2256
113
113
  tests/test_ontologies_mygene.py,sha256=BuBLm8VatzpK39-Ew_fFTK9ueLE4eqmKIDS5UKE59n8,1541
114
114
  tests/test_ontologies_renaming.py,sha256=k7bQzP24zG7W3fpULwk1me2sOWEWlxylr4Mhx1_gJJY,3740
@@ -126,8 +126,8 @@ tests/test_uncompartmentalize.py,sha256=nAk5kfAVLU9a2VWe2x2HYVcKqj-EnwmwddERIPRa
126
126
  tests/test_utils.py,sha256=JRJFmjDNZpjG59a-73JkTyGqa_a7Z8d0fE2cZt0CRII,22580
127
127
  tests/utils.py,sha256=SoWQ_5roJteFGcMaOeEiQ5ucwq3Z2Fa3AAs9iXHTsJY,749
128
128
  tests/test_data/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
129
- napistu-0.3.1.dist-info/METADATA,sha256=Dy28usEPYFT-rbmyJSn2lvcNhc7tU2kcCZsj8oTlRWQ,3413
130
- napistu-0.3.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
131
- napistu-0.3.1.dist-info/entry_points.txt,sha256=_QnaPOvJNA3IltxmZgWIiBoen-L1bPYX18YQfC7oJgQ,41
132
- napistu-0.3.1.dist-info/top_level.txt,sha256=Gpvk0a_PjrtqhYcQ9IDr3zR5LqpZ-uIHidQMIpjlvhY,14
133
- napistu-0.3.1.dist-info/RECORD,,
129
+ napistu-0.3.2.dev1.dist-info/METADATA,sha256=go09DE0iiD9UQmhyGSTdq2iGLGgODt84uZ9vKbridIg,3418
130
+ napistu-0.3.2.dev1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
131
+ napistu-0.3.2.dev1.dist-info/entry_points.txt,sha256=_QnaPOvJNA3IltxmZgWIiBoen-L1bPYX18YQfC7oJgQ,41
132
+ napistu-0.3.2.dev1.dist-info/top_level.txt,sha256=Gpvk0a_PjrtqhYcQ9IDr3zR5LqpZ-uIHidQMIpjlvhY,14
133
+ napistu-0.3.2.dev1.dist-info/RECORD,,
@@ -1,6 +1,8 @@
1
1
  from __future__ import annotations
2
2
 
3
+ import pytest
3
4
  import os
5
+ import tempfile
4
6
 
5
7
  import numpy as np
6
8
  import pandas as pd
@@ -214,11 +216,59 @@ def test_precomputed_distances_neighborhoods():
214
216
  assert upstream_disagreement_w_precompute.shape[0] == 0
215
217
 
216
218
 
217
- ################################################
218
- # __main__
219
- ################################################
220
-
221
- if __name__ == "__main__":
222
- test_precomputed_distances()
223
- test_precomputed_distances_shortest_paths()
224
- test_precomputed_distances_neighborhoods()
219
+ @pytest.mark.skip_on_windows
220
+ def test_precomputed_distances_serialization():
221
+ """
222
+ Test that validates the serialization -> deserialization approach works correctly.
223
+
224
+ Notes
225
+ -----
226
+ This function creates a sample DataFrame with the structure of precomputed
227
+ distances data, saves it to a temporary JSON file, loads it back, and
228
+ validates that all data is preserved correctly through the serialization
229
+ round-trip.
230
+ """
231
+ # Create a sample DataFrame that mimics the precomputed distances structure
232
+ sample_data = {
233
+ "sc_id_origin": {
234
+ 1: "SC00000000",
235
+ 3: "SC00000003",
236
+ 4: "SC00000004",
237
+ 5: "SC00000005",
238
+ 6: "SC00000011",
239
+ },
240
+ "sc_id_dest": {
241
+ 1: "SC00000001",
242
+ 3: "SC00000001",
243
+ 4: "SC00000001",
244
+ 5: "SC00000001",
245
+ 6: "SC00000001",
246
+ },
247
+ "path_length": {1: 1.0, 3: 4.0, 4: 6.0, 5: 6.0, 6: 1.0},
248
+ "path_upstream_weights": {1: 1.0, 3: 4.0, 4: 6.0, 5: 6.0, 6: 1.0},
249
+ "path_weights": {1: 1.0, 3: 4.0, 4: 6.0, 5: 6.0, 6: 1.0},
250
+ }
251
+
252
+ # Create original DataFrame
253
+ original_df = pd.DataFrame(sample_data)
254
+
255
+ # Create a temporary file path
256
+ with tempfile.NamedTemporaryFile(
257
+ mode="w", suffix=".json", delete=False
258
+ ) as tmp_file:
259
+ temp_path = tmp_file.name
260
+
261
+ try:
262
+ # Test serialization
263
+ precompute.save_precomputed_distances(original_df, temp_path)
264
+
265
+ # Test deserialization
266
+ loaded_df = precompute.load_precomputed_distances(temp_path)
267
+
268
+ # Validate that the loaded DataFrame is identical to the original
269
+ pd.testing.assert_frame_equal(original_df, loaded_df, check_like=True)
270
+
271
+ finally:
272
+ # Clean up the temporary file
273
+ if os.path.exists(temp_path):
274
+ os.remove(temp_path)