Skip to content

Commit

Permalink
Eliminate string construction of file:// urls. (#117)
Browse files Browse the repository at this point in the history
Use Pathlib's `as_uri()` method, which is cross-platform.

Test plan: `make test`
Part of spacetx/starfish#1296
  • Loading branch information
Tony Tung authored Aug 30, 2019
1 parent b2be1a1 commit 4e78b5f
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 103 deletions.
16 changes: 8 additions & 8 deletions tests/io_/test_resolve_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
class TestResolvePathOrUrl(unittest.TestCase):
def test_valid_local_path(self):
with tempfile.NamedTemporaryFile() as tfn:
abspath = os.path.realpath(tfn.name)
_, name, baseurl = resolve_path_or_url(abspath)
self.assertEqual(name, os.path.basename(abspath))
self.assertEqual("file://{}".format(os.path.dirname(abspath)), baseurl)
abspath = Path(tfn.name).resolve()
_, name, baseurl = resolve_path_or_url(fspath(abspath))
self.assertEqual(name, abspath.name)
self.assertEqual(abspath.parent.as_uri(), baseurl)

cwd = os.getcwd()
try:
os.chdir(os.path.dirname(abspath))
_, name, baseurl = resolve_path_or_url(os.path.basename(abspath))
self.assertEqual(name, os.path.basename(abspath))
self.assertEqual("file://{}".format(os.path.dirname(abspath)), baseurl)
os.chdir(fspath(abspath.parent))
_, name, baseurl = resolve_path_or_url(abspath.name)
self.assertEqual(name, abspath.name)
self.assertEqual(abspath.parent.as_uri(), baseurl)
finally:
os.chdir(cwd)

Expand Down
13 changes: 6 additions & 7 deletions tests/io_/v0_0_0/test_missing_shape.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import codecs
import json
import os
import tempfile
import unittest
import warnings
from pathlib import Path

import numpy as np

import slicedimage
from slicedimage._dimensions import DimensionNames
from slicedimage.io._keys import TileKeys, TileSetKeys

baseurl = "file://{}".format(os.path.abspath(os.path.dirname(__file__)))
baseurl = Path(__file__).parent.resolve().as_uri()


class TestMissingShape(unittest.TestCase):
Expand Down Expand Up @@ -39,8 +39,9 @@ def test_tileset_without_shapes(self):

with tempfile.TemporaryDirectory() as tempdir, \
tempfile.NamedTemporaryFile(suffix=".json", dir=tempdir) as partition_file:
partition_file_path = Path(partition_file.name)
partition_doc = slicedimage.v0_0_0.Writer().generate_partition_document(
image, "file://{}".format(partition_file.name))
image, partition_file_path.as_uri())

# remove the shape information from the tiles.
for tile in partition_doc[TileSetKeys.TILES]:
Expand All @@ -50,10 +51,8 @@ def test_tileset_without_shapes(self):
json.dump(partition_doc, writer(partition_file))
partition_file.flush()

basename = os.path.basename(partition_file.name)
baseurl = "file://{}".format(os.path.dirname(partition_file.name))

loaded = slicedimage.Reader.parse_doc(basename, baseurl)
loaded = slicedimage.Reader.parse_doc(
partition_file_path.name, partition_file_path.parent.as_uri())

for hyb in range(2):
for ch in range(2):
Expand Down
24 changes: 11 additions & 13 deletions tests/io_/v0_0_0/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
import os
import tempfile
import unittest
from pathlib import Path

import tifffile
import numpy as np
from slicedimage._compat import fspath

import slicedimage
from slicedimage._dimensions import DimensionNames
from tests.utils import build_skeleton_manifest

baseurl = "file://{}".format(os.path.abspath(os.path.dirname(__file__)))
baseurl = Path(__file__).parent.resolve().as_uri()


