Skip to content
Open
Changes from 1 commit
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
58 changes: 58 additions & 0 deletions scripts/i.spectral/i.spectral.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,23 @@
# % key: t
# % description: output to text file
# %end
# % flag
# % key: p
# % description: output to pyplot
# %end

import os
import atexit
from grass.script.utils import try_rmdir
from grass.script import core as gcore

# For pyplot
import matplotlib.pyplot as plt
import numpy as np



def cleanup():

Check failure on line 96 in scripts/i.spectral/i.spectral.py

View workflow job for this annotation

GitHub Actions / Python Code Quality Checks (ubuntu-22.04)

Ruff (E303)

scripts/i.spectral/i.spectral.py:96:1: E303 Too many blank lines (3)
try_rmdir(tmp_dir)


Expand Down Expand Up @@ -217,6 +226,52 @@
y_title=_("DN Value"),
)

def draw_pyplot(what, xlabels, coord_legend):

Check failure on line 229 in scripts/i.spectral/i.spectral.py

View workflow job for this annotation

GitHub Actions / Python Code Quality Checks (ubuntu-22.04)

Ruff (E302)

scripts/i.spectral/i.spectral.py:229:1: E302 Expected 2 blank lines, found 1
"""
Draw using pyplot
"""
plt.figure(figsize=(10, 6))
max_len = 0

for i, row in enumerate(what):
y = [float(val) for val in row[3:]] # data points
x = np.arange(1, len(y) + 1)
max_len = max(max_len, len(y))
if coord_legend:
label = str(tuple(row[0:2]))
else:
label = f"Pick {i + 1}"

Check failure on line 243 in scripts/i.spectral/i.spectral.py

View workflow job for this annotation

GitHub Actions / Python Code Quality Checks (ubuntu-22.04)

Ruff (SIM108)

scripts/i.spectral/i.spectral.py:240:9: SIM108 Use ternary operator `label = str(tuple(row[0:2])) if coord_legend else f"Pick {i + 1}"` instead of `if`-`else`-block
plt.plot(x, y, marker='', label=label)

Check failure on line 244 in scripts/i.spectral/i.spectral.py

View workflow job for this annotation

GitHub Actions / Python Code Quality Checks (ubuntu-22.04)

Ruff (Q000)

scripts/i.spectral/i.spectral.py:244:31: Q000 Single quotes found but double quotes preferred

Check failure on line 245 in scripts/i.spectral/i.spectral.py

View workflow job for this annotation

GitHub Actions / Python Code Quality Checks (ubuntu-22.04)

Ruff (W293)

scripts/i.spectral/i.spectral.py:245:1: W293 Blank line contains whitespace
# Assume 'what' is your data as given
row = what[0] # or any row you want to use
num_bands = len(row) - 3 # subtract the first three elements (coords/meta)
# Generate xlabels: ['Band1', 'Band2', ..., 'BandN']
xlabels = [f'B{i+1}' for i in range(num_bands)]

Check failure on line 250 in scripts/i.spectral/i.spectral.py

View workflow job for this annotation

GitHub Actions / Python Code Quality Checks (ubuntu-22.04)

Ruff (E226)

scripts/i.spectral/i.spectral.py:250:21: E226 Missing whitespace around arithmetic operator

Check failure on line 250 in scripts/i.spectral/i.spectral.py

View workflow job for this annotation

GitHub Actions / Python Code Quality Checks (ubuntu-22.04)

Ruff (Q000)

scripts/i.spectral/i.spectral.py:250:16: Q000 Single quotes found but double quotes preferred
# Determine up to 10 evenly spaced tick positions
max_ticks = 10
if num_bands <= max_ticks:
tick_indices = np.arange(num_bands)
else:
tick_indices = np.linspace(0, num_bands - 1, max_ticks, dtype=int)
# Set x-axis labels
if xlabels and isinstance(xlabels, (list, tuple)):
plt.xticks(np.arange(1, len(xlabels) + 1), xlabels, rotation=40)
else:
plt.xticks(np.arange(1, max_len + 1))

# Plot data
y = row[3:]
x = np.arange(1, num_bands + 1)
plt.xticks(ticks=x[tick_indices], labels=[xlabels[i] for i in tick_indices], rotation=40)
plt.grid(True, linestyle=':', linewidth=0.7, color='gray', alpha=0.7)

Check failure on line 267 in scripts/i.spectral/i.spectral.py

View workflow job for this annotation

GitHub Actions / Python Code Quality Checks (ubuntu-22.04)

Ruff (Q000)

scripts/i.spectral/i.spectral.py:267:56: Q000 Single quotes found but double quotes preferred

Check failure on line 267 in scripts/i.spectral/i.spectral.py

View workflow job for this annotation

GitHub Actions / Python Code Quality Checks (ubuntu-22.04)

Ruff (Q000)

scripts/i.spectral/i.spectral.py:267:30: Q000 Single quotes found but double quotes preferred
plt.title("Spectral signatures")
plt.xlabel("Bands")
plt.ylabel("DN Value")
plt.legend()
plt.tight_layout()
plt.show()


def main():
group = options["group"]
Expand All @@ -228,6 +283,7 @@
coord_legend = flags["c"]
gnuplot = flags["g"]
textfile = flags["t"]
pyplot = flags["p"]

global tmp_dir
tmp_dir = gcore.tempdir()
Expand Down Expand Up @@ -283,6 +339,8 @@
# build data files
if gnuplot:
draw_gnuplot(what, xlabels, output, img_fmt, coord_legend)
elif pyplot:
draw_pyplot(what, xlabels, coord_legend)
elif textfile:
write2textf(what, output)
else:
Expand Down
Loading