-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow using image parameters for preprocessing #14
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b695192
image_opts to be passed to preprocessors
markotoplak d9e45af
image opts that can come into a preprocessor
markotoplak 266f904
linelevel: perform the computation (but results are not yet arranged …
markotoplak 626664d
linelevel: arrange results too
markotoplak 44b9465
more robust implementation
markotoplak 5791466
Preprocess image: changing image properties influences the data, thus…
markotoplak e24b735
Utility classes for image proprocessing
markotoplak 52b5441
BackgroundFit: supports image settings
markotoplak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,94 @@ | ||
import numpy as np | ||
|
||
from Orange.data import Domain | ||
from Orange.preprocess import Preprocess | ||
from orangecontrib.spectroscopy.preprocess import CommonDomain, SelectColumn | ||
from orangecontrib.spectroscopy.utils import ( | ||
InvalidAxisException, | ||
values_to_linspace, | ||
index_values, | ||
) | ||
|
||
|
||
class PreprocessImageOpts(Preprocess): | ||
pass | ||
|
||
|
||
class PreprocessImageOpts2D(PreprocessImageOpts): | ||
def __call__(self, data, image_opts): | ||
common = self.image_transformer(data, image_opts) | ||
at = data.domain[image_opts["attr_value"]].copy( | ||
compute_value=SelectColumn(0, common) | ||
) | ||
domain = domain_with_single_attribute_in_x(at, data.domain) | ||
return data.transform(domain) | ||
|
||
def image_transformer(self, data, image_opts): | ||
raise NotImplementedError | ||
|
||
|
||
def axes_to_ndim_linspace(coordinates): | ||
# modified to avoid domains as much as possible | ||
ls = [] | ||
indices = [] | ||
|
||
for i in range(coordinates.shape[1]): | ||
coor = coordinates[:, i] | ||
lsa = values_to_linspace(coor) | ||
if lsa is None: | ||
raise InvalidAxisException(i) | ||
ls.append(lsa) | ||
indices.append(index_values(coor, lsa)) | ||
|
||
return ls, tuple(indices) | ||
|
||
|
||
def get_ndim_hyperspec(data, attrs): | ||
# mostly copied from orangecontrib.spectroscopy.utils, | ||
# but returns the indices too | ||
# also avoid Orange domains as much as possible | ||
coordinates = np.hstack([data.get_column(a).reshape(-1, 1) for a in attrs]) | ||
|
||
ls, indices = axes_to_ndim_linspace(coordinates) | ||
|
||
# set data | ||
new_shape = tuple([lsa[2] for lsa in ls]) + (data.X.shape[1],) | ||
hyperspec = np.ones(new_shape) * np.nan | ||
|
||
hyperspec[indices] = data.X | ||
|
||
return hyperspec, ls, indices | ||
|
||
|
||
def domain_with_single_attribute_in_x(attribute, domain): | ||
"""Create a domain with only the attribute in domain.attributes and ensure | ||
that the same attribute is removed from metas and class_vars if it was present | ||
there.""" | ||
class_vars = [a for a in domain.class_vars if a.name != attribute.name] | ||
metas = [a for a in domain.metas if a.name != attribute.name] | ||
return Domain([attribute], class_vars, metas) | ||
|
||
|
||
class CommonDomainImage2D(CommonDomain): | ||
def __init__(self, domain: Domain, image_opts: dict): | ||
self.domain = domain | ||
self.image_opts = image_opts | ||
|
||
def __call__(self, data): | ||
data = self.transform_domain(data) | ||
vat = data.domain[self.image_opts["attr_value"]] | ||
ndom = domain_with_single_attribute_in_x(vat, data.domain) | ||
data = data.transform(ndom) | ||
try: | ||
hypercube, _, indices = get_ndim_hyperspec( | ||
data, (self.image_opts["attr_x"], self.image_opts["attr_y"]) | ||
) | ||
image = hypercube[:, :, 0] | ||
transformed = self.transform_image(image) | ||
return transformed[indices].reshape(-1, 1) | ||
except InvalidAxisException: | ||
return np.full((len(data), 1), np.nan) | ||
return self.transformed(data) | ||
|
||
def transform_image(self, image): | ||
raise NotImplementedError |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious about this, is it faster? Should we just/also change the orangecontrib.spectroscopy.utils implementation? I think it used domains because
get_column()
didn't exist at that time.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did it because it seemed nicer to avoid domains where there are not needed. I do not expect any performance speedups, and yes, we could also do it in Spectroscopy.