terraproof 0.0.0-stage → 0.4.1
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.
- package/LICENSE +21 -0
- package/README.md +356 -2
- package/bin/terraproof.js +4 -0
- package/dist/router/apply.js +147 -0
- package/dist/router/classify.js +111 -0
- package/dist/router/explain.js +9 -0
- package/dist/router/index.js +272 -0
- package/dist/router/resolve.js +239 -0
- package/dist/router/score.js +60 -0
- package/dist/router/types.js +1 -0
- package/dist/router/validate.js +81 -0
- package/instructions/engineering.md +14 -0
- package/instructions/security.md +14 -0
- package/instructions/simplicity.md +13 -0
- package/package.json +65 -4
- package/routing/DOMAIN_ROUTING.md +161 -0
- package/routing/ROUTER_DESIGN.md +173 -0
- package/routing/SKILL_MANIFEST.md +86 -0
- package/routing/checks.json +121 -0
- package/routing/collections.json +47 -0
- package/routing/registry.json +2211 -0
- package/routing/rules.json +64 -0
- package/skills/accuracy-assessment/SKILL.md +213 -0
- package/skills/accuracy-assessment/UPSTREAM.md +7 -0
- package/skills/accuracy-assessment/references/stratified-estimator.md +127 -0
- package/skills/accuracy-assessment/skill.json +103 -0
- package/skills/brainstorming/SKILL.md +250 -0
- package/skills/brainstorming/UPSTREAM.md +29 -0
- package/skills/brainstorming/scripts/frame-template.html +213 -0
- package/skills/brainstorming/scripts/helper.js +167 -0
- package/skills/brainstorming/scripts/server.cjs +723 -0
- package/skills/brainstorming/scripts/start-server.sh +209 -0
- package/skills/brainstorming/scripts/stop-server.sh +120 -0
- package/skills/brainstorming/skill.json +73 -0
- package/skills/brainstorming/spec-document-reviewer-prompt.md +49 -0
- package/skills/brainstorming/visual-companion.md +299 -0
- package/skills/crs-and-units/SKILL.md +200 -0
- package/skills/crs-and-units/UPSTREAM.md +7 -0
- package/skills/crs-and-units/skill.json +74 -0
- package/skills/design-postgis-tables/SKILL.md +494 -0
- package/skills/design-postgis-tables/UPSTREAM.md +21 -0
- package/skills/design-postgis-tables/skill.json +90 -0
- package/skills/gdal/SKILL.md +125 -0
- package/skills/gdal/UPSTREAM.md +22 -0
- package/skills/gdal/references/gdal-recipes.md +103 -0
- package/skills/gdal/skill.json +66 -0
- package/skills/geomaster/README.md +105 -0
- package/skills/geomaster/SKILL.md +366 -0
- package/skills/geomaster/UPSTREAM.md +21 -0
- package/skills/geomaster/references/advanced-gis.md +376 -0
- package/skills/geomaster/references/big-data.md +363 -0
- package/skills/geomaster/references/code-examples.md +531 -0
- package/skills/geomaster/references/coordinate-systems.md +364 -0
- package/skills/geomaster/references/core-libraries.md +273 -0
- package/skills/geomaster/references/data-sources.md +330 -0
- package/skills/geomaster/references/gis-software.md +369 -0
- package/skills/geomaster/references/industry-applications.md +420 -0
- package/skills/geomaster/references/machine-learning.md +462 -0
- package/skills/geomaster/references/programming-languages.md +456 -0
- package/skills/geomaster/references/remote-sensing.md +370 -0
- package/skills/geomaster/references/scientific-domains.md +416 -0
- package/skills/geomaster/references/specialized-topics.md +428 -0
- package/skills/geomaster/references/troubleshooting.md +439 -0
- package/skills/geomaster/skill.json +157 -0
- package/skills/geopandas/SKILL.md +250 -0
- package/skills/geopandas/UPSTREAM.md +22 -0
- package/skills/geopandas/references/crs-management.md +231 -0
- package/skills/geopandas/references/data-io.md +323 -0
- package/skills/geopandas/references/data-structures.md +207 -0
- package/skills/geopandas/references/geometric-operations.md +262 -0
- package/skills/geopandas/references/spatial-analysis.md +294 -0
- package/skills/geopandas/references/visualization.md +230 -0
- package/skills/geopandas/scripts/_common.py +605 -0
- package/skills/geopandas/scripts/crs_reprojection_plan.py +210 -0
- package/skills/geopandas/scripts/export_plan.py +305 -0
- package/skills/geopandas/scripts/geometry_validity_report.py +227 -0
- package/skills/geopandas/scripts/sensitive_coordinates_checklist.py +230 -0
- package/skills/geopandas/scripts/spatial_join_audit.py +368 -0
- package/skills/geopandas/scripts/vector_inventory.py +140 -0
- package/skills/geopandas/skill.json +116 -0
- package/skills/geoparquet-validation/SKILL.md +125 -0
- package/skills/geoparquet-validation/UPSTREAM.md +24 -0
- package/skills/geoparquet-validation/references/distribution-best-practices.md +118 -0
- package/skills/geoparquet-validation/references/gpio-commands.md +264 -0
- package/skills/geoparquet-validation/references/tool-comparison.md +155 -0
- package/skills/geoparquet-validation/skill.json +71 -0
- package/skills/geospatial-data-discovery/SKILL.md +49 -0
- package/skills/geospatial-data-discovery/UPSTREAM.md +15 -0
- package/skills/geospatial-data-discovery/skill.json +51 -0
- package/skills/geospatial-frontend/SKILL.md +86 -0
- package/skills/geospatial-frontend/UPSTREAM.md +23 -0
- package/skills/geospatial-frontend/references/design-system.md +283 -0
- package/skills/geospatial-frontend/references/user-preferences.md +96 -0
- package/skills/geospatial-frontend/skill.json +87 -0
- package/skills/geozarr/SKILL.md +244 -0
- package/skills/geozarr/UPSTREAM.md +21 -0
- package/skills/geozarr/skill.json +97 -0
- package/skills/networkx/SKILL.md +440 -0
- package/skills/networkx/UPSTREAM.md +22 -0
- package/skills/networkx/references/algorithms.md +384 -0
- package/skills/networkx/references/generators.md +385 -0
- package/skills/networkx/references/graph-basics.md +284 -0
- package/skills/networkx/references/io.md +457 -0
- package/skills/networkx/references/visualization.md +531 -0
- package/skills/networkx/skill.json +93 -0
- package/skills/raster-processing/SKILL.md +222 -0
- package/skills/raster-processing/UPSTREAM.md +7 -0
- package/skills/raster-processing/skill.json +81 -0
- package/skills/sar-basics/SKILL.md +202 -0
- package/skills/sar-basics/UPSTREAM.md +7 -0
- package/skills/sar-basics/references/calibration-lut.md +98 -0
- package/skills/sar-basics/skill.json +97 -0
- package/skills/satellite-preprocessing/SKILL.md +241 -0
- package/skills/satellite-preprocessing/UPSTREAM.md +7 -0
- package/skills/satellite-preprocessing/skill.json +84 -0
- package/skills/scientific-visualization/SKILL.md +285 -0
- package/skills/scientific-visualization/UPSTREAM.md +25 -0
- package/skills/scientific-visualization/assets/color_palettes.py +263 -0
- package/skills/scientific-visualization/assets/nature.mplstyle +68 -0
- package/skills/scientific-visualization/assets/presentation.mplstyle +68 -0
- package/skills/scientific-visualization/assets/publication.mplstyle +77 -0
- package/skills/scientific-visualization/assets/publisher_profiles.json +269 -0
- package/skills/scientific-visualization/references/color_palettes.md +227 -0
- package/skills/scientific-visualization/references/journal_requirements.md +169 -0
- package/skills/scientific-visualization/references/matplotlib_examples.md +336 -0
- package/skills/scientific-visualization/references/publication_guidelines.md +196 -0
- package/skills/scientific-visualization/references/sources.md +76 -0
- package/skills/scientific-visualization/scripts/_common.py +136 -0
- package/skills/scientific-visualization/scripts/export_plan.py +493 -0
- package/skills/scientific-visualization/scripts/figure_export.py +642 -0
- package/skills/scientific-visualization/scripts/image_metadata.py +731 -0
- package/skills/scientific-visualization/scripts/palette_audit.py +327 -0
- package/skills/scientific-visualization/scripts/style_presets.py +501 -0
- package/skills/scientific-visualization/scripts/style_preview.py +232 -0
- package/skills/scientific-visualization/skill.json +95 -0
- package/skills/spectral-indices/SKILL.md +235 -0
- package/skills/spectral-indices/UPSTREAM.md +7 -0
- package/skills/spectral-indices/skill.json +88 -0
- package/skills/stac-and-cloud-access/SKILL.md +209 -0
- package/skills/stac-and-cloud-access/UPSTREAM.md +7 -0
- package/skills/stac-and-cloud-access/references/run-manifest.md +94 -0
- package/skills/stac-and-cloud-access/skill.json +98 -0
- package/skills/statistical-analysis/SKILL.md +446 -0
- package/skills/statistical-analysis/UPSTREAM.md +24 -0
- package/skills/statistical-analysis/references/assumptions_and_diagnostics.md +379 -0
- package/skills/statistical-analysis/references/bayesian_statistics.md +686 -0
- package/skills/statistical-analysis/references/effect_sizes_and_power.md +649 -0
- package/skills/statistical-analysis/references/reporting_standards.md +482 -0
- package/skills/statistical-analysis/references/test_selection_guide.md +129 -0
- package/skills/statistical-analysis/scripts/assumption_checks.py +652 -0
- package/skills/statistical-analysis/skill.json +127 -0
- package/skills/systematic-debugging/SKILL.md +283 -0
- package/skills/systematic-debugging/UPSTREAM.md +28 -0
- package/skills/systematic-debugging/condition-based-waiting-example.ts +158 -0
- package/skills/systematic-debugging/condition-based-waiting.md +115 -0
- package/skills/systematic-debugging/defense-in-depth.md +122 -0
- package/skills/systematic-debugging/find-polluter.sh +72 -0
- package/skills/systematic-debugging/root-cause-tracing.md +169 -0
- package/skills/systematic-debugging/skill.json +88 -0
- package/skills/time-series-and-change/SKILL.md +217 -0
- package/skills/time-series-and-change/UPSTREAM.md +7 -0
- package/skills/time-series-and-change/references/change-methods.md +42 -0
- package/skills/time-series-and-change/skill.json +116 -0
- package/skills/vector-quality/SKILL.md +204 -0
- package/skills/vector-quality/UPSTREAM.md +7 -0
- package/skills/vector-quality/skill.json +82 -0
- package/skills/verification/SKILL.md +120 -0
- package/skills/verification/UPSTREAM.md +31 -0
- package/skills/verification/skill.json +67 -0
- package/skills/writing-plans/SKILL.md +171 -0
- package/skills/writing-plans/UPSTREAM.md +23 -0
- package/skills/writing-plans/plan-document-reviewer-prompt.md +49 -0
- package/skills/writing-plans/skill.json +80 -0
- package/sources/SOURCES.md +374 -0
- package/third_party/isaaccorley-geospatial-skills/LICENSE +176 -0
- package/third_party/isaaccorley-geospatial-skills/NOTICE.md +17 -0
- package/third_party/kdense-scientific-agent-skills/LICENSE +21 -0
- package/third_party/kdense-scientific-agent-skills/NOTICE.md +19 -0
- package/third_party/superpowers/LICENSE +21 -0
- package/third_party/superpowers/NOTICE.md +17 -0
- package/third_party/timescale-pg-aiguide/LICENSE +176 -0
- package/third_party/timescale-pg-aiguide/NOTICE.md +14 -0
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
# NetworkX Input/Output
|
|
2
|
+
|
|
3
|
+
## Reading Graphs from Files
|
|
4
|
+
|
|
5
|
+
### Adjacency List Format
|
|
6
|
+
```python
|
|
7
|
+
# Read adjacency list (simple text format)
|
|
8
|
+
G = nx.read_adjlist('graph.adjlist')
|
|
9
|
+
|
|
10
|
+
# With node type conversion
|
|
11
|
+
G = nx.read_adjlist('graph.adjlist', nodetype=int)
|
|
12
|
+
|
|
13
|
+
# For directed graphs
|
|
14
|
+
G = nx.read_adjlist('graph.adjlist', create_using=nx.DiGraph())
|
|
15
|
+
|
|
16
|
+
# Write adjacency list
|
|
17
|
+
nx.write_adjlist(G, 'graph.adjlist')
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Example adjacency list format:
|
|
21
|
+
```
|
|
22
|
+
# node neighbors
|
|
23
|
+
0 1 2
|
|
24
|
+
1 0 3 4
|
|
25
|
+
2 0 3
|
|
26
|
+
3 1 2 4
|
|
27
|
+
4 1 3
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Edge List Format
|
|
31
|
+
```python
|
|
32
|
+
# Read edge list
|
|
33
|
+
G = nx.read_edgelist('graph.edgelist')
|
|
34
|
+
|
|
35
|
+
# With node types and edge data
|
|
36
|
+
G = nx.read_edgelist('graph.edgelist',
|
|
37
|
+
nodetype=int,
|
|
38
|
+
data=(('weight', float),))
|
|
39
|
+
|
|
40
|
+
# Read weighted edge list
|
|
41
|
+
G = nx.read_weighted_edgelist('weighted.edgelist')
|
|
42
|
+
|
|
43
|
+
# Write edge list
|
|
44
|
+
nx.write_edgelist(G, 'graph.edgelist')
|
|
45
|
+
|
|
46
|
+
# Write weighted edge list
|
|
47
|
+
nx.write_weighted_edgelist(G, 'weighted.edgelist')
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Example edge list format:
|
|
51
|
+
```
|
|
52
|
+
# source target
|
|
53
|
+
0 1
|
|
54
|
+
1 2
|
|
55
|
+
2 3
|
|
56
|
+
3 0
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Example weighted edge list:
|
|
60
|
+
```
|
|
61
|
+
# source target weight
|
|
62
|
+
0 1 0.5
|
|
63
|
+
1 2 1.0
|
|
64
|
+
2 3 0.75
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### GML (Graph Modelling Language)
|
|
68
|
+
```python
|
|
69
|
+
# Read GML (preserves all attributes)
|
|
70
|
+
G = nx.read_gml('graph.gml')
|
|
71
|
+
|
|
72
|
+
# Write GML
|
|
73
|
+
nx.write_gml(G, 'graph.gml')
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### GraphML Format
|
|
77
|
+
```python
|
|
78
|
+
# Read GraphML (XML-based format)
|
|
79
|
+
G = nx.read_graphml('graph.graphml')
|
|
80
|
+
|
|
81
|
+
# Write GraphML
|
|
82
|
+
nx.write_graphml(G, 'graph.graphml')
|
|
83
|
+
|
|
84
|
+
# With specific encoding
|
|
85
|
+
nx.write_graphml(G, 'graph.graphml', encoding='utf-8')
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### GEXF (Graph Exchange XML Format)
|
|
89
|
+
```python
|
|
90
|
+
# Read GEXF
|
|
91
|
+
G = nx.read_gexf('graph.gexf')
|
|
92
|
+
|
|
93
|
+
# Write GEXF
|
|
94
|
+
nx.write_gexf(G, 'graph.gexf')
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Pajek Format
|
|
98
|
+
```python
|
|
99
|
+
# Read Pajek .net files
|
|
100
|
+
G = nx.read_pajek('graph.net')
|
|
101
|
+
|
|
102
|
+
# Write Pajek format
|
|
103
|
+
nx.write_pajek(G, 'graph.net')
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### LEDA Format
|
|
107
|
+
```python
|
|
108
|
+
# Read LEDA format (read-only; NetworkX has no LEDA writer)
|
|
109
|
+
G = nx.read_leda('graph.leda')
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Working with Pandas
|
|
113
|
+
|
|
114
|
+
### From Pandas DataFrame
|
|
115
|
+
```python
|
|
116
|
+
import pandas as pd
|
|
117
|
+
|
|
118
|
+
# Create graph from edge list DataFrame
|
|
119
|
+
df = pd.DataFrame({
|
|
120
|
+
'source': [1, 2, 3, 4],
|
|
121
|
+
'target': [2, 3, 4, 1],
|
|
122
|
+
'weight': [0.5, 1.0, 0.75, 0.25]
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
# Create graph
|
|
126
|
+
G = nx.from_pandas_edgelist(df,
|
|
127
|
+
source='source',
|
|
128
|
+
target='target',
|
|
129
|
+
edge_attr='weight')
|
|
130
|
+
|
|
131
|
+
# With multiple edge attributes
|
|
132
|
+
G = nx.from_pandas_edgelist(df,
|
|
133
|
+
source='source',
|
|
134
|
+
target='target',
|
|
135
|
+
edge_attr=['weight', 'color', 'type'])
|
|
136
|
+
|
|
137
|
+
# Create directed graph
|
|
138
|
+
G = nx.from_pandas_edgelist(df,
|
|
139
|
+
source='source',
|
|
140
|
+
target='target',
|
|
141
|
+
create_using=nx.DiGraph())
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### To Pandas DataFrame
|
|
145
|
+
```python
|
|
146
|
+
# Convert graph to edge list DataFrame
|
|
147
|
+
df = nx.to_pandas_edgelist(G)
|
|
148
|
+
|
|
149
|
+
# With specific edge attributes
|
|
150
|
+
df = nx.to_pandas_edgelist(G, source='node1', target='node2')
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Adjacency Matrix with Pandas
|
|
154
|
+
```python
|
|
155
|
+
# Create DataFrame from adjacency matrix
|
|
156
|
+
df = nx.to_pandas_adjacency(G, dtype=int)
|
|
157
|
+
|
|
158
|
+
# Create graph from adjacency DataFrame
|
|
159
|
+
G = nx.from_pandas_adjacency(df)
|
|
160
|
+
|
|
161
|
+
# For directed graphs
|
|
162
|
+
G = nx.from_pandas_adjacency(df, create_using=nx.DiGraph())
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## NumPy and SciPy Integration
|
|
166
|
+
|
|
167
|
+
### Adjacency Matrix
|
|
168
|
+
```python
|
|
169
|
+
import numpy as np
|
|
170
|
+
|
|
171
|
+
# To NumPy adjacency matrix
|
|
172
|
+
A = nx.to_numpy_array(G, dtype=int)
|
|
173
|
+
|
|
174
|
+
# With specific node order
|
|
175
|
+
nodelist = [1, 2, 3, 4, 5]
|
|
176
|
+
A = nx.to_numpy_array(G, nodelist=nodelist)
|
|
177
|
+
|
|
178
|
+
# From NumPy array
|
|
179
|
+
G = nx.from_numpy_array(A)
|
|
180
|
+
|
|
181
|
+
# For directed graphs
|
|
182
|
+
G = nx.from_numpy_array(A, create_using=nx.DiGraph())
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### Sparse Matrix (SciPy)
|
|
186
|
+
```python
|
|
187
|
+
from scipy import sparse
|
|
188
|
+
|
|
189
|
+
# To sparse matrix
|
|
190
|
+
A = nx.to_scipy_sparse_array(G)
|
|
191
|
+
|
|
192
|
+
# With specific format (csr, csc, coo, etc.)
|
|
193
|
+
A_csr = nx.to_scipy_sparse_array(G, format='csr')
|
|
194
|
+
|
|
195
|
+
# From sparse matrix
|
|
196
|
+
G = nx.from_scipy_sparse_array(A)
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## JSON Format
|
|
200
|
+
|
|
201
|
+
### Node-Link Format
|
|
202
|
+
```python
|
|
203
|
+
import json
|
|
204
|
+
|
|
205
|
+
# To node-link format (good for d3.js)
|
|
206
|
+
data = nx.node_link_data(G)
|
|
207
|
+
with open('graph.json', 'w') as f:
|
|
208
|
+
json.dump(data, f)
|
|
209
|
+
|
|
210
|
+
# From node-link format
|
|
211
|
+
with open('graph.json', 'r') as f:
|
|
212
|
+
data = json.load(f)
|
|
213
|
+
G = nx.node_link_graph(data)
|
|
214
|
+
|
|
215
|
+
# Since NetworkX 3.6 the edge list is stored under the "edges" key.
|
|
216
|
+
# Older files (and some d3.js examples) use "links" — pass edges="links"
|
|
217
|
+
# to read or write that layout:
|
|
218
|
+
G = nx.node_link_graph(data, edges="links")
|
|
219
|
+
data = nx.node_link_data(G, edges="links")
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### Adjacency Data Format
|
|
223
|
+
```python
|
|
224
|
+
# To adjacency format
|
|
225
|
+
data = nx.adjacency_data(G)
|
|
226
|
+
with open('graph.json', 'w') as f:
|
|
227
|
+
json.dump(data, f)
|
|
228
|
+
|
|
229
|
+
# From adjacency format
|
|
230
|
+
with open('graph.json', 'r') as f:
|
|
231
|
+
data = json.load(f)
|
|
232
|
+
G = nx.adjacency_graph(data)
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### Tree Data Format
|
|
236
|
+
```python
|
|
237
|
+
# For tree graphs
|
|
238
|
+
data = nx.tree_data(G, root=0)
|
|
239
|
+
with open('tree.json', 'w') as f:
|
|
240
|
+
json.dump(data, f)
|
|
241
|
+
|
|
242
|
+
# From tree format
|
|
243
|
+
with open('tree.json', 'r') as f:
|
|
244
|
+
data = json.load(f)
|
|
245
|
+
G = nx.tree_graph(data)
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
## Pickle Format
|
|
249
|
+
|
|
250
|
+
### Binary Pickle
|
|
251
|
+
```python
|
|
252
|
+
import pickle
|
|
253
|
+
|
|
254
|
+
# Write pickle (preserves all Python objects)
|
|
255
|
+
with open('graph.pkl', 'wb') as f:
|
|
256
|
+
pickle.dump(G, f)
|
|
257
|
+
|
|
258
|
+
# Read pickle
|
|
259
|
+
with open('graph.pkl', 'rb') as f:
|
|
260
|
+
G = pickle.load(f)
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
Note: `nx.write_gpickle` / `nx.read_gpickle` were removed in NetworkX 3.0 — use the standard `pickle` module as shown above. Only unpickle files from trusted sources; pickle can execute arbitrary code on load.
|
|
264
|
+
|
|
265
|
+
## CSV Files
|
|
266
|
+
|
|
267
|
+
### Custom CSV Reading
|
|
268
|
+
```python
|
|
269
|
+
import csv
|
|
270
|
+
|
|
271
|
+
# Read edges from CSV
|
|
272
|
+
G = nx.Graph()
|
|
273
|
+
with open('edges.csv', 'r') as f:
|
|
274
|
+
reader = csv.DictReader(f)
|
|
275
|
+
for row in reader:
|
|
276
|
+
G.add_edge(row['source'], row['target'], weight=float(row['weight']))
|
|
277
|
+
|
|
278
|
+
# Write edges to CSV
|
|
279
|
+
with open('edges.csv', 'w', newline='') as f:
|
|
280
|
+
writer = csv.writer(f)
|
|
281
|
+
writer.writerow(['source', 'target', 'weight'])
|
|
282
|
+
for u, v, data in G.edges(data=True):
|
|
283
|
+
writer.writerow([u, v, data.get('weight', 1.0)])
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
## Database Integration
|
|
287
|
+
|
|
288
|
+
### SQL Databases
|
|
289
|
+
```python
|
|
290
|
+
import sqlite3
|
|
291
|
+
import pandas as pd
|
|
292
|
+
|
|
293
|
+
# Read from SQL database via pandas
|
|
294
|
+
conn = sqlite3.connect('network.db')
|
|
295
|
+
df = pd.read_sql_query("SELECT source, target, weight FROM edges", conn)
|
|
296
|
+
G = nx.from_pandas_edgelist(df, 'source', 'target', edge_attr='weight')
|
|
297
|
+
conn.close()
|
|
298
|
+
|
|
299
|
+
# When filtering on user-supplied values, always use parameterized queries —
|
|
300
|
+
# never interpolate user input into the SQL string:
|
|
301
|
+
conn = sqlite3.connect('network.db')
|
|
302
|
+
df = pd.read_sql_query(
|
|
303
|
+
"SELECT source, target, weight FROM edges WHERE weight > ?",
|
|
304
|
+
conn, params=(min_weight,)
|
|
305
|
+
)
|
|
306
|
+
conn.close()
|
|
307
|
+
|
|
308
|
+
# Write to SQL database
|
|
309
|
+
df = nx.to_pandas_edgelist(G)
|
|
310
|
+
conn = sqlite3.connect('network.db')
|
|
311
|
+
df.to_sql('edges', conn, if_exists='replace', index=False)
|
|
312
|
+
conn.close()
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
## Graph Formats for Visualization
|
|
316
|
+
|
|
317
|
+
### DOT Format (Graphviz)
|
|
318
|
+
```python
|
|
319
|
+
# Write DOT file for Graphviz
|
|
320
|
+
nx.drawing.nx_pydot.write_dot(G, 'graph.dot')
|
|
321
|
+
|
|
322
|
+
# Read DOT file
|
|
323
|
+
G = nx.drawing.nx_pydot.read_dot('graph.dot')
|
|
324
|
+
|
|
325
|
+
# Generate directly to image (requires Graphviz)
|
|
326
|
+
from networkx.drawing.nx_pydot import to_pydot
|
|
327
|
+
pydot_graph = to_pydot(G)
|
|
328
|
+
pydot_graph.write_png('graph.png')
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
## Cytoscape Integration
|
|
332
|
+
|
|
333
|
+
### Cytoscape JSON
|
|
334
|
+
```python
|
|
335
|
+
# Export for Cytoscape
|
|
336
|
+
data = nx.cytoscape_data(G)
|
|
337
|
+
with open('cytoscape.json', 'w') as f:
|
|
338
|
+
json.dump(data, f)
|
|
339
|
+
|
|
340
|
+
# Import from Cytoscape
|
|
341
|
+
with open('cytoscape.json', 'r') as f:
|
|
342
|
+
data = json.load(f)
|
|
343
|
+
G = nx.cytoscape_graph(data)
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
## Specialized Formats
|
|
347
|
+
|
|
348
|
+
### Matrix Market Format
|
|
349
|
+
```python
|
|
350
|
+
from scipy.io import mmread, mmwrite
|
|
351
|
+
|
|
352
|
+
# Read Matrix Market
|
|
353
|
+
A = mmread('graph.mtx')
|
|
354
|
+
G = nx.from_scipy_sparse_array(A)
|
|
355
|
+
|
|
356
|
+
# Write Matrix Market
|
|
357
|
+
A = nx.to_scipy_sparse_array(G)
|
|
358
|
+
mmwrite('graph.mtx', A)
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
### Geographic Networks (Shapefiles, GeoDataFrames)
|
|
362
|
+
`nx.read_shp` / `nx.write_shp` were removed in NetworkX 3.0. Use GeoPandas with momepy (or osmnx for street networks) instead:
|
|
363
|
+
```python
|
|
364
|
+
# uv pip install geopandas momepy
|
|
365
|
+
import geopandas as gpd
|
|
366
|
+
import momepy
|
|
367
|
+
|
|
368
|
+
# Read line geometries from a shapefile and convert to a graph
|
|
369
|
+
gdf = gpd.read_file('roads.shp')
|
|
370
|
+
G = momepy.gdf_to_nx(gdf, approach='primal')
|
|
371
|
+
|
|
372
|
+
# Convert back to GeoDataFrames
|
|
373
|
+
nodes_gdf, edges_gdf = momepy.nx_to_gdf(G)
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
## Format Selection Guidelines
|
|
377
|
+
|
|
378
|
+
### Choose Based on Requirements
|
|
379
|
+
|
|
380
|
+
**Adjacency List** - Simple, human-readable, no attributes
|
|
381
|
+
- Best for: Simple unweighted graphs, quick viewing
|
|
382
|
+
|
|
383
|
+
**Edge List** - Simple, supports weights, human-readable
|
|
384
|
+
- Best for: Weighted graphs, importing/exporting data
|
|
385
|
+
|
|
386
|
+
**GML/GraphML** - Full attribute preservation, XML-based
|
|
387
|
+
- Best for: Complete graph serialization with all metadata
|
|
388
|
+
|
|
389
|
+
**JSON** - Web-friendly, JavaScript integration
|
|
390
|
+
- Best for: Web applications, d3.js visualizations
|
|
391
|
+
|
|
392
|
+
**Pickle** - Fast, preserves Python objects, binary
|
|
393
|
+
- Best for: Python-only storage, complex attributes
|
|
394
|
+
|
|
395
|
+
**Pandas** - Data analysis integration, DataFrame operations
|
|
396
|
+
- Best for: Data processing pipelines, statistical analysis
|
|
397
|
+
|
|
398
|
+
**NumPy/SciPy** - Numerical computation, sparse matrices
|
|
399
|
+
- Best for: Matrix operations, scientific computing
|
|
400
|
+
|
|
401
|
+
**DOT** - Visualization, Graphviz integration
|
|
402
|
+
- Best for: Creating visual diagrams
|
|
403
|
+
|
|
404
|
+
## Performance Considerations
|
|
405
|
+
|
|
406
|
+
### Large Graphs
|
|
407
|
+
For large graphs, consider:
|
|
408
|
+
```python
|
|
409
|
+
# Use compressed formats
|
|
410
|
+
import gzip
|
|
411
|
+
with gzip.open('graph.adjlist.gz', 'wt') as f:
|
|
412
|
+
nx.write_adjlist(G, f)
|
|
413
|
+
|
|
414
|
+
with gzip.open('graph.adjlist.gz', 'rt') as f:
|
|
415
|
+
G = nx.read_adjlist(f)
|
|
416
|
+
|
|
417
|
+
# Use binary formats (faster than text formats)
|
|
418
|
+
with open('graph.pkl', 'wb') as f:
|
|
419
|
+
pickle.dump(G, f)
|
|
420
|
+
|
|
421
|
+
# Use sparse matrices for adjacency
|
|
422
|
+
A = nx.to_scipy_sparse_array(G, format='csr') # Memory efficient
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
### Incremental Loading
|
|
426
|
+
For very large graphs:
|
|
427
|
+
```python
|
|
428
|
+
# Load graph incrementally from edge list
|
|
429
|
+
G = nx.Graph()
|
|
430
|
+
with open('huge_graph.edgelist') as f:
|
|
431
|
+
for line in f:
|
|
432
|
+
u, v = line.strip().split()
|
|
433
|
+
G.add_edge(u, v)
|
|
434
|
+
|
|
435
|
+
# Process in chunks
|
|
436
|
+
if G.number_of_edges() % 100000 == 0:
|
|
437
|
+
print(f"Loaded {G.number_of_edges()} edges")
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
## Error Handling
|
|
441
|
+
|
|
442
|
+
### Robust File Reading
|
|
443
|
+
```python
|
|
444
|
+
try:
|
|
445
|
+
G = nx.read_graphml('graph.graphml')
|
|
446
|
+
except nx.NetworkXError as e:
|
|
447
|
+
print(f"Error reading GraphML: {e}")
|
|
448
|
+
except FileNotFoundError:
|
|
449
|
+
print("File not found")
|
|
450
|
+
G = nx.Graph()
|
|
451
|
+
|
|
452
|
+
# Check if file format is supported
|
|
453
|
+
if os.path.exists('graph.txt'):
|
|
454
|
+
with open('graph.txt') as f:
|
|
455
|
+
first_line = f.readline()
|
|
456
|
+
# Detect format and read accordingly
|
|
457
|
+
```
|