Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 12 additions & 8 deletions crates/engine/src/cmd/deeplearning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ fn train(s: &Session, p: &Value) -> Result<(DlModel, super::whole::Unread), Stri
/// training layer is read whole and the pixels under its polygons sampled there, as the network
/// learns).
struct Training {
raster: std::sync::Arc<rustgis_data::raster::Raster>,
/// Read as the model is fitted: from its file, for a layer read in place from one, in a
/// classification's run.
raster: super::workflows::Input,
bands: Vec<usize>,
/// The training layer's rows, read whole in [`Training::fit`].
rows: super::whole::Whole,
Expand All @@ -204,6 +206,7 @@ impl Training {
fn fit(self) -> Result<(DlModel, super::whole::Unread), String> {
let Training { raster, bands, rows, field, hidden, epochs, name, .. } = self;
let (ds, unread) = rows.read()?;
let raster = raster.read()?;
let model = Self::learn(&ds, field, &raster, &bands, &hidden, epochs, name).map_err(|e| unread.explain(e))?;
Ok((model, unread))
}
Expand Down Expand Up @@ -269,9 +272,9 @@ fn training_polygons(ds: &rustgis_data::Dataset, fi: usize) -> Result<(Vec<Strin
/// What training takes of the session: the raster, the training layer (read whole when the model
/// is fitted), and the model's name.
fn training(s: &Session, p: &Value) -> Result<Training, String> {
let r = raster(s, p)?;
r.require_coordinates()?;
let bands = bands_param(p, &r)?;
let r = super::imagery::input(s, p)?;
r.held().require_coordinates()?;
let bands = bands_param(p, r.held())?;
let tl = super::imagery::training_name(p)?;
let l = s
.map()
Expand Down Expand Up @@ -299,7 +302,7 @@ fn training(s: &Session, p: &Value) -> Result<Training, String> {
let (name, auto) = match ps(p, "model").map(str::trim).filter(|m| !m.is_empty()) {
Some(m) => (super::within("model", "a model's name", m, 200)?, None),
None => {
let base = format!("{}_model", r.name);
let base = format!("{}_model", r.held().name);
(free_name(s, &base), Some(base))
}
};
Expand Down Expand Up @@ -332,16 +335,17 @@ enum Model {
/// trains one), and the work that trains the model if need be and classifies the pixels. A model
/// trained is kept in the project once the classes are added.
pub(crate) fn classify_work(s: &Session, p: &Value) -> Result<super::workflows::Work, String> {
let r = raster(s, p)?;
r.require_coordinates()?;
let r = super::imagery::input(s, p)?;
r.held().require_coordinates()?;
let model = if p.get("training").is_some() {
Model::Train(training(s, p)?)
} else {
let i = model_index(s, p)?;
Model::Saved(s.project.dl_models.get(i).cloned().ok_or("no model")?)
};
let out = name(p, format!("{}_dl", r.name))?;
let out = name(p, format!("{}_dl", r.held().name))?;
Ok(Box::new(move || {
let r = r.read()?;
let (m, trained) = match model {
Model::Saved(m) => (m, None),
Model::Train(t) => {
Expand Down
116 changes: 71 additions & 45 deletions crates/engine/src/cmd/gp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1562,7 +1562,7 @@ pub(crate) fn tiled_inputs(
// reads no pixels through the run's inputs.
let Some(spec) = spec.filter(|t| t.id != "rasterToPmtiles") else { return Ok(read) };
let mut seen = std::collections::HashSet::new();
let (mut overviews, mut held_overviews, mut rounded) = (Vec::new(), Vec::new(), Vec::new());
let (mut said, mut held_overviews) = (Vec::new(), Vec::new());
for parameter in spec.params {
let value = params.get(parameter.name).unwrap_or(&Value::Null);
let names: Vec<&str> = match parameter.kind {
Expand Down Expand Up @@ -1613,47 +1613,9 @@ pub(crate) fn tiled_inputs(
}
continue;
}
// A batch's read is used again only while the file is the revision it was read at.
let revision = file_revisions(std::path::Path::new(path));
let kept = BATCH_READS.with(|b| b.borrow().as_ref().and_then(|reads| reads.get(path).cloned()));
let (r, count) = match kept.filter(|(.., at)| revision.is_some() && *at == revision) {
Some((r, count, _)) => (r, count),
None => {
#[cfg(test)]
TILED_READS.with(|n| n.set(n.get() + 1));
// The last file a batch read alone: let go before another is read, so a batch
// over several files holds one, and doesn't hold it uncounted while the next
// is planned.
BATCH_READS.with(|b| {
if let Some(reads) = b.borrow_mut().as_mut() {
reads.clear();
}
});
let _turn = tiled_turn().map_err(|e| format!("{}: {e}", l.name))?;
let r = std::sync::Arc::new(
rustgis_data::raster::read_path_with(std::path::Path::new(path), None, None)
.map_err(|e| format!("{}: {e}", l.name))?,
);
// Counted as held from before the next run's turn, for as long as anything
// (this run, the batch) holds it.
let bytes: usize = r.bands.iter().map(|b| b.data.len().saturating_mul(4)).sum();
let count = std::sync::Arc::new(rustgis_data::memory::Holding::of(bytes as u64));
BATCH_READS.with(|b| {
if let Some(reads) = b.borrow_mut().as_mut() {
reads.insert(path.to_string(), (r.clone(), count.clone(), revision.clone()));
}
});
(r, count)
}
};
let (r, count) = read_tiled(&l.name, path)?;
held.push(count);
let n: u64 = r.rounded.iter().sum();
if n > 0 {
rounded.push(format!("{} {n}", l.name));
}
if let Some((fw, fh)) = r.overview {
overviews.push(format!("{} at its {} × {} overview of the {fw} × {fh} raster", l.name, r.width, r.height));
}
said.push((l.name.clone(), r.clone()));
read.insert(key, r);
}
}
Expand All @@ -1664,16 +1626,80 @@ pub(crate) fn tiled_inputs(
held_overviews.join("; ")
));
}
messages.extend(tiled_read_notes(&said, "the tool's"));
Ok(read)
}

/// What a run says of the raster files read in place it read again from their file
/// ([`read_tiled`]), each with its layer's name: one notice of each kind, naming each raster.
/// `whose` says whose results are built on values rounded as they were read.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn tiled_read_notes(read: &[(String, std::sync::Arc<rustgis_data::raster::Raster>)], whose: &str) -> Vec<String> {
let (mut rounded, mut overviews) = (Vec::new(), Vec::new());
for (name, r) in read {
let n: u64 = r.rounded.iter().sum();
if n > 0 {
rounded.push(format!("{name} {n}"));
}
if let Some((fw, fh)) = r.overview {
overviews.push(format!("{name} at its {} × {} overview of the {fw} × {fh} raster", r.width, r.height));
}
}
let mut notes = Vec::new();
if !rounded.is_empty() {
messages.push(format!(
"values rounded to 32-bit floats as the file was read for the run, the tool's results built on them: {}",
notes.push(format!(
"values rounded to 32-bit floats as the file was read for the run, {whose} results built on them: {}",
rounded.join("; ")
));
}
if !overviews.is_empty() {
messages.push(format!("read at the finest level each can hold: {}", overviews.join("; ")));
notes.push(format!("read at the finest level each can hold: {}", overviews.join("; ")));
}
Ok(read)
notes
}

/// The raster file read in place at `path` (layer `name`'s), read again from the file at the
/// finest level the run can hold, with its count in [`rustgis_data::memory::held`]: a batch's
/// read of it while the file is unchanged ([`BATCH_READS`]), else read now, in its turn
/// ([`tiled_turn`]). The GeoTIFF reader looks at Cancel between its chunks through the progress
/// probe the run installed.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn read_tiled(
name: &str,
path: &str,
) -> Result<(std::sync::Arc<rustgis_data::raster::Raster>, std::sync::Arc<rustgis_data::memory::Holding>), String> {
// A batch's read is used again only while the file is the revision it was read at.
let revision = file_revisions(std::path::Path::new(path));
let kept = BATCH_READS.with(|b| b.borrow().as_ref().and_then(|reads| reads.get(path).cloned()));
let (r, count) = match kept.filter(|(.., at)| revision.is_some() && *at == revision) {
Some((r, count, _)) => (r, count),
None => {
#[cfg(test)]
TILED_READS.with(|n| n.set(n.get() + 1));
// The last file a batch read alone: let go before another is read, so a batch over
// several files holds one, and doesn't hold it uncounted while the next is planned.
BATCH_READS.with(|b| {
if let Some(reads) = b.borrow_mut().as_mut() {
reads.clear();
}
});
let _turn = tiled_turn().map_err(|e| format!("{name}: {e}"))?;
let r = std::sync::Arc::new(
rustgis_data::raster::read_path_with(std::path::Path::new(path), None, None).map_err(|e| format!("{name}: {e}"))?,
);
// Counted as held from before the next run's turn, for as long as anything (this
// run, the batch) holds it.
let bytes: usize = r.bands.iter().map(|b| b.data.len().saturating_mul(4)).sum();
let count = std::sync::Arc::new(rustgis_data::memory::Holding::of(bytes as u64));
BATCH_READS.with(|b| {
if let Some(reads) = b.borrow_mut().as_mut() {
reads.insert(path.to_string(), (r.clone(), count.clone(), revision.clone()));
}
});
(r, count)
}
};
Ok((r, count))
}

/// A raster file read in place for a run, its count in [`rustgis_data::memory::held`], which lasts
Expand Down
Loading
Loading