Skip to content

Commit eee476f

Browse files
committed
finished with documentation first draft
1 parent b873e78 commit eee476f

15 files changed

+627
-238
lines changed

CMakeLists.txt

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,33 @@ cmake_minimum_required (VERSION 2.6)
22

33
set (PROJECT_NAME cpp-matplotlib)
44

5-
SET (CMAKE_C_COMPILER "/usr/bin/clang-3.5")
6-
SET (CMAKE_CXX_COMPILER "/usr/bin/clang++-3.5")
5+
# If you want to use clang, uncomment these.
6+
#SET (CMAKE_C_COMPILER "/usr/bin/clang-3.5")
7+
#SET (CMAKE_CXX_COMPILER "/usr/bin/clang++-3.5")
78

89
project (${PROJECT_NAME})
10+
set (EXAMPLE_BIN ${PROJECT_NAME}-example)
911

1012
include_directories ("${PROJECT_SOURCE_DIR}/src")
1113

12-
add_executable (${PROJECT_NAME} src/main.cc)
14+
add_executable (${EXAMPLE_BIN} src/main.cc)
1315

1416
## Support for Clang's CompilationDatabase system
1517
set (CMAKE_EXPORT_COMPILE_COMMANDS 1)
1618

17-
## Set optional features. This will show up as a preprocessor variable
18-
## USE_MY_LIBRARY in source.
19-
#option (USE_MY_LIBRARY
20-
# "Use the provided library" ON)
21-
2219
## Compile and create a library. STATIC is default unless BUILD_SHARED_LIBS
2320
## is on.
24-
add_library (cpp_plot src/cpp_plot.cc src/ReqRepConnection.cc)
25-
add_library (ipython_protocol src/ipython_protocol.cc)
26-
set (EXTRA_LIBS ${EXTRA_LIBS} cpp_plot ipython_protocol)
27-
28-
#if (USE_MY_LIBRARY)
29-
30-
## Search for include files here as well
31-
#include_directories ("${PROJECT_SOURCE_DIR}/some_sub_path")
32-
33-
## Run Cmake also in this dir
34-
#add_subdirectory (some_sub_path)
35-
36-
#set (EXTRA_LIBS ${EXTRA_LIBS} LibraryName)
21+
add_library (cpp_plot
22+
src/cpp_plot.cc
23+
src/RequestSink.cc
24+
src/ipython_protocol.cc)
3725

38-
#endif (USE_MYMATH)
26+
set (EXTRA_LIBS ${EXTRA_LIBS} cpp_plot)
3927

4028
## Libraries to link with
41-
target_link_libraries (${PROJECT_NAME}
42-
zmq jsoncpp uuid ssl crypto
43-
${EXTRA_LIBS})
29+
target_link_libraries (${EXAMPLE_BIN}
30+
${EXTRA_LIBS}
31+
zmq jsoncpp uuid ssl crypto)
4432

4533
SET (CMAKE_C_FLAGS "-Wall -std=c11 -Wextra -Werror")
4634
SET (CMAKE_C_FLAGS_DEBUG "${CMAKE_CFLAGS} -g")

