All The President’s Men

October 30th, 2020

2020 – what a ride. We’re just a couple of days out from November 3rd, 2020 — election day.

What’s at stake is clear for both parties. Whomever the elected winners might be, they are inheriting no easy tasks. The COVID pandemic has caused a global economic contraction and killed hundreds of thousands. Civil unrest is at an all-time high due to racial injustice. The climate has proven a formidable challenger in ever more frequent and chaotic outbursts. And we are a more divided country than any time in recent history – certainly in my lifetime.

And It will be the duty of the elected officials and in particular, the President’s duty, to address these issues. It is their duty.

And We The People must find unity through the similarities we all share and cherish our differences with trust and respect for each of our own idea of the American Dream.

Now more than ever we must listen.

Now more than ever we must build.

Because without — our nation will fail.

beakr for R and what it means

October 15th, 2020

What is beakr?

beakr is an unopinionated and minimalist web framework for developing and deploying web services with R. It is designed to be as simple as possible for data scientists and engineerings to quickly write web applications, services, and APIs without worrying about lower-level details or high-level side-effects. In other words, beakr is made to be explicit, robust, and scalable – and the batteries are not included.

beakr is built on the httpuv package – which itself is built on top of the libuv and http-parser C libraries. So while beakr will be stable for most all R operations, it is not necessarily recommended for building extensive web-applications and is not designed to be an especially performant web framework. If you’re looking for a full-featured web framework see Shiny, django, etc. beakr is inspired by the minimalist and massively-expandable frameworks offered by Express.js and Flask.

beakr takes a programmatic approach to creating web-services and the framework focuses on just core HTTP protocol functionality (e.g POST, GET, PUT, etc.). To illustrate, we can create a “Hello World” application with beakr with only a few lines of code.

library(beakr)
beakr <- newBeakr()
beakr %>%
  httpGET("/hello", function(req, res, err) {
    "Hello, world!"
  }) %>% 
  listen() 

Running this script will serve the application (by default on host = 127.0.0.1 and port = 25118). When a client points to the path /hello a GET request is made to the server. The beakr instance is listening and handles it with a defined server response – in this case, the response will show an HTML page with the plain-text “Hello, world!”.

So what is beakr good for?

beakr is simple and flexible and as such is best suited for simplicity. It is a great tool to quickly and easily stand up web services without the limitations of heavier-weight contenders so you can worry less about having to learn a new framework and more about what the application should do. A few examples could be:

  • Building RESTful APIs
  • Building web-applications and services

Examples

REST API

It is increasingly important for data scientists to share operational data across computers, languages, and pipelines. beakr makes it easy to create micro-services like RESTful (Representational State Transfer) APIs and other infrastructure that are flexible, easy to deploy, scale, and maintain.

For example, we can easily build a RESTful API with two endpoints (/Sepal.Length, /Sepal.Width) that retrieve and respond with a JSON data object from Fischers Iris dataset.

library(beakr)

data("iris")

beakr <- newBeakr()

beakr %>% 
  httpGET("/iris/Sepal.Width", function(req, res, err) {
    res$json(iris$Sepal.Width)
  }) %>% 
  httpGET("/iris/Sepal.Length", function(req, res, err) {
    res$json(iris$Sepal.Length)
  }) %>%
  listen()

Now a client (web-browser) can access the JSON data at 127.0.0.1:25118/Sepal.Length and 127.0.0.1:25118/Sepal.Width.

Deploy a model

beakr can also be used to deploy models and algorithms as micro-services. For example, we can deploy a simple KNN-model to return the flower species (from the Iris dataset) to a client as plain text. Using the POST method we can create a beakr instance to handle recvieving a JSON object containing the sepal and petal lengths and widths (sl, sw, pl, pw, respectively).

First we can define and train a simplistic K-nearest neighbors flower model using the caret package.

library(caret)

# Load the Iris data set 
data('iris')

# Train using KNN
knn_model <- train(
  Species ~ ., 
  data = iris, 
  method = 'knn', 
  trControl = trainControl(method='cv', number=10), 
  metric = 'Accuracy'
)

With beakr‘s we can create and expose normal R functions using the built in decorate() function, which easily prepares functions to accept and respond as you’d expect. With this is mind, we can write a simple function to accept our petal and sepal parameters and return the predicted species using our model defined above.

# Function to predict the species using the trained model.
predict_species <- function(sl, sw, pl, pw) {
  test <- data.frame(
    Sepal.Length = as.numeric(sl),
    Sepal.Width = as.numeric(sw),
    Petal.Length = as.numeric(pl),
    Petal.Width = as.numeric(pw),
    Species = NA
  )
  predict(knn_model, test)
}
library(beakr)

