Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
Move vega spec definitions into their own files
Browse files Browse the repository at this point in the history
* Move viz compilation into viz dir
* Replace single-line strings with proper json files
* Use `xxd -i` to generate C++ headers for the JSON files
* Clean up the code in vega_spec.cpp
  • Loading branch information
Zach Nation authored and znation committed Jan 16, 2018
1 parent a577970 commit 2cb02ce
Show file tree
Hide file tree
Showing 20 changed files with 4,042 additions and 129 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Expand Up @@ -793,6 +793,8 @@ include_directories(SYSTEM src/external)
include_directories(SYSTEM src/old)
include_directories(SYSTEM src/platform)

# for build-time generated source code
include_directories(SYSTEM ${CMAKE_BINARY_DIR}/src)

if(EXISTS ${CMAKE_SOURCE_DIR}/subtree)
include_directories(SYSTEM subtree)
Expand Down
24 changes: 2 additions & 22 deletions src/unity/lib/CMakeLists.txt
@@ -1,23 +1,6 @@
project(unity)


set(VISUALIZATION_SOURCES
visualization/boxes_and_whiskers.cpp
visualization/categorical_heatmap.cpp
visualization/io_buffer.cpp
visualization/process_wrapper.cpp
visualization/groupby.cpp
visualization/heatmap.cpp
visualization/histogram.cpp
visualization/hyperloglog.cpp
visualization/item_frequency.cpp
visualization/scatter.cpp
visualization/show.cpp
visualization/thread.cpp
visualization/transformation.cpp
visualization/vega_data.cpp
visualization/vega_spec.cpp
)
subdirs(visualization)