README.md

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,65 @@
1+
# Contents
2+
3+
[About](#about)
4+
[Usage] (#usage)
5+
[Prereqs](#prereqs)
6+
[Building](#building)
7+
[Example](#example)
8+
9+
110
# About
211

312
An easy-to-use library for simple plotting from C++ via a ZeroMQ bridge to
4-
Python's Matplotlib.
13+
an [IPython](http://ipython.org/) kernel.
514

6-
# Prereqs
15+
It provides the ability to send [NumPy](http://www.numpy.org/) array
16+
compatible data to an IPython kernel session as well as execute arbitrary
17+
code.
18+
19+
Execution of arbitrary code is "protected" by IPython's kernel HMAC signing
20+
mechanism: only code signed by a secret key provided by IPython will run. As
21+
of 2015-03-19 it has been tested with IPython version 1.2.1 on Ubuntu 14.04.
22+
23+
24+
# Usage
25+
26+
Here we create some 1D data and plot it. The variable "A" will be available
27+
for working with in the IPython session, even after the C++ program finishes.
28+
29+
CppMatplotlib mpl{"/path/to/kernel-NNN.json"};
30+
mpl.Connect();
31+
32+
// Create a nice curve
33+
std::vector<NumpyArray::dtype> raw_data;
34+
double x = 0.0;
35+
while (x < 3.14159 * 4) {
36+
raw_data.push_back(std::sin(x));
37+
x += 0.05;
38+
}
39+
40+
// Send it to IPython for plotting
41+
NumpyArray data("A", raw_data);
42+
mpl.SendData(data);
43+
mpl.RunCode("plot(A)\n"
44+
"title('f(x) = sin(x)')\n"
45+
"xlabel('x')\n"
46+
"ylabel('f(x)')\n");
747

8-
## Ubuntu
48+
And the result will be ![Screenshot](screenshot.png?raw=true "Screenshot of
49+
sin(x)")
950

10-
sudo apt-get install libzmq3-dev python-zmq python-matplotlib
51+
See [src/main.cc](src/main.cc) for a complete program.
1152

12-
Some sort of pyqt
1353

14-
sudo apt-get install python-pyside.qtcore python-pyside.qtgui
54+
# Prereqs
55+
56+
## Ubuntu 14.04
57+
58+
sudo apt-get install ipython python-matplotlib libzmq3-dev \
59+
libjsoncpp-dev uuid-dev libssl-dev
1560

16-
# Build
61+
62+
# Building
1763

1864
git clone https://bitbucket.org/james_youngquist/cpp-matplotlib.git
1965
cd cpp-matplotlib
@@ -22,13 +68,29 @@ Some sort of pyqt
2268
cmake ..
2369
make
2470

25-
# Run (alpha demo)
71+
## Generating the documentation
72+
73+
cd cpp-matplotlib
74+
doxygen Doxyfile
75+
# open html/index.html
76+
77+
# Example
2678

2779
In terminal 1:
2880

29-
python cpp-matplotlib/pyplot_listener.py
81+
ipython kernel --pylab
82+
# this will print out the kernel PID to connect to, NNN below.
3083

3184
In terminal 2:
3285

33-
cpp-matplotlib/build/cpp-matplotlib
86+
# Once per kernel invocation:
87+
export KERNEL_CONFIG=`find ~/ -name kernel-NNN.json`
88+
89+
# Each time you run the program
90+
build/cpp-matplotlib-example $KERNEL_CONFIG
91+
92+
93+
In terminal 3 (if desired):
94+
95+
ipython console --existing kernel-NNN.json
3496

screenshot.png

53 KB
Loading

src/ReqRepConnection.cc

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/ReqRepConnection.hpp

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/RequestSink.cc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// Copyright (c) 2015 Jim Youngquist
3+
// under The MIT License (MIT)
4+
// full text in LICENSE file in root folder of this project.
5+
//
6+
7+
#include <exception>
8+
9+
#include "RequestSink.hpp"
10+
11+
RequestSink::RequestSink(const std::string &url) :
12+
context_{1},
13+
socket_{context_, ZMQ_REQ},
14+
url_{url},
15+
connected_{false}
16+
{}
17+
18+
bool RequestSink::Send(const std::string &buffer) {
19+
std::vector<uint8_t> byte_buffer(buffer.begin(), buffer.end());
20+
return Send(byte_buffer);
21+
}
22+
23+
bool RequestSink::Send(const std::vector<uint8_t> &buffer) {
24+
zmq::message_t request((void*)&buffer[0], buffer.size(), nullptr);
25+
socket_.send(request);
26+
27+
// Get the reply
28+
zmq::message_t reply;
29+
socket_.recv(&reply);
30+
std::string value{reinterpret_cast<char*>(reply.data()), reply.size()};
31+
if (value != "Success") {
32+
std::runtime_error(value.c_str());
33+
}
34+
35+
return true;
36+
}
37+
38+
bool RequestSink::Connect(void) {
39+
socket_.connect(url_.c_str());
40+
connected_ = true;
41+
42+
return true;
43+
}
44+

src/RequestSink.hpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//
2+
// Copyright (c) 2015 Jim Youngquist
3+
// under The MIT License (MIT)
4+
// full text in LICENSE file in root folder of this project.
5+
//
6+
7+
#pragma once
8+
9+
#include <string>
10+
#include <vector>
11+
12+
#include <zmq.hpp>
13+
14+
//======================================================================
15+
/** \brief This class wraps a ZeroMQ request-response socket connection.
16+
*
17+
* It acts as a one-way sink for data. Responses are not actually returned,
18+
* only checked that the "request" was successfully transmitted.
19+
*
20+
* Usage:
21+
\code
22+
RequestSink conn{"tcp://hostname:port"};
23+
conn.Connect();
24+
conn.Send("oh my goodness!");
25+
\endcode
26+
*/
27+
class RequestSink {
28+
public:
29+
//--------------------------------------------------
30+
/** \brief Initializes for a connection to a valid ZeroMQ endpoint, but does
31+
* not actually connect to it.
32+
*
33+
* \param url the ZeroMQ url to connect to.
34+
*/
35+
RequestSink(const std::string &url);
36+
37+
//--------------------------------------------------
38+
/** \brief Transmits a string.
39+
*
40+
* \param buffer the string to send.
41+
*
42+
* \returns whether transmission was successful.
43+
*/
44+
bool Send(const std::string &buffer);
45+
46+
//--------------------------------------------------
47+
/** \brief Transmits a byte vector
48+
*
49+
* \param buffer the bytes to send.
50+
*
51+
* \returns whether transmission was successful.
52+
*/
53+
bool Send(const std::vector<uint8_t> &buffer);
54+
55+
//--------------------------------------------------
56+
/** \brief Actually connects to a Request socket.
57+
*/
58+
bool Connect(void);
59+
60+
private:
61+
zmq::context_t context_;
62+
zmq::socket_t socket_;
63+
const std::string url_;
64+
bool connected_;
65+
};

0 commit comments

Comments
 (0)