newBeakr() %>%
  httpPOST("/predict-species", decorate(predict_species)) %>%
  handleErrors() %>%
  listen(host = "127.0.0.1", port = 25118)

By sending a POST request to 127.0.0.1:25118/predict-species with a JSON object containing labeled numeric data we can see it responds with an answer.

$ curl -X POST http://127.0.0.1:25118/predict-species \
  -H 'content-type: application/json' \
  -d '{ "sl": 5.3, "sw": 4, "pl": 1.6, "pw": 0.2 }'
  
> setosa

beakr really shines in these sort of things. At Mazama Science, we use beakr to provide services for server monitoring, logging, and serving data. It has become a proven tool and we at Mazama are excited to share!

This is a test

September 14th, 2020

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed pretium, nisl ut efficitur tristique, mi odio tempor nisl, nec interdum magna massa quis ex. Nullam finibus felis at ipsum fringilla, sed egestas mi porta. Fusce eget aliquet sapien. Nullam consequat justo justo, et gravida libero accumsan sit amet. Morbi eu suscipit velit. Nam bibendum, libero sed finibus blandit, nisl velit aliquam tellus, a pulvinar metus arcu tincidunt libero. Nunc lobortis nisi sed diam blandit rutrum eu quis nulla. Sed sollicitudin ultricies enim, rutrum interdum elit finibus in.

this isn't a test

Etiam consectetur ultricies erat, id pellentesque magna imperdiet et. Integer tincidunt non ex non fringilla. Nulla facilisi. Vestibulum ut elit non urna aliquam semper. Sed in diam hendrerit, placerat risus sit amet, semper augue. Pellentesque id ante vel neque consectetur lacinia a vel orci. In hac habitasse platea dictumst.

Proin neque purus, scelerisque non mauris id, condimentum efficitur turpis. Praesent vel velit nec tellus fringilla facilisis in ut mi. Ut velit massa, pulvinar at nibh sit amet, fringilla pulvinar justo. Vivamus mollis maximus euismod. In accumsan est vitae ante bibendum dignissim. Ut sed finibus velit, ac rutrum justo. Duis pellentesque ultrices orci vel semper. Cras ut enim quis dui efficitur dapibus id fringilla tellus. Cras quam leo, faucibus vitae facilisis et, posuere sed libero. Cras nunc nisi, lobortis non blandit at, tristique vel nibh. Duis vulputate, nibh at imperdiet porta, velit nunc porttitor odio, sit amet volutpat nisl neque et risus.

Etiam facilisis ipsum quis enim tempor, id tristique massa vestibulum. Cras tortor metus, venenatis non lectus vestibulum, efficitur egestas elit. Vivamus dignissim lacinia nulla, et euismod ante consectetur sit amet. Quisque ac sodales ex. Proin maximus blandit ex vel viverra. Nullam hendrerit, est ac euismod tempor, quam quam viverra sem, vel tincidunt leo arcu sit amet turpis. Fusce ultricies risus sit amet mi tempor dapibus.

Nam et pulvinar neque. Donec elementum, tellus quis auctor maximus, diam leo vulputate elit, sit amet egestas dolor ex a dolor. Mauris laoreet tortor a pulvinar laoreet. Pellentesque consectetur enim sit amet scelerisque pretium. Nam rutrum enim sed risus rutrum, vel suscipit lectus finibus. Aliquam sed consequat quam, at scelerisque arcu. Sed tristique lectus quis gravida feugiat. Curabitur viverra pretium dapibus. Sed ultricies nunc ac placerat semper. Nullam facilisis eros odio, ut pellentesque risus molestie vitae. Morbi dolor enim, convallis a fringilla in, pretium ut lectus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum sodales turpis vitae molestie congue. Ut fermentum tincidunt massa, eget dignissim justo efficitur eu. Maecenas et gravida purus. Nulla placerat at purus vitae cursus.

Intention

August 15th, 2019

What is the intention of this blog? To write. To become a better writer. To share. To learn. And to record. This is a personal blog.

If your eyes happen to come across this post – hello.

I expect this blog to be about a variety of topics. Like programming or mathematics – or just random thoughts; really this blog is a repository for information that I think is worth sharing and maybe even writing about – who knows, it’s an open book (blog?).

This blog will serve as my little slice of the internet.

And I welcome you.

Light-Toned Deposits in Noctis Labyrinthus of Mars. From psiupuxa.com