-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
MINOR - DQ incident severity classifier #15149
Merged
pmbrull
merged 4 commits into
open-metadata:main
from
TeddyCr:MINOR-incident-severity-classifier
Feb 13, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
51 changes: 51 additions & 0 deletions
51
...metadata/service/util/incidentSeverityClassifier/IncidentSeverityClassifierInterface.java
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.openmetadata.service.util.incidentSeverityClassifier; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.openmetadata.schema.EntityInterface; | ||
import org.openmetadata.schema.api.configuration.dataQuality.DataQualityConfiguration; | ||
import org.openmetadata.schema.tests.type.Severity; | ||
|
||
@Slf4j | ||
public abstract class IncidentSeverityClassifierInterface { | ||
protected static IncidentSeverityClassifierInterface instance; | ||
|
||
public static IncidentSeverityClassifierInterface getInstance() { | ||
if (instance == null) { | ||
LOG.info( | ||
"Incident severity classifier instance is null. Default to LogisticRegressionClassifier"); | ||
instance = new LogisticRegressionIncidentSeverityClassifier(); | ||
} | ||
return instance; | ||
} | ||
|
||
public static void createInstance(DataQualityConfiguration dataQualityConfiguration) { | ||
instance = getClassifierClass(dataQualityConfiguration.getSeverityIncidentClassifier()); | ||
} | ||
|
||
private static IncidentSeverityClassifierInterface getClassifierClass( | ||
String severityClassifierClassString) { | ||
IncidentSeverityClassifierInterface incidentSeverityClassifier; | ||
try { | ||
Class severityClassifierClass = Class.forName(severityClassifierClassString); | ||
Constructor severityClassifierConstructor = severityClassifierClass.getConstructor(); | ||
incidentSeverityClassifier = | ||
(IncidentSeverityClassifierInterface) severityClassifierConstructor.newInstance(); | ||
} catch (ClassNotFoundException | ||
| NoSuchMethodException | ||
| IllegalAccessException | ||
| InstantiationException | ||
| InvocationTargetException e) { | ||
LOG.error( | ||
"Error occurred while initializing the incident severity classifier. Default to LogisticRegressionClassifier", | ||
e); | ||
// If we encounter an error while initializing the incident severity classifier, we default to | ||
// the logistic regression classifier | ||
incidentSeverityClassifier = new LogisticRegressionIncidentSeverityClassifier(); | ||
} | ||
return incidentSeverityClassifier; | ||
} | ||
|
||
public abstract Severity classifyIncidentSeverity(EntityInterface entity); | ||
} |
127 changes: 127 additions & 0 deletions
127
...service/util/incidentSeverityClassifier/LogisticRegressionIncidentSeverityClassifier.java
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 |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package org.openmetadata.service.util.incidentSeverityClassifier; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.openmetadata.schema.EntityInterface; | ||
import org.openmetadata.schema.tests.type.Severity; | ||
import org.openmetadata.schema.type.TagLabel; | ||
|
||
@Slf4j | ||
public class LogisticRegressionIncidentSeverityClassifier | ||
extends IncidentSeverityClassifierInterface { | ||
// coef. matrix represents the weights of the logistic regression model. It has shape | ||
// (n_feature, n_class) where n_features are respectively: | ||
// - row 0: 'Tier' (1, 2, 3, 4, 5) for an asset | ||
// - row 1: 'HasOwner' 1 if the asset has an owner, 0 otherwise | ||
// - row 2: 'Followers' number of followers of the asset | ||
// - row 3: 'Votes' number of + votes of the asset. | ||
// Coef. matrix was generated using synthetic data. | ||
static final double[][] coefMatrix = { | ||
new double[] {-39.7199427, -3.16664212, 7.52955733, 16.7600252, 18.5970022}, | ||
new double[] {65.6563864, 9.33015912, -3.11353307, -13.7841793, -58.0888332}, | ||
new double[] {0.0102508192, 0.00490356651, -0.00162766138, -0.00622724217, -0.0072994822}, | ||
new double[] {0.0784018717, -0.01140259, -0.00911123152, -0.0237962385, -0.0340918118}, | ||
}; | ||
|
||
@Override | ||
public Severity classifyIncidentSeverity(EntityInterface entity) { | ||
double[] vectorX = getVectorX(entity); | ||
if (vectorX.length == 0) { | ||
return null; | ||
} | ||
try { | ||
double[] vectorZ = dotProduct(vectorX); | ||
double[] softmaxVector = softmax(vectorZ); | ||
int predictedClass = argmax(softmaxVector); | ||
switch (predictedClass) { | ||
case 0: | ||
return Severity.Severity1; | ||
case 1: | ||
return Severity.Severity2; | ||
case 2: | ||
return Severity.Severity3; | ||
case 3: | ||
return Severity.Severity4; | ||
case 4: | ||
return Severity.Severity5; | ||
} | ||
} catch (Exception e) { | ||
LOG.error("Error occurred while classifying incident severity", e); | ||
} | ||
return null; | ||
} | ||
|
||
private double[] dotProduct(double[] vectorX) { | ||
// compute the dot product of the input vector and the coef. matrix | ||
double[] result = new double[coefMatrix[0].length]; | ||
for (int i = 0; i < coefMatrix.length; i++) { | ||
int sum = 0; | ||
for (int j = 0; j < vectorX.length; j++) { | ||
sum += vectorX[j] * coefMatrix[j][i]; | ||
} | ||
result[i] = sum; | ||
} | ||
return result; | ||
} | ||
|
||
private double[] softmax(double[] vectorZ) { | ||
// compute the softmax of the z vector | ||
double expSum = Arrays.stream(vectorZ).map(Math::exp).sum(); | ||
double[] softmax = new double[vectorZ.length]; | ||
for (int i = 0; i < vectorZ.length; i++) { | ||
softmax[i] = Math.exp(vectorZ[i]) / expSum; | ||
} | ||
return softmax; | ||
} | ||
|
||
private int argmax(double[] softmaxVector) { | ||
// return the index of the max value in the softmax vector | ||
// (i.e. the predicted class) | ||
int maxIndex = 0; | ||
double argmax = 0; | ||
|
||
for (int i = 0; i < softmaxVector.length; i++) { | ||
if (softmaxVector[i] > argmax) { | ||
argmax = softmaxVector[i]; | ||
maxIndex = i; | ||
} | ||
} | ||
return maxIndex; | ||
} | ||
|
||
private double[] getVectorX(EntityInterface entity) { | ||
// get the input vector for the logistic regression model | ||
double hasOwner = entity.getOwner() != null ? 1 : 0; | ||
double followers = entity.getFollowers() != null ? entity.getFollowers().size() : 0; | ||
double votes = entity.getVotes() != null ? entity.getVotes().getUpVotes() : 0; | ||
double tier = entity.getTags() != null ? getTier(entity.getTags()) : 0; | ||
if (tier == 0) { | ||
// if we don't have a tier set we can't run the classifier | ||
return new double[] {}; | ||
} | ||
return new double[] {tier, hasOwner, followers, votes}; | ||
} | ||
|
||
private double getTier(List<TagLabel> tags) { | ||
// get the tier of the asset | ||
|
||
for (TagLabel tag : tags) { | ||
if (tag.getName().contains("Tier")) { | ||
switch (tag.getName()) { | ||
case "Tier1": | ||
return 1; | ||
case "Tier2": | ||
return 2; | ||
case "Tier3": | ||
return 3; | ||
case "Tier4": | ||
return 4; | ||
case "Tier5": | ||
return 5; | ||
} | ||
} | ||
} | ||
return 0; | ||
} | ||
} |
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
18 changes: 18 additions & 0 deletions
18
openmetadata-spec/src/main/resources/json/schema/configuration/dataQualityConfiguration.json
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"$id": "https://open-metadata.org/schema/entity/configuration/dataQualityConfiguration.json", | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"title": "DataQualityConfiguration", | ||
"description": "This schema defines the Data Quality Configuration", | ||
"type": "object", | ||
"javaType": "org.openmetadata.schema.api.configuration.dataQuality.DataQualityConfiguration", | ||
"properties": { | ||
"severityIncidentClassifier": { | ||
"description": "Class Name for the severity incident classifier.", | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"severityIncidentClassifier" | ||
], | ||
"additionalProperties": false | ||
} |
Oops, something went wrong.
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.
I'm super curious on how you computed this model. It can very well become a medium article!