class TestReader(unittest.TestCase):
Expand Down Expand Up @@ -60,6 +62,7 @@ def test_tiff(self):
Generate a tileset consisting of a single TIFF tile, and then read it.
"""
with tempfile.TemporaryDirectory() as tempdir:
tempdir_path = Path(tempdir)
# write the tiff file
data = np.random.randint(0, 65535, size=(120, 80), dtype=np.uint16)
with tifffile.TiffWriter(os.path.join(tempdir, "tile.tiff")) as tiff:
Expand Down Expand Up @@ -88,13 +91,10 @@ def test_tiff(self):
"format": "tiff",
},
)
with open(os.path.join(tempdir, "tileset.json"), "w") as fh:
with open(fspath(tempdir_path / "tileset.json"), "w") as fh:
fh.write(json.dumps(manifest))

result = slicedimage.Reader.parse_doc(
"tileset.json",
"file://{}".format(tempdir),
)
result = slicedimage.Reader.parse_doc("tileset.json", tempdir_path.as_uri())

self.assertTrue(np.array_equal(list(result.tiles())[0].numpy_array, data))

Expand Down Expand Up @@ -122,16 +122,14 @@ def test_numpy(self):
image.add_tile(tile)

with tempfile.TemporaryDirectory() as tempdir:
partition_path = os.path.join(tempdir, "tileset.json")
tempdir_path = Path(tempdir)
tileset_path = tempdir_path / "tileset.json"
partition_doc = slicedimage.v0_0_0.Writer().generate_partition_document(
image, "file://{}".format(partition_path))
with open(partition_path, "w") as fh:
image, (tempdir_path / "tileset.json").as_uri())
with open(fspath(tileset_path), "w") as fh:
json.dump(partition_doc, fh)

result = slicedimage.Reader.parse_doc(
"tileset.json",
"file://{}".format(tempdir),
)
result = slicedimage.Reader.parse_doc("tileset.json", tempdir_path.as_uri())

self.assertTrue(np.array_equal(list(result.tiles())[0].numpy_array, tile.numpy_array))

Expand Down
51 changes: 25 additions & 26 deletions tests/io_/v0_0_0/test_write.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import codecs
import hashlib
import json
import os
import tempfile
import unittest
from pathlib import Path

import tifffile
import numpy as np
from slicedimage._compat import fspath

import slicedimage
from slicedimage import ImageFormat
from slicedimage._dimensions import DimensionNames
from tests.utils import build_skeleton_manifest

baseurl = "file://{}".format(os.path.abspath(os.path.dirname(__file__)))
baseurl = Path(__file__).parent.resolve().as_uri()


class TestWrite(unittest.TestCase):
Expand Down Expand Up @@ -42,16 +43,15 @@ def test_write_tileset(self):

with tempfile.TemporaryDirectory() as tempdir, \
tempfile.NamedTemporaryFile(suffix=".json", dir=tempdir) as partition_file:
partition_file_path = Path(partition_file.name)
partition_doc = slicedimage.v0_0_0.Writer().generate_partition_document(
image, "file://{}".format(partition_file.name))
image, partition_file_path.as_uri())
writer = codecs.getwriter("utf-8")
json.dump(partition_doc, writer(partition_file))
partition_file.flush()

basename = os.path.basename(partition_file.name)
baseurl = "file://{}".format(os.path.dirname(partition_file.name))

loaded = slicedimage.Reader.parse_doc(basename, baseurl)
loaded = slicedimage.Reader.parse_doc(
partition_file_path.name, partition_file_path.parent.as_uri())

for hyb in range(2):
for ch in range(2):
Expand Down Expand Up @@ -96,16 +96,15 @@ def test_write_collection(self):

with tempfile.TemporaryDirectory() as tempdir, \
tempfile.NamedTemporaryFile(suffix=".json", dir=tempdir) as partition_file:
partition_file_path = Path(partition_file.name)
partition_doc = slicedimage.v0_0_0.Writer().generate_partition_document(
collection, "file://{}".format(partition_file.name))
collection, partition_file_path.as_uri())
writer = codecs.getwriter("utf-8")
json.dump(partition_doc, writer(partition_file))
partition_file.flush()

basename = os.path.basename(partition_file.name)
baseurl = "file://{}".format(os.path.dirname(partition_file.name))

loaded = slicedimage.Reader.parse_doc(basename, baseurl)
loaded = slicedimage.Reader.parse_doc(
partition_file_path.name, partition_file_path.parent.as_uri())

for hyb in range(2):
for ch in range(2):
Expand All @@ -131,11 +130,12 @@ def test_checksum_on_write(self):
"""
# write the tiff file
with tempfile.TemporaryDirectory() as tempdir:
tempdir_path = Path(tempdir)
file_path = tempdir_path / "tile.tiff"
data = np.random.randint(0, 65535, size=(120, 80), dtype=np.uint16)
file_path = os.path.join(tempdir, "tile.tiff")
with tifffile.TiffWriter(file_path) as tiff:
with tifffile.TiffWriter(fspath(file_path)) as tiff:
tiff.save(data)
with open(file_path, "rb") as fh:
with open(fspath(file_path), "rb") as fh:
checksum = hashlib.sha256(fh.read()).hexdigest()

