Testing Class Material
This commit is contained in:
commit
05b1f6cdd5
85 changed files with 102796 additions and 0 deletions
Binary file not shown.
After Width: | Height: | Size: 415 KiB |
22
extra_slides/packaging/noiser_project_final/noiser/main.py
Normal file
22
extra_slides/packaging/noiser_project_final/noiser/main.py
Normal file
|
@ -0,0 +1,22 @@
|
|||
import os.path
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
from scipy.ndimage import imread
|
||||
from pkg_resources import resource_filename
|
||||
|
||||
from noiser.noise import white_noise
|
||||
from noiser.utils import copy_image
|
||||
|
||||
|
||||
def main():
|
||||
path = resource_filename('noiser', os.path.join('images', 'baboon_kandinsky.png'))
|
||||
print(path)
|
||||
img = imread(path)
|
||||
noisy = copy_image(white_noise(img, 20))
|
||||
plt.imshow(noisy)
|
||||
plt.draw()
|
||||
plt.show()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
|
@ -0,0 +1,7 @@
|
|||
import numpy as np
|
||||
|
||||
|
||||
def white_noise(image, std):
|
||||
noise = np.random.normal(scale=std, size=image.shape).astype(image.dtype)
|
||||
noisy = image + noise
|
||||
return noisy
|
|
@ -0,0 +1,8 @@
|
|||
from setuptools import setup, find_packages
|
||||
|
||||
setup(
|
||||
name='Noiser',
|
||||
version='1.0',
|
||||
packages=find_packages(),
|
||||
)
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
import numpy as np
|
||||
from numpy.testing import assert_allclose
|
||||
|
||||
from noiser.noise import white_noise
|
||||
|
||||
|
||||
def test_white_noise():
|
||||
n_images, height, width = 201, 101, 102
|
||||
dtype = np.float32
|
||||
|
||||
# Create ``n_images`` identical image.
|
||||
base_image = np.random.rand(1, height, width, 3).astype(dtype) - 0.5
|
||||
images = np.repeat(base_image, n_images, axis=0)
|
||||
|
||||
std = 0.13
|
||||
noisy = white_noise(images, std=std)
|
||||
|
||||
# dtype and shape are preserved.
|
||||
assert noisy.dtype == dtype
|
||||
assert noisy.shape == images.shape
|
||||
|
||||
# Mean and std of noisy image match expectations.
|
||||
assert_allclose(images.mean(0), base_image[0], atol=1e-4)
|
||||
assert np.isclose((noisy - images).std(), std, atol=1e-4)
|
|
@ -0,0 +1,14 @@
|
|||
import numpy as np
|
||||
from numpy.testing import assert_array_equal
|
||||
|
||||
from noiser.utils import copy_image
|
||||
|
||||
|
||||
def test_copy_image():
|
||||
height, width = 101, 102
|
||||
dtype = np.float32
|
||||
|
||||
image = np.random.rand(height, width, 3).astype(dtype)
|
||||
copy = copy_image(image)
|
||||
assert_array_equal(copy, image)
|
||||
|
15
extra_slides/packaging/noiser_project_final/noiser/utils.pyx
Normal file
15
extra_slides/packaging/noiser_project_final/noiser/utils.pyx
Normal file
|
@ -0,0 +1,15 @@
|
|||
import numpy as np
|
||||
cimport numpy as np
|
||||
|
||||
|
||||
def copy_image(np.ndarray img):
|
||||
cdef int h = img.shape[0]
|
||||
cdef int w = img.shape[1]
|
||||
cdef int c = img.shape[2]
|
||||
cdef np.ndarray copy = np.empty([h, w, c], dtype=img.dtype)
|
||||
|
||||
for i in range(h):
|
||||
for j in range(w):
|
||||
for k in range(c):
|
||||
copy[i, j, k] = img[i, j, k]
|
||||
return copy
|
29
extra_slides/packaging/noiser_project_final/setup.py
Normal file
29
extra_slides/packaging/noiser_project_final/setup.py
Normal file
|
@ -0,0 +1,29 @@
|
|||
from Cython.Build import cythonize
|
||||
import numpy
|
||||
from setuptools import setup, find_packages
|
||||
from setuptools.extension import Extension
|
||||
|
||||
|
||||
extensions = [
|
||||
Extension(
|
||||
'noiser.utils',
|
||||
["noiser/utils.pyx"],
|
||||
include_dirs=[numpy.get_include()],
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
setup(
|
||||
name='Noiser',
|
||||
version='1.0',
|
||||
packages=find_packages(),
|
||||
ext_modules=cythonize(extensions),
|
||||
entry_points={
|
||||
'console_scripts': [
|
||||
'baboon=noiser.main:main',
|
||||
]
|
||||
},
|
||||
package_data={
|
||||
'noiser': ['images/*.png'],
|
||||
}
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue