Skip to content

Commit

Permalink
Merge pull request #192 from jpmorganchase/partial-updates
Browse files Browse the repository at this point in the history
Allow table.update() to accept partial rows
  • Loading branch information
texodus authored Aug 15, 2018
2 parents e36f34d + f204bce commit d48d996
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
4 changes: 4 additions & 0 deletions packages/perspective/src/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ _fill_col(val dcol, t_col_sptr col, t_bool is_arrow)
} else {
for (auto i = 0; i < nrows; ++i)
{
if (dcol[i].equals(val::undefined())) continue;
auto elem = dcol[i].as<T>();
col->set_nth(i, elem);
}
Expand Down Expand Up @@ -298,6 +299,7 @@ _fill_col<t_time>(val dcol, t_col_sptr col, t_bool is_arrow)
} else {
for (auto i = 0; i < nrows; ++i)
{
if (dcol[i].equals(val::undefined())) continue;
auto elem = static_cast<t_int64>(dcol[i].as<t_float64>());
col->set_nth(i, elem);
}
Expand All @@ -322,6 +324,7 @@ _fill_col<t_bool>(val dcol, t_col_sptr col, t_bool is_arrow)
} else {
for (auto i = 0; i < nrows; ++i)
{
if (dcol[i].equals(val::undefined())) continue;
auto elem = dcol[i].as<t_bool>();
col->set_nth(i, elem);
}
Expand Down Expand Up @@ -378,6 +381,7 @@ _fill_col<std::string>(val dcol, t_col_sptr col, t_bool is_arrow)
} else {
for (auto i = 0; i < nrows; ++i)
{
if (dcol[i].equals(val::undefined())) continue;
std::wstring welem = dcol[i].as<std::wstring>();
std::wstring_convert<utf16convert_type, wchar_t> converter;
std::string elem = converter.to_bytes(welem);
Expand Down
2 changes: 1 addition & 1 deletion packages/perspective/src/js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ worker.prototype._handle = function (e) {
};

worker.prototype.table = function (data, options) {
return new table(this, data, options);
return new table(this, data, options || {});
};

worker.prototype.terminate = function () {
Expand Down
26 changes: 13 additions & 13 deletions packages/perspective/src/js/perspective.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ function infer_type(x) {
*/
function parse_data(data, names, types) {
let preloaded = types ? true : false;
names = names || [];
if (types === undefined) {
types = []
} else {
Expand All @@ -123,22 +122,19 @@ function parse_data(data, names, types) {
if (data.length === 0) {
throw "Not yet implemented: instantiate empty grid without column type";
}
let inferredType, i;
let col;
let MAX_CHECK = 50;
for (let ix = 0; ix < MAX_CHECK && ix < data.length; ix ++) {
if (names.length > 0) {
let max_check = 50;
if (names === undefined) {
names = Object.keys(data[0]);
for (let ix = 0; ix < Math.min(max_check, data.length); ix ++) {
let next = Object.keys(data[ix]);
if (names.length !== next.length) {
if (MAX_CHECK === 50) console.warn("Array data has inconsistent rows");
if (next.length > names.length) {
if (max_check === 50) console.warn("Array data has inconsistent rows");
console.warn("Extending from " + names.length + " to " + next.length);
names = next;
MAX_CHECK *= 2;
max_check *= 2;
}
}
} else {
names = Object.keys(data[ix]);
}
}
for (let n in names) {
Expand All @@ -160,10 +156,13 @@ function parse_data(data, names, types) {
console.warn(`Could not infer type for column ${name}`)
inferredType = __MODULE__.t_dtype.DTYPE_STR;
}
col = [];
let col = [];
const parser = new DateParser();
for (let x = 0; x < data.length; x ++) {
if (!(name in data[x]) || data[x][name] === undefined) continue;
if (!(name in data[x]) || data[x][name] === undefined) {
col.push(undefined);
continue;
};
if (inferredType.value === __MODULE__.t_dtype.DTYPE_FLOAT64.value) {
col.push(Number(data[x][name]));
} else if (inferredType.value === __MODULE__.t_dtype.DTYPE_INT32.value) {
Expand All @@ -181,7 +180,7 @@ function parse_data(data, names, types) {
col.push(false);
}
} else {
col.push(cell);
col.push(!!cell);
}
} else if (inferredType.value === __MODULE__.t_dtype.DTYPE_TIME.value) {
let val = data[x][name];
Expand Down Expand Up @@ -224,6 +223,7 @@ function parse_data(data, names, types) {
//if (this.initialized) {
// throw "Cannot update already initialized table with schema.";
// }
names = [];

// Empty type dict
for (let name in data) {
Expand Down
25 changes: 24 additions & 1 deletion packages/perspective/test/js/updates.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

import _ from "underscore";
import papaparse from "papaparse";

import arrow from "../arrow/test.arrow";

Expand Down Expand Up @@ -291,6 +290,30 @@ module.exports = (perspective) => {
table.update(data_2);
});

it("partial update", function (done) {
var partial = [
{'x': 5, 'y':'a'},
{'y':'b', 'z': true},
];
var expected = [
{'x': 5, 'y':'a', 'z': true},
{'x': 2, 'y':'b', 'z': true},
{'x': 3, 'y':'c', 'z': true},
{'x': 4, 'y':'d', 'z': false}
];
var table = perspective.table(meta, {index: 'y'});
var view = table.view();
table.update(data);
view.on_update(async function (new_data) {
expect(new_data).toEqual(expected.slice(0, 2));
let json = await view.to_json();
expect(json).toEqual(expected);
done();
});
table.update(partial);
});


});

describe("Viewport", function() {
Expand Down

0 comments on commit d48d996

Please sign in to comment.