Skip to content

Commit

Permalink
Reduced number of panics
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanRace committed Nov 16, 2022
1 parent 82f14b1 commit 319e601
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 77 deletions.
2 changes: 1 addition & 1 deletion src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fn handle_data_events(
);

let cell_segmentation = commands
.spawn_bundle(SpriteBundle {
.spawn(SpriteBundle {
transform: Transform::from_xyz(0.0, 0.0, 1.0),
texture: textures.add(image),
sprite: Sprite {
Expand Down
145 changes: 75 additions & 70 deletions src/imc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::io::BufWriter;
use std::{
collections::{HashMap, HashSet},
fs::File,
io::BufReader,
path::PathBuf,
sync::Arc,
time::Instant,
Expand All @@ -12,16 +11,12 @@ use std::{
use bevy::{
prelude::*,
reflect::TypeUuid,
render::{
render_resource::{Extent3d, TextureDimension, TextureFormat},
texture::{CompressedImageFormats, ImageType},
},
render::render_resource::{Extent3d, TextureDimension, TextureFormat},
sprite::Anchor,
tasks::{AsyncComputeTaskPool, Task},
};
use futures_lite::future;

use image::ImageFormat;
use nalgebra::Matrix4;
use tiff::encoder::{colortype, TiffEncoder};

Expand All @@ -36,7 +31,7 @@ use smartcore::{

use crate::camera::BoundingBox;
use crate::image_plugin::{
ComputeImage, ComputeTileImage, ImageControl, ImageUpdateType, Opacity, TiledImage, ToTileImage,
ComputeTileImage, ImageControl, ImageUpdateType, Opacity, TiledImage, ToTileImage,
};
use crate::{
annotation::{Annotation, PixelAnnotationConf},
Expand Down Expand Up @@ -628,6 +623,11 @@ fn process_classifier_results(

// TODO: Check error and spawn it if necessary
if let Err(error) = classifier {
commands.spawn(Message {
severity: Severity::Error,
message: format!("Error classifing: {}", error),
});

continue;
}

Expand All @@ -644,7 +644,7 @@ fn process_classifier_results(
let mut data = vec![0; (region.width * region.height) as usize * 4];

for (index, label) in result.predicted_labels.iter().enumerate() {
let index = (index * 4) as usize;
let index = index * 4;

let label = *label as usize;
let colour = result.labels[label].colour;
Expand All @@ -657,8 +657,8 @@ fn process_classifier_results(

let image = Image::new(
Extent3d {
width: region.width as u32,
height: region.height as u32,
width: region.width,
height: region.height,
depth_or_array_layers: 1,
},
TextureDimension::D2,
Expand Down Expand Up @@ -1184,22 +1184,18 @@ impl IMCDataset {
self.mcd.channels()
}

pub fn channel_image(&self, identifier: &ChannelIdentifier) -> HashMap<u16, ChannelImage> {
pub fn channel_image(
&self,
identifier: &ChannelIdentifier,
) -> Result<HashMap<u16, ChannelImage>, MCDError> {
let mut image_map = HashMap::new();

for acquisition in self.mcd.acquisitions() {
match acquisition.channel_image(identifier, None) {
Ok(data) => {
image_map.insert(acquisition.id(), ChannelImage(data));
}
Err(MCDError::InvalidChannel { channel }) => {
// No such channel, so we don't add data
}
_ => todo!(),
}
let data = acquisition.channel_image(identifier, None)?;
image_map.insert(acquisition.id(), ChannelImage(data));
}

image_map
Ok(image_map)
}

pub fn acquisitions_in(
Expand Down Expand Up @@ -1404,66 +1400,75 @@ fn generate_channel_image(

if let Ok(imc) = q_imc.get(parent.get()) {
let start = Instant::now();
let mut channel_images = imc.channel_image(&generate.identifier);

let duration = start.elapsed();

println!("Time elapsed loading data is: {:?}", duration);
let mut min_value = f32::MAX;
let mut max_value = f32::MIN;
let mut image_entities = HashSet::with_capacity(channel_images.len());

for (acq_id, acquisition_entity) in image_control.entities.iter() {
if let Ok(acquisition) = q_acquisition.get(*acquisition_entity) {
if let Some(channel_image) = channel_images.remove(&acquisition.id) {
// If the image is empty, then we don't need to do anything
if channel_image.width() == 0 || channel_image.height() == 0 {
continue;
}
match imc.channel_image(&generate.identifier) {
Ok(mut channel_images) => {
let duration = start.elapsed();

println!("Time elapsed loading data is: {:?}", duration);
let mut min_value = f32::MAX;
let mut max_value = f32::MIN;
let mut image_entities = HashSet::with_capacity(channel_images.len());

for (acq_id, acquisition_entity) in image_control.entities.iter() {
if let Ok(acquisition) = q_acquisition.get(*acquisition_entity) {
if let Some(channel_image) = channel_images.remove(&acquisition.id) {
// If the image is empty, then we don't need to do anything
if channel_image.width() == 0 || channel_image.height() == 0 {
continue;
}

let image_range = channel_image.intensity_range();
let image_range = channel_image.intensity_range();

if image_range.0 < min_value {
min_value = image_range.0
}
if image_range.0 > max_value {
max_value = image_range.0
}
if image_range.1 < min_value {
min_value = image_range.1
}
if image_range.1 > max_value {
max_value = image_range.1
}
if image_range.0 < min_value {
min_value = image_range.0
}
if image_range.0 > max_value {
max_value = image_range.0
}
if image_range.1 < min_value {
min_value = image_range.1
}
if image_range.1 > max_value {
max_value = image_range.1
}

let channel_image_entity = commands
.spawn(AcquisitionChannelImage {
acquisition_entity: *acquisition_entity,
data: Some(channel_data.add(channel_image)),
})
.id();
let channel_image_entity = commands
.spawn(AcquisitionChannelImage {
acquisition_entity: *acquisition_entity,
data: Some(channel_data.add(channel_image)),
})
.id();

image_entities.insert(channel_image_entity);
image_entities.insert(channel_image_entity);

commands.entity(entity).add_child(channel_image_entity);
} else {
let channel_image_entity = commands
.spawn(AcquisitionChannelImage {
acquisition_entity: *acquisition_entity,
data: None,
})
.id();
commands.entity(entity).add_child(channel_image_entity);
} else {
let channel_image_entity = commands
.spawn(AcquisitionChannelImage {
acquisition_entity: *acquisition_entity,
data: None,
})
.id();

image_entities.insert(channel_image_entity);
image_entities.insert(channel_image_entity);

commands.entity(entity).add_child(channel_image_entity);
commands.entity(entity).add_child(channel_image_entity);
}
}
}

image_control.histogram = vec![];
image_control.intensity_range = (min_value, max_value);
image_control.colour_domain = (min_value, max_value);
}
Err(error) => {
commands.spawn(Message {
severity: Severity::Error,
message: format!("Failed to load channel data: {}", error),
});
}
}

image_control.histogram = vec![];
image_control.intensity_range = (min_value, max_value);
image_control.colour_domain = (min_value, max_value);
}
}
}
13 changes: 7 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//#![warn(clippy::missing_docs_in_private_items)]
#![warn(missing_docs)]
#![warn(clippy::unwrap_used)]
//! Biquinho - visualisation of imaging mass cytometry data.
//!
//! Functionality is split out into separate plugins. This main file deals only with the setup.
Expand All @@ -22,7 +23,7 @@
//! - [ ] Implement means of dragging entire dataset
//! - [ ] Provide option to load new dataset above loaded datasets
use std::{fs::File, io::BufReader, path::PathBuf};
use std::{fs::File, path::PathBuf};

/// AnnotationPlugin - handles everything related to drawing, saving and loading annotations.
mod annotation;
Expand Down Expand Up @@ -183,7 +184,7 @@ fn create_transform(
height: f32,
flip_required: bool,
) -> Transform {
let translate_transform = Transform::from_xyz((width / 2.0) as f32, (height / 2.0) as f32, 0.0);
let translate_transform = Transform::from_xyz(width / 2.0, height / 2.0, 0.0);
//let scale_transform = Transform::from_scale(Vec3::new(1.0, 1.0, 0.0));
let transform = Transform::from_matrix(transform.into());

Expand Down Expand Up @@ -226,7 +227,7 @@ fn setup(
for y in 0..=num_gridlines {
let y_value = (-(num_gridlines / 2) + y) as f32 * grid_spacing;

commands.spawn_bundle(SpriteBundle {
commands.spawn(SpriteBundle {
transform: Transform::from_xyz(0.0, y_value, 0.0),
sprite: Sprite {
custom_size: Some(Vec2::new(grid_length, grid_thickness)),
Expand All @@ -237,7 +238,7 @@ fn setup(
});

if y_value >= 0.0 {
commands.spawn_bundle(Text2dBundle {
commands.spawn(Text2dBundle {
// Use `Text` directly
text: Text {
// Construct a `Vec` of `TextSection`s
Expand All @@ -264,7 +265,7 @@ fn setup(
for x in 0..=num_gridlines {
let x_value = (-(num_gridlines / 2) + x) as f32 * grid_spacing;

commands.spawn_bundle(SpriteBundle {
commands.spawn(SpriteBundle {
//material: materials.add(Color::rgb(0.5, 0.5, 1.0).into()),
transform: Transform::from_xyz(x_value, 0.0, 0.0),
sprite: Sprite {
Expand All @@ -277,7 +278,7 @@ fn setup(

if x_value >= 0.0 {
commands
.spawn_bundle(Text2dBundle {
.spawn(Text2dBundle {
// Use `Text` directly
text: Text {
// Construct a `Vec` of `TextSection`s
Expand Down

0 comments on commit 319e601

Please sign in to comment.