Skip to content
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

[RFC][WIP] Auto map vars #750

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions orangecontrib/spectroscopy/io/agilent.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
agilentMosaicTiles
from orangecontrib.spectroscopy.io.util import SpectralFileFormat, _spectra_from_image, \
TileFileFormat, ConstantBytesVisibleImage
from orangecontrib.spectroscopy.utils import MAP_X_VAR, MAP_Y_VAR


def load_visible_images(vis_img_list: list[dict]) -> list[ConstantBytesVisibleImage]:
Expand Down Expand Up @@ -213,8 +214,8 @@ def read_tile(self):

attrs = [Orange.data.ContinuousVariable.make("%f" % f) for f in features]
domain = Orange.data.Domain(attrs, None,
metas=[Orange.data.ContinuousVariable.make("map_x"),
Orange.data.ContinuousVariable.make("map_y")]
metas=[Orange.data.ContinuousVariable.make(MAP_X_VAR),
Orange.data.ContinuousVariable.make(MAP_Y_VAR)]
)

try:
Expand Down
7 changes: 4 additions & 3 deletions orangecontrib/spectroscopy/io/ascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from orangecontrib.spectroscopy.util import getx
from orangecontrib.spectroscopy.io.util import SpectralFileFormat
from orangecontrib.spectroscopy.utils import MAP_X_VAR, MAP_Y_VAR


class AsciiColReader(FileFormat, SpectralFileFormat):
Expand Down Expand Up @@ -57,7 +58,7 @@ def read(self):
domain = Orange.data.Domain([ContinuousVariable.make("%f" % f) for f in dom_vals], None)
tbl = np.loadtxt(f, ndmin=2)
data = Table.from_numpy(domain, X=tbl[:, 2:])
metas = [ContinuousVariable.make('map_x'), ContinuousVariable.make('map_y')]
metas = [ContinuousVariable.make(MAP_X_VAR), ContinuousVariable.make(MAP_Y_VAR)]
domain = Orange.data.Domain(domain.attributes, None, metas=metas)
data = data.transform(domain)
with data.unlocked(data.metas):
Expand All @@ -68,8 +69,8 @@ def read(self):
@staticmethod
def write_file(filename, data):
wavelengths = getx(data)
map_x = data.domain["map_x"] if "map_x" in data.domain else ContinuousVariable("map_x")
map_y = data.domain["map_y"] if "map_y" in data.domain else ContinuousVariable("map_y")
map_x = data.domain[MAP_X_VAR] if MAP_X_VAR in data.domain else ContinuousVariable(MAP_X_VAR)
map_y = data.domain[MAP_Y_VAR] if MAP_Y_VAR in data.domain else ContinuousVariable(MAP_Y_VAR)
ndom = Domain([map_x, map_y] + list(data.domain.attributes))
data = data.transform(ndom)
with open(filename, "wb") as f:
Expand Down
18 changes: 10 additions & 8 deletions orangecontrib/spectroscopy/io/opus.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from .util import ConstantBytesVisibleImage

from orangecontrib.spectroscopy.utils import MAP_X_VAR, MAP_Y_VAR


