forked from google/neuroglancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(python): format and lint with ruff
- Loading branch information
Showing
117 changed files
with
5,133 additions
and
3,712 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,7 +56,6 @@ jobs: | |
strategy: | ||
matrix: | ||
python-version: | ||
- '3.8' | ||
- '3.9' | ||
- '3.10' | ||
- '3.11' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import nox | ||
|
||
nox.options.reuse_existing_virtualenvs = True | ||
nox.options.error_on_external_run = True | ||
|
||
|
||
@nox.session | ||
def lint(session): | ||
session.install("-r", "python/requirements-lint.txt") | ||
session.run("ruff", "check", ".") | ||
|
||
|
||
@nox.session | ||
def format(session): | ||
session.install("-r", "python/requirements-lint.txt") | ||
session.run("ruff", "format", ".") | ||
|
||
|
||
@nox.session | ||
def mypy(session): | ||
session.install("-r", "python/requirements-mypy.txt") | ||
session.run("mypy", ".") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,50 @@ | ||
[build-system] | ||
requires = ["setuptools>=64", "wheel", "setuptools_scm", "oldest-supported-numpy"] | ||
requires = [ | ||
"setuptools>=64", | ||
"wheel", | ||
"setuptools_scm", | ||
"oldest-supported-numpy", | ||
] | ||
|
||
[tool.ruff] | ||
target-version = "py39" | ||
select = [ | ||
"E", # pycodestyle | ||
"W", # pycodestyle | ||
"F", # pyflakes | ||
"I", # isort | ||
"UP", # pyupgrade | ||
"NPY", # numpy | ||
] | ||
ignore = [ | ||
"E501", # Line length regulated by ruff format | ||
] | ||
|
||
[tool.mypy] | ||
show_error_codes = true | ||
pretty = true | ||
exclude = [ | ||
"noxfile\\.py", | ||
"^src/", | ||
"/guide_video_recorder/", | ||
"^docs/", | ||
] | ||
|
||
[[tool.mypy.overrides]] | ||
module = [ | ||
"apitools", | ||
"apitools.*", | ||
"numcodecs", | ||
"google", | ||
"google.*", | ||
"zarr", | ||
"zarrita", | ||
"tensorstore", | ||
"dask", | ||
"dask.*", | ||
"scipy", | ||
"scipy.*", | ||
"cloudvolume", | ||
"trio", | ||
] | ||
ignore_missing_imports = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 74 additions & 47 deletions
121
python/examples/agglomeration_split_tool_csv_to_sqlite.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,83 @@ | ||
import pandas | ||
import argparse | ||
import sqlite3 | ||
|
||
import neuroglancer.equivalence_map | ||
import argparse | ||
import numpy as np | ||
import pandas | ||
|
||
|
||
def load_edges2(path, include_agglo_id=False): | ||
edges = [] | ||
dtype = {'segment_a': np.uint64, 'segment_b': np.uint64, 'score': np.float64, 'x': np.int64, 'y': np.int64, 'z': np.int64} | ||
if include_agglo_id: | ||
dtype['agglo_id'] = np.uint64 | ||
df = pandas.read_csv(path, sep=',', dtype=dtype) | ||
return df | ||
dtype = { | ||
"segment_a": np.uint64, | ||
"segment_b": np.uint64, | ||
"score": np.float64, | ||
"x": np.int64, | ||
"y": np.int64, | ||
"z": np.int64, | ||
} | ||
if include_agglo_id: | ||
dtype["agglo_id"] = np.uint64 | ||
df = pandas.read_csv(path, sep=",", dtype=dtype) | ||
return df | ||
|
||
|
||
def write_db(edges_csv_path, output_path, include_agglo_id=False): | ||
print("Loading edges") | ||
edges = load_edges2(edges_csv_path, include_agglo_id=include_agglo_id) | ||
|
||
all_eqs = neuroglancer.equivalence_map.EquivalenceMap() | ||
print("Creating equivalence map for agglomeration") | ||
for a, b in edges[["segment_a", "segment_b"]].values: | ||
all_eqs.union(a, b) | ||
|
||
conn = sqlite3.connect(output_path) | ||
c = conn.cursor() | ||
|
||
c.execute("CREATE TABLE supervoxels (supervoxel_id INTEGER, agglo_id INTEGER)") | ||
c.execute( | ||
"CREATE INDEX supervoxels_by_supervoxel_id_index ON supervoxels (supervoxel_id)" | ||
) | ||
c.execute("CREATE INDEX supervoxels_by_agglo_id_index ON supervoxels (agglo_id)") | ||
c.execute( | ||
"CREATE TABLE edges (agglo_id INTEGER, segment_a INTEGER, segment_b INTEGER, score REAL, x INTEGER, y INTEGER, z INTEGER)" | ||
) | ||
c.execute("CREATE INDEX edges_by_agglo_id_index ON edges (agglo_id)") | ||
|
||
print("Writing supervoxels table") | ||
c.executemany( | ||
"INSERT INTO supervoxels VALUES (?,?)", | ||
((int(x), int(all_eqs[x])) for x in all_eqs), | ||
) | ||
|
||
print("Writing edges table") | ||
c.executemany( | ||
"INSERT INTO edges VALUES (?, ?, ?, ?, ?, ?, ?)", | ||
( | ||
( | ||
int(all_eqs[segment_a]), | ||
int(segment_a), | ||
int(segment_b), | ||
float(score), | ||
int(x), | ||
int(y), | ||
int(z), | ||
) | ||
for (segment_a, segment_b), score, (x, y, z) in zip( | ||
edges[["segment_a", "segment_b"]].values, | ||
edges["score"].values, | ||
edges[["x", "y", "z"]].values, | ||
) | ||
), | ||
) | ||
print("Committing") | ||
conn.commit() | ||
conn.close() | ||
|
||
|
||
if __name__ == "__main__": | ||
ap = argparse.ArgumentParser() | ||
ap.add_argument("csv", help="Path to CSV file specifying edges.") | ||
ap.add_argument("db", help="Output path to sqlite3 db.") | ||
args = ap.parse_args() | ||
|
||
print('Loading edges') | ||
edges = load_edges2(edges_csv_path, include_agglo_id=include_agglo_id) | ||
|
||
all_eqs = neuroglancer.equivalence_map.EquivalenceMap() | ||
print('Creating equivalence map for agglomeration') | ||
for a, b in edges[['segment_a', 'segment_b']].values: | ||
all_eqs.union(a, b) | ||
|
||
conn = sqlite3.connect(output_path) | ||
c = conn.cursor() | ||
|
||
c.execute('CREATE TABLE supervoxels (supervoxel_id INTEGER, agglo_id INTEGER)') | ||
c.execute('CREATE INDEX supervoxels_by_supervoxel_id_index ON supervoxels (supervoxel_id)') | ||
c.execute('CREATE INDEX supervoxels_by_agglo_id_index ON supervoxels (agglo_id)') | ||
c.execute('CREATE TABLE edges (agglo_id INTEGER, segment_a INTEGER, segment_b INTEGER, score REAL, x INTEGER, y INTEGER, z INTEGER)') | ||
c.execute('CREATE INDEX edges_by_agglo_id_index ON edges (agglo_id)') | ||
|
||
print('Writing supervoxels table') | ||
c.executemany('INSERT INTO supervoxels VALUES (?,?)', | ||
((int(x), int(all_eqs[x])) for x in all_eqs.keys())) | ||
|
||
print('Writing edges table') | ||
c.executemany( | ||
'INSERT INTO edges VALUES (?, ?, ?, ?, ?, ?, ?)', | ||
((int(all_eqs[segment_a]), int(segment_a), int(segment_b), float(score), int(x), int(y), int(z)) | ||
for (segment_a, segment_b), score, | ||
(x, y, z) in zip(edges[['segment_a', 'segment_b']].values, edges['score'] | ||
.values, edges[['x', 'y', 'z']].values))) | ||
print('Committing') | ||
conn.commit() | ||
conn.close() | ||
|
||
if __name__ == '__main__': | ||
ap = argparse.ArgumentParser() | ||
ap.add_argument('csv', help='Path to CSV file specifying edges.') | ||
ap.add_argument('db', help='Output path to sqlite3 db.') | ||
args = ap.parse_args() | ||
|
||
write_db(args.csv, args.db) | ||
write_db(args.csv, args.db) |
Oops, something went wrong.