Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove header rows for column-only views #540

Merged
merged 1 commit into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions cpp/perspective/src/cpp/data_slice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,33 @@ namespace perspective {

template <typename CTX_T>
t_data_slice<CTX_T>::t_data_slice(std::shared_ptr<CTX_T> ctx, t_uindex start_row,
t_uindex end_row, t_uindex start_col, t_uindex end_col,
const std::shared_ptr<std::vector<t_tscalar>>& slice, std::vector<std::string> column_names)
t_uindex end_row, t_uindex start_col, t_uindex end_col, t_uindex row_offset,
t_uindex col_offset, const std::shared_ptr<std::vector<t_tscalar>>& slice,
std::vector<std::string> column_names)
: m_ctx(ctx)
, m_start_row(start_row)
, m_end_row(end_row)
, m_start_col(start_col)
, m_end_col(end_col)
, m_row_offset(row_offset)
, m_col_offset(col_offset)
, m_slice(slice)
, m_column_names(column_names) {
m_stride = m_end_col - m_start_col;
}

template <typename CTX_T>
t_data_slice<CTX_T>::t_data_slice(std::shared_ptr<CTX_T> ctx, t_uindex start_row,
t_uindex end_row, t_uindex start_col, t_uindex end_col,
const std::shared_ptr<std::vector<t_tscalar>>& slice, std::vector<std::string> column_names,
std::vector<t_uindex> column_indices)
t_uindex end_row, t_uindex start_col, t_uindex end_col, t_uindex row_offset,
t_uindex col_offset, const std::shared_ptr<std::vector<t_tscalar>>& slice,
std::vector<std::string> column_names, std::vector<t_uindex> column_indices)
: m_ctx(ctx)
, m_start_row(start_row)
, m_end_row(end_row)
, m_start_col(start_col)
, m_end_col(end_col)
, m_row_offset(row_offset)
, m_col_offset(col_offset)
, m_slice(slice)
, m_column_names(column_names)
, m_column_indices(column_indices) {
Expand All @@ -49,6 +54,7 @@ t_data_slice<CTX_T>::~t_data_slice() {}
template <typename CTX_T>
t_tscalar
t_data_slice<CTX_T>::get(t_uindex ridx, t_uindex cidx) const {
ridx += m_row_offset;
t_uindex idx = get_slice_idx(ridx, cidx);
t_tscalar rv;
if (idx >= m_slice->size()) {
Expand Down
22 changes: 16 additions & 6 deletions cpp/perspective/src/cpp/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ View<CTX_T>::View(t_pool* pool, std::shared_ptr<CTX_T> ctx, std::shared_ptr<t_gn
, m_gnode(gnode)
, m_name(name)
, m_separator(separator)
, m_col_offset(0)
, m_config(config) {

// We should deprecate t_pivot and just use string column names throughout
Expand All @@ -34,6 +35,9 @@ View<CTX_T>::View(t_pool* pool, std::shared_ptr<CTX_T> ctx, std::shared_ptr<t_gn
m_aggregates = m_config.get_aggregates();
m_filters = m_config.get_fterms();
m_sorts = m_config.get_sortspecs();

// configure data window for column-only rows
is_column_only() ? m_row_offset = 1 : m_row_offset = 0;
}

template <typename CTX_T>
Expand Down Expand Up @@ -230,8 +234,8 @@ View<t_ctx0>::get_data(
auto slice_ptr = std::make_shared<std::vector<t_tscalar>>(
m_ctx->get_data(start_row, end_row, start_col, end_col));
auto col_names = _column_names();
auto data_slice_ptr = std::make_shared<t_data_slice<t_ctx0>>(
m_ctx, start_row, end_row, start_col, end_col, slice_ptr, col_names);
auto data_slice_ptr = std::make_shared<t_data_slice<t_ctx0>>(m_ctx, start_row, end_row,
start_col, end_col, m_row_offset, m_col_offset, slice_ptr, col_names);
return data_slice_ptr;
}

Expand All @@ -243,8 +247,8 @@ View<t_ctx1>::get_data(
m_ctx->get_data(start_row, end_row, start_col, end_col));
auto col_names = _column_names();
col_names.insert(col_names.begin(), "__ROW_PATH__");
auto data_slice_ptr = std::make_shared<t_data_slice<t_ctx1>>(
m_ctx, start_row, end_row, start_col, end_col, slice_ptr, col_names);
auto data_slice_ptr = std::make_shared<t_data_slice<t_ctx1>>(m_ctx, start_row, end_row,
start_col, end_col, m_row_offset, m_col_offset, slice_ptr, col_names);
return data_slice_ptr;
}

Expand All @@ -257,6 +261,11 @@ View<t_ctx2>::get_data(
std::vector<std::string> column_names;
bool is_sorted = m_sorts.size() > 0;

if (is_column_only()) {
start_row += m_row_offset;
end_row += m_row_offset;
}

if (is_sorted) {
/**
* Perspective generates headers for sorted columns, so we have to
Expand Down Expand Up @@ -297,8 +306,9 @@ View<t_ctx2>::get_data(

column_names.insert(column_names.begin(), "__ROW_PATH__");
auto slice_ptr = std::make_shared<std::vector<t_tscalar>>(slice);
auto data_slice_ptr = std::make_shared<t_data_slice<t_ctx2>>(
m_ctx, start_row, end_row, start_col, end_col, slice_ptr, column_names, column_indices);
auto data_slice_ptr
= std::make_shared<t_data_slice<t_ctx2>>(m_ctx, start_row, end_row, start_col, end_col,
m_row_offset, m_col_offset, slice_ptr, column_names, column_indices);
return data_slice_ptr;
}

Expand Down
6 changes: 4 additions & 2 deletions cpp/perspective/src/include/perspective/data_slice.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ template <typename CTX_T>
class PERSPECTIVE_EXPORT t_data_slice {
public:
t_data_slice(std::shared_ptr<CTX_T> ctx, t_uindex start_row, t_uindex end_row,
t_uindex start_col, t_uindex end_col,
t_uindex start_col, t_uindex end_col, t_uindex row_offset, t_uindex col_offset,
const std::shared_ptr<std::vector<t_tscalar>>& slice,
std::vector<std::string> column_names);

t_data_slice(std::shared_ptr<CTX_T> ctx, t_uindex start_row, t_uindex end_row,
t_uindex start_col, t_uindex end_col,
t_uindex start_col, t_uindex end_col, t_uindex row_offset, t_uindex col_offset,
const std::shared_ptr<std::vector<t_tscalar>>& slice,
std::vector<std::string> column_names, std::vector<t_uindex> column_indices);

Expand Down Expand Up @@ -97,6 +97,8 @@ class PERSPECTIVE_EXPORT t_data_slice {
t_uindex m_end_row;
t_uindex m_start_col;
t_uindex m_end_col;
t_uindex m_row_offset;
t_uindex m_col_offset;
t_uindex m_stride;
std::shared_ptr<std::vector<t_tscalar>> m_slice;
std::vector<std::string> m_column_names;
Expand Down
2 changes: 2 additions & 0 deletions cpp/perspective/src/include/perspective/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ class PERSPECTIVE_EXPORT View {
std::vector<t_fterm> m_filters;
std::vector<t_sortspec> m_sorts;
bool m_column_only;
t_uindex m_row_offset;
t_uindex m_col_offset;

t_config m_config;
};
Expand Down
8 changes: 4 additions & 4 deletions packages/perspective/src/js/perspective.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ export default function(Module) {

const viewport = this.config.viewport ? this.config.viewport : {};
const start_row = options.start_row || (viewport.top ? viewport.top : 0);
const end_row = (options.end_row || (viewport.height ? start_row + viewport.height : max_rows)) + (this.column_only ? 1 : 0);
const end_row = options.end_row || (viewport.height ? start_row + viewport.height : max_rows);
const start_col = options.start_col || (viewport.left ? viewport.left : 0);
const end_col = Math.min(max_cols, (options.end_col || (viewport.width ? start_col + viewport.width : max_cols)) * (hidden + 1));

Expand Down Expand Up @@ -363,9 +363,9 @@ export default function(Module) {
formatter.addRow(data, row);
}

if (this.column_only) {
/* if (this.column_only) {
data = formatter.slice(data, 1);
}
} */

return formatter.formatData(data, options.config);
};
Expand Down Expand Up @@ -950,7 +950,7 @@ export default function(Module) {
throw new Error(`Duplicate configuration parameter "${key}"`);
}
} else if (key === "aggregate") {
console.warn(`Deprecated: "aggregate" config parameter has been replaced by "aggregates" amd "columns"`);
console.warn(`Deprecated: "aggregate" config parameter has been replaced by "aggregates" and "columns"`);
config[key] = _config[key];
} else if (defaults.CONFIG_VALID_KEYS.indexOf(key) > -1) {
config[key] = _config[key];
Expand Down
42 changes: 40 additions & 2 deletions packages/perspective/test/js/to_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,25 @@ var int_float_string_data = [

module.exports = perspective => {
describe("data slice", function() {
it("data slice should respect start/end rows", async function() {
it("should filter out invalid start rows", async function() {
let table = perspective.table(int_float_string_data);
let view = table.view();
let json = await view.to_json({
start_row: 5
});
expect(json).toEqual([]);
});

it("should filter out invalid start columns", async function() {
let table = perspective.table(int_float_string_data);
let view = table.view();
let json = await view.to_json({
start_col: 5
});
expect(json).toEqual([{}, {}, {}, {}]);
});

it("should respect start/end rows", async function() {
let table = perspective.table(int_float_string_data);
let view = table.view();
let json = await view.to_json({
Expand All @@ -28,7 +46,7 @@ module.exports = perspective => {
expect(json[0]).toEqual(comparator);
});

it("data slice should respect start/end columns", async function() {
it("should respect start/end columns", async function() {
let table = perspective.table(int_float_string_data);
let view = table.view();
let json = await view.to_columns({
Expand Down Expand Up @@ -72,6 +90,26 @@ module.exports = perspective => {
}
});

it("column-only views should not have header rows", async function() {
let table = perspective.table([{x: 1, y: "a"}, {x: 2, y: "b"}]);
let view = table.view({
column_pivots: ["x"]
});
let json = await view.to_json();
expect(json).toEqual([{"1|x": 1, "1|y": "a", "2|x": null, "2|y": null}, {"1|x": null, "1|y": null, "2|x": 2, "2|y": "b"}]);
});

it("column-only views should return correct windows of data", async function() {
let table = perspective.table([{x: 1, y: "a"}, {x: 2, y: "b"}]);
let view = table.view({
column_pivots: ["x"]
});
let json = await view.to_json({
start_row: 1
});
expect(json).toEqual([{"1|x": null, "1|y": null, "2|x": 2, "2|y": "b"}]);
});

it("two-sided views should have row paths", async function() {
let table = perspective.table(int_float_string_data);
let view = table.view({
Expand Down