manifest = build_skeleton_manifest()
Expand All @@ -160,29 +160,28 @@ def test_checksum_on_write(self):
"sha256": checksum,
},
)
with open(os.path.join(tempdir, "tileset.json"), "w") as fh:
with open(fspath(tempdir_path / "tileset.json"), "w") as fh:
fh.write(json.dumps(manifest))

image = slicedimage.Reader.parse_doc(
"tileset.json",
"file://{}".format(tempdir),
tempdir_path.as_uri(),
{"cache": {"size_limit": 0}}, # disabled
)

with tempfile.TemporaryDirectory() as output_tempdir, \
tempfile.NamedTemporaryFile(
suffix=".json", dir=output_tempdir) as partition_file:
partition_file_path = Path(partition_file.name)
partition_doc = slicedimage.v0_0_0.Writer().generate_partition_document(
image, "file://{}".format(partition_file.name))
image, partition_file_path.as_uri())

writer = codecs.getwriter("utf-8")
json.dump(partition_doc, writer(partition_file))
partition_file.flush()

basename = os.path.basename(partition_file.name)
baseurl = "file://{}".format(os.path.dirname(partition_file.name))

loaded = slicedimage.Reader.parse_doc(basename, baseurl)
loaded = slicedimage.Reader.parse_doc(
partition_file_path.name, partition_file_path.parent.as_uri())

loaded.tiles()[0].numpy_array

Expand Down Expand Up @@ -212,16 +211,16 @@ def test_write_tiff(self):
with tempfile.TemporaryDirectory() as tempdir, \
tempfile.NamedTemporaryFile(suffix=".json", dir=tempdir) as partition_file:
# create the tileset and save it.
partition_file_path = Path(partition_file.name)
partition_doc = slicedimage.v0_0_0.Writer().generate_partition_document(
image, "file://{}".format(partition_file.name), tile_format=ImageFormat.TIFF)
image, partition_file_path.as_uri(), tile_format=ImageFormat.TIFF)
writer = codecs.getwriter("utf-8")
json.dump(partition_doc, writer(partition_file))
partition_file.flush()

# construct a URL to the tileset we wrote, and load the tileset.
basename = os.path.basename(partition_file.name)
baseurl = "file://{}".format(os.path.dirname(partition_file.name))
loaded = slicedimage.Reader.parse_doc(basename, baseurl)
loaded = slicedimage.Reader.parse_doc(
partition_file_path.name, partition_file_path.parent.as_uri())

# compare the tiles we loaded to the tiles we set up.
for hyb in range(2):
Expand Down
2 changes: 1 addition & 1 deletion tests/io_/v0_1_0/test_http_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

@pytest.fixture(scope="module")
def http_server(timeout_seconds=5):
with tempfile. TemporaryDirectory() as tempdir:
with tempfile.TemporaryDirectory() as tempdir:

port = unused_tcp_port()

Expand Down
15 changes: 7 additions & 8 deletions tests/io_/v0_1_0/test_missing_shape.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import codecs
import json
import os
import tempfile
import unittest
import warnings
from pathlib import Path

import numpy as np

