Skip to content
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
11 changes: 8 additions & 3 deletions scripts/builtin/dbscan.dml
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@
# ----------------------------------------------------------------------------------------------------------------------

m_dbscan = function (Matrix[Double] X, Double eps = 0.5, Integer minPts = 5)
return (Matrix[Double] clusterMembers, Matrix[Double] clusterModel)
return (Matrix[Double] X, Matrix[Double] clusterModel, Double eps)
{
#check input parameter assertions
if(minPts < 0) { stop("DBSCAN: Stopping due to invalid inputs: minPts should be greater than 0"); }
if(eps < 0) { stop("DBSCAN: Stopping due to invalid inputs: Epsilon (eps) should be greater than 0"); }
if(eps < 0)
{
print("DBSCAN: Epsilon (eps) should be greater than 0. Setting eps = 0.5");
eps = 0.5
}

UNASSIGNED = 0;

Expand Down Expand Up @@ -76,6 +80,7 @@ m_dbscan = function (Matrix[Double] X, Double eps = 0.5, Integer minPts = 5)
clusterMembers = components(G=adjacency, verbose=FALSE);
# noise to 0
clusterMembers = clusterMembers * (rowSums(adjacency) > 0);
clusterModel = removeEmpty(target=X, margin="rows", select = (clusterMembers > 0))
clusterModel = removeEmpty(target=X, margin="rows", select = (clusterMembers > 0))
X = clusterMembers
}
}
17 changes: 9 additions & 8 deletions scripts/builtin/dbscanApply.dml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# ----------------------------------------------------------------------------
# NAME TYPE DEFAULT MEANING
# ----------------------------------------------------------------------------
# Xtest Matrix[Double] --- The input Matrix to do outlier detection on.
# X Matrix[Double] --- The input Matrix to do outlier detection on.
# clusterModel Matrix[Double] --- Model of clusters to predict outliers against.
# eps Double 0.5 Maximum distance between two points for one to be considered reachable for the other.

Expand All @@ -36,23 +36,24 @@
# outlierPoints Matrix[Double] --- Predicted outliers


m_dbscanApply = function (Matrix[Double] Xtest, Matrix[Double] clusterModel, Double eps = 0.5)
return (Matrix[double] outlierPoints)
m_dbscanApply = function (Matrix[Double] X, Matrix[Double] clusterModel, Double eps)
return (Matrix[Double] cluster, Matrix[Double] outlierPoints)
{
num_features_Xtest = ncol(Xtest);
num_rows_Xtest = nrow(Xtest);
num_features_Xtest = ncol(X);
num_rows_Xtest = nrow(X);
num_features_model = ncol(clusterModel);
num_rows_model = nrow(clusterModel);

if(num_features_Xtest != num_features_model) {stop("DBSCAN Outlier: Stopping due to invalid inputs: features need to match");}
if(eps < 0) { stop("DBSCAN Outlier: Stopping due to invalid inputs: Epsilon (eps) should be greater than 0"); }
if(num_rows_model <= 0) { stop("DBSCAN Outlier: Stopping due to invalid inputs: Model is empty"); }

X = rbind(clusterModel, Xtest);
neighbors = dist(X);
Xall = rbind(clusterModel, X);
neighbors = dist(Xall);
neighbors = replace(target = neighbors, pattern = 0, replacement = 2.225e-307);
neighbors = neighbors - diag(diag(neighbors));
Xtest_dists = neighbors[(num_rows_model+1):nrow(X), 1:num_rows_model];
Xtest_dists = neighbors[(num_rows_model+1):nrow(Xall), 1:num_rows_model];
withinEps = ((Xtest_dists <= eps) * (0 < Xtest_dists));
outlierPoints = rowSums(withinEps) >= 1;
cluster = removeEmpty(target=outlierPoints, margin="rows", select=outlierPoints)
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private void runOutlierByDBSCAN(boolean defaultProb, int seedA, int seedB, doubl
String HOME = SCRIPT_DIR + TEST_DIR;

fullDMLScriptName = HOME + TEST_NAME + ".dml";
programArgs = new String[]{"-explain","-nvargs",
programArgs = new String[]{"-nvargs",
"X=" + input("A"), "Y=" + input("B"),"Z=" + output("C"), "eps=" + epsDB, "minPts=" + minPts};
fullRScriptName = HOME + TEST_NAME + ".R";
rCmd = getRCmd(inputDir(), inputDir(), Double.toString(epsDB), Integer.toString(minPts), expectedDir());
Expand Down
2 changes: 1 addition & 1 deletion src/test/scripts/functions/builtin/dbscan.dml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
X = read($X);
eps = as.double($eps);
minPts = as.integer($minPts);
[Y, model] = dbscan(X, eps, minPts);
[Y, model, eps] = dbscan(X, eps, minPts);
write(Y, $Y);
4 changes: 2 additions & 2 deletions src/test/scripts/functions/builtin/dbscanApply.dml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ Y = read($Y)
eps = as.double($eps);
minPts = as.integer($minPts);

[indices, clusterModel] = dbscan(X = X, eps = eps, minPts = minPts);
Z = dbscanApply(Xtest=Y, clusterModel = clusterModel, eps = eps);
[indices, clusterModel, eps] = dbscan(X = X, eps = eps, minPts = minPts);
[C, Z] = dbscanApply(X=Y, clusterModel = clusterModel, eps = eps);
write(Z, $Z);