forked from apache/incubator-wayang
-
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.
[WAYANG-#8] add PyFlatMapOperator and correction of types capture
Signed-off-by: bertty <[email protected]>
- Loading branch information
Showing
11 changed files
with
198 additions
and
62 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
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
49 changes: 49 additions & 0 deletions
49
python/src/pywy/platforms/python/operator/py_unary_flatmap.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,49 @@ | ||
from itertools import chain | ||
from typing import Set, List, Type | ||
|
||
from pywy.core.channel import CH_T | ||
from pywy.operators.unary import FlatmapOperator | ||
from pywy.platforms.python.operator.py_execution_operator import PyExecutionOperator | ||
from pywy.platforms.python.channels import ( | ||
ChannelDescriptor, | ||
PyIteratorChannel, | ||
PY_ITERATOR_CHANNEL_DESCRIPTOR, | ||
PY_CALLABLE_CHANNEL_DESCRIPTOR, | ||
PyCallableChannel | ||
) | ||
|
||
|
||
class PyFlatmapOperator(FlatmapOperator, PyExecutionOperator): | ||
|
||
def __init__(self, origin: FlatmapOperator = None): | ||
fm_function = None if origin is None else origin.fm_function | ||
super().__init__(fm_function) | ||
|
||
def execute(self, inputs: List[Type[CH_T]], outputs: List[Type[CH_T]]): | ||
self.validate_channels(inputs, outputs) | ||
udf = self.fm_function | ||
if isinstance(inputs[0], PyIteratorChannel): | ||
py_in_iter_channel: PyIteratorChannel = inputs[0] | ||
py_out_iter_channel: PyIteratorChannel = outputs[0] | ||
py_out_iter_channel.accept_iterable(chain.from_iterable(map(udf, py_in_iter_channel.provide_iterable()))) | ||
elif isinstance(inputs[0], PyCallableChannel): | ||
py_in_call_channel: PyCallableChannel = inputs[0] | ||
py_out_call_channel: PyCallableChannel = outputs[0] | ||
|
||
def fm_func(iterator): | ||
return chain.from_iterable(map(udf, iterator)) | ||
|
||
py_out_call_channel.accept_callable( | ||
PyCallableChannel.concatenate( | ||
fm_func, | ||
py_in_call_channel.provide_callable() | ||
) | ||
) | ||
else: | ||
raise Exception("Channel Type does not supported") | ||
|
||
def get_input_channeldescriptors(self) -> Set[ChannelDescriptor]: | ||
return {PY_ITERATOR_CHANNEL_DESCRIPTOR, PY_CALLABLE_CHANNEL_DESCRIPTOR} | ||
|
||
def get_output_channeldescriptors(self) -> Set[ChannelDescriptor]: | ||
return {PY_ITERATOR_CHANNEL_DESCRIPTOR, PY_CALLABLE_CHANNEL_DESCRIPTOR} |
124 changes: 89 additions & 35 deletions
124
python/src/pywy/tests/integration/python_platform_test.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 |
---|---|---|
@@ -1,73 +1,127 @@ | ||
import logging | ||
import os | ||
import unittest | ||
import tempfile | ||
from typing import List | ||
from itertools import chain | ||
from typing import List, Iterable | ||
|
||
from pywy.config import RC_TEST_DIR as ROOT | ||
from pywy.dataquanta import WayangContext | ||
from pywy.plugins import PYTHON | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class TestIntegrationPythonPlatform(unittest.TestCase): | ||
|
||
file_10e0: str | ||
|
||
def setUp(self): | ||
self.file_10e0 = "{}/10e0MB.input".format(ROOT) | ||
pass | ||
|
||
def test_grep(self): | ||
@staticmethod | ||
def seed_small_grep(validation_file): | ||
def pre(a: str) -> bool: | ||
return 'six' in a | ||
|
||
fd, path_tmp = tempfile.mkstemp() | ||
|
||
WayangContext() \ | ||
dq = WayangContext() \ | ||
.register(PYTHON) \ | ||
.textfile(self.file_10e0) \ | ||
.filter(pre) \ | ||
.store_textfile(path_tmp) | ||
|
||
lines_filter: List[str] | ||
with open(self.file_10e0, 'r') as f: | ||
lines_filter = list(filter(pre, f.readlines())) | ||
selectivity = len(list(lines_filter)) | ||
.textfile(validation_file) \ | ||
.filter(pre) | ||
|
||
return dq, path_tmp, pre | ||
|
||
def validate_files(self, | ||
validation_file, | ||
outputed_file, | ||
read_and_convert_validation, | ||
read_and_convert_outputed, | ||
delete_outputed=True, | ||
print_variable=False): | ||
lines_filter: List[int] | ||
with open(validation_file, 'r') as f: | ||
lines_filter = list(read_and_convert_validation(f)) | ||
selectivity = len(lines_filter) | ||
|
||
lines_platform: List[str] | ||
with open(path_tmp, 'r') as fp: | ||
lines_platform = fp.readlines() | ||
lines_platform: List[int] | ||
with open(outputed_file, 'r') as fp: | ||
lines_platform = list(read_and_convert_outputed(fp)) | ||
elements = len(lines_platform) | ||
os.remove(path_tmp) | ||
|
||
if delete_outputed: | ||
os.remove(outputed_file) | ||
|
||
if print_variable: | ||
logger.info(f"{lines_platform=}") | ||
logger.info(f"{lines_filter=}") | ||
logger.info(f"{elements=}") | ||
logger.info(f"{selectivity=}") | ||
|
||
self.assertEqual(selectivity, elements) | ||
self.assertEqual(lines_filter, lines_platform) | ||
|
||
def test_grep(self): | ||
|
||
dq, path_tmp, pre = self.seed_small_grep(self.file_10e0) | ||
|
||
dq.store_textfile(path_tmp) | ||
|
||
def convert_validation(file): | ||
return filter(pre, file.readlines()) | ||
|
||
def convert_outputed(file): | ||
return file.readlines() | ||
|
||
self.validate_files( | ||
self.file_10e0, | ||
path_tmp, | ||
convert_validation, | ||
convert_outputed | ||
) | ||
|
||
def test_dummy_map(self): | ||
def pre(a: str) -> bool: | ||
return 'six' in a | ||
|
||
def convert(a: str) -> int: | ||
return len(a) | ||
|
||
fd, path_tmp = tempfile.mkstemp() | ||
dq, path_tmp, pre = self.seed_small_grep(self.file_10e0) | ||
|
||
WayangContext() \ | ||
.register(PYTHON) \ | ||
.textfile(self.file_10e0) \ | ||
.filter(pre) \ | ||
.map(convert) \ | ||
dq.map(convert) \ | ||
.store_textfile(path_tmp) | ||
|
||
lines_filter: List[int] | ||
with open(self.file_10e0, 'r') as f: | ||
lines_filter = list(map(convert, filter(pre, f.readlines()))) | ||
selectivity = len(list(lines_filter)) | ||
def convert_validation(file): | ||
return map(convert, filter(pre, file.readlines())) | ||
|
||
lines_platform: List[int] | ||
with open(path_tmp, 'r') as fp: | ||
lines_platform = list(map(lambda x: int(x), fp.readlines())) | ||
elements = len(lines_platform) | ||
os.remove(path_tmp) | ||
def convert_outputed(file): | ||
return map(lambda x: int(x), file.read().splitlines()) | ||
|
||
self.assertEqual(selectivity, elements) | ||
self.assertEqual(lines_filter, lines_platform) | ||
self.validate_files( | ||
self.file_10e0, | ||
path_tmp, | ||
convert_validation, | ||
convert_outputed | ||
) | ||
|
||
def test_dummy_flatmap(self): | ||
def fm_func(string: str) -> Iterable[str]: | ||
return string.strip().split(" ") | ||
|
||
dq, path_tmp, pre = self.seed_small_grep(self.file_10e0) | ||
|
||
dq.flatmap(fm_func) \ | ||
.store_textfile(path_tmp, '\n') | ||
|
||
def convert_validation(file): | ||
return chain.from_iterable(map(fm_func, filter(pre, file.readlines()))) | ||
|
||
def convert_outputed(file): | ||
return file.read().splitlines() | ||
|
||
self.validate_files( | ||
self.file_10e0, | ||
path_tmp, | ||
convert_validation, | ||
convert_outputed | ||
) |
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.