-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
17 changed files
with
422 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.. _menpo-visualize-bytes_str: | ||
|
||
.. currentmodule:: menpo.visualize | ||
|
||
bytes_str | ||
========= | ||
.. autofunction:: bytes_str |
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 |
---|---|---|
|
@@ -39,6 +39,7 @@ Print Utilities | |
.. toctree:: | ||
:maxdepth: 1 | ||
|
||
print_progress | ||
print_dynamic | ||
progress_bar_str | ||
print_bytes | ||
bytes_str |
This file was deleted.
Oops, something went wrong.
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,7 @@ | ||
.. _menpo-visualize-print_progress: | ||
|
||
.. currentmodule:: menpo.visualize | ||
|
||
print_progress | ||
============== | ||
.. autofunction:: print_progress |
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,50 @@ | ||
from menpo.image import Image, MaskedImage | ||
import numpy as np | ||
|
||
|
||
def test_rescale_pixels(): | ||
img = Image.init_blank((10, 10), n_channels=1) | ||
img.pixels[:, 6:, 6:] = 2 | ||
|
||
img_rescaled = img.rescale_pixels(0, 255) | ||
assert np.min(img_rescaled.pixels) == 0 | ||
assert np.max(img_rescaled.pixels) == 255 | ||
assert np.all(img_rescaled.pixels[:, 6:, 6:] == 255) | ||
|
||
|
||
def test_rescale_pixels_all_channels(): | ||
img = Image.init_blank((10, 10), n_channels=2) | ||
img.pixels[0, 6:, 6:] = 2 | ||
img.pixels[1, 6:, 6:] = 4 | ||
|
||
img_rescaled = img.rescale_pixels(0, 100, per_channel=False) | ||
assert np.min(img_rescaled.pixels) == 0 | ||
assert np.max(img_rescaled.pixels) == 100 | ||
assert np.all(img_rescaled.pixels[0, 6:, 6:] == 50) | ||
assert np.all(img_rescaled.pixels[1, 6:, 6:] == 100) | ||
|
||
|
||
def test_rescale_pixels_per_channel(): | ||
img = Image.init_blank((10, 10), n_channels=2) | ||
img.pixels[0, 6:, 6:] = 2 | ||
img.pixels[1, 6:, 6:] = 4 | ||
|
||
img_rescaled = img.rescale_pixels(0, 100, per_channel=True) | ||
assert np.min(img_rescaled.pixels) == 0 | ||
assert np.max(img_rescaled.pixels) == 100 | ||
assert np.all(img_rescaled.pixels[0, 6:, 6:] == 100) | ||
assert np.all(img_rescaled.pixels[1, 6:, 6:] == 100) | ||
|
||
|
||
def test_rescale_pixels_only_masked(): | ||
img = MaskedImage.init_blank((10, 10), n_channels=1, fill=1) | ||
img.pixels[0, 0, 0] = 0 | ||
img.pixels[0, 6:, 6:] = 2 | ||
img.mask.pixels[:, 6:, 6:] = False | ||
|
||
img_rescaled = img.rescale_pixels(0, 100) | ||
assert np.min(img_rescaled.pixels) == 0 | ||
assert np.max(img_rescaled.pixels) == 100 | ||
assert img_rescaled.pixels[0, 0, 0] == 0 | ||
assert img_rescaled.pixels[0, 1, 1] == 100 | ||
assert np.all(img_rescaled.mask.pixels == img.mask.pixels) |
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
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
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,8 +1,55 @@ | ||
from contextlib import contextmanager | ||
from pathlib import Path, PurePath | ||
try: | ||
import cPickle as pickle | ||
except ImportError: | ||
import cPickle as pickle # request cPickle manually on Py2 | ||
except ImportError: # Py3 | ||
import pickle | ||
|
||
|
||
# -------------- Custom pickle behavior for pathlib.Path objects ------------ # | ||
# | ||
# We make heavy use of pathlib.Path throughout Menpo - most obviously, any | ||
# imported item has it's path automatically duck-typed on at self.path. | ||
# | ||
# This causes issues with serialization however. pathlib has two families of | ||
# paths - PurePaths, which you can perform manipulation on but have no | ||
# connection to the file system, and concrete paths - paths which additionally | ||
# have functional methods on them like .is_dir() and .mkdir(). You are not | ||
# allowed to instantiate a concrete path for one platform (e.g. a WindowsPath) | ||
# on a different platform (e.g. Linux - which is a PosixPath) as the additional | ||
# filesystem methods will not make sense. | ||
# | ||
# As we attach concrete paths liberally in Menpo, many Menpo objects | ||
# serialized on one platform (e.g. OS X) will not be unpickable on another | ||
# (e.g. Windows). | ||
# | ||
# To alleviate this issue, we override the pickling behavior of concrete paths | ||
# so that they pickle out as PurePaths. This ensures you can always open | ||
# pickled objects on other platforms. We do this using a context manager, so | ||
# don't effect the pickle behavior of these objects globally. | ||
# | ||
def _pure_path_reduce(self): | ||
# Pickled paths should go to pure paths so pickles are | ||
# useful across different OSes | ||
return PurePath(self).__class__, tuple(self.parts) | ||
|
||
|
||
@contextmanager | ||
def pickle_paths_as_pure(): | ||
r""" | ||
Pickle pathlib.Path subclasses as their corresponding pathlib.PurePath | ||
""" | ||
# save out the original method | ||
default_reduce = Path.__reduce__ | ||
# switch out to our PurePath varient | ||
Path.__reduce__ = _pure_path_reduce | ||
try: | ||
yield | ||
finally: | ||
# always clean up - restore to default behavior | ||
Path.__reduce__ = default_reduce | ||
|
||
|
||
def pickle_export(obj, file_handle): | ||
pickle.dump(obj, file_handle, protocol=2) | ||
with pickle_paths_as_pure(): | ||
pickle.dump(obj, file_handle, protocol=2) |
Oops, something went wrong.