Skip to content

Commit

Permalink
regenerate html
Browse files Browse the repository at this point in the history
  • Loading branch information
kadyb authored Aug 16, 2023
1 parent c9728d3 commit da451b5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Tutorial.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ output:

Prediction on large datasets can be time-consuming, but with enough computing
power, this task can be parallelized easily. Some algorithms provide native
multithreading like `predict()` function in `{ranger}` package (but this is
multithreading like `predict()` function in the `{ranger}` package (but this is
not standard). In this situation, we have to parallelize the calculations
ourselves. There are several approaches to do this, but for this example we will
divide a large out of memory raster into smaller blocks and make predictions
Expand Down
24 changes: 16 additions & 8 deletions Tutorial.html
Original file line number Diff line number Diff line change
Expand Up @@ -1497,18 +1497,18 @@ <h4 class="author">Krzysztof Dyba</h4>
</div>


<pre class="r"><code>library(&quot;stars&quot;)
set.seed(1) # define a random seed</code></pre>
<p>Prediction on large datasets can be time-consuming, but with enough
computing power, this task can be parallelized easily. Some algorithms
provide native multithreading like <code>predict()</code> function in
<code>{ranger}</code> package (but this is not standard). In this
the <code>{ranger}</code> package (but this is not standard). In this
situation, we have to parallelize the calculations ourselves. There are
several approaches to do this, but for this example we will divide a
large out of memory raster into smaller blocks and make predictions
using multithreading.</p>
<div id="data-loading" class="section level2">
<h2>Data loading</h2>
<pre class="r"><code>library(&quot;stars&quot;)
set.seed(1) # define a random seed</code></pre>
<p>The first step is to list the rasters representing the blue (B2),
green (B3), red (B4), and near infrared (B5) bands to be loaded using
the <code>list.files()</code> function. We need to define two additional
Expand All @@ -1525,8 +1525,8 @@ <h2>Data loading</h2>
do this, we use the <code>read_stars()</code> function with the
arguments <code>proxy = TRUE</code> and <code>along = 3</code>. This
second argument will cause the bands (layers) to be combined into a
three-dimensional matrix: $ [longitude,  latitude,  spectral  band]
$.</p>
three-dimensional matrix: <span class="math inline">\([longitude, \
latitude, \ spectral \ band]\)</span>.</p>
<pre class="r"><code>rasters = read_stars(files, proxy = TRUE, along = 3)</code></pre>
<p>We can replace the default filenames with band names, and rename the
third dimension as follows:</p>
Expand All @@ -1544,7 +1544,15 @@ <h2>Sampling</h2>
smp = st_sample(bbox, size = 20000) # sample points from the extent of the polygon
smp = st_extract(rasters, smp) # extract the pixel values for those points
smp = st_drop_geometry(st_as_sf(smp)) # convert to a data frame (remove geometry)
smp = na.omit(smp) # remove missing values (NA)</code></pre>
smp = na.omit(smp) # remove missing values (NA)
head(smp)</code></pre>
<pre><code>## Blue Green Red NIR
## 1 9908 11349 11862 19186
## 2 8187 9252 8641 21080
## 3 9377 10576 10837 15875
## 6 9264 10154 10422 13587
## 8 7571 8276 8065 11121
## 9 8090 8799 8162 20219</code></pre>
</div>
<div id="modelling" class="section level2">
<h2>Modelling</h2>
Expand Down Expand Up @@ -1652,12 +1660,12 @@ <h3>Multiple threads</h3>
save_path = file.path(tempdir(), paste0(&quot;tile_&quot;, i, &quot;.tif&quot;))
write_stars(pr, save_path, NA_value = 0, options = &quot;COMPRESS=NONE&quot;,
type = &quot;Byte&quot;, chunk_size = dim(tile))
save_path
return(save_path)
}

end_time_2 = difftime(Sys.time(), start_time, units = &quot;secs&quot;)
end_time_2 = round(end_time_2)</code></pre>
<p>The prediction took 248 seconds. By using three threads instead of
<p>The prediction took 264 seconds. By using three threads instead of
one, we have cut the operation time by half!</p>
</div>
</div>
Expand Down

0 comments on commit da451b5

Please sign in to comment.