Skip to content

Commit

Permalink
corrected numeric scales for #3
Browse files Browse the repository at this point in the history
  • Loading branch information
SymbolixAU committed Sep 5, 2018
1 parent 5f69431 commit fe3a9dd
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 11 deletions.
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ rcpp_colour_str_variable_hex <- function(x, palette, na_colour) {
.Call(`_RcppViridis_rcpp_colour_str_variable_hex`, x, palette, na_colour)
}

rcpp_colour_dte_variable_hex <- function(x, palette, na_colour) {
.Call(`_RcppViridis_rcpp_colour_dte_variable_hex`, x, palette, na_colour)
}

rcpp_is_hex_colour <- function(hex) {
.Call(`_RcppViridis_rcpp_is_hex_colour`, hex)
}
Expand Down
15 changes: 10 additions & 5 deletions R/colour_variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@
#' colour_variables(x = 1:5, palette = "cividis")
#'
#' @export
colour_variables <- function( x, palette = "viridis", na_colour = "#808080") {
colour_variables <- function( x, palette = "viridis", na_colour = "#808080" ) {
palette <- match.arg(palette, choices = c("viridis","inferno","plasma","magma","cividis"))
UseMethod("colour_variables")
}

#' @export
colour_variables.character <- function(x, palette = "viridis", na_colour = "#808080") {
rcpp_colour_str_variable_hex(x, palette, na_colour)
colour_variables.character <- function( x, palette = "viridis", na_colour = "#808080" ) {
rcpp_colour_str_variable_hex( x, palette, na_colour )
}

#' #' @export
#' colour_variables.Date <- function( x, palette = "viridis", na_colour = "#808080" ) {
#' rcpp_colour_dte_variable_hex( x, palette, na_colour )
#' }

#' @export
colour_variables.default <- function(x, palette = "viridis", na_colour = "#808080" ) {
rcpp_colour_num_variable_hex(x, palette, na_colour)
colour_variables.default <- function( x, palette = "viridis", na_colour = "#808080" ) {
rcpp_colour_num_variable_hex( x, palette, na_colour )
}

28 changes: 28 additions & 0 deletions R/scratch.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
## Benchmarks

# library(scales)
# library(microbenchmark)
# x <- 1:1e5
# n <- length(x)
# microbenchmark(
# scales = { x <- scales::col_numeric(viridisLite::viridis(n), domain = x)(x) },
# rcpp = { y <- RcppViridis::colour_variables(x) },
# times = 1
# )


# df <- data.frame(a = 10, x = 1:10)
# df$col <- mapview:::zcolColors(df$x)
# barplot(height = df$a, col = df$col, border = NA, space = 0)
#
# df$col <- RcppViridis::colour_variables(df$x)
# barplot(height = df$a, col = df$col, border = NA, space = 0)
#
#
# df <- data.frame(a = 10, x = c(1:5,NA,7,NA,9,10))
# df$col2 = mapview:::zcolColors(df$x, na.color = "#00FF00")
# barplot(height = df$a, col = df$col2, border = NA, space = 0)
#
# df$col2 <- RcppViridis::colour_variables(df$x, na_colour = "#00FF00")
# barplot(height = df$a, col = df$col2, border = NA, space = 0)

#
# library(Rcpp)
#
Expand Down
Binary file added Rplots.pdf
Binary file not shown.
11 changes: 11 additions & 0 deletions inst/include/RcppViridis_Base.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef RCPP_VIRIDIS_BASE_H
#define RCPP_VIRIDIS_BASE_H

// extensions to base::
Rcpp::NumericVector m_diff( Rcpp::NumericVector x );

Rcpp::NumericVector m_range( Rcpp::NumericVector x );

Rcpp::NumericVector m_rescale( Rcpp::NumericVector x);

#endif
18 changes: 18 additions & 0 deletions man/colour_variables.default.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion src/ColourVariables.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <Rcpp.h>
#include "RcppViridis.hpp"
#include "RcppViridis_Base.hpp"
#include "RcppViridisMagma.hpp"
#include "RcppViridisInferno.hpp"
#include "RcppViridisPlasma.hpp"
Expand Down Expand Up @@ -43,10 +44,16 @@ Rcpp::StringVector colour_variable_hex( Rcpp::NumericVector x, std::string palet
Rcpp::stop("invalid NA Colour");
}


//Rcpp::NumericVector vals = m_unique(x);
Rcpp::NumericVector scaledVals = m_rescale(x);
Rcpp::StringVector hex_strings(n);

/*
double max_x = max( na_omit( x ) ) ;
double scale_x = 255 / max_x;
*/

int i = 0;

// TODO(allow user to select start and end points of the vectors)
Expand Down Expand Up @@ -87,11 +94,13 @@ Rcpp::StringVector colour_variable_hex( Rcpp::NumericVector x, std::string palet
std::string hex_str;

for( i = 0; i < n; i ++ ) {
this_x = x[i] * scale_x;
//this_x = x[i] * scale_x;
// mat_colours(i, 0) = round( spline_red( this_x ) * 255) ;
// mat_colours(i, 1) = round( spline_green( this_x ) * 255);
// mat_colours(i, 2) = round( spline_blue( this_x ) * 255);

this_x = scaledVals[i] * 255;

if ( R_IsNA( this_x) || R_IsNaN( this_x ) ) {
hex_strings[i] = na_colour;
} else {
Expand Down Expand Up @@ -119,3 +128,11 @@ Rcpp::StringVector rcpp_colour_str_variable_hex( Rcpp::StringVector x, std::stri
return colour_variable_hex( x, palette, na_colour );
}

// [[Rcpp::export]]
Rcpp::StringVector rcpp_colour_dte_variable_hex( Rcpp::DateVector x, std::string palette, std::string na_colour) {
Rcpp::NumericVector y = as< Rcpp::NumericVector>(x);
return colour_variable_hex( y, palette, na_colour );
}



14 changes: 14 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// rcpp_colour_dte_variable_hex
Rcpp::StringVector rcpp_colour_dte_variable_hex(Rcpp::DateVector x, std::string palette, std::string na_colour);
RcppExport SEXP _RcppViridis_rcpp_colour_dte_variable_hex(SEXP xSEXP, SEXP paletteSEXP, SEXP na_colourSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Rcpp::DateVector >::type x(xSEXP);
Rcpp::traits::input_parameter< std::string >::type palette(paletteSEXP);
Rcpp::traits::input_parameter< std::string >::type na_colour(na_colourSEXP);
rcpp_result_gen = Rcpp::wrap(rcpp_colour_dte_variable_hex(x, palette, na_colour));
return rcpp_result_gen;
END_RCPP
}
// rcpp_is_hex_colour
Rcpp::LogicalVector rcpp_is_hex_colour(Rcpp::StringVector hex);
RcppExport SEXP _RcppViridis_rcpp_is_hex_colour(SEXP hexSEXP) {
Expand All @@ -46,6 +59,7 @@ END_RCPP
static const R_CallMethodDef CallEntries[] = {
{"_RcppViridis_rcpp_colour_num_variable_hex", (DL_FUNC) &_RcppViridis_rcpp_colour_num_variable_hex, 3},
{"_RcppViridis_rcpp_colour_str_variable_hex", (DL_FUNC) &_RcppViridis_rcpp_colour_str_variable_hex, 3},
{"_RcppViridis_rcpp_colour_dte_variable_hex", (DL_FUNC) &_RcppViridis_rcpp_colour_dte_variable_hex, 3},
{"_RcppViridis_rcpp_is_hex_colour", (DL_FUNC) &_RcppViridis_rcpp_is_hex_colour, 1},
{NULL, NULL, 0}
};
Expand Down
39 changes: 39 additions & 0 deletions src/RcppViridis_Base.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <Rcpp.h>
#include <algorithm>
using namespace Rcpp;

Rcpp::NumericVector m_diff(Rcpp::NumericVector x) {
int n = x.size() - 1;
Rcpp::NumericVector difference(n);
int i = 0;
for (i = 0; i < n; i++) {
difference[i] = x[i+1] - x[i];
}
return difference;
}

Rcpp::NumericVector m_range(Rcpp::NumericVector x) {
Rcpp::NumericVector rng(2);
rng[0] = min(na_omit(x));
rng[1] = max(na_omit(x));
return rng;
}


// Always rescales to (0, 1)
Rcpp::NumericVector m_rescale( Rcpp::NumericVector x ) {
int n = x.size();
Rcpp::NumericVector rescaled(n);
Rcpp::NumericVector rng = m_range(x);
Rcpp::NumericVector diff_from = m_diff(rng); // should only be one value!

double this_diff = std::max(1.0, diff_from[0]);

int i = 0;

for (i = 0; i < n; i++) {
rescaled[i] = (x[i] - rng[0]) / this_diff;
}
return rescaled;
}

5 changes: 1 addition & 4 deletions src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ std::string ConvertRGBtoHex(int num) {
}

std::string ConvertRGBtoHex(int r, int g, int b) {
int rgbNum = ((r & 0xff) << 16)
| ((g & 0xff) << 8)
| (b & 0xff);

int rgbNum = ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
return '#' + ConvertRGBtoHex(rgbNum);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-colour_variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test_that("NAs Numerics are handled", {
expect_true(colour_variables(NA) == "#808080")
expect_true("#808080" %in% colour_variables(c(1,2,NA,4)))
expect_true(sum("#808080" == colour_variables(c(1, NA)))==1)
expect_true(sum("#808080" == colour_variables(c(1,NA,NaN,Inf,-Inf,1))) == 4)
expect_true(sum("#808080" == colour_variables(c(1,NA,NaN,Inf,-Inf,1))) == 6)
expect_true("#000000" == colour_variables(NA, na_colour = "#000000"))
})

Expand Down

0 comments on commit fe3a9dd

Please sign in to comment.