-
Notifications
You must be signed in to change notification settings - Fork 24
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
improve the backend tree validator #2832
Merged
youri-k
merged 5 commits into
backend-unit-tests
from
backend-nml-parser-improve-validation
Jul 9, 2018
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
865d1f4
enhance the backend tree validator with some functions the frontend a…
c4fba6c
implement some of the pr advice #2832
950aa53
implement pr feedback #2832
33e4a54
Merge branch 'backend-unit-tests' into backend-nml-parser-improve-val…
youri-k 70b5c56
fix merge errors and make code DRYer #2832
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package com.scalableminds.webknossos.datastore.tracings.skeleton | ||
|
||
import com.scalableminds.webknossos.datastore.SkeletonTracing.{Edge, Tree} | ||
import com.scalableminds.webknossos.datastore.SkeletonTracing._ | ||
import com.scalableminds.util.datastructures.UnionFind | ||
import net.liftweb.common.{Box, Failure, Full} | ||
import net.liftweb.util.A | ||
|
@@ -9,6 +9,14 @@ import scala.collection.mutable | |
|
||
object TreeValidator { | ||
|
||
def validateAdditionalInformation(trees: Seq[Tree], branchPoints: Seq[BranchPoint], comments: Seq[Comment], treeGroups: Seq[TreeGroup]): Box[Unit] = | ||
for{ | ||
_ <- checkNoDuplicateTreeGroupIds(treeGroups) | ||
_ <- checkAllTreeGroupIdsUsedExist(trees, treeGroups) | ||
_ <- checkAllNodesUsedInBranchPointsExist(trees, branchPoints) | ||
_ <- checkAllNodesUsedInCommentsExist(trees, comments) | ||
} yield Full(()) | ||
|
||
def validateTrees(trees: Seq[Tree]): Box[Unit] = { | ||
for { | ||
_ <- checkNoDuplicateTreeIds(trees) | ||
|
@@ -102,7 +110,51 @@ object TreeValidator { | |
} | ||
} | ||
|
||
private def checkNoDuplicateTreeGroupIds(treeGroups: Seq[TreeGroup]) = { | ||
val treeGroupIds = getAllTreeGroupIds(treeGroups, Seq[Int]()) | ||
val distinctTreeGroupIds = treeGroupIds.distinct | ||
if (treeGroupIds.size == distinctTreeGroupIds.size) { | ||
Full(()) | ||
} else { | ||
Failure(s"Duplicate treeGroupIds: ${treeGroupIds.diff(distinctTreeGroupIds).mkString(", ")}") | ||
} | ||
} | ||
|
||
private def checkAllNodesUsedInCommentsExist(trees: Seq[Tree], comments: Seq[Comment]): Box[Unit] = { | ||
val nodesInAllTrees = trees.flatMap(_.nodes.map(_.id)) | ||
val nodesInComments = comments.map(_.nodeId).distinct | ||
|
||
val nodesOnlyInComments = nodesInComments.diff(nodesInAllTrees) | ||
if (nodesOnlyInComments.isEmpty) { | ||
Full(()) | ||
} else { | ||
Failure(s"Some comments refer to non-existent nodes. comments: ${nodesOnlyInComments.mkString(", ")}") | ||
} | ||
} | ||
|
||
private def checkAllNodesUsedInBranchPointsExist(trees: Seq[Tree], branchPoints: Seq[BranchPoint]): Box[Unit] = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please make this and similar functions a bit more DRY. There are multiple functions checking that some nodes exist in the trees. |
||
val nodesInAllTrees = trees.flatMap(_.nodes).map(_.id) | ||
val nodesInBranchPoints = branchPoints.map(_.nodeId).distinct | ||
|
||
val nodesOnlyInBranchPoints = nodesInBranchPoints.diff(nodesInAllTrees) | ||
if (nodesOnlyInBranchPoints.isEmpty) { | ||
Full(()) | ||
} else { | ||
Failure(s"Some branchPoints refer to non-existent nodes. branchPoints: ${nodesOnlyInBranchPoints.mkString(", ")}") | ||
} | ||
} | ||
|
||
private def checkAllTreeGroupIdsUsedExist(trees: Seq[Tree], treeGroups: Seq[TreeGroup]) = { | ||
val existingTreeGroups = getAllTreeGroupIds(treeGroups, Seq[Int]()) | ||
val treeGroupsInTrees = trees.flatMap(_.groupId).distinct | ||
|
||
val treeGroupsOnlyInTrees = treeGroupsInTrees.diff(existingTreeGroups) | ||
if (treeGroupsOnlyInTrees.isEmpty) { | ||
Full(()) | ||
} else { | ||
Failure(s"Some treeGroups used in trees don't exist. treeGroups: ${treeGroupsOnlyInTrees.mkString(", ")}") | ||
} | ||
} | ||
|
||
private def foldOverTrees(trees: Seq[Tree])(block: Tree => Box[Unit]) = { | ||
trees.foldLeft[Box[Unit]](Full(())){ | ||
|
@@ -113,4 +165,11 @@ object TreeValidator { | |
} | ||
} | ||
|
||
private def getAllTreeGroupIds(treeGroups: Seq[TreeGroup], ids: Seq[Int]): Seq[Int] = { | ||
treeGroups match { | ||
case (head :: tail) => getAllTreeGroupIds(tail ++ head.children, head.groupId +: ids) | ||
case _ => ids | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tbh I think this function is a layer of indirection to much, as this is called exactly once. Please move those calls directly to the
parse
function.