Skip to content

Commit ff3c8ae

Browse files
timblakelycopybara-github
authored andcommitted
Remove dependency on TensorFlow in favor of using gpathlib/pathlib for internal and external file access, respectively.
PiperOrigin-RevId: 728310349
1 parent fc2ae79 commit ff3c8ae

File tree

5 files changed

+14
-34
lines changed

5 files changed

+14
-34
lines changed

connectomics/common/file.py

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# coding=utf-8
2-
# Copyright 2022 The Google Research Authors.
2+
# Copyright 2025 The Google Research Authors.
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -26,9 +26,12 @@
2626
import dataclasses_json
2727
import tensorstore as ts
2828

29+
PathLike = Union[str, pathlib.PurePath]
2930

3031
T = TypeVar('T', bound=dataclasses_json.DataClassJsonMixin)
31-
PathLike = Union[str, pathlib.Path]
32+
33+
# Local Alias
34+
Path = pathlib.Path
3235

3336

3437
def save_dataclass_json(
@@ -298,29 +301,6 @@ def load_dataclass_json(
298301
)
299302

300303

301-
# TODO(timblakely): Remove dependency on TF when there's a common API to read
302-
# files internally and externally.
303-
import tensorflow as tf
304-
305-
Copy = tf.io.gfile.copy
306-
DeleteRecursively = tf.io.gfile.rmtree
307-
Exists = tf.io.gfile.exists
308-
Glob = tf.io.gfile.glob
309-
IsDirectory = tf.io.gfile.isdir
310-
ListDirectory = tf.io.gfile.listdir
311-
MakeDirs = tf.io.gfile.makedirs
312-
MkDir = tf.io.gfile.mkdir
313-
Open = tf.io.gfile.GFile
314-
Remove = tf.io.gfile.remove
315-
Rename = tf.io.gfile.rename
316-
Stat = tf.io.gfile.stat
317-
Walk = tf.io.gfile.walk
318-
319-
GFile = tf.io.gfile.GFile
320-
321-
NotFoundError = tf.errors.NotFoundError
322-
323-
324304
# TODO(timblakely): Remove in favor the above serialization.
325305
def load_dataclass(
326306
constructor: type[T], v: Union[str, dict[str, Any], T, None]
@@ -344,7 +324,7 @@ def load_dataclass(
344324
return constructor.from_json(v)
345325
except json.JSONDecodeError:
346326
# File path; attempt to load.
347-
with Open(v) as f:
327+
with Path(v).open() as f:
348328
return constructor.from_json(f.read())
349329
else:
350330
return constructor.from_dict(typing.cast(dict[str, Any], v))

connectomics/common/file_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def test_dataclass_from_json(self):
134134

135135
def test_dataclass_from_file(self):
136136
fname = os.path.join(FLAGS.test_tmpdir, 'dc_file')
137-
with file.Open(fname, 'w') as f:
137+
with file.Path(fname).open('wt') as f:
138138
f.write("""{
139139
"spec": {
140140
"test": "foo"
@@ -179,7 +179,7 @@ def test_dataclass_loader(self):
179179
self.assertEqual(new_conf, ts_conf)
180180

181181
fname = os.path.join(FLAGS.test_tmpdir, 'dc_loader')
182-
with file.Open(fname, 'w') as f:
182+
with file.Path(fname).open('wt') as f:
183183
f.write(ts_conf.to_json())
184184
new_conf = loader(fname)
185185
self.assertEqual(new_conf, ts_conf)

connectomics/common/io_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def load_equivalences(
4444
equiv_graph = nx.Graph()
4545

4646
for path in paths:
47-
with file.Open(path, "r") as f:
47+
with file.Path(path).open("rt") as f:
4848
reader = pd.read_csv(
4949
f, sep=",", engine="c", comment="#", chunksize=4096, header=None
5050
)
@@ -64,13 +64,13 @@ def load_equivalences(
6464

6565
def load_relabel_map(path: str) -> dict[int, int]:
6666
"""Loads a label map from a text file."""
67-
with file.Open(path) as f:
67+
with file.Path(path).open("r") as f:
6868
df = pd.read_csv(f, engine="c", comment="#", header=None, dtype=np.uint64)
6969
return {int(a): int(b) for a, b in df.to_numpy(dtype=np.uint64)}
7070

7171

7272
def load_object_list(path: str) -> set[int]:
7373
"""Loads an object list from a text file."""
74-
with file.Open(path) as f:
74+
with file.Path(path).open("r") as f:
7575
df = pd.read_csv(f, engine="c", comment="#", header=None, dtype=np.uint64)
7676
return set(int(x) for x in df.to_numpy(dtype=np.uint64).ravel())

connectomics/jax/models/util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ def model_from_dict_config(
187187

188188
def save_config(config: ml_collections.ConfigDict, path: file.PathLike):
189189
"""Saves model config to a file."""
190-
with file.Open(path, 'w') as f:
190+
with file.Path(path).open('wt') as f:
191191
f.write(config.to_json_best_effort() + '\n')
192192

193193

194194
def load_config(path: file.PathLike) -> ml_collections.ConfigDict:
195195
"""Loads a model config from a file."""
196-
with file.Open(path, 'r') as f:
196+
with file.Path(path).open('rt') as f:
197197
return ml_collections.ConfigDict(json.loads(f.read()))

connectomics/volume/decorators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1576,7 +1576,7 @@ def __init__(self, dim: int | str, json_path: str):
15761576
self._json_path = json_path
15771577

15781578
def decorate(self, input_ts: ts.TensorStore) -> ts.TensorStore:
1579-
indices = json_lib.loads(file.Open(self._json_path, 'r').read())
1579+
indices = json_lib.loads(file.Path(self._json_path).read_text())
15801580
return input_ts[ts.d[self._dim][indices]]
15811581

15821582

0 commit comments

Comments
 (0)