Accessing and Modifying WISER State¶
Tool plugins and context-menu plugins can access and manipulate WISER
application state through the ApplicationState class. The plugin can
access an instance of this class via the "wiser" value in the context
passed to the plugin.
- class wiser.gui.app_state.ApplicationState(app, config: ApplicationConfig | None = None)[source]¶
This class holds all WISER application state.
- take_next_id() int[source]¶
Returns the next ID for use with an object, and also increments the internal “next ID” value.
- get_current_dir() str[source]¶
Returns the current directory of the application. This is the last directory that the user accessed in a load or save operation, so that the next load or save can start at the same directory.
- set_current_dir(current_dir: str) None[source]¶
Sets the current directory of the application. This is the last directory that the user accessed in a load or save operation, so that the next load or save can start at the same directory.
- update_cwd_from_path(path: str) None[source]¶
This helper function makes it easier to update the current working directory (CWD) at the end of a file-load or file-save operation. The specified path is assumed to be either a directory or a file:
If it is a directory then the current directory is set to that path.
If it is a file then the directory portion of the path is taken and used for the current directory.
The function only updates the current directory if the filesystem actually reports it as a valid directory. If the directory can’t be identified from the specified path (e.g. the OS doesn’t report the path as a valid directory), this function simply logs a warning.
- open_file(file_path)[source]¶
A general file-open operation in WISER. This method can be used for loading any kind of data file whose type and contents can be identified automatically. This operation should not be used for importing ASCII spectral data, regions of interest, etc. since WISER cannot identify the file’s contents automatically.
- add_dataset(dataset: RasterDataSet, view_dataset: bool = True)[source]¶
Add a dataset to the application state. A unique numeric ID is assigned to the dataset, which is also set on the dataset itself.
The method will fire a signal indicating that the dataset was added.
- has_dataset(ds_id: int) bool[source]¶
Returns whether the dataset with the specified numeric ID is in the application state.
- get_dataset(ds_id: int) RasterDataSet[source]¶
Return the dataset with the specified numeric ID. If the ID is unrecognized then a KeyError will be raised.
- get_datasets() List[RasterDataSet][source]¶
Return a list of datasets in the application state. The returned list is separate from the internal application-state data structures, and therefore may be mutated by the caller without harm.
- remove_dataset(ds_id: int)[source]¶
Remove the dataset with the specified numeric ID from the application state and the cache. If the ID is unrecognized then a KeyError will be raised.
The method will fire a signal indicating that the dataset was removed.
- multiple_datasets_same_size()[source]¶
This function returns True if there are multiple datasets, and they are all the same size. If either of these cases is not true then False is returned.
- multiple_displayed_datasets_same_size()[source]¶
This function returns True if there are multiple visible datasets and they are all the same size.
- add_spectral_library(library)[source]¶
Add a spectral library to the application state, assigning a new ID to the library. The method will fire a signal indicating that the spectral library was added, including the ID assigned to the library.
- has_spectral_library(lib_id: int) bool[source]¶
Returns whether the spectral library with the specified numeric ID is in the application state.
- get_spectral_library(lib_id)[source]¶
Return the spectral library with the specified ID. If the ID is unrecognized, a KeyError will be raised.
- get_spectral_libraries()[source]¶
Return a list of all the spectral libraries in the application state.
- remove_spectral_library(lib_id)[source]¶
Remove the specified spectral library from the application state. The method will fire a signal indicating that the spectral library was removed.
- get_config(option: str, default=None, as_type=None)[source]¶
Returns the value of the specified config option. An optional default value may be specified.
Options are specified as a sequence of names separated by dots ‘.’, just like a series of object-member accesses on an object hierarchy.
- add_roi(roi: RegionOfInterest, make_name_unique=False) None[source]¶
Add a Region of Interest to WISER’s state. A
ValueErroris raised if the ROI does not have a unique name.
- remove_roi(roi_id: int) None[source]¶
Removes the specified Region of Interest from WISER’s state. A
KeyErroris raised if no ROI has the specified ID.
- get_roi(**kwargs) RegionOfInterest | None[source]¶
Retrieve a Region of Interest from WISER’s state. The caller may specify an
idkeyword-argument to retrieve an ROI by ID, or anamekeyword-argument to retrieve an ROI by name. Names are case-sensitive.
- get_rois() List[RegionOfInterest][source]¶
Returns a list of all Regions of Interest in WISER’s application state.
- has_spectrum(spectrum_id: int) bool[source]¶
Returns whether the spectrum with the specified numeric ID is in the application state.
- get_spectrum(spectrum_id: int) Spectrum[source]¶
Retrieve a spectrum from WISER’s state. A
KeyErroris raised if the ID doesn’t correspond to a spectrum.
- get_active_spectrum()[source]¶
Retrieve the current active spectrum. The “active spectrum” is the spectrum that the user most recently selected, or it may be the output of a band-math expression, plugin, etc. that computes a spectrum.
- set_active_spectrum(spectrum: Spectrum)[source]¶
Set the current active spectrum to be the specified spectrum. The “active spectrum” is the spectrum that the user most recently selected, or it may be the output of a band-math expression, plugin, etc. that computes a spectrum.
- collect_spectrum(spectrum: Spectrum)[source]¶
Add the specified spectrum to the “collected spectra” group.
Note that the specified spectrum cannot be the current “active spectrum”; if it is, a
RuntimeErrorwill be raised. The current “active spectrum” is collected via thecollect_active_spectrum()method.
- collect_active_spectrum()[source]¶
Add the current “active spectrum” to the “collected spectra” group.
A
RuntimeErrorwill be raised if there is no active spectrum when this method is called.
- remove_collected_spectrum(index)[source]¶
Removes a collected spectrum from the list of collected spectra. The spectrum to remove is specified by a 0-based index.
- show_spectra_in_plot(spectra: List[Spectrum], plot_title: str | None = None)[source]¶
Takes the list of spectra passed in and displays it in a generic spectrum plot.
Operations involving loading and storing raster data sets will require the use
of a loader object, also accessible off of the ApplicationState object.
See the get_loader() function above. The loader offers these operations:
- class wiser.raster.RasterDataLoader[source]¶
A loader for loading 2D raster data-sets from the local filesystem, using GDAL (Geospatial Data Abstraction Library) for reading the data.
- load_normal_dataset(impl: RasterDataImpl, data_cache: DataCache) List[RasterDataSet][source]¶
The normal way to load in a dataset
- load_from_file(path, data_cache=None, interactive=True, subdataset_name='') List[RasterDataSet][source]¶
Load a raster data-set from the specified path. Returns a list of
RasterDataSetobject.
- dataset_from_numpy_array(arr: ndarray, cache: DataCache = None) RasterDataSet[source]¶
Given a NumPy ndarray, this function returns a RasterDataSet object that uses the array for its raster data. The input ndarray must have three dimensions; they are interpreted as [spectral][spatial_y][spatial_x].
Raises a ValueError if the input array doesn’t have 3 dimensions.
Loading Raster Data into WISER¶
The above classes expose two ways to load raster data into WISER. The first
option is to use the ApplicationState.open_file() function, which simply
loads a file into WISER. Several kinds of files (raster images, spectral
libraries, and a few other kinds of files) are supported by this method. No
value is returned, so this function is not useful for getting access to the
contents of a specific raster file.
To open a raster data file and get back an object to access its contents
requires the use of the RasterDataLoader. The loader is accessed from the
ApplicationState.get_loader() method, as described above. Note that any
raster image loaded this way is not automatically displayed; after it has been
loaded and possibly manipulated, it must be added to the ApplicationState
object with the add_dataset() method, before it will be displayed.
Similarly, a NumPy array can be converted into a RasterDataSet object using
the RasterDataLoader.dataset_from_numpy_array() method. It will likely be
desirable to add metadata to the resulting RasterDataSet object (it is
convenient to copy it from an existing data-set, if the new object was generated
from an existing data-set), but this is not required.
Finally, it is not advisable to change a RasterDataSet object after handing
it to WISER for display, as WISER currently expects that the object will not
change once it has been given to WISER.