Skip to content

Commit 813254a

Browse files
authored
Merge pull request #216 from siapy/fix
2 parents c90e7dd + d29536f commit 813254a

File tree

13 files changed

+575
-187
lines changed

13 files changed

+575
-187
lines changed

CONTRIBUTING.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [Pdm](#pdm)
1010
- [Highlights](#highlights)
1111
- [Key commands](#key-commands)
12+
- [Design philosophy](#design-philosophy)
1213
- [Testing with pytest](#testing-with-pytest)
1314
- [GitHub Actions workflows](#github-actions-workflows)
1415
- [Maintainers](#maintainers)
@@ -57,6 +58,16 @@ pdm shell # Activate the PDM environment, similar to activating a virtualenv
5758
pdm sync # Synchronize the project's dependencies
5859
```
5960

61+
### Design philosophy
62+
63+
SiaPy is built with several key design principles:
64+
65+
1. **Type Safety**: Comprehensive type hints throughout the codebase
66+
2. **Modular Design**: Composable components that can be used independently
67+
3. **Consistent APIs**: Uniform interface patterns across the library
68+
4. **Pythonic Interfaces**: Following Python best practices and conventions
69+
5. **Error Handling**: Structured exception hierarchy for clear error reporting
70+
6071
### Testing with pytest
6172

6273
- Tests are in the _tests/_ directory.

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ __Documentation__: <https://siapy.github.io/siapy-lib/>
3737

3838
__SiaPy__ is a versatile Python library designed for processing and analyzing spectral images. It is particularly useful for scientific and academic purposes, but it also serves well for quick prototyping.
3939

40-
Built on top of the well-known [spectral](https://github.com/spectralpython/spectral) library, SiaPy extends its capabilities with additional features and functionalities.
41-
4240
### Key Features
4341

4442
- __Image Processing__: Easily read, display, and manipulate spectral image data.
@@ -57,7 +55,7 @@ To install the siapy library, use the following command:
5755
pip install siapy
5856
```
5957

60-
For detailed information and additional options, please refer to the [instructions](https://siapy.github.io/siapy-lib/install/).
58+
For detailed information and additional options, please refer to the [instructions](https://siapy.github.io/siapy-lib/latest/install/).
6159

6260
## 💻 Examples
6361

docs/concepts/entities.md

Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
# Entities
2+
3+
Entities form the core data structures in SiaPy that represent fundamental elements of spectral image processing. They provide consistent interfaces for working with various kinds of spectral data and spatial information.
4+
5+
## Overview
6+
7+
The SiaPy Entities module defines a set of interconnected classes that represent the foundational building blocks for spectral image analysis:
8+
9+
- **SpectralImage**: A generic container for different types of hyperspectral/multispectral images
10+
- **SpectralImageSet**: A collection of spectral images
11+
- **Pixels**: Spatial coordinates within an image
12+
- **Signatures**: Spectral signals associated with specific pixel locations
13+
- **Shape**: Geometric shapes associated with image locations (points, lines, polygons)
14+
15+
These entity classes are designed to work together, forming a cohesive system for analyzing spectral imagery.
16+
17+
## SpectralImage
18+
19+
A `SpectralImage` is the primary container for spectral image data. It's a generic class that can wrap different image backends:
20+
21+
```python
22+
from siapy.entities import SpectralImage
23+
24+
# Load from ENVI format
25+
image = SpectralImage.spy_open(
26+
header_path="path/to/header.hdr",
27+
image_path="path/to/image.img"
28+
)
29+
30+
# Load from GeoTIFF or other raster formats
31+
image = SpectralImage.rasterio_open(filepath="path/to/image.tif")
32+
33+
# Create from NumPy array
34+
import numpy as np
35+
array = np.zeros((100, 100, 10)) # height, width, bands
36+
image = SpectralImage.from_numpy(array)
37+
```
38+
39+
### Key Properties
40+
41+
- **shape**: Dimensions as (height, width, bands)
42+
- **width**, **height**: Image dimensions
43+
- **bands**: Number of spectral bands
44+
- **wavelengths**: List of wavelength values for each band
45+
- **default_bands**: Default bands for RGB visualization
46+
- **metadata**: Dictionary of metadata from the image file
47+
- **filepath**: Path to the source file
48+
- **camera_id**: Camera identifier (if available)
49+
- **geometric_shapes**: Associated geometric shapes collection
50+
51+
### Key Methods
52+
53+
- **to_numpy()**: Convert to NumPy array
54+
- **to_display()**: Convert to PIL Image for visualization
55+
- **to_xarray()**: Convert to xarray.DataArray
56+
- **to_signatures()**: Extract signatures at specified pixels
57+
- **to_subarray()**: Extract a subarray for a region of interest
58+
- **average_intensity()**: Calculate mean intensity across specified axes
59+
60+
## Pixels
61+
62+
The `Pixels` class represents spatial coordinates within an image, typically stored as x,y pairs.
63+
64+
```python
65+
from siapy.entities import Pixels
66+
67+
# Create from list of coordinates
68+
pixels = Pixels.from_iterable([(10, 20), (30, 40), (50, 60)])
69+
70+
# Load from parquet file
71+
pixels = Pixels.load_from_parquet("pixels.parquet")
72+
```
73+
74+
### Key Properties
75+
76+
- **df**: Underlying pandas DataFrame with x,y coordinates
77+
- **coords**: Coordinate system definition
78+
79+
### Key Methods
80+
81+
- **x()**, **y()**: Access x and y coordinates as pandas Series
82+
- **to_numpy()**: Convert to NumPy array
83+
- **to_list()**: Convert to list of coordinates
84+
- **as_type()**: Convert coordinates to a specific data type
85+
- **get_coordinate()**: Get a specific coordinate pair
86+
- **df_homogenious()**: Get homogeneous coordinates (x,y,1)
87+
88+
## Signatures
89+
90+
The `Signatures` class combines `Pixels` with their corresponding spectral signals.
91+
92+
```python
93+
from siapy.entities import Signatures, Pixels, Signals
94+
95+
# Create from pixels and signals
96+
pixels = Pixels.from_iterable([(10, 20), (30, 40)])
97+
signals_df = pd.DataFrame([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]) # 2 pixels, 3 bands
98+
signals = Signals(signals_df)
99+
signatures = Signatures(pixels, signals)
100+
101+
# Extract signatures from an image at specific pixels
102+
pixels = Pixels.from_iterable([(10, 20), (30, 40)])
103+
signatures = spectral_image.to_signatures(pixels)
104+
```
105+
106+
### Key Properties
107+
108+
- **pixels**: The `Pixels` object with coordinate information
109+
- **signals**: The `Signals` object with spectral values
110+
111+
### Key Methods
112+
113+
- **to_dataframe()**: Convert to a pandas DataFrame
114+
- **to_dataframe_multiindex()**: Convert to a DataFrame with MultiIndex columns
115+
- **to_numpy()**: Convert to tuple of NumPy arrays (pixels, signals)
116+
- **to_dict()**: Convert to dictionary representation
117+
- **reset_index()**: Reset DataFrame indices
118+
- **copy()**: Create a deep copy
119+
120+
## Signals
121+
122+
The `Signals` class represents spectral values associated with pixels.
123+
124+
```python
125+
from siapy.entities.signatures import Signals
126+
127+
# Create from DataFrame
128+
signals_df = pd.DataFrame([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]) # 2 pixels, 3 bands
129+
signals = Signals(signals_df)
130+
131+
# Create from iterable
132+
signals = Signals.from_iterable([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]])
133+
```
134+
135+
### Key Properties
136+
137+
- **df**: Underlying pandas DataFrame with spectral values
138+
139+
### Key Methods
140+
141+
- **to_numpy()**: Convert to NumPy array
142+
- **average_signal()**: Calculate mean signal across specified axis
143+
- **save_to_parquet()**: Save to parquet file
144+
145+
## Shape
146+
147+
The `Shape` class represents geometric shapes that can be associated with images, such as points, lines, and polygons.
148+
149+
```python
150+
from siapy.entities import Shape
151+
from siapy.entities import Pixels
152+
153+
# Create a point
154+
point = Shape.from_point(10, 20)
155+
156+
# Create a polygon from pixels
157+
pixels = Pixels.from_iterable([(0, 0), (10, 0), (10, 10), (0, 10)])
158+
polygon = Shape.from_polygon(pixels)
159+
160+
# Load from shapefile
161+
shape = Shape.open_shapefile("path/to/shapefile.shp")
162+
```
163+
164+
### Shape Types
165+
166+
- **Point**: Single coordinate point (x,y)
167+
- **LineString**: Series of connected points forming a line
168+
- **Polygon**: Closed shape with interior area
169+
- **MultiPoint**: Collection of independent points
170+
- **MultiLineString**: Collection of independent lines
171+
- **MultiPolygon**: Collection of independent polygons
172+
173+
### Key Properties
174+
175+
- **geometry**: The underlying shapely geometry
176+
- **label**: Optional label for the shape
177+
- **shape_type**: Type of geometry (point, line, polygon, etc.)
178+
- **bounds**: Bounding box of the shape
179+
- **centroid**: Centroid point of the shape
180+
181+
### Key Methods
182+
183+
- **buffer()**: Create a buffered version of the shape
184+
- **intersection()**: Find intersection with another shape
185+
- **union()**: Combine with another shape
186+
- **to_file()**: Save to a shapefile
187+
188+
## GeometricShapes
189+
190+
The `GeometricShapes` class manages a collection of shapes associated with a spectral image.
191+
192+
```python
193+
# Access shapes associated with an image
194+
image = SpectralImage.spy_open(header_path="...", image_path="...")
195+
shapes = image.geometric_shapes
196+
197+
# Add a shape
198+
polygon = Shape.from_rectangle(10, 20, 30, 40)
199+
shapes.append(polygon)
200+
201+
# Find a shape by name
202+
shape = shapes.get_by_name("vegetation")
203+
```
204+
205+
### Key Methods
206+
207+
- **append()**, **extend()**: Add shapes to the collection
208+
- **remove()**, **pop()**, **clear()**: Remove shapes
209+
- **index()**, **count()**: Find and count shapes
210+
- **get_by_name()**: Find a shape by its label
211+
212+
## SpectralImageSet
213+
214+
The `SpectralImageSet` class manages a collection of spectral images.
215+
216+
```python
217+
from siapy.entities import SpectralImageSet
218+
from pathlib import Path
219+
220+
# Load multiple images
221+
header_paths = list(Path("data_dir").glob("*.hdr"))
222+
image_paths = list(Path("data_dir").glob("*.img"))
223+
image_set = SpectralImageSet.spy_open(
224+
header_paths=header_paths,
225+
image_paths=image_paths
226+
)
227+
228+
# Access images
229+
first_image = image_set[0]
230+
231+
# Sort images
232+
image_set.sort()
233+
```
234+
235+
### Key Properties
236+
237+
- **images**: List of SpectralImage objects
238+
- **cameras_id**: List of unique camera IDs
239+
240+
### Key Methods
241+
242+
- **images_by_camera_id()**: Get images from a specific camera
243+
- **sort()**: Sort the images
244+
245+
## Relationship Between Entities
246+
247+
The entities in SiaPy form a cohesive system:
248+
249+
1. A `SpectralImage` contains pixel data across multiple spectral bands
250+
2. `Pixels` represent spatial coordinates within that image
251+
3. `Signals` contain spectral values at those coordinates
252+
4. `Signatures` combine pixels and signals to represent spectral signatures at specific locations
253+
5. `Shape` objects define geometric regions in the image
254+
6. `GeometricShapes` organize multiple shapes associated with an image
255+
7. `SpectralImageSet` manages multiple related spectral images
256+
257+
This modular design allows for flexible workflows in spectral image analysis - from loading image data, to selecting regions of interest, to extracting and analyzing spectral signatures.

docs/concepts/overview.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Library Overview
2+
3+
## Architecture
4+
5+
SiaPy follows a modular architecture organized around key components that work together to provide a comprehensive toolkit for spectral image analysis:
6+
7+
``` sh
8+
siapy/
9+
├── core/ # Core functionality
10+
├── datasets/ # Dataset handling and management
11+
├── entities/ # Key data structures and representations
12+
├── features/ # Feature extraction and analysis
13+
├── optimizers/ # Optimization algorithms and machine learning
14+
├── transformations/ # Transformation operations
15+
└── utils/ # Utility functions and helpers
16+
```
17+
18+
## Core Components
19+
20+
### Core (`siapy.core`)
21+
22+
The foundation of the library providing essential functionality:
23+
24+
- **Logging**: Centralized logging functionality
25+
- **Exception handling**: Custom exceptions for consistent error handling
26+
- **Type definitions**: Common types used throughout the library
27+
- **Configuration**: System paths and global configuration settings
28+
29+
### Entities (`siapy.entities`)
30+
31+
Fundamental data structures that represent spectral imaging data:
32+
33+
- **SpectralImage**: An abstraction for various image formats
34+
- **SpectralImageSet**: Collection of spectral images with batch operations
35+
- **Pixels**: Representation of pixel coordinates and groups
36+
- **Shapes**: Geometric shapes for images' regions selection and masking
37+
- **Signatures**: Spectral signatures extracted from images
38+
39+
### Datasets (`siapy.datasets`)
40+
41+
Tools for managing and working with datasets:
42+
43+
- **Tabular datasets**: Handling tabular data with spectral information
44+
45+
### Features (`siapy.features`)
46+
47+
Functionality for working with spectral features:
48+
49+
- **Features**: Abstractions for feature extraction and selection
50+
- **Spectral indices**: Calculation of various spectral indices
51+
52+
### Transformations (`siapy.transformations`)
53+
54+
Transformation capabilities:
55+
56+
- **Co-registration**: Aligning images from different sources
57+
- **Image processing**: Functions for image manipulation
58+
59+
### Optimizers (`siapy.optimizers`)
60+
61+
Optimization, hyperparameter tuning and evaluation:
62+
63+
- **Optimization**: Machine learning training and optimization of hyperparameters
64+
- **Evaluation metrics and scoring mechanisms**: Tools for assessing model performance
65+
66+
### Utils (`siapy.utils`)
67+
68+
Utility and plotting functions:
69+
70+
- **Plotting**: Visualization tools for spectral data
71+
- **Image utilities**: Helper functions for image processing
72+
- **Signature utilities**: Functions for working with spectral signatures

0 commit comments

Comments
 (0)