forked from opensearch-project/ml-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixing metrics correlation algorithm (opensearch-project#1448)
* fixing metrics correlation algorithm Signed-off-by: Dhrubo Saha <[email protected]> * addressing comments Signed-off-by: Dhrubo Saha <[email protected]> * addressing comment Signed-off-by: Dhrubo Saha <[email protected]> --------- Signed-off-by: Dhrubo Saha <[email protected]>
- Loading branch information
Showing
8 changed files
with
276 additions
and
31 deletions.
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
84 changes: 84 additions & 0 deletions
84
common/src/test/java/org/opensearch/ml/common/model/MetricsCorrelationModelConfigTests.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,84 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.model; | ||
|
||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
import org.opensearch.common.io.stream.BytesStreamOutput; | ||
import org.opensearch.common.xcontent.XContentType; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.core.xcontent.XContentParser; | ||
import org.opensearch.ml.common.TestHelper; | ||
|
||
import java.io.IOException; | ||
import java.util.function.Function; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.opensearch.core.xcontent.ToXContent.EMPTY_PARAMS; | ||
|
||
public class MetricsCorrelationModelConfigTests { | ||
|
||
MetricsCorrelationModelConfig config; | ||
Function<XContentParser, MetricsCorrelationModelConfig> function; | ||
@Rule | ||
public ExpectedException exceptionRule = ExpectedException.none(); | ||
|
||
@Before | ||
public void setUp() { | ||
config = MetricsCorrelationModelConfig.builder() | ||
.modelType("testModelType") | ||
.allConfig("{\"field1\":\"value1\",\"field2\":\"value2\"}") | ||
.build(); | ||
function = parser -> { | ||
try { | ||
return MetricsCorrelationModelConfig.parse(parser); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to parse MetricsCorrelationModelConfig", e); | ||
} | ||
}; | ||
} | ||
|
||
@Test | ||
public void toXContent() throws IOException { | ||
XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent()); | ||
config.toXContent(builder, EMPTY_PARAMS); | ||
String configContent = TestHelper.xContentBuilderToString(builder); | ||
assertEquals("{\"model_type\":\"testModelType\",\"all_config\":\"{\\\"field1\\\":\\\"value1\\\",\\\"field2\\\":\\\"value2\\\"}\"}", configContent); | ||
} | ||
|
||
@Test | ||
public void nullFields_ModelType() { | ||
exceptionRule.expect(IllegalArgumentException.class); | ||
exceptionRule.expectMessage("model type is null"); | ||
config = MetricsCorrelationModelConfig.builder() | ||
.build(); | ||
} | ||
|
||
@Test | ||
public void parse() throws IOException { | ||
String content = "{\"wrong_field\":\"test_value\", \"model_type\":\"testModelType\",\"embedding_dimension\":100,\"framework_type\":\"SENTENCE_TRANSFORMERS\",\"all_config\":\"{\\\"field1\\\":\\\"value1\\\",\\\"field2\\\":\\\"value2\\\"}\"}"; | ||
TestHelper.testParseFromString(config, content, function); | ||
} | ||
|
||
@Test | ||
public void readInputStream_Success() throws IOException { | ||
readInputStream(config); | ||
} | ||
|
||
public void readInputStream(MetricsCorrelationModelConfig config) throws IOException { | ||
BytesStreamOutput bytesStreamOutput = new BytesStreamOutput(); | ||
config.writeTo(bytesStreamOutput); | ||
|
||
StreamInput streamInput = bytesStreamOutput.bytes().streamInput(); | ||
MetricsCorrelationModelConfig parsedConfig = new MetricsCorrelationModelConfig(streamInput); | ||
assertEquals(config.getModelType(), parsedConfig.getModelType()); | ||
assertEquals(config.getAllConfig(), parsedConfig.getAllConfig()); | ||
assertEquals(config.getWriteableName(), parsedConfig.getWriteableName()); | ||
} | ||
} |
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
Oops, something went wrong.