TiagoOlivoto / pliman

Tools for Plant Image Analysis
https://tiagoolivoto.github.io/pliman/
GNU General Public License v3.0
53 stars 9 forks source link

Bug with the function "mosaic_analyze" when the argument "segment_individuals = TRUE" #16

Open HelcioDuartePereira opened 5 months ago

HelcioDuartePereira commented 5 months ago

Hi Tiago

I am using the pliman (latest version) to extract some features about the canopy of citrus plants (area, diameter, etc). I imported the mosaic and shapefile (made in QGIS) into R without any problem. They are in the same CRS (coordinate reference system).

My mosaic (object orto) looks like this:

print(orto) # 3 camadas: R, G e B class : SpatRaster dimensions : 8317, 5725, 3 (nrow, ncol, nlyr) resolution : 0.01999664, 0.01999765 (x, y) extent : 252903.9, 253018.4, 7515062, 7515229 (xmin, xmax, ymin, ymax) coord. ref. : WGS 84 / UTM zone 23S (EPSG:32723) source : Ortomosaico-citrus-abril-2024.tif names : red, green, blue

My shapefile (object parcelas) looks like this:

Simple feature collection with 373 features and 3 fields Geometry type: POLYGON Dimension: XY Bounding box: xmin: 252926.2 ymin: 7515102 xmax: 252989.8 ymax: 7515189 Projected CRS: WGS 84 / UTM zone 23S First 10 features: fid id Genotype geometry 1 1 1 H4 POLYGON ((252926.7 7515189,... 2 2 2 H6 POLYGON ((252926.4 7515186,... 3 3 3 H10 POLYGON ((252926.4 7515183,... 4 4 4 H39 POLYGON ((252926.9 7515181,... 5 5 5 V3 POLYGON ((252927.1 7515178,... 6 6 6 V6 POLYGON ((252926.8 7515176,...

I am trying to analyze the mosaic with the code below:

mosaic_analyze(mosaic = orto, r = 1, g = 2, b = 3, shapefile = parcelas, include_if = "centroid", segment_individuals = TRUE, segment_index = "NGRDI", threshold = 0.08, invert = FALSE, watershed = TRUE)

However, the following error always happens:

Extracting data from block 1 Error in dplyr::select(): ! Can't subset columns that don't exist. ✖ Column block doesn't exist. Run rlang::last_trace() to see where the error occurred.

If I click on the last message about the error the following is shown:

rlang::last_trace() <error/vctrs_error_subscript_oob> Error in dplyr::select(): ! Can't subset columns that don't exist. ✖ Column block doesn't exist.

Backtrace: ▆

  1. └─pliman::mosaic_analyze(...)
  2. ├─dplyr::select(...)
  3. └─dplyr:::select.data.frame(...) Run rlang::last_trace(drop = FALSE) to see 18 hidden frames.

rlang::last_trace(drop = FALSE) <error/vctrs_error_subscript_oob> Error in dplyr::select(): ! Can't subset columns that don't exist. ✖ Column block doesn't exist.

Backtrace: ▆

  1. ├─pliman::mosaic_analyze(...)
  2. │ ├─dplyr::select(...)
  3. │ └─dplyr:::select.data.frame(...)
  4. │ └─tidyselect::eval_select(expr(c(...)), data = .data, error_call = error_call)
  5. │ └─tidyselect:::eval_select_impl(...)
  6. │ ├─tidyselect:::with_subscript_errors(...)
  7. │ │ └─rlang::try_fetch(...)
  8. │ │ └─base::withCallingHandlers(...)
  9. │ └─tidyselect:::vars_select_eval(...)
  10. │ └─tidyselect:::walk_data_tree(expr, data_mask, context_mask)
  11. │ └─tidyselect:::eval_c(expr, data_mask, context_mask)
  12. │ └─tidyselect:::reduce_sels(node, data_mask, context_mask, init = init)
  13. │ └─tidyselect:::walk_data_tree(new, data_mask, context_mask)
  14. │ └─tidyselect:::as_indices_sel_impl(...)
  15. │ └─tidyselect:::as_indices_impl(...)
  16. │ └─tidyselect:::chr_as_locations(x, vars, call = call, arg = arg)
  17. │ └─vctrs::vec_as_location(...)
  18. └─vctrs (local) <fn>()
  19. └─vctrs:::stop_subscript_oob(...)
  20. └─vctrs:::stop_subscript(...)
  21. └─rlang::abort(...)

I was thinking that the problem shoud be my shapefile. So, I did a small example of shapefile (on the same mosaic) in plimanshiny and tried to run the same code with the "new" shapefile, but the error was shown again.

Helcio

TiagoOlivoto commented 5 months ago

Hi @HelcioDuartePereira ! This is due the fact that "block" and "plot_id" are mandatory columns (I should include a treatment error for this!). Are you sure your are using the development version of pliman and plimanshiny?

I made a small example, creating the shapefile with plimanshiny and saving it as "lettuce_shp.rds" (why not perform all analyses within plimanshiny?)

image

library(pliman)
#> |==========================================================|
#> | Tools for Plant Image Analysis (pliman 2.1.0)            |
#> | Author: Tiago Olivoto                                    |
#> | Type `citation('pliman')` to know how to cite pliman     |
#> | Visit 'http://bit.ly/pkg_pliman' for a complete tutorial |
#> |==========================================================|
setwd("D:/testes")
ras <- mosaic_input("05-03-2024_lett.tif")
#> class       : SpatRaster 
#> dimensions  : 3445, 1747, 3  (nrow, ncol, nlyr)
#> resolution  : 0.01, 0.01  (x, y)
#> extent      : 236479.4, 236496.8, 7929163, 7929197  (xmin, xmax, ymin, ymax)
#> coord. ref. : WGS 84 / UTM zone 23S (EPSG:32723) 
#> source      : 05-03-2024_lett.tif 
#> colors RGB  : 1, 2, 3 
#> names       : lettuce_1, lettuce_2, lettuce_3 
#> min values  :         1,         0,         0 
#> max values  :       255,       255,       255
shapefile <- shapefile_input("lettuce_shp.rds")
#>  class       : SpatVector 
#>  geometry    : polygons 
#>  dimensions  : 72, 3  (geometries, attributes)
#>  extent      : 236483.8, 236491.7, 7929167, 7929194  (xmin, xmax, ymin, ymax)
#>  coord. ref. : WGS 84 / UTM zone 23S (EPSG:32723) 
#>  names       : unique_id block plot_id
#>  type        :     <int> <chr>   <chr>
#>  values      :         1   B01   P0001
#>                        2   B01   P0008
#>                        3   B01   P0009

mosaic_analyze(ras,
               shapefile = shapefile,
               plot_index = "GLI",
               segment_index = "GLI",
               segment_individuals = TRUE)
#>  
#> Building the mosaic...
#> Using downsample = 2 so that the number of rendered pixels approximates the `max_pixels`
#>  
#> Computing the indexes...
#>  
#> Extracting data from block 1 
#>  
#> Preparing to plot...
#>  Done!
#> $result_plot
#> Simple feature collection with 72 features and 4 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 236483.8 ymin: 7929167 xmax: 236491.7 ymax: 7929194
#> Projected CRS: WGS 84 / UTM zone 23S
#> First 10 features:
#>    unique_id block plot_id     mean.GLI                       geometry
#> 1          1   B01   P0001 -0.062621370 POLYGON ((236483.8 7929194,...
#> 2          2   B01   P0008 -0.030286949 POLYGON ((236483.9 7929192,...
#> 3          3   B01   P0009 -0.005732136 POLYGON ((236484 7929191, 2...
#> 4          4   B01   P0016 -0.021559661 POLYGON ((236484.1 7929189,...
#> 5          5   B01   P0017 -0.068187401 POLYGON ((236484.2 7929188,...
#> 6          6   B01   P0024 -0.045827858 POLYGON ((236484.3 7929186,...
#> 7          7   B01   P0025 -0.030057969 POLYGON ((236484.4 7929185,...
#> 8          8   B01   P0032 -0.027310185 POLYGON ((236484.5 7929183,...
#> 9          9   B01   P0033 -0.071393937 POLYGON ((236484.6 7929182,...
#> 10        10   B01   P0040 -0.036239289 POLYGON ((236484.7 7929180,...
#> 
#> $result_plot_summ
#> Simple feature collection with 72 features and 15 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 236483.8 ymin: 7929167 xmax: 236491.7 ymax: 7929194
#> Projected CRS: WGS 84 / UTM zone 23S
#> # A tibble: 72 × 16
#>    block plot_id     n area_sum   area coverage perimeter length width diam_min
#>    <chr> <chr>   <dbl>    <dbl>  <dbl>    <dbl>     <dbl>  <dbl> <dbl>    <dbl>
#>  1 B01   P0001      15    0.291 0.0194   0.129      0.554  0.172 0.159    0.128
#>  2 B01   P0002      16    0.410 0.0256   0.181      0.635  0.198 0.185    0.152
#>  3 B01   P0003      16    0.573 0.0358   0.253      0.773  0.235 0.220    0.176
#>  4 B01   P0004      16    0.497 0.0311   0.219      0.719  0.221 0.203    0.160
#>  5 B01   P0005      16    0.217 0.0136   0.0958     0.463  0.144 0.135    0.107
#>  6 B01   P0006      16    0.724 0.0453   0.320      0.864  0.260 0.241    0.204
#>  7 B01   P0007      16    0.398 0.0249   0.176      0.622  0.195 0.179    0.147
#>  8 B01   P0008      16    0.534 0.0334   0.236      0.759  0.228 0.205    0.168
#>  9 B01   P0009      16    0.659 0.0412   0.291      0.852  0.259 0.227    0.180
#> 10 B01   P0010      16    0.651 0.0407   0.287      0.823  0.250 0.230    0.194
#> # ℹ 62 more rows
#> # ℹ 6 more variables: diam_mean <dbl>, diam_max <dbl>, unique_id <dbl>,
#> #   mean.GLI <dbl>, plot_area [m^2], geometry <POLYGON [m]>
#> 
#> $result_indiv
#> Simple feature collection with 1139 features and 14 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 236484.4 ymin: 7929167 xmax: 236491.3 ymax: 7929194
#> Projected CRS: WGS 84 / UTM zone 23S
#> First 10 features:
#>    block plot_id individual        x       y       area perimeter    length
#> 1    B01   P0001        649 236484.9 7929193 0.02978516 0.6994113 0.2159660
#> 2    B01   P0001        663 236484.9 7929193 0.02734375 0.6676955 0.2005139
#> 3    B01   P0001        745 236484.9 7929193 0.02563477 0.6569848 0.1921283
#> 4    B01   P0001        867 236485.1 7929193 0.02197266 0.6028427 0.1801924
#> 5    B01   P0001        872 236485.1 7929193 0.02221680 0.5828427 0.1868534
#> 6    B01   P0001        878 236484.7 7929193 0.02404785 0.6287006 0.1878888
#> 7    B01   P0001        923 236484.7 7929193 0.02221680 0.6028427 0.1843726
#> 8    B01   P0001        971 236485.0 7929193 0.02062988 0.5828427 0.1920252
#> 9    B01   P0001        973 236484.6 7929193 0.01867676 0.5428427 0.1699673
#> 10   B01   P0001        985 236484.5 7929193 0.01574707 0.4945584 0.1555050
#>        width  diam_min diam_mean  diam_max unique_id  mean.GLI
#> 1  0.1989128 0.1630066 0.1963783 0.2263625         1 0.1866175
#> 2  0.1999823 0.1675258 0.1905091 0.2240183         1 0.1895649
#> 3  0.1915655 0.1563362 0.1812418 0.2071473         1 0.1786323
#> 4  0.1614369 0.1383754 0.1680798 0.2033878         1 0.1853317
#> 5  0.1697465 0.1500752 0.1689604 0.1969100         1 0.1869786
#> 6  0.1920182 0.1404371 0.1741295 0.2026813         1 0.1787455
#> 7  0.1691803 0.1228457 0.1687339 0.1982483         1 0.1950610
#> 8  0.1573873 0.1266327 0.1635710 0.2024084         1 0.1650223
#> 9  0.1501876 0.1231384 0.1528330 0.1851201         1 0.1679245
#> 10 0.1413682 0.1203744 0.1429408 0.1714961         1 0.1798203
#>                          geometry
#> 1  POLYGON ((236484.8 7929193,...
#> 2  POLYGON ((236484.9 7929193,...
#> 3  POLYGON ((236484.9 7929193,...
#> 4  POLYGON ((236485 7929193, 2...
#> 5  POLYGON ((236485 7929193, 2...
#> 6  POLYGON ((236484.7 7929193,...
#> 7  POLYGON ((236484.7 7929193,...
#> 8  POLYGON ((236485 7929193, 2...
#> 9  POLYGON ((236484.6 7929193,...
#> 10 POLYGON ((236484.5 7929193,...
#> 
#> $result_individ_map
#> NULL
#> 
#> $map_plot
#> 
#> $map_indiv
#> 
#> $shapefile
#> $shapefile$`1`
#> Simple feature collection with 72 features and 3 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 236483.8 ymin: 7929167 xmax: 236491.7 ymax: 7929194
#> Projected CRS: WGS 84 / UTM zone 23S
#> First 10 features:
#>    unique_id block plot_id                       geometry
#> 1          1   B01   P0001 POLYGON ((236483.8 7929194,...
#> 2          2   B01   P0008 POLYGON ((236483.9 7929192,...
#> 3          3   B01   P0009 POLYGON ((236484 7929191, 2...
#> 4          4   B01   P0016 POLYGON ((236484.1 7929189,...
#> 5          5   B01   P0017 POLYGON ((236484.2 7929188,...
#> 6          6   B01   P0024 POLYGON ((236484.3 7929186,...
#> 7          7   B01   P0025 POLYGON ((236484.4 7929185,...
#> 8          8   B01   P0032 POLYGON ((236484.5 7929183,...
#> 9          9   B01   P0033 POLYGON ((236484.6 7929182,...
#> 10        10   B01   P0040 POLYGON ((236484.7 7929180,...

image

Created on 2024-06-11 with reprex v2.1.0

Hope it helps!

HelcioDuartePereira commented 5 months ago

Hi @TiagoOlivoto

Thanks a lot for your help! Now, after adjusting the columns of the shapefile, all the code runs right.

I am using the pliman package frequently, but the app plimanshiny I started these days. Certainly I will use it more and more.

In this work that I reported the bug, I had already made all the shapefile (373 irregular plots) in QGIS and I would like to take advantage of the work already done.

Thanks. Helcio