Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Target naming #256

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 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
41 changes: 37 additions & 4 deletions src/dataset/impl_dataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl<R: Records, S> DatasetBase<R, S> {
targets,
weights: Array1::zeros(0),
feature_names: Vec::new(),
target_names: Vec::new(),
}
}

Expand Down Expand Up @@ -81,13 +82,14 @@ impl<R: Records, S> DatasetBase<R, S> {
/// Updates the records of a dataset
///
/// This function overwrites the records in a dataset. It also invalidates the weights and
/// feature names.
/// feature/target names.
pub fn with_records<T: Records>(self, records: T) -> DatasetBase<T, S> {
DatasetBase {
records,
targets: self.targets,
weights: Array1::zeros(0),
feature_names: Vec::new(),
target_names: Vec::new(),
}
}

Expand All @@ -100,6 +102,7 @@ impl<R: Records, S> DatasetBase<R, S> {
targets,
weights: self.weights,
feature_names: self.feature_names,
target_names: self.target_names,
}
}

Expand All @@ -118,6 +121,15 @@ impl<R: Records, S> DatasetBase<R, S> {

self
}

/// Updates the target names of a dataset
pub fn with_target_names<I: Into<String>>(mut self, names: Vec<I>) -> DatasetBase<R, S> {
let target_names = names.into_iter().map(|x| x.into()).collect();

self.target_names = target_names;

self
}
Comment on lines +126 to +132
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should check the length of the input vector so that it's equal to number of targets. The input should be Vec so that it can be assigned directly. You can also try implementing this change for feature_names, but that might require more work to change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the input for the function is already Vec(I) where I implements Into(String). Unless I am mistaken you want something like?

pub fn with_target_names(mut self, names: Vec<String>) -> DatasetBase<R, S> {
        if names.len() == self.ntargets() {
            self.target_names = names;
        } else {
            // raise some error to user stating the number of targets is X or default to class_{0..ntargets}?
        }
        self
    }

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah pretty much. I don't mind panicking here, so you can just assert the condition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha

}

impl<L, R: Records, T: AsTargets<Elem = L>> DatasetBase<R, T> {
Expand All @@ -143,6 +155,7 @@ impl<L, R: Records, T: AsTargets<Elem = L>> DatasetBase<R, T> {
targets,
weights,
feature_names,
target_names,
..
} = self;

Expand All @@ -153,6 +166,20 @@ impl<L, R: Records, T: AsTargets<Elem = L>> DatasetBase<R, T> {
targets: targets.map(fnc),
weights,
feature_names,
target_names,
}
}

/// Returns target names
///
/// A target name gives a human-readable string describing the purpose of a single target.
pub fn target_names(&self) -> Vec<String> {
if !self.target_names.is_empty() {
self.target_names.clone()
} else {
(0..self.ntargets())
.map(|idx| format!("class-{}", idx))
.collect()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are no names, this method should instead just return empty list. The signature should be fn target_names(&self) -> &[String] to prevent cloning. Apply this change to feature_names as well for consistency.

}
}

Expand Down Expand Up @@ -216,6 +243,7 @@ where
DatasetBase::new(records, targets)
.with_feature_names(self.feature_names.clone())
.with_weights(self.weights.clone())
.with_target_names(self.target_names.clone())
}

/// Iterate over features
Expand Down Expand Up @@ -289,11 +317,13 @@ where
};
let dataset1 = DatasetBase::new(records_first, targets_first)
.with_weights(first_weights)
.with_feature_names(self.feature_names.clone());
.with_feature_names(self.feature_names.clone())
.with_target_names(self.target_names.clone());

let dataset2 = DatasetBase::new(records_second, targets_second)
.with_weights(second_weights)
.with_feature_names(self.feature_names.clone());
.with_feature_names(self.feature_names.clone())
.with_target_names(self.target_names.clone());

(dataset1, dataset2)
}
Expand Down Expand Up @@ -339,7 +369,8 @@ where
label,
DatasetBase::new(self.records().view(), targets)
.with_feature_names(self.feature_names.clone())
.with_weights(self.weights.clone()),
.with_weights(self.weights.clone())
.with_target_names(self.target_names.clone()),
)
})
.collect())
Expand Down Expand Up @@ -395,6 +426,7 @@ impl<F, D: Data<Elem = F>, I: Dimension> From<ArrayBase<D, I>>
targets: empty_targets,
weights: Array1::zeros(0),
feature_names: Vec::new(),
target_names: Vec::new(),
}
}
}
Expand All @@ -411,6 +443,7 @@ where
targets: rec_tar.1,
weights: Array1::zeros(0),
feature_names: Vec::new(),
target_names: Vec::new(),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/dataset/impl_targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ where
weights: Array1::from(weights),
targets,
feature_names: self.feature_names.clone(),
target_names: self.target_names.clone(),
}
}
}
4 changes: 3 additions & 1 deletion src/dataset/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,17 @@ where
if self.target_or_feature && self.dataset.nfeatures() <= self.idx {
return None;
}

let mut records = self.dataset.records.view();
let mut targets = self.dataset.targets.as_targets();
let feature_names;
let mut target_names = vec!["class".to_string()];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just write let mut target_names;

let weights = self.dataset.weights.clone();

if !self.target_or_feature {
// This branch should only run for 2D targets
targets.collapse_axis(Axis(1), self.idx);
feature_names = self.dataset.feature_names.clone();
target_names = self.dataset.target_names.clone();
oojo12 marked this conversation as resolved.
Show resolved Hide resolved
} else {
records.collapse_axis(Axis(1), self.idx);
oojo12 marked this conversation as resolved.
Show resolved Hide resolved
if self.dataset.feature_names.len() == records.len_of(Axis(1)) {
Expand All @@ -103,6 +104,7 @@ where
targets,
weights,
feature_names,
target_names,
};

Some(dataset_view)
Expand Down
9 changes: 9 additions & 0 deletions src/dataset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ impl Deref for Pr {
/// * `targets`: a two-/one-dimension matrix with dimensionality (nsamples, ntargets)
/// * `weights`: optional weights for each sample with dimensionality (nsamples)
/// * `feature_names`: optional descriptive feature names with dimensionality (nfeatures)
/// * `target_names`: optional descriptive target names with dimensionality (1)
oojo12 marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Trait bounds
///
Expand All @@ -180,6 +181,7 @@ where

pub weights: Array1<f32>,
feature_names: Vec<String>,
target_names: Vec<String>,
}

/// Targets with precomputed, counted labels
Expand Down Expand Up @@ -333,6 +335,13 @@ mod tests {
use ndarray::{array, Array1, Array2, Axis};
use rand::{rngs::SmallRng, SeedableRng};

#[test]
fn set_target_name() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add target name verification to one or two tests with target_iter just to verify that your iterator changes work correctly?

Copy link
Contributor Author

@oojo12 oojo12 Nov 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed lines 551-556 now test this.

let dataset = Dataset::new(array![[1., 2.], [1., 2.]], array![0., 1.])
.with_target_names(vec!["test"]);
assert_eq!(dataset.target_names, vec!["test"]);
}

#[test]
fn dataset_implements_required_methods() {
let mut rng = SmallRng::seed_from_u64(42);
Expand Down