import slicedimage
from slicedimage._dimensions import DimensionNames
from slicedimage.io._keys import TileKeys, TileSetKeys

baseurl = "file://{}".format(os.path.abspath(os.path.dirname(__file__)))
baseurl = Path(__file__).parent.resolve().as_uri()


class TestMissingShape(unittest.TestCase):
Expand Down Expand Up @@ -39,8 +39,9 @@ def test_tileset_without_shapes(self):

with tempfile.TemporaryDirectory() as tempdir, \
tempfile.NamedTemporaryFile(suffix=".json", dir=tempdir) as partition_file:
partition_doc = slicedimage.v0_0_0.Writer().generate_partition_document(
image, "file://{}".format(partition_file.name))
partition_file_path = Path(partition_file.name)
partition_doc = slicedimage.v0_1_0.Writer().generate_partition_document(
image, partition_file_path.as_uri())

# remove the shape information from the tiles.
for tile in partition_doc[TileSetKeys.TILES]:
Expand All @@ -50,10 +51,8 @@ def test_tileset_without_shapes(self):
json.dump(partition_doc, writer(partition_file))
partition_file.flush()

basename = os.path.basename(partition_file.name)
baseurl = "file://{}".format(os.path.dirname(partition_file.name))

loaded = slicedimage.Reader.parse_doc(basename, baseurl)
loaded = slicedimage.Reader.parse_doc(
partition_file_path.name, partition_file_path.parent.as_uri())

for hyb in range(2):
for ch in range(2):
Expand Down
24 changes: 11 additions & 13 deletions tests/io_/v0_1_0/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
import os
import tempfile
import unittest
from pathlib import Path

import tifffile
import numpy as np
from slicedimage._compat import fspath

import slicedimage
from slicedimage._dimensions import DimensionNames
from tests.utils import build_skeleton_manifest

baseurl = "file://{}".format(os.path.abspath(os.path.dirname(__file__)))
baseurl = Path(__file__).parent.resolve().as_uri()


class TestReader(unittest.TestCase):
Expand Down Expand Up @@ -60,6 +62,7 @@ def test_tiff(self):
Generate a tileset consisting of a single TIFF tile, and then read it.
"""
with tempfile.TemporaryDirectory() as tempdir:
tempdir_path = Path(tempdir)
# write the tiff file
data = np.random.randint(0, 65535, size=(120, 80), dtype=np.uint16)
with tifffile.TiffWriter(os.path.join(tempdir, "tile.tiff")) as tiff:
Expand Down Expand Up @@ -88,13 +91,10 @@ def test_tiff(self):
"format": "tiff",
},
)
with open(os.path.join(tempdir, "tileset.json"), "w") as fh:
with open(fspath(tempdir_path / "tileset.json"), "w") as fh:
fh.write(json.dumps(manifest))

result = slicedimage.Reader.parse_doc(
"tileset.json",
"file://{}".format(tempdir),
)
result = slicedimage.Reader.parse_doc("tileset.json", tempdir_path.as_uri())

self.assertTrue(np.array_equal(list(result.tiles())[0].numpy_array, data))

Expand Down Expand Up @@ -122,16 +122,14 @@ def test_numpy(self):
image.add_tile(tile)

with tempfile.TemporaryDirectory() as tempdir:
partition_path = os.path.join(tempdir, "tileset.json")
tempdir_path = Path(tempdir)
partition_file_path = tempdir_path / "tileset.json"
partition_doc = slicedimage.v0_0_0.Writer().generate_partition_document(
image, "file://{}".format(partition_path))
with open(partition_path, "w") as fh:
image, partition_file_path.as_uri())
with open(fspath(partition_file_path), "w") as fh:
json.dump(partition_doc, fh)

result = slicedimage.Reader.parse_doc(
"tileset.json",
"file://{}".format(tempdir),
)
result = slicedimage.Reader.parse_doc("tileset.json", tempdir_path.as_uri())

self.assertTrue(np.array_equal(list(result.tiles())[0].numpy_array, tile.numpy_array))

Expand Down
Loading

0 comments on commit 4e78b5f

Please sign in to comment.