Skip to content

Commit

Permalink
Merge pull request #654 from stuart-cls/standard-map-vars
Browse files Browse the repository at this point in the history
Standard map vars
  • Loading branch information
markotoplak authored Sep 27, 2024
2 parents bc77ae1 + 19eed70 commit f5fdbb2
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 deletions.
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
19 changes: 11 additions & 8 deletions orangecontrib/spectroscopy/io/opus.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import numpy as np
from Orange.data import FileFormat, ContinuousVariable, StringVariable, TimeVariable

from orangecontrib.spectroscopy.utils import MAP_X_VAR, MAP_Y_VAR

from .util import ConstantBytesVisibleImage



class OPUSReader(FileFormat):
"""Reader for OPUS files"""

Expand Down Expand Up @@ -58,8 +61,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 +80,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 +96,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 +112,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
3 changes: 3 additions & 0 deletions orangecontrib/spectroscopy/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

from Orange.data import Domain, Table

MAP_X_VAR = "map_x"
MAP_Y_VAR = "map_y"


def apply_columns_numpy(array, function, selector=None, chunk_size=10 ** 7, callback=None):
"""Split the array by columns, applies selection and then the function.
Expand Down

0 comments on commit f5fdbb2

Please sign in to comment.