matlab-prodserver-client 24.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- google/__init__.py +7 -0
- google/protobuf_ml/__init__.py +33 -0
- google/protobuf_ml/descriptor.py +1230 -0
- google/protobuf_ml/descriptor_database.py +177 -0
- google/protobuf_ml/descriptor_pb2.py +1925 -0
- google/protobuf_ml/descriptor_pool.py +1295 -0
- google/protobuf_ml/internal/__init__.py +0 -0
- google/protobuf_ml/internal/_parameterized.py +443 -0
- google/protobuf_ml/internal/api_implementation.py +168 -0
- google/protobuf_ml/internal/builder.py +130 -0
- google/protobuf_ml/internal/containers.py +708 -0
- google/protobuf_ml/internal/decoder.py +1068 -0
- google/protobuf_ml/internal/descriptor_database_test.py +127 -0
- google/protobuf_ml/internal/descriptor_pool_test.py +1149 -0
- google/protobuf_ml/internal/descriptor_test.py +1076 -0
- google/protobuf_ml/internal/encoder.py +829 -0
- google/protobuf_ml/internal/enum_type_wrapper.py +124 -0
- google/protobuf_ml/internal/extension_dict.py +213 -0
- google/protobuf_ml/internal/generator_test.py +354 -0
- google/protobuf_ml/internal/import_test.py +49 -0
- google/protobuf_ml/internal/json_format_test.py +1285 -0
- google/protobuf_ml/internal/keywords_test.py +103 -0
- google/protobuf_ml/internal/message_factory_test.py +299 -0
- google/protobuf_ml/internal/message_listener.py +78 -0
- google/protobuf_ml/internal/message_test.py +2573 -0
- google/protobuf_ml/internal/proto_builder_test.py +106 -0
- google/protobuf_ml/internal/python_message.py +1539 -0
- google/protobuf_ml/internal/reflection_test.py +3381 -0
- google/protobuf_ml/internal/service_reflection_test.py +139 -0
- google/protobuf_ml/internal/symbol_database_test.py +133 -0
- google/protobuf_ml/internal/test_util.py +878 -0
- google/protobuf_ml/internal/testing_refleaks.py +142 -0
- google/protobuf_ml/internal/text_encoding_test.py +67 -0
- google/protobuf_ml/internal/text_format_test.py +2447 -0
- google/protobuf_ml/internal/type_checkers.py +435 -0
- google/protobuf_ml/internal/unknown_fields_test.py +461 -0
- google/protobuf_ml/internal/well_known_types.py +887 -0
- google/protobuf_ml/internal/well_known_types_test.py +1013 -0
- google/protobuf_ml/internal/wire_format.py +268 -0
- google/protobuf_ml/internal/wire_format_test.py +252 -0
- google/protobuf_ml/json_format.py +912 -0
- google/protobuf_ml/message.py +424 -0
- google/protobuf_ml/message_factory.py +189 -0
- google/protobuf_ml/proto_builder.py +134 -0
- google/protobuf_ml/reflection.py +95 -0
- google/protobuf_ml/service.py +228 -0
- google/protobuf_ml/service_reflection.py +295 -0
- google/protobuf_ml/symbol_database.py +194 -0
- google/protobuf_ml/text_encoding.py +110 -0
- google/protobuf_ml/text_format.py +1842 -0
- google/protobuf_ml/unknown_fields.py +120 -0
- matlab/__init__.py +43 -0
- matlab/bin/win64/libmwfoundation_matlabdata.dll +0 -0
- matlab/bin/win64/libmwfoundation_matlabdata_standalone.dll +0 -0
- matlab/extern/bin/win64/libMatlabDataArray.dll +0 -0
- matlab/extern/bin/win64/matlabmultidimarrayforpython.cp310-win_amd64.pyd +0 -0
- matlab/extern/bin/win64/matlabmultidimarrayforpython.cp39-win_amd64.pyd +0 -0
- matlab/extern/bin/win64/matlabmultidimarrayforpython.pyd +0 -0
- matlab/production_server/__init__.py +2 -0
- matlab/production_server/_internal_mps/__init__.py +2 -0
- matlab/production_server/_internal_mps/feval_handler.py +76 -0
- matlab/production_server/_internal_mps/invocation_handler_utils.py +52 -0
- matlab/production_server/_internal_mps/matlab_array_builder.py +201 -0
- matlab/production_server/_internal_mps/matlab_response_builder.py +134 -0
- matlab/production_server/_proto_generated/MATLABArray_pb2.py +129 -0
- matlab/production_server/_proto_generated/MATLABError_pb2.py +27 -0
- matlab/production_server/_proto_generated/MATLABParams_pb2.py +26 -0
- matlab/production_server/_proto_generated/MATLABResult_pb2.py +27 -0
- matlab/production_server/_proto_generated/__init__.py +0 -0
- matlab/production_server/client.py +140 -0
- matlab/production_server/mpsexceptions.py +56 -0
- matlab_prodserver_client-24.2.dist-info/LICENSE.txt +27 -0
- matlab_prodserver_client-24.2.dist-info/METADATA +94 -0
- matlab_prodserver_client-24.2.dist-info/RECORD +77 -0
- matlab_prodserver_client-24.2.dist-info/WHEEL +5 -0
- matlab_prodserver_client-24.2.dist-info/top_level.txt +3 -0
- six.py +953 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# Protocol Buffers - Google's data interchange format
|
|
2
|
+
# Copyright 2008 Google Inc. All rights reserved.
|
|
3
|
+
# https://developers.google.com/protocol-buffers/
|
|
4
|
+
#
|
|
5
|
+
# Redistribution and use in source and binary forms, with or without
|
|
6
|
+
# modification, are permitted provided that the following conditions are
|
|
7
|
+
# met:
|
|
8
|
+
#
|
|
9
|
+
# * Redistributions of source code must retain the above copyright
|
|
10
|
+
# notice, this list of conditions and the following disclaimer.
|
|
11
|
+
# * Redistributions in binary form must reproduce the above
|
|
12
|
+
# copyright notice, this list of conditions and the following disclaimer
|
|
13
|
+
# in the documentation and/or other materials provided with the
|
|
14
|
+
# distribution.
|
|
15
|
+
# * Neither the name of Google Inc. nor the names of its
|
|
16
|
+
# contributors may be used to endorse or promote products derived from
|
|
17
|
+
# this software without specific prior written permission.
|
|
18
|
+
#
|
|
19
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
20
|
+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
21
|
+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
22
|
+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
23
|
+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
24
|
+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
25
|
+
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
26
|
+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
27
|
+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
28
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
30
|
+
|
|
31
|
+
"""Contains Unknown Fields APIs.
|
|
32
|
+
|
|
33
|
+
Simple usage example:
|
|
34
|
+
unknown_field_set = UnknownFieldSet(message)
|
|
35
|
+
for unknown_field in unknown_field_set:
|
|
36
|
+
wire_type = unknown_field.wire_type
|
|
37
|
+
field_number = unknown_field.field_number
|
|
38
|
+
data = unknown_field.data
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
from google.protobuf_ml.internal import api_implementation
|
|
43
|
+
|
|
44
|
+
if api_implementation._c_module is not None: # pylint: disable=protected-access
|
|
45
|
+
UnknownFieldSet = api_implementation._c_module.UnknownFieldSet # pylint: disable=protected-access
|
|
46
|
+
else:
|
|
47
|
+
from google.protobuf_ml.internal import decoder # pylint: disable=g-import-not-at-top
|
|
48
|
+
from google.protobuf_ml.internal import wire_format # pylint: disable=g-import-not-at-top
|
|
49
|
+
|
|
50
|
+
class UnknownField:
|
|
51
|
+
"""A parsed unknown field."""
|
|
52
|
+
|
|
53
|
+
# Disallows assignment to other attributes.
|
|
54
|
+
__slots__ = ['_field_number', '_wire_type', '_data']
|
|
55
|
+
|
|
56
|
+
def __init__(self, field_number, wire_type, data):
|
|
57
|
+
self._field_number = field_number
|
|
58
|
+
self._wire_type = wire_type
|
|
59
|
+
self._data = data
|
|
60
|
+
return
|
|
61
|
+
|
|
62
|
+
@property
|
|
63
|
+
def field_number(self):
|
|
64
|
+
return self._field_number
|
|
65
|
+
|
|
66
|
+
@property
|
|
67
|
+
def wire_type(self):
|
|
68
|
+
return self._wire_type
|
|
69
|
+
|
|
70
|
+
@property
|
|
71
|
+
def data(self):
|
|
72
|
+
return self._data
|
|
73
|
+
|
|
74
|
+
class UnknownFieldSet:
|
|
75
|
+
"""UnknownField container."""
|
|
76
|
+
|
|
77
|
+
# Disallows assignment to other attributes.
|
|
78
|
+
__slots__ = ['_values']
|
|
79
|
+
|
|
80
|
+
def __init__(self, msg):
|
|
81
|
+
|
|
82
|
+
def InternalAdd(field_number, wire_type, data):
|
|
83
|
+
unknown_field = UnknownField(field_number, wire_type, data)
|
|
84
|
+
self._values.append(unknown_field)
|
|
85
|
+
|
|
86
|
+
self._values = []
|
|
87
|
+
msg_des = msg.DESCRIPTOR
|
|
88
|
+
# pylint: disable=protected-access
|
|
89
|
+
unknown_fields = msg._unknown_fields
|
|
90
|
+
if (msg_des.has_options and
|
|
91
|
+
msg_des.GetOptions().message_set_wire_format):
|
|
92
|
+
local_decoder = decoder.UnknownMessageSetItemDecoder()
|
|
93
|
+
for _, buffer in unknown_fields:
|
|
94
|
+
(field_number, data) = local_decoder(memoryview(buffer))
|
|
95
|
+
InternalAdd(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED, data)
|
|
96
|
+
else:
|
|
97
|
+
for tag_bytes, buffer in unknown_fields:
|
|
98
|
+
# pylint: disable=protected-access
|
|
99
|
+
(tag, _) = decoder._DecodeVarint(tag_bytes, 0)
|
|
100
|
+
field_number, wire_type = wire_format.UnpackTag(tag)
|
|
101
|
+
if field_number == 0:
|
|
102
|
+
raise RuntimeError('Field number 0 is illegal.')
|
|
103
|
+
(data, _) = decoder._DecodeUnknownField(
|
|
104
|
+
memoryview(buffer), 0, wire_type)
|
|
105
|
+
InternalAdd(field_number, wire_type, data)
|
|
106
|
+
|
|
107
|
+
def __getitem__(self, index):
|
|
108
|
+
size = len(self._values)
|
|
109
|
+
if index < 0:
|
|
110
|
+
index += size
|
|
111
|
+
if index < 0 or index >= size:
|
|
112
|
+
raise IndexError('index %d out of range'.index)
|
|
113
|
+
|
|
114
|
+
return self._values[index]
|
|
115
|
+
|
|
116
|
+
def __len__(self):
|
|
117
|
+
return len(self._values)
|
|
118
|
+
|
|
119
|
+
def __iter__(self):
|
|
120
|
+
return iter(self._values)
|
matlab/__init__.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Copyright 2014-2022 MathWorks, Inc.
|
|
2
|
+
"""
|
|
3
|
+
Array interface between Python and MATLAB
|
|
4
|
+
|
|
5
|
+
This package defines classes and exceptions that create and manage
|
|
6
|
+
multidimensional arrays in Python that are passed between Python and MATLAB.
|
|
7
|
+
|
|
8
|
+
Modules
|
|
9
|
+
-------
|
|
10
|
+
* mcpyarray - type-specific multidimensional array classes for working
|
|
11
|
+
with MATLAB, implemented in C++
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
import os
|
|
15
|
+
import platform
|
|
16
|
+
import sys
|
|
17
|
+
from pkgutil import extend_path
|
|
18
|
+
__path__ = extend_path(__path__, '__name__')
|
|
19
|
+
|
|
20
|
+
_package_folder = os.path.dirname(os.path.realpath(__file__))
|
|
21
|
+
sys.path.append(_package_folder)
|
|
22
|
+
|
|
23
|
+
platform_name = platform.system()
|
|
24
|
+
if platform_name == 'Windows':
|
|
25
|
+
arch = 'win64'
|
|
26
|
+
elif platform_name == 'Linux':
|
|
27
|
+
arch = 'glnxa64'
|
|
28
|
+
elif platform_name == 'Darwin':
|
|
29
|
+
platform_arch = platform.machine()
|
|
30
|
+
if platform_arch == 'x86_64':
|
|
31
|
+
arch = 'maci64'
|
|
32
|
+
elif platform_arch == 'arm64':
|
|
33
|
+
arch = 'maca64'
|
|
34
|
+
else:
|
|
35
|
+
raise RuntimeError('MacOS architecture {0} is not supported.'.format(platform_arch))
|
|
36
|
+
else:
|
|
37
|
+
raise RuntimeError('Operating system {0} is not supported.'.format(platform_name))
|
|
38
|
+
|
|
39
|
+
_mcpyarray_folder = os.path.join(_package_folder, 'extern', 'bin', arch)
|
|
40
|
+
sys.path.insert(0, _mcpyarray_folder)
|
|
41
|
+
|
|
42
|
+
from matlabmultidimarrayforpython import double, single, uint8, int8, uint16, \
|
|
43
|
+
int16, uint32, int32, uint64, int64, logical, ShapeError, SizeError
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# Copyright 2014-2022, The MathWorks, Inc.
|
|
2
|
+
#
|
|
3
|
+
|
|
4
|
+
from .invocation_handler_utils import create_matlab_params, \
|
|
5
|
+
process_matlab_response, check_matlab_result_for_errors
|
|
6
|
+
from matlab.production_server._proto_generated import MATLABResult_pb2
|
|
7
|
+
from matlab.production_server.mpsexceptions import IllegalStateError, \
|
|
8
|
+
MATLABException, InvalidSchemeError
|
|
9
|
+
from urllib import parse
|
|
10
|
+
import http.client as httplib
|
|
11
|
+
import sys
|
|
12
|
+
from sys import platform
|
|
13
|
+
if sys.platform != 'darwin':
|
|
14
|
+
import ssl
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class FevalHandler(object):
|
|
19
|
+
def __init__(self, client):
|
|
20
|
+
parsed = parse.urlparse(client.url)
|
|
21
|
+
self._scheme = parsed.scheme
|
|
22
|
+
if not self._scheme == 'http' and not self._scheme == 'https':
|
|
23
|
+
raise InvalidSchemeError('MWHttpClient only supports HTTP and HTTPS')
|
|
24
|
+
self._hostname = parsed.hostname
|
|
25
|
+
self._port = parsed.port
|
|
26
|
+
self._client = client
|
|
27
|
+
self._conn = None
|
|
28
|
+
self._context = client.ssl_context
|
|
29
|
+
self._reset_connection()
|
|
30
|
+
|
|
31
|
+
def _reset_connection(self):
|
|
32
|
+
if self._conn:
|
|
33
|
+
self._conn.close()
|
|
34
|
+
|
|
35
|
+
if self._scheme == 'http':
|
|
36
|
+
self._conn = httplib.HTTPConnection(self._hostname, self._port, timeout=self._client.timeout)
|
|
37
|
+
elif self._scheme == 'https':
|
|
38
|
+
self._conn = httplib.HTTPSConnection(self._hostname, self._port, timeout=self._client.timeout,
|
|
39
|
+
context=self._context)
|
|
40
|
+
self._is_close = False
|
|
41
|
+
self._conn.connect()
|
|
42
|
+
|
|
43
|
+
if self._client.timeout:
|
|
44
|
+
self._conn.sock.settimeout(self._client.timeout)
|
|
45
|
+
|
|
46
|
+
def process_request(self, ctf, ml_func, *args, **kwargs):
|
|
47
|
+
if self._is_close:
|
|
48
|
+
raise IllegalStateError(
|
|
49
|
+
"This instance of MWHttpClient is in the closed state. " +
|
|
50
|
+
"Once closed, an instance of MWHttpClient cannot be used "
|
|
51
|
+
"to create a proxy instance. " +
|
|
52
|
+
"You will have to create a new instance of MWHttpClient.")
|
|
53
|
+
matlab_params = create_matlab_params(*args, **kwargs)
|
|
54
|
+
serialized_data = matlab_params.SerializeToString()
|
|
55
|
+
resource_id = parse.quote('/' + ctf.name + '/' + ml_func)
|
|
56
|
+
headers = {"User-Agent": "MATLAB Production Server Python client",
|
|
57
|
+
"Content-Type": "application/x-google-protobuf"}
|
|
58
|
+
|
|
59
|
+
self._conn.request('POST', resource_id, serialized_data,
|
|
60
|
+
headers=headers)
|
|
61
|
+
try:
|
|
62
|
+
response = self._conn.getresponse()
|
|
63
|
+
data = response.read()
|
|
64
|
+
except:
|
|
65
|
+
# We've likely timed out on the connection; we must burn this one
|
|
66
|
+
# because we're unwilling to wait any longer for more data, which
|
|
67
|
+
# renders this socket useless
|
|
68
|
+
#
|
|
69
|
+
self._reset_connection()
|
|
70
|
+
raise
|
|
71
|
+
if not response.status == 200:
|
|
72
|
+
raise httplib.HTTPException(response.reason)
|
|
73
|
+
matlab_result = MATLABResult_pb2.MATLAB_Result()
|
|
74
|
+
matlab_result.ParseFromString(data)
|
|
75
|
+
check_matlab_result_for_errors(ml_func, matlab_params, matlab_result)
|
|
76
|
+
return process_matlab_response(matlab_params.nargout, matlab_result)
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# Copyright 2014-2022, The MathWorks, Inc.
|
|
2
|
+
#
|
|
3
|
+
|
|
4
|
+
from .matlab_array_builder import create_matlab_array
|
|
5
|
+
from .matlab_response_builder import create_matlab_result
|
|
6
|
+
from matlab.production_server.mpsexceptions \
|
|
7
|
+
import MATLABException, NargoutMismatchError
|
|
8
|
+
from matlab.production_server._proto_generated import MATLABParams_pb2
|
|
9
|
+
|
|
10
|
+
def create_matlab_params(*args, **kwargs):
|
|
11
|
+
"""
|
|
12
|
+
creates serializable message for input arguments
|
|
13
|
+
"""
|
|
14
|
+
matlab_params = MATLABParams_pb2.MATLAB_Params()
|
|
15
|
+
matlab_params.nargout = 1
|
|
16
|
+
for key, value in kwargs.items():
|
|
17
|
+
if key == 'nargout':
|
|
18
|
+
if not isinstance(value, int):
|
|
19
|
+
raise TypeError(
|
|
20
|
+
'number of output arguments should be a non-negative int')
|
|
21
|
+
if value < 0:
|
|
22
|
+
raise ValueError('nargout value out of range')
|
|
23
|
+
matlab_params.nargout = value
|
|
24
|
+
else:
|
|
25
|
+
raise TypeError('unexpected keyword argument : ' + str(key))
|
|
26
|
+
ml_ref = [(create_matlab_array(arg)).__get_ml_ref__() for arg in args]
|
|
27
|
+
matlab_params.rhs.extend(ml_ref)
|
|
28
|
+
return matlab_params
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def process_matlab_response(nargout, matlab_result):
|
|
32
|
+
"""
|
|
33
|
+
returns MATLAB result to Python
|
|
34
|
+
"""
|
|
35
|
+
response = matlab_result.lhs
|
|
36
|
+
result = [create_matlab_result(ml_output) for ml_output in response]
|
|
37
|
+
if nargout == 1:
|
|
38
|
+
return result[0]
|
|
39
|
+
return result
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def check_matlab_result_for_errors(function_name,
|
|
43
|
+
matlab_params, matlab_result):
|
|
44
|
+
"""
|
|
45
|
+
check MATLAB response for errors
|
|
46
|
+
"""
|
|
47
|
+
matlab_error = matlab_result.error
|
|
48
|
+
if matlab_error.message:
|
|
49
|
+
raise MATLABException(matlab_result.error)
|
|
50
|
+
|
|
51
|
+
if not (matlab_params.nargout == len(matlab_result.lhs)):
|
|
52
|
+
raise NargoutMismatchError(function_name, matlab_params, matlab_result)
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
# Copyright 2014-2023 The MathWorks, Inc.
|
|
2
|
+
#
|
|
3
|
+
import os
|
|
4
|
+
import sys
|
|
5
|
+
import matlab
|
|
6
|
+
import struct
|
|
7
|
+
from matlab.production_server._proto_generated \
|
|
8
|
+
import MATLABArray_pb2 as MATLABArray
|
|
9
|
+
|
|
10
|
+
# Since integers have unlimited precision in Python 3, it makes sense to map them to the largest
|
|
11
|
+
# specific size available, namely int64.
|
|
12
|
+
_int_mw_type = MATLABArray.MATLAB_Array.INT64
|
|
13
|
+
_int_ext = 'int64'
|
|
14
|
+
|
|
15
|
+
long_type = int
|
|
16
|
+
unicode_type = str
|
|
17
|
+
|
|
18
|
+
def getBytes(arr):
|
|
19
|
+
return arr.tobytes()
|
|
20
|
+
|
|
21
|
+
class MATLABArrayBuilder(object):
|
|
22
|
+
def get_mw_type(self):
|
|
23
|
+
return self.ml_ref.type
|
|
24
|
+
|
|
25
|
+
def __init__(self, mw_type):
|
|
26
|
+
self.ml_ref = MATLABArray.MATLAB_Array()
|
|
27
|
+
self.ml_ref.type = mw_type
|
|
28
|
+
|
|
29
|
+
def __get_ml_ref__(self):
|
|
30
|
+
return self.ml_ref
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class MATLABByteArray(MATLABArrayBuilder):
|
|
34
|
+
def __init__(self, ml_input=[]):
|
|
35
|
+
message_info = _TypeExtensions[type(ml_input)]
|
|
36
|
+
super(MATLABByteArray, self).__init__(message_info['MWType'])
|
|
37
|
+
|
|
38
|
+
extension_type = message_info['Extension']
|
|
39
|
+
try:
|
|
40
|
+
self.ml_ref.dimension.extend(ml_input.size)
|
|
41
|
+
if ml_input._is_complex:
|
|
42
|
+
# array.array.tobytes() is called to convert the array to an array of machine values
|
|
43
|
+
# and return the bytes representation.
|
|
44
|
+
self.ml_ref.__getattribute__(extension_type).elements = \
|
|
45
|
+
getBytes(ml_input._real.toarray())
|
|
46
|
+
self.ml_ref.__getattribute__(extension_type).imag_elements = \
|
|
47
|
+
getBytes(ml_input._imag.toarray())
|
|
48
|
+
else:
|
|
49
|
+
self.ml_ref.__getattribute__(extension_type).elements = \
|
|
50
|
+
getBytes(ml_input._data.toarray())
|
|
51
|
+
|
|
52
|
+
except AttributeError:
|
|
53
|
+
self.ml_ref.dimension.extend([1, len(ml_input)])
|
|
54
|
+
if bytearray == type(ml_input):
|
|
55
|
+
self.ml_ref.__getattribute__(extension_type).elements = \
|
|
56
|
+
bytes(ml_input)
|
|
57
|
+
elif isinstance(ml_input, bytes):
|
|
58
|
+
self.ml_ref.__getattribute__(extension_type).elements = \
|
|
59
|
+
ml_input
|
|
60
|
+
else:
|
|
61
|
+
self.ml_ref.__getattribute__(extension_type).elements = \
|
|
62
|
+
''.join(chr(x) for x in ml_input).encode()
|
|
63
|
+
|
|
64
|
+
class MATLABNumericArray(MATLABArrayBuilder):
|
|
65
|
+
def __init__(self, ml_input=[]):
|
|
66
|
+
message_info = _TypeExtensions[type(ml_input)]
|
|
67
|
+
super(MATLABNumericArray, self).__init__(message_info['MWType'])
|
|
68
|
+
|
|
69
|
+
extension_type = message_info['Extension']
|
|
70
|
+
try:
|
|
71
|
+
self.ml_ref.dimension.extend(ml_input.size)
|
|
72
|
+
if ml_input._is_complex:
|
|
73
|
+
self.ml_ref.__getattribute__(
|
|
74
|
+
extension_type).elements.extend(ml_input._real)
|
|
75
|
+
self.ml_ref.__getattribute__(
|
|
76
|
+
extension_type).imag_elements.extend(ml_input._imag)
|
|
77
|
+
else:
|
|
78
|
+
self.ml_ref.__getattribute__(
|
|
79
|
+
extension_type).elements.extend(ml_input._data)
|
|
80
|
+
except AttributeError:
|
|
81
|
+
self.ml_ref.dimension.extend([1, 1])
|
|
82
|
+
if isinstance(ml_input, complex):
|
|
83
|
+
self.ml_ref.__getattribute__(
|
|
84
|
+
extension_type).elements.extend([ml_input.real])
|
|
85
|
+
self.ml_ref.__getattribute__(
|
|
86
|
+
extension_type).imag_elements.extend([ml_input.imag])
|
|
87
|
+
else:
|
|
88
|
+
self.ml_ref.__getattribute__(
|
|
89
|
+
extension_type).elements.extend([ml_input])
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class MATLABCharArray(MATLABArrayBuilder):
|
|
93
|
+
def __init__(self, ml_input=''):
|
|
94
|
+
super(MATLABCharArray, self).__init__(MATLABArray.MATLAB_Array.CHAR)
|
|
95
|
+
if len(ml_input) == 0:
|
|
96
|
+
self.ml_ref.dimension.extend([0, 0])
|
|
97
|
+
else:
|
|
98
|
+
self.ml_ref.dimension.extend([1, len(ml_input)])
|
|
99
|
+
self.ml_ref.mwchar.elements.extend(
|
|
100
|
+
[ord(x) for x in ml_input])
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
class MATLABStruct(MATLABArrayBuilder):
|
|
104
|
+
def __init__(self, ml_input={}):
|
|
105
|
+
super(MATLABStruct, self).__init__(MATLABArray.MATLAB_Array.STRUCT)
|
|
106
|
+
self.ml_ref.dimension.extend([1, 1])
|
|
107
|
+
self._fields = ml_input.keys()
|
|
108
|
+
try:
|
|
109
|
+
self.ml_ref.struct_.field_names.extend(
|
|
110
|
+
self._fields)
|
|
111
|
+
except TypeError:
|
|
112
|
+
raise TypeError("field names must be strings")
|
|
113
|
+
self._values = ml_input.values()
|
|
114
|
+
struct_values = [create_matlab_array(val).__get_ml_ref__()
|
|
115
|
+
for val in self._values]
|
|
116
|
+
self.ml_ref.struct_.elements.extend(
|
|
117
|
+
struct_values)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
class MATLABCellArray(MATLABArrayBuilder):
|
|
121
|
+
def __init__(self, ml_input=[]):
|
|
122
|
+
super(MATLABCellArray, self).__init__(MATLABArray.MATLAB_Array.CELL)
|
|
123
|
+
self.ml_ref.dimension.extend([1, len(ml_input)])
|
|
124
|
+
self.ml_ref.cell.elements.extend(
|
|
125
|
+
[(create_matlab_array(val)).__get_ml_ref__() for val in ml_input])
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
_MATLABArrayBuilderClass = {
|
|
129
|
+
matlab.uint8: MATLABByteArray,
|
|
130
|
+
matlab.int8: MATLABByteArray,
|
|
131
|
+
matlab.uint16: MATLABNumericArray,
|
|
132
|
+
matlab.int16: MATLABNumericArray,
|
|
133
|
+
matlab.uint32: MATLABNumericArray,
|
|
134
|
+
matlab.int32: MATLABNumericArray,
|
|
135
|
+
matlab.uint64: MATLABNumericArray,
|
|
136
|
+
matlab.int64: MATLABNumericArray,
|
|
137
|
+
matlab.single: MATLABNumericArray,
|
|
138
|
+
matlab.double: MATLABNumericArray,
|
|
139
|
+
matlab.logical: MATLABNumericArray,
|
|
140
|
+
bool: MATLABNumericArray,
|
|
141
|
+
int: MATLABNumericArray,
|
|
142
|
+
long_type: MATLABNumericArray,
|
|
143
|
+
float: MATLABNumericArray,
|
|
144
|
+
complex: MATLABNumericArray,
|
|
145
|
+
bytes: MATLABByteArray,
|
|
146
|
+
bytearray: MATLABByteArray,
|
|
147
|
+
list: MATLABCellArray,
|
|
148
|
+
tuple: MATLABCellArray,
|
|
149
|
+
dict: MATLABStruct,
|
|
150
|
+
set: MATLABCellArray,
|
|
151
|
+
frozenset: MATLABCellArray,
|
|
152
|
+
str: MATLABCharArray,
|
|
153
|
+
unicode_type: MATLABCharArray
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
_TypeExtensions = {
|
|
157
|
+
matlab.uint8: {'MWType': MATLABArray.MATLAB_Array.UINT8,
|
|
158
|
+
'Extension': 'uint8'},
|
|
159
|
+
matlab.int8: {'MWType': MATLABArray.MATLAB_Array.INT8,
|
|
160
|
+
'Extension': 'int8'},
|
|
161
|
+
matlab.uint16: {'MWType': MATLABArray.MATLAB_Array.UINT16,
|
|
162
|
+
'Extension': 'uint16'},
|
|
163
|
+
matlab.int16: {'MWType': MATLABArray.MATLAB_Array.INT16,
|
|
164
|
+
'Extension': 'int16'},
|
|
165
|
+
matlab.uint32: {'MWType': MATLABArray.MATLAB_Array.UINT32,
|
|
166
|
+
'Extension': 'uint32'},
|
|
167
|
+
matlab.int32: {'MWType': MATLABArray.MATLAB_Array.INT32,
|
|
168
|
+
'Extension': 'int32'},
|
|
169
|
+
matlab.uint64: {'MWType': MATLABArray.MATLAB_Array.UINT64,
|
|
170
|
+
'Extension': 'uint64'},
|
|
171
|
+
matlab.int64: {'MWType': MATLABArray.MATLAB_Array.INT64,
|
|
172
|
+
'Extension': 'int64'},
|
|
173
|
+
matlab.single: {'MWType': MATLABArray.MATLAB_Array.SINGLE,
|
|
174
|
+
'Extension': 'single'},
|
|
175
|
+
matlab.double: {'MWType': MATLABArray.MATLAB_Array.DOUBLE,
|
|
176
|
+
'Extension': 'mwdouble'},
|
|
177
|
+
matlab.logical: {'MWType': MATLABArray.MATLAB_Array.LOGICAL,
|
|
178
|
+
'Extension': 'logical'},
|
|
179
|
+
# build-in python types
|
|
180
|
+
bool: {'MWType': MATLABArray.MATLAB_Array.LOGICAL,
|
|
181
|
+
'Extension': 'logical'},
|
|
182
|
+
int: {'MWType': _int_mw_type,
|
|
183
|
+
'Extension': _int_ext},
|
|
184
|
+
long_type: {'MWType': MATLABArray.MATLAB_Array.INT64,
|
|
185
|
+
'Extension': 'int64'},
|
|
186
|
+
float: {'MWType': MATLABArray.MATLAB_Array.DOUBLE,
|
|
187
|
+
'Extension': 'mwdouble'},
|
|
188
|
+
complex: {'MWType': MATLABArray.MATLAB_Array.DOUBLE,
|
|
189
|
+
'Extension': 'mwdouble'},
|
|
190
|
+
bytes: {'MWType': MATLABArray.MATLAB_Array.UINT8,
|
|
191
|
+
'Extension': 'uint8'},
|
|
192
|
+
bytearray: {'MWType': MATLABArray.MATLAB_Array.UINT8,
|
|
193
|
+
'Extension': 'uint8'},
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def create_matlab_array(input):
|
|
198
|
+
try:
|
|
199
|
+
return _MATLABArrayBuilderClass[type(input)](input)
|
|
200
|
+
except KeyError:
|
|
201
|
+
raise TypeError("unsupported python type : " + str(type(input)))
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# Copyright 2014-2023, The MathWorks, Inc.
|
|
2
|
+
#
|
|
3
|
+
|
|
4
|
+
from matlab.production_server._proto_generated \
|
|
5
|
+
import MATLABArray_pb2 as MATLABArray
|
|
6
|
+
from .matlab_array_builder import _int_mw_type
|
|
7
|
+
import matlab
|
|
8
|
+
import itertools
|
|
9
|
+
import os
|
|
10
|
+
import sys
|
|
11
|
+
|
|
12
|
+
def create_matlab_result(ml_output):
|
|
13
|
+
if ml_output.type == MATLABArray.MATLAB_Array.CHAR:
|
|
14
|
+
if not len(ml_output.dimension) == 2 or \
|
|
15
|
+
not ml_output.dimension[0] == 1:
|
|
16
|
+
raise ValueError('Only 1xN char array is supported')
|
|
17
|
+
return ''.join([chr(c) for c in
|
|
18
|
+
ml_output.mwchar.elements])
|
|
19
|
+
|
|
20
|
+
if ml_output.type == MATLABArray.MATLAB_Array.STRUCT:
|
|
21
|
+
if not ml_output.dimension == [1, 1]:
|
|
22
|
+
raise ValueError('Only STRUCT scalar is supported')
|
|
23
|
+
|
|
24
|
+
fields = ml_output.struct_.field_names
|
|
25
|
+
values = [create_matlab_result(val)
|
|
26
|
+
for val in ml_output.struct_.elements]
|
|
27
|
+
output_struct = {}
|
|
28
|
+
for field, value in itertools.zip_longest(fields, values):
|
|
29
|
+
output_struct[field] = value
|
|
30
|
+
return output_struct
|
|
31
|
+
|
|
32
|
+
if ml_output.type == MATLABArray.MATLAB_Array.CELL:
|
|
33
|
+
if not len(ml_output.dimension) == 2 or not \
|
|
34
|
+
(ml_output.dimension[0] == 1 or ml_output.dimension[1] == 1):
|
|
35
|
+
raise ValueError('Only 1xN and Nx1 CELL array is supported')
|
|
36
|
+
return [create_matlab_result(val)
|
|
37
|
+
for val in ml_output.cell.elements]
|
|
38
|
+
|
|
39
|
+
try:
|
|
40
|
+
message_info = _MATLABResponseBuilder[ml_output.type]
|
|
41
|
+
except KeyError:
|
|
42
|
+
raise TypeError('unsupported matlab type')
|
|
43
|
+
|
|
44
|
+
extension_type = message_info['Extension']
|
|
45
|
+
matlab_array = ml_output.__getattribute__(extension_type)
|
|
46
|
+
if ml_output.dimension == [1, 1]:
|
|
47
|
+
if extension_type == 'int8' or extension_type == 'uint8':
|
|
48
|
+
# In Python3 the output has been already converted into integer
|
|
49
|
+
converter = ord if isinstance(matlab_array.elements[0], bytes) else lambda x: x
|
|
50
|
+
if len(matlab_array.imag_elements) == 1:
|
|
51
|
+
return complex(
|
|
52
|
+
converter(matlab_array.elements[0]),
|
|
53
|
+
converter(matlab_array.imag_elements[0]))
|
|
54
|
+
|
|
55
|
+
return converter(matlab_array.elements[0])
|
|
56
|
+
|
|
57
|
+
try:
|
|
58
|
+
if len(matlab_array.imag_elements) == 1:
|
|
59
|
+
return complex(
|
|
60
|
+
matlab_array.elements[0],
|
|
61
|
+
matlab_array.imag_elements[0])
|
|
62
|
+
|
|
63
|
+
# coerce Long back to Int here if _int_mw_type == INT64
|
|
64
|
+
# and extension_type is '(u)int64'
|
|
65
|
+
if (_int_mw_type == MATLABArray.MATLAB_Array.INT64
|
|
66
|
+
and extension_type == 'int64') or (extension_type == 'int16') \
|
|
67
|
+
or (extension_type == 'uint16'):
|
|
68
|
+
return int(matlab_array.elements[0])
|
|
69
|
+
|
|
70
|
+
return matlab_array.elements[0]
|
|
71
|
+
except AttributeError:
|
|
72
|
+
assert extension_type == 'logical'
|
|
73
|
+
return matlab_array.elements[0]
|
|
74
|
+
|
|
75
|
+
else:
|
|
76
|
+
if extension_type == 'int8' or extension_type == 'uint8':
|
|
77
|
+
import array
|
|
78
|
+
ext_to_array_type = {'int8': 'b', 'uint8': 'B'}
|
|
79
|
+
real = array.array(ext_to_array_type[extension_type])
|
|
80
|
+
imag = array.array(ext_to_array_type[extension_type])
|
|
81
|
+
if len(matlab_array.imag_elements) != 0:
|
|
82
|
+
real.frombytes(matlab_array.elements)
|
|
83
|
+
imag.frombytes(matlab_array.imag_elements)
|
|
84
|
+
|
|
85
|
+
zipped = [(ir + ii*1j) for (ir, ii) in zip(real, imag)]
|
|
86
|
+
mlarr = message_info['MLArray_Constructor'](
|
|
87
|
+
zipped, size=ml_output.dimension, is_complex=True)
|
|
88
|
+
return mlarr
|
|
89
|
+
real.frombytes(matlab_array.elements)
|
|
90
|
+
|
|
91
|
+
mlarr = message_info['MLArray_Constructor'](real, size=ml_output.dimension)
|
|
92
|
+
return mlarr
|
|
93
|
+
|
|
94
|
+
# elements is a repeatedscalarfieldcontainer type
|
|
95
|
+
# and not a sequence type
|
|
96
|
+
try:
|
|
97
|
+
if len(matlab_array.imag_elements) != 0:
|
|
98
|
+
initializer = [complex(x, y) for x, y in zip(
|
|
99
|
+
matlab_array.elements,
|
|
100
|
+
matlab_array.imag_elements)]
|
|
101
|
+
return message_info['MLArray_Constructor'](
|
|
102
|
+
initializer, ml_output.dimension, is_complex=True)
|
|
103
|
+
except AttributeError:
|
|
104
|
+
assert extension_type == 'logical'
|
|
105
|
+
|
|
106
|
+
return message_info['MLArray_Constructor'](
|
|
107
|
+
matlab_array.elements._values,
|
|
108
|
+
ml_output.dimension)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
_MATLABResponseBuilder = {
|
|
112
|
+
MATLABArray.MATLAB_Array.UINT8: {'Extension': 'uint8',
|
|
113
|
+
'MLArray_Constructor': matlab.uint8},
|
|
114
|
+
MATLABArray.MATLAB_Array.INT8: {'Extension': 'int8',
|
|
115
|
+
'MLArray_Constructor': matlab.int8},
|
|
116
|
+
MATLABArray.MATLAB_Array.UINT16: {'Extension': 'uint16',
|
|
117
|
+
'MLArray_Constructor': matlab.uint16},
|
|
118
|
+
MATLABArray.MATLAB_Array.INT16: {'Extension': 'int16',
|
|
119
|
+
'MLArray_Constructor': matlab.int16},
|
|
120
|
+
MATLABArray.MATLAB_Array.UINT32: {'Extension': 'uint32',
|
|
121
|
+
'MLArray_Constructor': matlab.uint32},
|
|
122
|
+
MATLABArray.MATLAB_Array.INT32: {'Extension': 'int32',
|
|
123
|
+
'MLArray_Constructor': matlab.int32},
|
|
124
|
+
MATLABArray.MATLAB_Array.UINT64: {'Extension': 'uint64',
|
|
125
|
+
'MLArray_Constructor': matlab.uint64},
|
|
126
|
+
MATLABArray.MATLAB_Array.INT64: {'Extension': 'int64',
|
|
127
|
+
'MLArray_Constructor': matlab.int64},
|
|
128
|
+
MATLABArray.MATLAB_Array.SINGLE: {'Extension': 'single',
|
|
129
|
+
'MLArray_Constructor': matlab.single},
|
|
130
|
+
MATLABArray.MATLAB_Array.DOUBLE: {'Extension': 'mwdouble',
|
|
131
|
+
'MLArray_Constructor': matlab.double},
|
|
132
|
+
MATLABArray.MATLAB_Array.LOGICAL: {'Extension': 'logical',
|
|
133
|
+
'MLArray_Constructor': matlab.logical},
|
|
134
|
+
}
|