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

Use num-traits Num for input requiring numbers #94

Merged
merged 1 commit into from
Sep 25, 2024
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ categories = ["mathematics", "science"]
keywords = ["plot", "2D", "3D", "python", "matplotlib"]

[dependencies]
num-traits = "0.2"
19 changes: 5 additions & 14 deletions src/barplot.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{vector_to_array, generate_list_quoted, AsVector, GraphMaker};
use super::{generate_list_quoted, vector_to_array, AsVector, GraphMaker};
use num_traits::Num;
use std::fmt::Write;

/// Generates a Barplot plot
Expand Down Expand Up @@ -138,14 +139,10 @@ impl Barplot {
}

/// Draws the bar plot
///
/// # Notes
///
/// * The type `U` of the input array must be a number.
pub fn draw<'a, T, U>(&mut self, x: &'a T, y: &'a T)
where
T: AsVector<'a, U>,
U: 'a + std::fmt::Display,
U: 'a + std::fmt::Display + Num,
{
vector_to_array(&mut self.buffer, "x", x);
vector_to_array(&mut self.buffer, "y", y);
Expand All @@ -170,16 +167,10 @@ impl Barplot {
}

/// Draws the bar plot with strings
///
/// # Notes
///
/// * The type `S` of the input array must be a string.
/// * The type `U` of the input array must be a number.
pub fn draw_with_str<'a, S, T, U>(&mut self, x: &[S], y: &'a T)
pub fn draw_with_str<'a, T, U>(&mut self, x: &[&str], y: &'a T)
where
S: std::fmt::Display,
T: AsVector<'a, U>,
U: 'a + std::fmt::Display,
U: 'a + std::fmt::Display + Num,
{
generate_list_quoted(&mut self.buffer, "x", x);
vector_to_array(&mut self.buffer, "y", y);
Expand Down
69 changes: 31 additions & 38 deletions src/boxplot.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{generate_list, generate_nested_list, matrix_to_array, AsMatrix, GraphMaker};
use num_traits::Num;
use std::fmt::Write;

/// Draw a box and whisker plot
Expand Down Expand Up @@ -86,18 +87,18 @@ use std::fmt::Write;
///
/// See also integration test in the **tests** directory.
pub struct Boxplot {
symbol: String, // The default symbol for flier (outlier) points.
horizontal: bool, // Horizontal boxplot (default is false)
whisker: Option<f64>, // The position of the whiskers
positions: Vec<f64>, // The positions of the boxes
width: Option<f64>, // The width of the boxes
no_fliers: bool, // Disables fliers
patch_artist: bool, // If false, produces boxes with the Line2D artist. Otherwise, boxes are drawn with Patch artists.
medianprops: String, // The properties of the median
boxprops: String, // The properties of the box
whiskerprops: String, // The properties of the whisker
extra: String, // Extra commands (comma separated)
buffer: String, // Buffer
symbol: String, // The default symbol for flier (outlier) points.
horizontal: bool, // Horizontal boxplot (default is false)
whisker: Option<f64>, // The position of the whiskers
positions: Vec<f64>, // The positions of the boxes
width: Option<f64>, // The width of the boxes
no_fliers: bool, // Disables fliers
patch_artist: bool, // Enables the use of Patch artist to draw boxes
median_props: String, // The properties of the median
box_props: String, // The properties of the box
whisker_props: String, // The properties of the whisker
extra: String, // Extra commands (comma separated)
buffer: String, // Buffer
}

impl Boxplot {
Expand All @@ -111,9 +112,9 @@ impl Boxplot {
width: None,
no_fliers: false,
patch_artist: false,
medianprops: String::new(),
boxprops: String::new(),
whiskerprops: String::new(),
median_props: String::new(),
box_props: String::new(),
whisker_props: String::new(),
extra: String::new(),
buffer: String::new(),
}
Expand All @@ -125,13 +126,9 @@ impl Boxplot {
///
/// * `data` -- Is a sequence of 1D arrays such that a boxplot is drawn for each array in the sequence.
/// [From Matplotlib](https://matplotlib.org/3.6.3/api/_as_gen/matplotlib.pyplot.boxplot.html)
///
/// # Notes
///
/// * The type `T` must be a number.
pub fn draw<T>(&mut self, data: &Vec<Vec<T>>)
where
T: std::fmt::Display,
T: std::fmt::Display + Num,
{
generate_nested_list(&mut self.buffer, "x", data);
if self.positions.len() > 0 {
Expand All @@ -147,14 +144,10 @@ impl Boxplot {
///
/// * `data` -- Is a 2D array (matrix) such that a boxplot is drawn for each column in the matrix.
/// [From Matplotlib](https://matplotlib.org/3.6.3/api/_as_gen/matplotlib.pyplot.boxplot.html)
///
/// # Notes
///
/// * The type `U` must be a number.
pub fn draw_mat<'a, T, U>(&mut self, data: &'a T)
where
T: AsMatrix<'a, U>,
U: 'a + std::fmt::Display,
U: 'a + std::fmt::Display + Num,
{
matrix_to_array(&mut self.buffer, "x", data);
if self.positions.len() > 0 {
Expand Down Expand Up @@ -204,7 +197,7 @@ impl Boxplot {
self
}

/// Enable fill the boxes
/// Enables the use of Patch artist to draw boxes instead of Line2D artist
pub fn set_patch_artist(&mut self, flag: bool) -> &mut Self {
self.patch_artist = flag;
self
Expand All @@ -214,19 +207,19 @@ impl Boxplot {
///
/// [See Matplotlib's documentation](https://matplotlib.org/3.6.3/api/_as_gen/matplotlib.pyplot.boxplot.html)
pub fn set_medianprops(&mut self, props: &str) -> &mut Self {
self.medianprops = props.to_string();
self.median_props = props.to_string();
self
}

/// Set the properties of the box
pub fn set_boxprops(&mut self, props: &str) -> &mut Self {
self.boxprops = props.to_string();
self.box_props = props.to_string();
self
}

/// Set the properties of the whisker
pub fn set_whiskerprops(&mut self, props: &str) -> &mut Self {
self.whiskerprops = props.to_string();
self.whisker_props = props.to_string();
self
}

Expand Down Expand Up @@ -268,14 +261,14 @@ impl Boxplot {
if self.patch_artist {
write!(&mut opt, ",patch_artist=True").unwrap();
}
if self.medianprops != "" {
write!(&mut opt, ",medianprops={}", self.medianprops).unwrap();
if self.median_props != "" {
write!(&mut opt, ",medianprops={}", self.median_props).unwrap();
}
if self.boxprops != "" {
write!(&mut opt, ",boxprops={}", self.boxprops).unwrap();
if self.box_props != "" {
write!(&mut opt, ",boxprops={}", self.box_props).unwrap();
}
if self.whiskerprops != "" {
write!(&mut opt, ",whiskerprops={}", self.whiskerprops).unwrap();
if self.whisker_props != "" {
write!(&mut opt, ",whiskerprops={}", self.whisker_props).unwrap();
}
if self.extra != "" {
write!(&mut opt, ",{}", self.extra).unwrap();
Expand Down Expand Up @@ -310,9 +303,9 @@ mod tests {
assert_eq!(boxes.width, None);
assert_eq!(boxes.no_fliers, false);
assert_eq!(boxes.patch_artist, false);
assert_eq!(boxes.medianprops.len(), 0);
assert_eq!(boxes.boxprops.len(), 0);
assert_eq!(boxes.whiskerprops.len(), 0);
assert_eq!(boxes.median_props.len(), 0);
assert_eq!(boxes.box_props.len(), 0);
assert_eq!(boxes.whisker_props.len(), 0);
assert_eq!(boxes.extra.len(), 0);
assert_eq!(boxes.buffer.len(), 0);
}
Expand Down
10 changes: 3 additions & 7 deletions src/contour.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{matrix_to_array, vector_to_array, generate_list_quoted, AsMatrix, GraphMaker};
use super::{generate_list_quoted, matrix_to_array, vector_to_array, AsMatrix, GraphMaker};
use num_traits::Num;
use std::fmt::Write;

/// Generates a contour plot
Expand Down Expand Up @@ -113,15 +114,10 @@ impl Contour {
/// * `no_labels` -- skip adding labels to the lines contour (if enabled)
/// * `no_colorbar` -- skip drawing a colorbar
/// * `with_selected` -- draw a line contour with a selected level (e.g., 0.0) on top of everything
///
/// # Notes
///
/// * The type `U` of the input matrices must be a number.
///
pub fn draw<'a, T, U>(&mut self, x: &'a T, y: &'a T, z: &'a T)
where
T: AsMatrix<'a, U>,
U: 'a + std::fmt::Display,
U: 'a + std::fmt::Display + Num,
{
matrix_to_array(&mut self.buffer, "x", x);
matrix_to_array(&mut self.buffer, "y", y);
Expand Down
9 changes: 5 additions & 4 deletions src/conversions.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use super::{AsMatrix, AsVector};
use num_traits::Num;
use std::fmt::Write;

/// Generates a Python list
pub(crate) fn generate_list<T>(buf: &mut String, name: &str, data: &[T])
where
T: std::fmt::Display,
T: std::fmt::Display + Num,
{
write!(buf, "{}=[", name).unwrap();
for val in data.into_iter() {
Expand All @@ -29,7 +30,7 @@ where
pub(crate) fn vector_to_array<'a, T, U>(buf: &mut String, name: &str, vector: &'a T)
where
T: AsVector<'a, U>,
U: 'a + std::fmt::Display,
U: 'a + std::fmt::Display + Num,
{
write!(buf, "{}=np.array([", name).unwrap();
let m = vector.vec_size();
Expand All @@ -42,7 +43,7 @@ where
/// Generates a nested Python list
pub(crate) fn generate_nested_list<T>(buf: &mut String, name: &str, data: &Vec<Vec<T>>)
where
T: std::fmt::Display,
T: std::fmt::Display + Num,
{
write!(buf, "{}=[", name).unwrap();
for row in data.into_iter() {
Expand All @@ -59,7 +60,7 @@ where
pub(crate) fn matrix_to_array<'a, T, U>(buf: &mut String, name: &str, matrix: &'a T)
where
T: AsMatrix<'a, U>,
U: 'a + std::fmt::Display,
U: 'a + std::fmt::Display + Num,
{
write!(buf, "{}=np.array([", name).unwrap();
let (m, n) = matrix.size();
Expand Down
20 changes: 4 additions & 16 deletions src/curve.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{vector_to_array, AsVector, GraphMaker};
use crate::quote_marker;
use num_traits::Num;
use std::fmt::Write;

/// Holds either the second point coordinates of a ray or the slope of the ray
Expand Down Expand Up @@ -281,14 +282,10 @@ impl Curve {
///
/// * `x` - abscissa values
/// * `y` - ordinate values
///
/// # Notes
///
/// * The type `U` of the input array must be a number.
pub fn draw<'a, T, U>(&mut self, x: &'a T, y: &'a T)
where
T: AsVector<'a, U>,
U: 'a + std::fmt::Display,
U: 'a + std::fmt::Display + Num,
{
vector_to_array(&mut self.buffer, "x", x);
vector_to_array(&mut self.buffer, "y", y);
Expand All @@ -299,14 +296,10 @@ impl Curve {
/// Draws curve on a previously drawn figure with the same x
///
/// * `y` - ordinate values on the right-hand side
///
/// # Notes
///
/// * The type `U` of the input array must be a number.
pub fn draw_with_twin_x<'a, T, U>(&mut self, y: &'a T)
where
T: AsVector<'a, U>,
U: 'a + std::fmt::Display,
U: 'a + std::fmt::Display + Num,
{
vector_to_array(&mut self.buffer, "y2", y);
let opt = self.options();
Expand All @@ -328,15 +321,10 @@ impl Curve {
/// * `x` - x values
/// * `y` - y values
/// * `z` - z values
///
/// # Notes
///
/// * The type `U` of the input array must be a number.
///
pub fn draw_3d<'a, T, U>(&mut self, x: &'a T, y: &'a T, z: &'a T)
where
T: AsVector<'a, U>,
U: 'a + std::fmt::Display,
U: 'a + std::fmt::Display + Num,
{
vector_to_array(&mut self.buffer, "x", x);
vector_to_array(&mut self.buffer, "y", y);
Expand Down
13 changes: 4 additions & 9 deletions src/histogram.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{generate_list_quoted, generate_nested_list, GraphMaker};
use num_traits::Num;
use std::fmt::Write;

/// Generates a Histogram plot
Expand Down Expand Up @@ -78,17 +79,11 @@ impl Histogram {
///
/// # Input
///
/// * `values` - values
/// * `labels` - labels
///
/// # Notes
///
/// * The type `T` must be a number.
/// * The type `U` must be a String or &str.
///
/// * `values` -- holds the values
/// * `labels` -- holds the labels
pub fn draw<T, U>(&mut self, values: &Vec<Vec<T>>, labels: &[U])
where
T: std::fmt::Display,
T: std::fmt::Display + Num,
U: std::fmt::Display,
{
let opt = self.options();
Expand Down
7 changes: 2 additions & 5 deletions src/image.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{matrix_to_array, AsMatrix, GraphMaker};
use num_traits::Num;
use std::fmt::Write;

/// Generates an image plot (imshow)
Expand Down Expand Up @@ -54,14 +55,10 @@ impl Image {
}

/// (imshow) Displays data as an image
///
/// # Notes
///
/// * The type `U` of the input array must be a number.
pub fn draw<'a, T, U>(&mut self, data: &'a T)
where
T: AsMatrix<'a, U>,
U: 'a + std::fmt::Display,
U: 'a + std::fmt::Display + Num,
{
matrix_to_array(&mut self.buffer, "data", data);
let opt = self.options();
Expand Down
5 changes: 3 additions & 2 deletions src/plot.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{call_python3, generate_list_quoted, vector_to_array, AsVector, Legend, StrError, SuperTitleParams};
use num_traits::Num;
use std::ffi::OsStr;
use std::fmt::Write;
use std::fs::{self, File};
Expand Down Expand Up @@ -705,7 +706,7 @@ impl Plot {
where
S: std::fmt::Display,
T: AsVector<'a, U>,
U: 'a + std::fmt::Display,
U: 'a + std::fmt::Display + Num,
{
assert_eq!(ticks.vec_size(), labels.len());
vector_to_array(&mut self.buffer, "tx", ticks);
Expand All @@ -723,7 +724,7 @@ impl Plot {
where
S: std::fmt::Display,
T: AsVector<'a, U>,
U: 'a + std::fmt::Display,
U: 'a + std::fmt::Display + Num,
{
assert_eq!(ticks.vec_size(), labels.len());
vector_to_array(&mut self.buffer, "ty", ticks);
Expand Down
Loading