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

added method selectMaxProbe for CollapseDatasetTool #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
54 changes: 54 additions & 0 deletions jasmine/spec/collapse_tool_test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,58 @@
describe('collapse_tool_test', function () {
it('maxmeanprobe',
function () {
var heatmap = new morpheus.HeatMap({
dataset: new morpheus.Dataset({
array: [[1, 3], [4, 6], [7, 9], [10, 12]],
rows: 4,
columns: 2
})
});
heatmap.getProject().getFullDataset().getRowMetadata()
.add('id').array = ['a', 'b', 'a', 'a'];
var newHeatMap = new morpheus.CollapseDatasetTool().execute({
heatMap: heatmap,
project: heatmap.getProject(),
input: {
collapse_method: 'Maximum Mean Probe',
collapse: 'Rows',
collapse_to_fields: ['id']
}
});
expect(newHeatMap.getProject().getFullDataset())
.toBeDatasetValues(new morpheus.Dataset({
array: [[10, 12], [4, 6]],
rows: 2,
columns: 2
}), 0.00001);
});
it('maxmedianprobe',
function () {
var heatmap = new morpheus.HeatMap({
dataset: new morpheus.Dataset({
array: [[1, 3], [4, 6], [7, 9], [10, 12]],
rows: 4,
columns: 2
})
});
heatmap.getProject().getFullDataset().getRowMetadata()
.add('id').array = ['a', 'b', 'a', 'a'];
var newHeatMap = new morpheus.CollapseDatasetTool().execute({
heatMap: heatmap,
project: heatmap.getProject(),
input: {
collapse_method: 'Maximum Median Probe',
collapse: 'Rows',
collapse_to_fields: ['id']
}
});
expect(newHeatMap.getProject().getFullDataset())
.toBeDatasetValues(new morpheus.Dataset({
array: [[10, 12], [4, 6]],
rows: 2,
columns: 2
}), 0.00001);
});
it('mean',
function () {
var heatmap = new morpheus.HeatMap({
Expand Down
2 changes: 1 addition & 1 deletion js/morpheus-latest.min.js

Large diffs are not rendered by default.

130 changes: 125 additions & 5 deletions js/morpheus.js
Original file line number Diff line number Diff line change
Expand Up @@ -8591,6 +8591,43 @@ morpheus.MolarConcentration.CONCENTRATIONS = [



morpheus.MaximumMeanProbe = function(probes) {
return morpheus.MaximumUnivariateFunction(probes, morpheus.Mean);
};

morpheus.MaximumMeanProbe.toString = function() {
return "Maximum Mean Probe";
};

morpheus.MaximumMeanProbe.selectOne = true;

morpheus.MaximumMedianProbe = function(probes) {
return morpheus.MaximumUnivariateFunction(probes, morpheus.Median);
};

morpheus.MaximumMedianProbe.toString = function() {
return "Maximum Median Probe";
};

morpheus.MaximumMedianProbe.selectOne = true;

morpheus.MaximumUnivariateFunction = function(rowView, fun) {
var curMax = Number.NEGATIVE_INFINITY;
var curIndex = -1;
for (var i = 0; i < rowView.dataset.getRowCount(); i++) {
rowView.setIndex(i);
var mean = fun(rowView);
if (mean > curMax) {
curMax = mean;
curIndex = i;
}
}
return {
value : curMax,
index : curIndex
}
};

morpheus.Positions = function () {
this.spaces = undefined;
this.defaultPositionFunction = function (index) {
Expand Down Expand Up @@ -13317,7 +13354,8 @@ morpheus.ChartTool.getTooltip = function (options) {
morpheus.CollapseDatasetTool = function () {
};
morpheus.CollapseDatasetTool.Functions = [morpheus.Mean, morpheus.Median,
new morpheus.MaxPercentiles([25, 75]), morpheus.Min, morpheus.Max, morpheus.Percentile, morpheus.Sum];
new morpheus.MaxPercentiles([25, 75]), morpheus.Min, morpheus.Max, morpheus.Percentile, morpheus.Sum,
morpheus.MaximumMeanProbe, morpheus.MaximumMedianProbe];
morpheus.CollapseDatasetTool.Functions.fromString = function (s) {
for (var i = 0; i < morpheus.CollapseDatasetTool.Functions.length; i++) {
if (morpheus.CollapseDatasetTool.Functions[i].toString() === s) {
Expand All @@ -13344,6 +13382,7 @@ morpheus.CollapseDatasetTool.prototype = {
form.setVisible('percentile', false);
form.$form.find('[name=collapse_method]').on('change', function (e) {
form.setVisible('percentile', $(this).val() === morpheus.Percentile.toString());
form.setVisible('collapse', !morpheus.CollapseDatasetTool.Functions.fromString($(this).val()).selectOne);
});

setValue('Rows');
Expand Down Expand Up @@ -13392,7 +13431,8 @@ morpheus.CollapseDatasetTool.prototype = {
}
var allFields = morpheus.MetadataUtil.getMetadataNames(dataset
.getRowMetadata());
dataset = morpheus.CollapseDataset(dataset, collapseToFields, f, true);
var collapseMethod = f.selectOne ? morpheus.SelectRow : morpheus.CollapseDataset;
dataset = collapseMethod(dataset, collapseToFields, f, true);
if (!rows) {
dataset = new morpheus.TransposedDatasetView(dataset);
}
Expand All @@ -13404,9 +13444,9 @@ morpheus.CollapseDatasetTool.prototype = {
set.remove(field);
});
// hide fields that were not part of collapse to
set.forEach(function (val, name) {
heatMap.setTrackVisible(name, false, !rows);
});
// set.forEach(function (val, name) {
// heatMap.setTrackVisible(name, false, !rows);
// });
return new morpheus.HeatMap({
name: heatMap.getName(),
dataset: dataset,
Expand Down Expand Up @@ -38056,6 +38096,86 @@ morpheus.CollapseDataset = function (dataset, collapseToFields,
return collapsedDataset;
};


morpheus.SelectRow = function (dataset, collapseToFields,
summarizeFunction, shallowCopy) {
var vectors = [];
var nfields = collapseToFields.length;

for (var i = 0; i < nfields; i++) {
var v = dataset.getRowMetadata().getByName(collapseToFields[i]);
if (!v) {
throw collapseToFields[i]
+ ' not found. Available fields are '
+ morpheus.MetadataUtil.getMetadataNames(dataset
.getRowMetadata());
}
vectors.push(v);
}

var idToIndices = morpheus.VectorUtil.createValuesToIndicesMap(vectors);

var collapsedDataset = new morpheus.Dataset({
name: dataset.getName(),
rows: idToIndices.size(),
columns: dataset.getColumnCount(),
dataType: 'Float32'
});


var nseries = dataset.getSeriesCount();
for (var series = 1; series < nseries; series++) {
collapsedDataset.addSeries({
name: dataset.getName(i),
dataType: 'Float32'
});
}
if (shallowCopy) {
collapsedDataset.setColumnMetadata(dataset.getColumnMetadata());
} else {
morpheus.MetadataUtil.copy(dataset.getColumnMetadata(),
collapsedDataset.getColumnMetadata());
}

var names = morpheus.MetadataUtil.getMetadataNames(dataset.getRowMetadata());
var collapseToVectors = [];
var allvectors = [];
for (var i = 0; i < names.length; i++) {
var v = dataset.getRowMetadata().getByName(names[i]);
collapseToVectors.push(collapsedDataset.getRowMetadata().add(names[i]));
allvectors.push(v);
}

var counter = 0;

idToIndices
.forEach(function (rowIndices, key) {
var slice = morpheus.DatasetUtil.slicedView(dataset,
rowIndices, null);
var view = new morpheus.DatasetRowView(slice);
var indexInDataset;
for (var series = 0; series < nseries; series++) {
view.setSeriesIndex(series);
var chosenIndex = summarizeFunction(view).index;
view.setIndex(chosenIndex);
indexInDataset = view.dataset.rowIndices[chosenIndex];

for (var j = 0, ncols = dataset.getColumnCount(); j < ncols; j++) {
collapsedDataset.setValue(counter, j,
view.getValue(j), series);
}
}
for (var i = 0; i < names.length; i++) {
var collapsedToVector = collapseToVectors[i];
var vector = allvectors[i];
collapsedToVector.setValue(counter, vector
.getValue(indexInDataset));
}
counter++;
});
return collapsedDataset;
};

morpheus.CompleteLinkage = function (nelements, distmatrix) {
var j;
var n;
Expand Down
80 changes: 80 additions & 0 deletions src/algorithm/collapse_dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,83 @@ morpheus.CollapseDataset = function (dataset, collapseToFields,
}
return collapsedDataset;
};


morpheus.SelectRow = function (dataset, collapseToFields,
summarizeFunction, shallowCopy) {
var vectors = [];
var nfields = collapseToFields.length;

for (var i = 0; i < nfields; i++) {
var v = dataset.getRowMetadata().getByName(collapseToFields[i]);
if (!v) {
throw collapseToFields[i]
+ ' not found. Available fields are '
+ morpheus.MetadataUtil.getMetadataNames(dataset
.getRowMetadata());
}
vectors.push(v);
}

var idToIndices = morpheus.VectorUtil.createValuesToIndicesMap(vectors);

var collapsedDataset = new morpheus.Dataset({
name: dataset.getName(),
rows: idToIndices.size(),
columns: dataset.getColumnCount(),
dataType: 'Float32'
});


var nseries = dataset.getSeriesCount();
for (var series = 1; series < nseries; series++) {
collapsedDataset.addSeries({
name: dataset.getName(i),
dataType: 'Float32'
});
}
if (shallowCopy) {
collapsedDataset.setColumnMetadata(dataset.getColumnMetadata());
} else {
morpheus.MetadataUtil.copy(dataset.getColumnMetadata(),
collapsedDataset.getColumnMetadata());
}

var names = morpheus.MetadataUtil.getMetadataNames(dataset.getRowMetadata());
var collapseToVectors = [];
var allvectors = [];
for (var i = 0; i < names.length; i++) {
var v = dataset.getRowMetadata().getByName(names[i]);
collapseToVectors.push(collapsedDataset.getRowMetadata().add(names[i]));
allvectors.push(v);
}

var counter = 0;

idToIndices
.forEach(function (rowIndices, key) {
var slice = morpheus.DatasetUtil.slicedView(dataset,
rowIndices, null);
var view = new morpheus.DatasetRowView(slice);
var indexInDataset;
for (var series = 0; series < nseries; series++) {
view.setSeriesIndex(series);
var chosenIndex = summarizeFunction(view).index;
view.setIndex(chosenIndex);
indexInDataset = view.dataset.rowIndices[chosenIndex];

for (var j = 0, ncols = dataset.getColumnCount(); j < ncols; j++) {
collapsedDataset.setValue(counter, j,
view.getValue(j), series);
}
}
for (var i = 0; i < names.length; i++) {
var collapsedToVector = collapseToVectors[i];
var vector = allvectors[i];
collapsedToVector.setValue(counter, vector
.getValue(indexInDataset));
}
counter++;
});
return collapsedDataset;
};
36 changes: 36 additions & 0 deletions src/matrix/multivariate_functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
morpheus.MaximumMeanProbe = function(probes) {
return morpheus.MaximumUnivariateFunction(probes, morpheus.Mean);
};

morpheus.MaximumMeanProbe.toString = function() {
return "Maximum Mean Probe";
};

morpheus.MaximumMeanProbe.selectOne = true;

morpheus.MaximumMedianProbe = function(probes) {
return morpheus.MaximumUnivariateFunction(probes, morpheus.Median);
};

morpheus.MaximumMedianProbe.toString = function() {
return "Maximum Median Probe";
};

morpheus.MaximumMedianProbe.selectOne = true;

morpheus.MaximumUnivariateFunction = function(rowView, fun) {
var curMax = Number.NEGATIVE_INFINITY;
var curIndex = -1;
for (var i = 0; i < rowView.dataset.getRowCount(); i++) {
rowView.setIndex(i);
var mean = fun(rowView);
if (mean > curMax) {
curMax = mean;
curIndex = i;
}
}
return {
value : curMax,
index : curIndex
}
};
13 changes: 8 additions & 5 deletions src/tools/collapse_dataset_tool.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
morpheus.CollapseDatasetTool = function () {
};
morpheus.CollapseDatasetTool.Functions = [morpheus.Mean, morpheus.Median,
new morpheus.MaxPercentiles([25, 75]), morpheus.Min, morpheus.Max, morpheus.Percentile, morpheus.Sum];
new morpheus.MaxPercentiles([25, 75]), morpheus.Min, morpheus.Max, morpheus.Percentile, morpheus.Sum,
morpheus.MaximumMeanProbe, morpheus.MaximumMedianProbe];
morpheus.CollapseDatasetTool.Functions.fromString = function (s) {
for (var i = 0; i < morpheus.CollapseDatasetTool.Functions.length; i++) {
if (morpheus.CollapseDatasetTool.Functions[i].toString() === s) {
Expand All @@ -28,6 +29,7 @@ morpheus.CollapseDatasetTool.prototype = {
form.setVisible('percentile', false);
form.$form.find('[name=collapse_method]').on('change', function (e) {
form.setVisible('percentile', $(this).val() === morpheus.Percentile.toString());
form.setVisible('collapse', !morpheus.CollapseDatasetTool.Functions.fromString($(this).val()).selectOne);
});

setValue('Rows');
Expand Down Expand Up @@ -76,7 +78,8 @@ morpheus.CollapseDatasetTool.prototype = {
}
var allFields = morpheus.MetadataUtil.getMetadataNames(dataset
.getRowMetadata());
dataset = morpheus.CollapseDataset(dataset, collapseToFields, f, true);
var collapseMethod = f.selectOne ? morpheus.SelectRow : morpheus.CollapseDataset;
dataset = collapseMethod(dataset, collapseToFields, f, true);
if (!rows) {
dataset = new morpheus.TransposedDatasetView(dataset);
}
Expand All @@ -88,9 +91,9 @@ morpheus.CollapseDatasetTool.prototype = {
set.remove(field);
});
// hide fields that were not part of collapse to
set.forEach(function (val, name) {
heatMap.setTrackVisible(name, false, !rows);
});
// set.forEach(function (val, name) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need anymore for hiding unused fields for collapsing, because in old tab they should remain unchanged and on the new tab, they won't be shown anyway.

// heatMap.setTrackVisible(name, false, !rows);
// });
return new morpheus.HeatMap({
name: heatMap.getName(),
dataset: dataset,
Expand Down