class OPUSReader(FileFormat):
"""Reader for OPUS files"""
Expand Down Expand Up @@ -58,8 +60,8 @@ def read(self):
if type(data) == opusFC.MultiRegionDataReturn:
y_data = []
meta_data = []
metas.extend([ContinuousVariable.make('map_x'),
ContinuousVariable.make('map_y'),
metas.extend([ContinuousVariable.make(MAP_X_VAR),
ContinuousVariable.make(MAP_Y_VAR),
StringVariable.make('map_region'),
TimeVariable.make('start_time')])
for region in data.regions:
Expand All @@ -77,8 +79,8 @@ def read(self):
elif type(data) == opusFC.MultiRegionTRCDataReturn:
y_data = []
meta_data = []
metas.extend([ContinuousVariable.make('map_x'),
ContinuousVariable.make('map_y'),
metas.extend([ContinuousVariable.make(MAP_X_VAR),
ContinuousVariable.make(MAP_Y_VAR),
StringVariable.make('map_region')])
attrs = [ContinuousVariable.make(repr(data.labels[i]))
for i in range(len(data.labels))]
Expand All @@ -93,8 +95,8 @@ def read(self):
meta_data = np.vstack(meta_data)

elif type(data) == opusFC.ImageDataReturn:
metas.extend([ContinuousVariable.make('map_x'),
ContinuousVariable.make('map_y')])
metas.extend([ContinuousVariable.make(MAP_X_VAR),
ContinuousVariable.make(MAP_Y_VAR)])

data_3D = data.spectra

Expand All @@ -109,8 +111,8 @@ def read(self):
meta_data = np.vstack((meta_data, coord))

elif type(data) == opusFC.ImageTRCDataReturn:
metas.extend([ContinuousVariable.make('map_x'),
ContinuousVariable.make('map_y')])
metas.extend([ContinuousVariable.make(MAP_X_VAR),
ContinuousVariable.make(MAP_Y_VAR)])

attrs = [ContinuousVariable.make(repr(data.labels[i]))
for i in range(len(data.labels))]
Expand Down
5 changes: 3 additions & 2 deletions orangecontrib/spectroscopy/io/ptir.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from Orange.data import FileFormat

from orangecontrib.spectroscopy.io.util import SpectralFileFormat, _spectra_from_image
from orangecontrib.spectroscopy.utils import MAP_X_VAR, MAP_Y_VAR


class PTIRFileReader(FileFormat, SpectralFileFormat):
Expand Down Expand Up @@ -174,8 +175,8 @@ def read_spectra(self):
metas = np.array([x_locs[x_loc], y_locs[y_loc]]).T

domain = Orange.data.Domain([], None,
metas=[Orange.data.ContinuousVariable.make("map_x"),
Orange.data.ContinuousVariable.make("map_y")]
metas=[Orange.data.ContinuousVariable.make(MAP_X_VAR),
Orange.data.ContinuousVariable.make(MAP_Y_VAR)]
)
data = Orange.data.Table.from_numpy(domain, X=np.zeros((len(spectra), 0)),
metas=np.asarray(metas, dtype=object))
Expand Down
6 changes: 4 additions & 2 deletions orangecontrib/spectroscopy/io/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import numpy as np
from Orange.data import Domain, ContinuousVariable, Table

from orangecontrib.spectroscopy.utils import MAP_X_VAR, MAP_Y_VAR


class SpectralFileFormat:

Expand All @@ -29,8 +31,8 @@ def _metatable_maplocs(x_locs, y_locs):
metas = np.vstack((x_locs, y_locs)).T

domain = Domain([], None,
metas=[ContinuousVariable.make("map_x"),
ContinuousVariable.make("map_y")]
metas=[ContinuousVariable.make(MAP_X_VAR),
ContinuousVariable.make(MAP_Y_VAR)]
)
data = Table.from_numpy(domain, X=np.zeros((len(metas), 0)),
metas=np.asarray(metas, dtype=object))
Expand Down
28 changes: 27 additions & 1 deletion orangecontrib/spectroscopy/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import numpy as np

from Orange.data import Domain, Table
from Orange.data import Domain, Table, ContinuousVariable
from Orange.widgets.utils.itemmodels import DomainModel
from orangewidget.utils.itemmodels import PyListModel

MAP_X_VAR = "map_x"
MAP_Y_VAR = "map_y"


def apply_columns_numpy(array, function, selector=None, chunk_size=10 ** 7, callback=None):
Expand Down Expand Up @@ -145,3 +150,24 @@ def split_to_size(size, interval):
intervals.append(slice(pos, pos + min(size - pos, interval)))
pos += min(size, interval)
return intervals


class XYDomainModel(DomainModel):
XY = (DomainModel.METAS, PyListModel.Separator,
DomainModel.CLASSES)

def __init__(self, order=XY, valid_types=ContinuousVariable, **kwargs):
super().__init__(order=order, valid_types=valid_types, **kwargs)

def set_domain(self, domain):
restore_order = False
if domain is not None and self.order == self.XY:
attr_x = [domain[var] for var in domain if var.name == MAP_X_VAR]
attr_y = [domain[var] for var in domain if var.name == MAP_Y_VAR]
xy = attr_x + attr_y
if len(xy) == 2:
self.order = (xy, PyListModel.Separator) + self.order
restore_order = True
super().set_domain(domain)
if restore_order:
self.order = self.order[2:]
5 changes: 2 additions & 3 deletions orangecontrib/spectroscopy/widgets/owhyper.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from orangewidget.utils.visual_settings_dlg import VisualSettingsDialog

from orangecontrib.spectroscopy.preprocess import Integrate
from orangecontrib.spectroscopy.utils import values_to_linspace, index_values_nan, split_to_size
from orangecontrib.spectroscopy.utils import values_to_linspace, index_values_nan, split_to_size, XYDomainModel

from orangecontrib.spectroscopy.widgets.owspectra import InteractiveViewBox, \
MenuFocus, CurvePlot, SELECTONE, SELECTMANY, INDIVIDUAL, AVERAGE, \
Expand Down Expand Up @@ -287,8 +287,7 @@ def color_palette_model(palettes, iconsize=QSize(64, 16)):
class AxesSettingsMixin:

def __init__(self):
self.xy_model = DomainModel(DomainModel.METAS | DomainModel.CLASSES,
valid_types=DomainModel.PRIMITIVE)
self.xy_model = XYDomainModel()

def setup_axes_settings_box(self):
box = gui.vBox(self)
Expand Down
Loading