forked from rtdip/core
-
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.
Mirico Metadata Transformer and Unit Tests (rtdip#758)
* mirico metadata transformer Signed-off-by: Chloe Ching <[email protected]> * unit tests and update sql query builder to remove warning Signed-off-by: Chloe Ching <[email protected]> * update docs Signed-off-by: Chloe Ching <[email protected]> * update mirico transformer docs Signed-off-by: Chloe Ching <[email protected]> * update docs Signed-off-by: Chloe Ching <[email protected]> --------- Signed-off-by: Chloe Ching <[email protected]>
- Loading branch information
Showing
9 changed files
with
241 additions
and
6 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
119 changes: 119 additions & 0 deletions
119
src/sdk/python/rtdip_sdk/pipelines/transformers/spark/mirico_json_to_metadata.py
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,119 @@ | ||
# Copyright 2022 RTDIP | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from pyspark.sql import DataFrame | ||
import logging | ||
from pyspark.sql.functions import ( | ||
from_json, | ||
col, | ||
lit, | ||
concat_ws, | ||
upper, | ||
expr, | ||
) | ||
from ...._sdk_utils.compare_versions import ( | ||
_package_version_meets_minimum, | ||
) | ||
from ..interfaces import TransformerInterface | ||
from ..._pipeline_utils.models import Libraries, SystemType | ||
from ..._pipeline_utils.spark import MIRICO_METADATA_SCHEMA | ||
|
||
|
||
class MiricoJsonToMetadataTransformer(TransformerInterface): | ||
""" | ||
Converts a Spark Dataframe column containing a json string created from Mirico to the Metadata Model. | ||
Example | ||
-------- | ||
```python | ||
from rtdip_sdk.pipelines.transformers import MiricoJsonToMetadataTransformer | ||
mirico_json_to_metadata_transformer = MiricoJsonToMetadataTransformer( | ||
data=df | ||
source_column_name="body" | ||
) | ||
result = mirico_json_to_metadata_transformer.transform() | ||
``` | ||
Parameters: | ||
data (DataFrame): Dataframe containing the column with Mirico data | ||
source_column_name (str): Spark Dataframe column containing the Json Mirico data | ||
""" | ||
|
||
data: DataFrame | ||
source_column_name: str | ||
|
||
def __init__(self, data: DataFrame, source_column_name: str) -> None: | ||
_package_version_meets_minimum("pyspark", "3.4.0") | ||
self.data = data | ||
self.source_column_name = source_column_name | ||
|
||
@staticmethod | ||
def system_type(): | ||
""" | ||
Attributes: | ||
SystemType (Environment): Requires PYSPARK | ||
""" | ||
return SystemType.PYSPARK | ||
|
||
@staticmethod | ||
def libraries(): | ||
libraries = Libraries() | ||
return libraries | ||
|
||
@staticmethod | ||
def settings() -> dict: | ||
return {} | ||
|
||
def pre_transform_validation(self): | ||
return True | ||
|
||
def post_transform_validation(self): | ||
return True | ||
|
||
def transform(self) -> DataFrame: | ||
""" | ||
Returns: | ||
DataFrame: A dataframe with the specified column converted to Metadata model | ||
""" | ||
|
||
df = self.data.select( | ||
from_json(self.source_column_name, MIRICO_METADATA_SCHEMA).alias("body"), | ||
) | ||
|
||
tag_name_expr = concat_ws( | ||
"_", | ||
*[ | ||
upper(col("body.siteName")), | ||
upper(col("body.retroName")), | ||
upper(col("body.gasType")), | ||
] | ||
) | ||
|
||
df = df.select( | ||
tag_name_expr.alias("TagName"), | ||
lit("").alias("Description"), | ||
lit("").alias("UoM"), | ||
expr( | ||
"""struct( | ||
body.retroAltitude, | ||
body.retroLongitude, | ||
body.retroLatitude, | ||
body.sensorAltitude, | ||
body.sensorLongitude, | ||
body.sensorLatitude)""" | ||
).alias("Properties"), | ||
).dropDuplicates(["TagName"]) | ||
|
||
return df.select("TagName", "Description", "UoM", "Properties") |
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
100 changes: 100 additions & 0 deletions
100
tests/sdk/python/rtdip_sdk/pipelines/transformers/spark/test_mirico_json_to_metadata.py
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,100 @@ | ||
# Copyright 2022 RTDIP | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import sys | ||
|
||
sys.path.insert(0, ".") | ||
from src.sdk.python.rtdip_sdk.pipelines.transformers.spark.mirico_json_to_metadata import ( | ||
MiricoJsonToMetadataTransformer, | ||
) | ||
from src.sdk.python.rtdip_sdk.pipelines._pipeline_utils.models import ( | ||
Libraries, | ||
SystemType, | ||
) | ||
|
||
from pyspark.sql import SparkSession, DataFrame | ||
from pyspark.sql.types import StructType, StructField, StringType, FloatType | ||
import pytest | ||
from src.sdk.python.rtdip_sdk._sdk_utils.compare_versions import ( | ||
_package_version_meets_minimum, | ||
) | ||
|
||
|
||
def test_mirico_json_to_metadata(spark_session: SparkSession): | ||
mirico_json_data = '{"gasType": "test", "retroLongitude": 123.45, "retroLatitude": 123.45 , "sensorAltitude": 123.45, "sensorLongitude": 123.45, "sensorLatitude": 123.45, "retroName": "test", "siteName": "test", "retroAltitude": 123.45}' | ||
mirico_df: DataFrame = spark_session.createDataFrame([{"body": mirico_json_data}]) | ||
|
||
expected_schema = StructType( | ||
[ | ||
StructField("TagName", StringType(), False), | ||
StructField("Description", StringType(), False), | ||
StructField("UoM", StringType(), False), | ||
StructField( | ||
"Properties", | ||
StructType( | ||
[ | ||
StructField("retroAltitude", FloatType(), True), | ||
StructField("retroLongitude", FloatType(), True), | ||
StructField("retroLatitude", FloatType(), True), | ||
StructField("sensorAltitude", FloatType(), True), | ||
StructField("sensorLongitude", FloatType(), True), | ||
StructField("sensorLatitude", FloatType(), True), | ||
] | ||
), | ||
False, | ||
), | ||
] | ||
) | ||
|
||
expected_data = [ | ||
{ | ||
"TagName": "TEST_TEST_TEST", | ||
"Description": "", | ||
"UoM": "", | ||
"Properties": { | ||
"retroAltitude": 123.45, | ||
"retroLongitude": 123.45, | ||
"retroLatitude": 123.45, | ||
"sensorAltitude": 123.45, | ||
"sensorLongitude": 123.45, | ||
"sensorLatitude": 123.45, | ||
}, | ||
} | ||
] | ||
|
||
expected_df: DataFrame = spark_session.createDataFrame( | ||
schema=expected_schema, data=expected_data | ||
) | ||
|
||
try: | ||
if _package_version_meets_minimum("pyspark", "3.4.0"): | ||
mirico_json_to_metadata_transformer = MiricoJsonToMetadataTransformer( | ||
data=mirico_df, source_column_name="body" | ||
) | ||
actual_df = mirico_json_to_metadata_transformer.transform() | ||
|
||
assert ( | ||
mirico_json_to_metadata_transformer.system_type() == SystemType.PYSPARK | ||
) | ||
assert isinstance( | ||
mirico_json_to_metadata_transformer.libraries(), Libraries | ||
) | ||
|
||
assert expected_schema == actual_df.schema | ||
assert expected_df.collect() == actual_df.collect() | ||
except: | ||
with pytest.raises(Exception): | ||
mirico_json_to_metadata_transformer = MiricoJsonToMetadataTransformer( | ||
data=mirico_df, source_column_name="body" | ||
) |