1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
"""
Property Traits
===============
The ``Image`` class has three traits which are closely related: ``scan_size``,
``scan_width`` and ``scan_height``. We would ideally like to keep all of these
synchronized. This can be done with trait observation, as shown in the
previous section, but this sort of pattern is common enough that Traits has
some in-built helpers.
Instead of declaring that ``scan_width = Float()`` we could instead declare it
to be a ``Property`` trait type. Property traits are similar to ``@property``
decorators in standard Python in that rather than storing a value, they compute
a derived value via a "getter", and optionally store values via a "setter".
Traits uses specially named methods of the form ``_get_<property>`` and
``_set_property`` for these "getters" and "setters." If there is a "getter"
but no "setter" then the property is read-only.
Additionally, we need to know when the value of the property might change, and
so we need to declare what traits to observe to know when the property might
change. What all this means is that we can define ``scan_width`` as a
property by::
class Image(HasTraits):
...
scan_width = Property(Float, depends_on='scan_size')
def _get_scan_width(self):
return self.scan_size[0]
def _set_scan_width(self, value):
self.scan_size = (value, self.scan_height)
Traits will then take care of hooking up all the required observers to make
everything work as expected; and the `Property` can also be observed if
desired.
Simple `Property` traits like this are computed "lazily": the value is only
calculated when you ask for it.
Cached Properties
-----------------
It would be quite easy to turn the histogram function into a read-only
property. This might look something like this::
class Image(HasTraits):
...
histogram = Property(Array, depends_on='image')
def _get_histogram(self):
hist, bins = np.histogram(
self.image,
bins=256,
range=(0, 256),
density=True,
)
return hist
This works, but it has a downside that the histogram is re-computed every
time the property is accessed. For small images, this is probably OK, but
for larger images, or if you are working with many images at once, this may
impact performance. In these cases, you can specify that the property
should **cache** the returned value and use that value until the trait(s)
the property depend on changes::
class Image(HasTraits):
...
histogram = Property(Array, depends_on='image')
@cached_property
def _get_histogram(self):
hist, bins = np.histogram(
self.image,
bins=256,
range=(0, 256),
density=True,
)
return hist
This has the trade-off that the result of the computation is being stored
in memory, but in this case the memory is only a few hundred bytes, and so
is unlikely to cause problems; but you probably wouldn't want to do this
with a multi-gigabyte array.
Exercise
--------
Make ``pixel_area`` a read-only property.
"""
import os
import datetime
from PIL import Image as PILImage
import numpy as np
from traits.api import (
Array, Date, File, Float, HasTraits, Property, Str, Tuple,
cached_property, observe
)
class Image(HasTraits):
""" An SEM image stored in a file. """
filename = File(exists=True)
sample_id = Str()
operator = Str("N/A")
date_acquired = Date()
scan_size = Tuple(Float, Float)
scan_width = Property(Float, depends_on='scan_size')
scan_height = Property(Float, depends_on='scan_size')
image = Array(shape=(None, None), dtype='uint8')
histogram = Property(Array, depends_on='image')
pixel_area = Float()
# Trait observers
@observe('filename')
def read_image(self, event):
pil_image = PILImage.open(self.filename).convert("L")
self.image = np.array(pil_image)
# compute some extra secondary attributes from the image
if self.image.size > 0:
self.pixel_area = (
self.scan_height * self.scan_width / self.image.size
)
else:
self.pixel_area = 0
# Trait default methods
def _date_acquired_default(self):
return datetime.date.today()
def _scan_size_default(self):
return (1e-5, 1e-5)
# Trait property methods
def _get_scan_width(self):
return self.scan_size[0]
def _set_scan_width(self, value):
self.scan_size = (value, self.scan_size[1])
def _get_scan_height(self):
return self.scan_size[1]
def _set_scan_height(self, value):
self.scan_size = (self.scan_size[0], value)
@cached_property
def _get_histogram(self):
hist, bins = np.histogram(
self.image,
bins=256,
range=(0, 256),
density=True,
)
return hist
# ---------------------------------------------------------------------------
# Demo code
# ---------------------------------------------------------------------------
this_dir = os.path.dirname(__file__)
image_dir = os.path.join(this_dir, "images")
filename = os.path.join(image_dir, "sample_0001.png")
# load the image
image = Image(
filename=filename,
operator="Hannes",
sample_id="0001",
)
# perform some sample computations
print("Scan size:", image.scan_size)
print("Change scan width to 1e-4")
image.scan_width = 1e-4
print("Scan size:", image.scan_size)
for filename in os.listdir(image_dir):
if os.path.splitext(filename)[1] == '.png':
print()
print("Changing filename to {}".format(filename))
image.filename = os.path.join(image_dir, filename)
print(
"The most frequent intensity of {} is {}".format(
filename,
image.histogram.argmax(),
)
)
|