make_library(unity_core
SOURCES
Expand Down Expand Up @@ -60,13 +43,10 @@ make_library(unity_core
startup_teardown
pylambda_worker
numerics
visualization
EXTERNAL_VISIBILITY
)

add_dependencies(unity_core
visualization_client
)

make_shared_library_from_static(unity_core_shared
REQUIRES
unity_core)
Expand Down
8 changes: 2 additions & 6 deletions src/unity/lib/unity_sarray.cpp
Expand Up @@ -2826,7 +2826,6 @@ void unity_sarray::show(const std::string& path_to_client,
case flex_type_enum::FLOAT:
::turi::visualization::run_thread([path_to_client, _title, _xlabel, _ylabel, self]() {
process_wrapper ew(path_to_client);
vega_spec vs;

histogram hist;
std::string title = _title;
Expand All @@ -2847,8 +2846,7 @@ void unity_sarray::show(const std::string& path_to_client,
ylabel = "Count";
}

vs << histogram_spec(title, xlabel, ylabel);
ew << vs.get_spec();
ew << histogram_spec(title, xlabel, ylabel);

hist.init(self);
while (ew.good()) {
Expand All @@ -2873,7 +2871,6 @@ void unity_sarray::show(const std::string& path_to_client,
case flex_type_enum::STRING:
::turi::visualization::run_thread([path_to_client, _title, _xlabel, _ylabel, self]() {
process_wrapper ew(path_to_client);
vega_spec vs;

item_frequency item_freq;
item_freq.init(self);
Expand All @@ -2898,8 +2895,7 @@ void unity_sarray::show(const std::string& path_to_client,
ylabel = "Values";
}

vs << categorical_spec(length_list, title, xlabel, ylabel);
ew << vs.get_spec();
ew << categorical_spec(length_list, title, xlabel, ylabel);

while (ew.good()) {
vega_data vd;
Expand Down
5 changes: 1 addition & 4 deletions src/unity/lib/unity_sframe.cpp
Expand Up @@ -1642,11 +1642,8 @@ void unity_sframe::show(const std::string& path_to_client) {

::turi::visualization::run_thread([path_to_client, column_transformers, column_names, self]() {

vega_spec vs;
visualization::process_wrapper ew(path_to_client);

vs << summary_view_spec(column_transformers.size());
ew << vs.get_spec();
ew << summary_view_spec(column_transformers.size());

const static size_t expected_batch_size = 5000000;
double num_rows_processed = 0;
Expand Down
70 changes: 70 additions & 0 deletions src/unity/lib/visualization/CMakeLists.txt
@@ -0,0 +1,70 @@
project(unity)

make_library(
visualization
SOURCES
boxes_and_whiskers.cpp
categorical_heatmap.cpp
groupby.cpp
heatmap.cpp
histogram.cpp
hyperloglog.cpp
io_buffer.cpp
item_frequency.cpp
process_wrapper.cpp
scatter.cpp
show.cpp
thread.cpp
transformation.cpp
vega_data.cpp
vega_spec.cpp
REQUIRES
EXTERNAL_VISIBILITY
)

set(GENERATED_VEGA_JSON_HEADERS
vega_spec/boxes_and_whiskers.json
vega_spec/categorical.json
vega_spec/categorical_heatmap.json
vega_spec/heatmap.json
vega_spec/histogram.json
vega_spec/scatter.json
vega_spec/summary_view.json
)

file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/vega_spec)

foreach(SOURCE_FILE ${GENERATED_VEGA_JSON_HEADERS})

string(
REGEX REPLACE
".json$"
".h"
GENERATED_FILE
${CMAKE_CURRENT_BINARY_DIR}/${SOURCE_FILE}
)

add_custom_command(
OUTPUT ${GENERATED_FILE}
COMMAND xxd -i ${SOURCE_FILE} > ${GENERATED_FILE}
MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE_FILE}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Generating ${GENERATED_FILE}"
VERBATIM
)

string(
MAKE_C_IDENTIFIER
${SOURCE_FILE}
TARGET_NAME
)

add_custom_target(
Generate_${TARGET_NAME}
DEPENDS
${GENERATED_FILE}
)

add_dependencies(visualization Generate_${TARGET_NAME})

endforeach(SOURCE_FILE)
4 changes: 1 addition & 3 deletions src/unity/lib/visualization/boxes_and_whiskers.cpp
Expand Up @@ -86,9 +86,7 @@ void ::turi::visualization::show_boxes_and_whiskers(const std::string& path_to_c


process_wrapper ew(path_to_client);
vega_spec vs;
vs << boxes_and_whiskers_spec(xlabel, ylabel, title);
ew << vs.get_spec();
ew << boxes_and_whiskers_spec(xlabel, ylabel, title);

boxes_and_whiskers bw;

Expand Down
4 changes: 1 addition & 3 deletions src/unity/lib/visualization/categorical_heatmap.cpp
Expand Up @@ -76,9 +76,7 @@ void ::turi::visualization::show_categorical_heatmap(const std::string& path_to_
DASSERT_EQ(x.size(), y.size());

process_wrapper ew(path_to_client);
vega_spec vs;
vs << categorical_heatmap_spec(xlabel, ylabel, title);
ew << vs.get_spec();
ew << categorical_heatmap_spec(xlabel, ylabel, title);

categorical_heatmap hm;

Expand Down
4 changes: 1 addition & 3 deletions src/unity/lib/visualization/heatmap.cpp
Expand Up @@ -44,9 +44,7 @@ void turi::visualization::show_heatmap(const std::string& path_to_client,
::turi::visualization::run_thread([path_to_client, x, y, xlabel, ylabel, title]() {

process_wrapper ew(path_to_client);
vega_spec vs;
vs << heatmap_spec(xlabel, ylabel, title);
ew << vs.get_spec();
ew << heatmap_spec(xlabel, ylabel, title);

heatmap hm;
gl_sframe temp_sf;
Expand Down
1 change: 0 additions & 1 deletion src/unity/lib/visualization/histogram.cpp
Expand Up @@ -4,7 +4,6 @@
* be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
*/
#include "histogram.hpp"
#include "vega_spec.hpp"

#include <parallel/lambda_omp.hpp>

Expand Down
4 changes: 1 addition & 3 deletions src/unity/lib/visualization/scatter.cpp
Expand Up @@ -39,9 +39,7 @@ void turi::visualization::show_scatter(const std::string& path_to_client,
DASSERT_EQ(x.size(), y.size());

process_wrapper ew(path_to_client);
vega_spec vs;
vs << scatter_spec(xlabel, ylabel, title);
ew << vs.get_spec();
ew << scatter_spec(xlabel, ylabel, title);

vega_data vd;

Expand Down
192 changes: 122 additions & 70 deletions src/unity/lib/visualization/vega_spec.cpp

Large diffs are not rendered by default.

18 changes: 4 additions & 14 deletions src/unity/lib/visualization/vega_spec.hpp
Expand Up @@ -16,24 +16,14 @@ namespace turi {
std::string histogram_spec(std::string title, std::string xlabel, std::string ylabel, double sizeMultiplier = 1.0);
std::string categorical_spec(size_t length_list, std::string title, std::string xlabel, std::string ylabel, double sizeMultiplier = 1.0);
std::string summary_view_spec(size_t length_elements, double sizeMultiplier = 1.0);
std::string scatter_spec(const std::string& x_name = "", const std::string& y_name = "", const std::string& title_name = "");
std::string heatmap_spec(const std::string& x_name = "", const std::string& y_name = "", const std::string& title_name = "");
std::string categorical_heatmap_spec(const std::string& x_name = "", const std::string& y_name = "", const std::string& title_name = "");
std::string boxes_and_whiskers_spec(const std::string& x_name = "", const std::string& y_name = "", const std::string& title_name = "");
std::string scatter_spec(std::string xlabel = "", std::string ylabel = "", std::string title = "");
std::string heatmap_spec(std::string xlabel = "", std::string ylabel = "", std::string title = "");
std::string categorical_heatmap_spec(std::string xlabel = "", std::string ylabel = "", std::string title = "");
std::string boxes_and_whiskers_spec(std::string xlabel = "", std::string ylabel = "", std::string title = "");

// Utility for escaping JSON string literals. Not concerned with Vega implications of the contents of those strings.
std::string escape_string(const std::string& str);

class EXPORT vega_spec {
protected:
std::stringstream m_spec;

public:
vega_spec();
virtual ~vega_spec();
virtual vega_spec& operator<<(const std::string&);
virtual std::string get_spec();
};
}
}

Expand Down

0 comments on commit 2cb02ce

Please sign in to comment.