Skip to content

Commit

Permalink
Merge pull request #35 from cpmech/add-features-2
Browse files Browse the repository at this point in the history
Add features 2
  • Loading branch information
cpmech authored Oct 9, 2023
2 parents 9b6b26e + 8083a69 commit 401fd0b
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 34 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/test_and_coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ jobs:
- uses: actions/checkout@v2
- name: Install Libraries
run: |
sudo apt-get update -y && sudo apt-get install -y liblapacke-dev libopenblas-dev python3-pip
sudo pip3 install matplotlib
bash zscripts/01-ubuntu-matplotlib-and-openblas.bash
- name: Install Rust
run: |
rustup toolchain install nightly --component llvm-tools-preview
Expand Down
16 changes: 16 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
"barstacked",
"bbox",
"borderaxespad",
"boxstyle",
"CARETDOWN",
"CARETDOWNBASE",
"CARETLEFT",
"CARETLEFTBASE",
"CARETRIGHT",
"CARETRIGHTBASE",
"CARETUP",
"CARETUPBASE",
"clabel",
"CLOSEPOLY",
"cmap",
Expand All @@ -32,6 +41,7 @@
"histtype",
"hspace",
"joinstyle",
"larrow",
"lineplot",
"linestyle",
"linestyles",
Expand All @@ -48,10 +58,16 @@
"patheffects",
"polyline",
"pyplot",
"rarrow",
"roundtooth",
"rstride",
"savefig",
"stepfilled",
"suptitle",
"TICKDOWN",
"TICKLEFT",
"TICKRIGHT",
"TICKUP",
"toolkits",
"wspace",
"xaxis",
Expand Down
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ categories = ["mathematics", "science"]
keywords = ["plot", "2D", "3D", "python", "matplotlib"]

[dependencies]
russell_lab = "0.5"
russell_chk = "0.5"
russell_lab = { path = "../russell/russell_lab", version = "0.5" }
55 changes: 30 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,52 @@

