Skip to content

Commit

Permalink
added cppmpl namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
daemacles committed Mar 20, 2015
1 parent f723ef9 commit 2c0e48d
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 34 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ set (CMAKE_EXPORT_COMPILE_COMMANDS 1)

## Compile and create a library. STATIC is default unless BUILD_SHARED_LIBS
## is on.
add_library (cpp_plot
src/cpp_plot.cc
add_library (cpp_mpl
src/cpp_mpl.cc
src/RequestSink.cc
src/ipython_protocol.cc)

set (EXTRA_LIBS ${EXTRA_LIBS} cpp_plot)
set (EXTRA_LIBS ${EXTRA_LIBS} cpp_mpl)

## Libraries to link with
target_link_libraries (${EXAMPLE_BIN}
Expand Down
69 changes: 45 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* [Usage](#usage)
* [Prereqs](#prereqs)
* [Building](#building)
* [Example](#example)
* [Running the Example](#running-the-example)


# About

An easy-to-use library for simple plotting from C++ via a ZeroMQ bridge to
an [IPython](http://ipython.org/) kernel.
An easy-to-use **C++11** library for simple plotting from C++ via a ZeroMQ
bridge to an [IPython](http://ipython.org/) kernel.

It provides the ability to send [NumPy](http://www.numpy.org/) array
compatible data to an IPython kernel session as well as execute arbitrary
Expand Down Expand Up @@ -43,29 +43,39 @@ Here we create some 1D data and plot it. The numpy.array "MyData" will be
available for working with in the IPython session, even after the C++ program
finishes.

All library code lives in the <tt>cppmpl</tt> namespace.

```c++
CppMatplotlib mpl{"/path/to/kernel-NNN.json"};
mpl.Connect();

// Create a nice curve
std::vector<NumpyArray::dtype> raw_data;
double x = 0.0;
while (x < 3.14159 * 4) {
raw_data.push_back(std::sin(x));
x += 0.05;
#include "cpp_mpl.hpp"

int main() {
// ...

cppmpl::CppMatplotlib mpl{"/path/to/kernel-NNN.json"};
mpl.Connect();

// Create a nice curve
std::vector<cppmpl::NumpyArray::dtype> raw_data;
double x = 0.0;
while (x < 3.14159 * 4) {
raw_data.push_back(std::sin(x));
x += 0.05;
}

// Send it to IPython for plotting
cppmpl::NumpyArray data("MyData", raw_data);
mpl.SendData(data);
mpl.RunCode("plot(MyData)\n"
"title('f(x) = sin(x)')\n"
"xlabel('x')\n"
"ylabel('f(x)')\n");

// NOTE: if you want to store the python in an external file, use the
// convenience function LoadFile("my_code.py"), as in,
// mpl.RunCode(cppmpl::LoadFile("plotting_code.py"));

// ...
}

// Send it to IPython for plotting
NumpyArray data("MyData", raw_data);
mpl.SendData(data);
mpl.RunCode("plot(MyData)\n"
"title('f(x) = sin(x)')\n"
"xlabel('x')\n"
"ylabel('f(x)')\n");

// NOTE: if you want to store the python in an external file, use the
// convenience function LoadFile("my_code.py"), as in,
// mpl.RunCode(LoadFile("plotting_code.py"));
```

And the result is ![Screenshot](screenshot.png?raw=true)
Expand All @@ -92,6 +102,17 @@ In [84]: print MyData[9]
[ 1.73986214]
```

## Compiling / Linking

When linking against <tt>cpp_plot.{a,so}</tt>, you also need to link against
libzmq, libjsoncpp, libuuid, and libcripto:

```
$ g++ my_prog.cc -std=c++11 /path/to/libcpp_plot.a -ljsoncpp -lzmq -luuid -lcrypto
```

Or just modify CMakeFiles.txt...


# Prereqs

Expand Down
3 changes: 3 additions & 0 deletions src/RequestSink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include "RequestSink.hpp"

namespace cppmpl {

RequestSink::RequestSink(const std::string &url) :
context_{1},
socket_{context_, ZMQ_REQ},
Expand Down Expand Up @@ -42,3 +44,4 @@ bool RequestSink::Connect(void) {
return true;
}

}
4 changes: 4 additions & 0 deletions src/RequestSink.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include <zmq.hpp>

namespace cppmpl {

//======================================================================
/** \brief This class wraps a ZeroMQ request-response socket connection.
*
Expand Down Expand Up @@ -63,3 +65,5 @@ class RequestSink {
const std::string url_;
bool connected_;
};

}
6 changes: 5 additions & 1 deletion src/cpp_plot.cc → src/cpp_mpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@

#include <zmq.hpp>

#include "cpp_plot.hpp"
#include "cpp_mpl.hpp"

#include "ipython_protocol.hpp"
#include "RequestSink.hpp"

namespace cppmpl {

// Names of the python variables
static const std::string THREAD_VAR_NAME{"cpp_ipython_listener_thread"};
static const std::string PORT_VAR_NAME{"cpp_ipython_listener_thread_port"};
Expand Down Expand Up @@ -118,3 +120,5 @@ bool CppMatplotlib::SendData(const NumpyArray &data) {
void CppMatplotlib::RunCode(const std::string &code) {
upSession_->Shell().RunCode(code);
}

} // namespace
5 changes: 4 additions & 1 deletion src/cpp_plot.hpp → src/cpp_mpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
#include <string>
#include <vector>

namespace cppmpl {

// Forward declarations
struct IPyKernelConfig;
class IPythonSession;
class RequestSink;


// Reads an entire file into a string
//--------------------------------------------------
/** \brief Reads an entire file into a string. Make sure you have enough
Expand Down Expand Up @@ -238,3 +239,5 @@ class CppMatplotlib {
std::unique_ptr<RequestSink> upData_conn_;
std::unique_ptr<IPythonSession> upSession_;
};

} // namespace
4 changes: 3 additions & 1 deletion src/ipython_protocol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include "ipython_protocol.hpp"

namespace cppmpl {

/// Delimeter used by the iPython messaging protocol to separate ZMQ
/// identities from message data.
static const std::string DELIM{"<IDS|MSG>"};
Expand Down Expand Up @@ -288,4 +290,4 @@ void IPythonSession::Connect (void) {
shell_connection_.Connect();
}


} // namespace
4 changes: 4 additions & 0 deletions src/ipython_protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ extern "C" {
#include <openssl/hmac.h>
#include <zmq.hpp>

namespace cppmpl {

// forward declarations
struct IPyKernelConfig;
struct IPythonMessage;
Expand Down Expand Up @@ -358,3 +360,5 @@ class IPythonSession {
zmq::context_t zmq_context_;
ShellConnection shell_connection_;
};

} // namespace
8 changes: 4 additions & 4 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <string>
#include <vector>

#include "cpp_plot.hpp"
#include "cpp_mpl.hpp"

int main(int argc, char **argv) {
if (argc < 2) {
Expand All @@ -20,18 +20,18 @@ int main(int argc, char **argv) {
exit(-1);
}

CppMatplotlib mpl{argv[1]};
cppmpl::CppMatplotlib mpl{argv[1]};
mpl.Connect();

std::vector<NumpyArray::dtype> raw_data;
std::vector<cppmpl::NumpyArray::dtype> raw_data;

double x = 0.0;
while (x < 3.14159 * 4) {
raw_data.push_back(std::sin(x));
x += 0.05;
}

NumpyArray data("A", raw_data);
cppmpl::NumpyArray data("A", raw_data);
mpl.SendData(data);
mpl.RunCode("plot(A)\n"
"title('f(x) = sin(x)')\n"
Expand Down

0 comments on commit 2c0e48d

Please sign in to comment.