Skip to content

Commit

Permalink
r.boxplot improvements (#754)
Browse files Browse the repository at this point in the history
Deal with cases in which the color table contains more categories as the
raster layer.
Deal with cases in which some but not all categories have a label.
  • Loading branch information
ecodiv committed May 20, 2022
1 parent 00caf54 commit 894de29
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/raster/r.boxplot/r.boxplot.py
Expand Up @@ -418,10 +418,13 @@ def bxp_zones(
).outputs.stdout
labels = labels.replace("\r", "").split("\n")
labels = [_f for _f in labels if _f]
labels = [_y[1] for _y in [_x.split("|") for _x in labels]]
labels = [_y for _y in [_x.split("|") for _x in labels]]
labelsids = [int(_z[0]) for _z in labels]
labels = [_z[0] if len(_z[1]) == 0 else _z[1] for _z in labels]

# Get colors
if bpcolors:
# Get list with color rgb codes and corresponding category id
zones_color = Module("r.colors.out", map=zones, stdout_=PIPE).outputs.stdout
zones_color = zones_color.replace("\r", "").split("\n")
zones_color = [_f for _f in zones_color if _f]
Expand All @@ -430,6 +433,13 @@ def bxp_zones(
for _x in zones_color
if not _x.startswith("nv") and not _x.startswith("default")
]
zones_colorids = [int(_y[0]) for _y in [_x.split(" ") for _x in zones_color]]
# Select the actual raster categories and extract the rgb values
zones_color = [
zones_color[id]
for id, _ in enumerate(zones_color)
if zones_colorids[id] in labelsids
]
zones_color = [_y[1] for _y in [_x.split(" ") for _x in zones_color]]
zones_color = [_z.split(":") for _z in zones_color]
zones_rgb = [[int(_x) / 255 for _x in _y] for _y in zones_color]
Expand Down Expand Up @@ -477,12 +487,9 @@ def bxp_zones(
# Construct per zone the boxplot
for i in ordered_list:

# Get or create the boxplot labels
# Get boxplot label and stats
zone_name = labels[i]
quantstats_i = order_bpl[i]
if labels[i]:
zone_name = labels[i]
else:
zone_name = quantstats_i[0]

# Extract the stats to construct boxplot ith zone
min_value = float(quantstats_i[1])
Expand Down

0 comments on commit 894de29

Please sign in to comment.