[![codecov](https://codecov.io/gh/cpmech/plotpy/branch/main/graph/badge.svg?token=SUBRKUN63U)](https://codecov.io/gh/cpmech/plotpy)

This library implements high-level functions to generate plots and drawings.
Although we use Python/Matplotlib, the goal is to provide a convenient Rust library
that is **different** than Matplotlib. The difference happens because we want **convenience**
for the Rust developer while getting the **fantastic quality of Matplotlib** 😀.
## Contents

Internally, we use [Matplotlib](https://matplotlib.org/) via a Python 3 script.
First, we generate a python code in a directory of your choice (e.g., `/tmp/plotpy`),
and then we call **python3** using Rust's `std::process::Command`.
* [Introduction](#introduction)
* [Installation on Debian/Ubuntu/Linux](#installation)
* [Installation on macOS](#macos)
* [Setting Cargo.toml](#cargo)
* [Examples](#examples)

For more information (and examples), check out the [plotpy documentation on docs.rs](https://docs.rs/plotpy)
## <a name="introduction"></a> Introduction

See also the [examples directory](https://github.com/cpmech/plotpy/tree/main/examples) with the output
of the [integration tests](https://github.com/cpmech/plotpy/tree/main/tests).
This crate implements high-level functions to generate plots and drawings. Although we use Python/Matplotlib, the goal is to provide a convenient Rust library that is **different** than Matplotlib. The difference happens because we want **convenience** for the Rust developer while getting the **fantastic quality of Matplotlib** 😀.

## Documentation
Internally, we use [Matplotlib](https://matplotlib.org/) via a Python 3 script. First, we generate a python code in a directory of your choice (e.g., `/tmp/plotpy`), and then we call **python3** using Rust's `std::process::Command`.

[Plotpy documentation on docs.rs](https://docs.rs/plotpy)
For more information (and examples), check out the [plotpy documentation on docs.rs](https://docs.rs/plotpy).

## Installation
See also the [examples directory](https://github.com/cpmech/plotpy/tree/main/examples) with the output of the [integration tests](https://github.com/cpmech/plotpy/tree/main/tests).

For convenience, we use [Russell Lab](https://github.com/cpmech/russell).
## <a name="installation"></a> Installation on Debian/Ubuntu/Linux

Thus, we need the following libraries:
In addition to Matplotlib (version >= 3.3.0), this crate depends on `russell_lab`, which, in turn, depends on an efficient BLAS library such as [OpenBLAS](https://github.com/OpenMathLib/OpenBLAS) and [Intel MKL](https://www.intel.com/content/www/us/en/docs/onemkl/developer-reference-c/2023-2/overview.html). Thus, we have two options:

### Latest Ubuntu dependencies
1. Use the standard Debian packages based on OpenBLAS (default)
2. **(XOR)** Install Intel MKL, which includes LAPACK

Option 2 requires the following environment variable:

```bash
sudo apt-get install liblapacke-dev libopenblas-dev python3-pip
pip install matplotlib
export RUSSELL_LAB_USE_INTEL_MKL=1
```

### Earlier Ubuntu dependencies
For convenience, you may use the scripts in the [zscripts](https://github.com/cpmech/plotpy/tree/main/zscripts) directory.

**1.** Install Matplotlib and use the standard Debian packages based on OpenBLAS:

```bash
sudo apt-get install liblapacke-dev libopenblas-dev python3-pip3
pip3 install matplotlib
bash zscripts/01-ubuntu-matplotlib-and-openblas.bash
```

**Note** We use pip3 because the version of Matplotlib needs to be at least 3.3.0 and the earlier Ubuntu comes with 3.1.2.
**2.** Install Matplotlib and install Intel MKL:

```bash
bash zscripts/02-ubuntu-matplotlib-and-intel-mkl.bash
```

### macOS dependencies
### <a name="macos"></a> Installation on macOS

In macOS, you may use [Homebrew](https://brew.sh/) (and pip) to install the dependencies:

Expand All @@ -57,7 +62,7 @@ pip3 install matplotlib
export LIBRARY_PATH=$LIBRARY_PATH:$(brew --prefix)/opt/openblas/lib:$(brew --prefix)/opt/lapack/lib
```

### Cargo.toml
## <a name="cargo"></a> Setting Cargo.toml

[![Crates.io](https://img.shields.io/crates/v/plotpy.svg)](https://crates.io/crates/plotpy)

Expand All @@ -68,7 +73,7 @@ export LIBRARY_PATH=$LIBRARY_PATH:$(brew --prefix)/opt/openblas/lib:$(brew --pre
plotpy = "*"
```

## Examples
## <a name="examples"></a> Examples

### Contour

Expand Down
31 changes: 31 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,37 @@ def set_equal_axes():
print('ERROR: set_box_aspect is missing in this version of Matplotlib')
";

const PY_NUM_MARKERS: [&str; 12] = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"];

/// Quotes or not the marker style
///
/// This is needed because the following markers are python numbers,
/// and not strings (so, they must not be quoted):
///
/// ```text
/// 0 (TICKLEFT)
/// 1 (TICKRIGHT)
/// 2 (TICKUP)
/// 3 (TICKDOWN)
/// 4 (CARETLEFT)
/// 5 (CARETRIGHT)
/// 6 (CARETUP)
/// 7 (CARETDOWN)
/// 8 (CARETLEFTBASE)
/// 9 (CARETRIGHTBASE)
/// 10 (CARETUPBASE)
/// 11 (CARETDOWNBASE)
/// ```
///
/// See: <https://matplotlib.org/stable/api/markers_api.html>
pub(crate) fn quote_marker(maker_style: &str) -> String {
if PY_NUM_MARKERS.contains(&maker_style) {
String::from(maker_style)
} else {
format!("'{}'", maker_style)
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#[cfg(test)]
Expand Down
9 changes: 8 additions & 1 deletion src/curve.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{vector_to_array, AsVector, GraphMaker};
use crate::quote_marker;
use std::fmt::Write;

/// Holds either the second point coordinates of a ray or the slope of the ray
Expand Down Expand Up @@ -433,7 +434,7 @@ impl Curve {
write!(&mut opt, ",markersize={}", self.marker_size).unwrap();
}
if self.marker_style != "" {
write!(&mut opt, ",marker='{}'", self.marker_style).unwrap();
write!(&mut opt, ",marker={}", quote_marker(&self.marker_style)).unwrap();
}

// clipping
Expand Down Expand Up @@ -513,6 +514,12 @@ mod tests {
,marker='o'\
,clip_on=False"
);
let mut curve = Curve::new();
for i in 5..12 {
curve.set_marker_style(&format!("{}", i));
let options = curve.options();
assert_eq!(options, format!(",marker={}", i));
}
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl Plot {
///
/// # Input
///
/// * `grid_handle` -- an identifier for GridSpec to be used later with [Plot::set_grid]
/// * `grid_handle` -- an identifier for GridSpec to be used later with [Plot::set_subplot_grid]
/// * `row` -- number of rows in the grid
/// * `col` -- number of columns in the grid
/// * `options` -- (may be empty) Comma separated options. Example `"wspace=0,hspace=0.35"`.
Expand Down
3 changes: 1 addition & 2 deletions src/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,7 @@ impl GraphMaker for Surface {
mod tests {
use super::Surface;
use crate::GraphMaker;
use russell_chk::vec_approx_eq;
use russell_lab::Matrix;
use russell_lab::{vec_approx_eq, Matrix};

#[test]
fn new_works() {
Expand Down
2 changes: 1 addition & 1 deletion tests/test_curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn test_curve() -> Result<(), StrError> {
curve2
.set_line_style("None")
.set_marker_line_color("#1862ab")
.set_marker_style("s")
.set_marker_style("8")
.set_marker_void(true);

// draw curves
Expand Down
19 changes: 19 additions & 0 deletions zscripts/01-ubuntu-matplotlib-and-openblas.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

set -e

# fake sudo function to be used by docker build
sudo () {
[[ $EUID = 0 ]] || set -- command sudo "$@"
"$@"
}

# install dependencies
sudo apt-get update -y && \
sudo apt-get install -y --no-install-recommends \
g++ \
gdb \
gfortran \
liblapacke-dev \
libopenblas-dev \
python3-matplotlib
22 changes: 22 additions & 0 deletions zscripts/02-ubuntu-matplotlib-and-intel-mkl.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

set -e

# fake sudo function to be used by docker build
sudo () {
[[ $EUID = 0 ]] || set -- command sudo "$@"
"$@"
}

# install dependencies
sudo apt-get update -y && \
sudo apt-get install -y --no-install-recommends \
g++ \
gdb \
git \
libmetis-dev \
make \
python3-matplotlib

# install Intel MKL
bash zscripts/install-intel-mkl-linux.bash
27 changes: 27 additions & 0 deletions zscripts/install-intel-mkl-linux.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

set -e

# fake sudo function to be used by docker build
sudo () {
[[ $EUID = 0 ]] || set -- command sudo "$@"
"$@"
}

# update sources.list
wget -O- https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB | gpg --dearmor | sudo tee /usr/share/keyrings/oneapi-archive-keyring.gpg > /dev/null
sudo echo "deb [signed-by=/usr/share/keyrings/oneapi-archive-keyring.gpg] https://apt.repos.intel.com/oneapi all main" | sudo tee /etc/apt/sources.list.d/oneAPI.list

# install packages
sudo apt-get update -y && \
sudo apt-get install -y --no-install-recommends \
intel-oneapi-mkl \
intel-oneapi-mkl-devel

LIBDIR1="/opt/intel/oneapi/mkl/latest/lib/intel64"
LIBDIR2="/opt/intel/oneapi/compiler/latest/linux/compiler/lib/intel64_lin"

# update ldconfig
echo "${LIBDIR1}" | sudo tee /etc/ld.so.conf.d/intel-oneapi-mkl-and-compiler.conf >/dev/null
echo "${LIBDIR2}" | sudo tee -a /etc/ld.so.conf.d/intel-oneapi-mkl-and-compiler.conf >/dev/null
sudo ldconfig

0 comments on commit 401fd0b

Please sign in to comment.