Skip to content

Commit cad236c

Browse files
author
Yucheng Low
authored
Merge pull request turi-code#350 from hoytak/internal-merge-by-hoyt-and-a-squirrel
Merge of updates in internal repository.
2 parents 6563c26 + 41ab602 commit cad236c

File tree

17 files changed

+777
-121
lines changed

17 files changed

+777
-121
lines changed

oss_src/unity/extensions/json/decoder.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ static variant_type _list_from_serializable(const flexible_type& data, const sch
7878
_check_type(schema, JSON::types::VECTOR);
7979
flex_vec ret;
8080
for (const auto& value : data_list) {
81-
ret.push_back(value);
81+
schema_t value_schema;
82+
value_schema.insert(std::make_pair("type", JSON::types::FLOAT));
83+
variant_type deserialized_value = _any_from_serializable(value, value_schema);
84+
ret.push_back(variant_get_value<flex_float>(deserialized_value));
8285
}
8386
return ret;
8487
} else {

oss_src/unity/extensions/json/encoder.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ static void _to_serializable(flexible_type& data, schema_t& schema, const flex_v
4141
schema.insert(std::make_pair("type", JSON::types::VECTOR));
4242
flex_list ret;
4343
for (const auto& value : input) {
44-
ret.push_back(value);
44+
flexible_type serialized_value;
45+
schema_t serialized_schema;
46+
_any_to_serializable(serialized_value, serialized_schema, value);
47+
ret.push_back(serialized_value);
4548
}
4649
data = ret;
4750
}

oss_src/unity/lib/version_number.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
* of the BSD license. See the LICENSE file for details.
77
*/
88

9-
#define __UNITY_VERSION__ "1.9"//#{{VERSION_STRING}}
9+
#define __UNITY_VERSION__ "1.10"//#{{VERSION_STRING}}

oss_src/unity/python/sframe/cython/cy_flexible_type.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ cdef extern from "<flexible_type/flexible_type.hpp>" namespace "graphlab":
8888
void reset()
8989

9090
cdef flexible_type FLEX_UNDEFINED
91+
9192
cdef void swap "std::swap"(flexible_type&, flexible_type&)
9293

9394
cdef int TIMEZONE_RESOLUTION_IN_SECONDS "graphlab::flex_date_time::TIMEZONE_RESOLUTION_IN_SECONDS"
@@ -102,6 +103,7 @@ ctypedef map[string, flexible_type] gl_options_map
102103
# If we just want to work with the enum types, use these.
103104
cdef flex_type_enum flex_type_enum_from_pytype(type t) except *
104105
cdef type pytype_from_flex_type_enum(flex_type_enum e)
106+
cpdef type pytype_from_type_name(str)
105107
cpdef type pytype_from_dtype(object dt)
106108
cdef flex_type_enum flex_type_from_dtype(object dt)
107109
cpdef type pytype_from_array_typecode(str a)

oss_src/unity/python/sframe/cython/cy_flexible_type.pyx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,34 @@ _type_lookup_by_type_enum[<int>IMAGE] = _image_type
427427
cdef type pytype_from_flex_type_enum(flex_type_enum e):
428428
return _type_lookup_by_type_enum[<int> e]
429429

430+
################################################################################
431+
# Name of enum type only
432+
433+
cdef dict _type_lookup_by_type_enum_name = {
434+
"integer" : int,
435+
"datetime" : datetime_type,
436+
"dictionary" : dict,
437+
"float" : float,
438+
"string" : str,
439+
"array" : array_type,
440+
"list" : list,
441+
"image" : _image_type,
442+
"undefined" : none_type}
443+
444+
# Also add in the names of each of the types in order to recognize
445+
# them as well.
446+
for _t in list(_type_lookup_by_type_enum_name.values()):
447+
_type_lookup_by_type_enum_name[_t.__name__.lower()] = _t
448+
449+
cpdef type pytype_from_type_name(str s):
450+
global _type_lookup_by_type_enum_name
451+
try:
452+
return _type_lookup_by_type_enum_name[s.lower()]
453+
except KeyError:
454+
raise ValueError("'%s' not a recogizable type name; valid names are %s."
455+
% (s, ','.join(sorted(set(_type_lookup_by_type_enum_name.keys())))))
456+
457+
430458
################################################################################
431459
# Looking up the translation code to enum type
432460

oss_src/unity/python/sframe/data_structures/image.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
of the BSD license. See the LICENSE file for details.
77
'''
88
try:
9-
import io as _StringIO
9+
from io import BytesIO as _StringIO
1010
except ImportError:
11-
import StringIO as _StringIO
11+
from StringIO import StringIO as _StringIO
1212

1313
from ..deps import numpy as _np, HAS_NUMPY as _HAS_NUMPY
1414
import array as _array
@@ -175,8 +175,6 @@ def pixel_data(self):
175175
"""
176176

177177
try:
178-
global _PIL_image
179-
from PIL import Image as _PIL_image
180178
pil_img = self._to_pil_image()
181179
if _HAS_NUMPY:
182180
return _np.asarray(pil_img)
@@ -209,17 +207,18 @@ def __repr__(self):
209207
return ret
210208

211209
def _to_pil_image(self):
210+
from PIL import Image as _PIL_image
212211
if self._format_enum == _format[_RAW]:
213212
if self.channels == 1:
214-
img = _PIL_image.frombytes('L', (self._width, self._height), str(self._image_data))
213+
img = _PIL_image.frombytes('L', (self._width, self._height), bytes(self._image_data))
215214
elif self.channels == 3:
216-
img = _PIL_image.frombytes('RGB', (self._width, self._height), str(self._image_data))
215+
img = _PIL_image.frombytes('RGB', (self._width, self._height), bytes(self._image_data))
217216
elif self.channels == 4:
218-
img = _PIL_image.frombytes('RGBA', (self._width, self._height), str(self._image_data))
217+
img = _PIL_image.frombytes('RGBA', (self._width, self._height), bytes(self._image_data))
219218
else:
220219
raise ValueError('Unsupported channel size: ' + str(self.channels))
221220
else:
222-
img = _PIL_image.open(_StringIO.StringIO(self._image_data))
221+
img = _PIL_image.open(_StringIO(self._image_data))
223222
return img
224223

225224
def show(self):
@@ -248,8 +247,6 @@ def show(self):
248247
@_show_dispatch(Image)
249248
def show(obj):
250249
try:
251-
global _PIL_image
252-
from PIL import Image as _PIL_image
253250
img = obj._to_pil_image()
254251
img.show()
255252
except ImportError:

oss_src/unity/python/sframe/data_structures/sarray.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -562,13 +562,13 @@ def from_avro(cls, filename):
562562
@classmethod
563563
def where(cls, condition, istrue, isfalse, dtype=None):
564564
"""
565-
Selects elements from either istrue or isfalse depending on the value
565+
Selects elements from either istrue or isfalse depending on the value
566566
of the condition SArray.
567567
568568
Parameters
569569
----------
570570
condition : SArray
571-
An SArray of values such that for each value, if non-zero, yields a
571+
An SArray of values such that for each value, if non-zero, yields a
572572
value from istrue, otherwise from isfalse.
573573
574574
istrue : SArray or constant
@@ -585,8 +585,8 @@ def where(cls, condition, istrue, isfalse, dtype=None):
585585
586586
Examples
587587
--------
588-
589-
Returns an SArray with the same values as g with values above 10
588+
589+
Returns an SArray with the same values as g with values above 10
590590
clipped to 10
591591
592592
>>> g = SArray([6,7,8,9,10,11,12,13])
@@ -595,7 +595,7 @@ def where(cls, condition, istrue, isfalse, dtype=None):
595595
Rows: 8
596596
[6, 7, 8, 9, 10, 10, 10, 10]
597597
598-
Returns an SArray with the same values as g with values below 10
598+
Returns an SArray with the same values as g with values below 10
599599
clipped to 10
600600
601601
>>> SArray.where(g > 10, g, 10)
@@ -832,15 +832,6 @@ def contains(self, item):
832832
If the current SArray contains dictionaries, this produces a 1
833833
for each row if 'item' is a key in the dictionary.
834834
835-
If the current SArray contains strings and item is a string. Produces a 1
836-
for each row if 'item' is a substring of the row and 0 otherwise.
837-
838-
If the current SArray contains list or arrays, this produces a 1
839-
for each row if 'item' is an element of the list or array.
840-
841-
If the current SArray contains dictionaries, this produces a 1
842-
for each row if 'item' is a key in the dictionary.
843-
844835
Parameters
845836
----------
846837
item : any type
@@ -849,7 +840,7 @@ def contains(self, item):
849840
Returns
850841
-------
851842
out : SArray
852-
A binary SArray where a non-zero value denotes that the item
843+
A binary SArray where a non-zero value denotes that the item
853844
was found in the row. And 0 if it is not found.
854845
855846
Examples
@@ -869,7 +860,7 @@ def contains(self, item):
869860
870861
See Also
871862
--------
872-
is_in
863+
is_in
873864
"""
874865
return SArray(_proxy = self.__proxy__.left_scalar_operator(item, 'in'))
875866

@@ -881,7 +872,7 @@ def is_in(self, other):
881872
Conceptually equivalent to:
882873
883874
>>> sa.apply(lambda x: x in other)
884-
875+
885876
If the current SArray contains strings and other is a string. Produces a 1
886877
for each row if the row is a substring of 'other', and 0 otherwise.
887878
@@ -1991,7 +1982,7 @@ def hash(self, seed=0):
19911982
Parameters
19921983
----------
19931984
seed : int
1994-
Defaults to 0. Can be changed to different values to get
1985+
Defaults to 0. Can be changed to different values to get
19951986
different hash results.
19961987
19971988
Returns

oss_src/unity/python/sframe/test/test_flexible_type.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from ..cython.cy_flexible_type import infer_type_of_list
1919
from ..cython.cy_flexible_type import _get_inferred_column_type, _all_convertable
2020
from ..cython.cy_flexible_type import _check_ft_pyobject_hint_path
21+
from ..cython.cy_flexible_type import pytype_from_type_name
2122
from ..util.timezone import GMT
2223
import datetime
2324
from itertools import product
@@ -525,3 +526,16 @@ def test_flexible_type_hint(self):
525526
_check_ft_pyobject_hint_path({1:1}, dict)
526527
_check_ft_pyobject_hint_path(array.array('i', [1,2]), array.array)
527528
_check_ft_pyobject_hint_path(array.array('d', [1,2]), array.array)
529+
530+
def test_pytype_from_type_name(self):
531+
532+
self.assertEquals(pytype_from_type_name("str"), str)
533+
self.assertEquals(pytype_from_type_name("string"), str)
534+
self.assertEquals(pytype_from_type_name("float"), float)
535+
self.assertEquals(pytype_from_type_name("datetime"), datetime.datetime)
536+
self.assertEquals(pytype_from_type_name("image"), image.Image)
537+
self.assertEquals(pytype_from_type_name("list"), list)
538+
self.assertEquals(pytype_from_type_name("undefined"), type(None))
539+
540+
self.assertRaises(ValueError, lambda: pytype_from_type_name("happiness"))
541+

oss_src/unity/python/sframe/test/test_image_type.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,17 @@ def test_lambda(self):
198198
for i in range(len(sa_width)):
199199
self.assertEqual(sa[i].width, sa_width[i])
200200

201+
# Lambda returning height
202+
sa_height = sa.apply(lambda x: x.height)
203+
for i in range(len(sa_height)):
204+
self.assertEqual(sa[i].height, sa_height[i])
205+
206+
# Lambda returning channels
207+
sa_channels = sa.apply(lambda x: x.channels)
208+
for i in range(len(sa_channels)):
209+
self.assertEqual(sa[i].channels, sa_channels[i])
210+
211+
201212
# Lambda returning resized self
202213
sa_resized = sa.apply(lambda x: image_analysis.resize(x, int(x.width / 2), int(x.height / 2)))
203214
for i in range(len(sa_resized)):
@@ -218,3 +229,15 @@ def test_generate_mean(self):
218229

219230
self.assertEqual(average, img3)
220231
self.assertEqual(average2, img2)
232+
233+
def test_pixel_data(self):
234+
fifties = bytearray([50] * 100)
235+
img = image.Image(_image_data=fifties, _channels=1, _height=1, _width=100, _image_data_size=100, _format_enum=2)
236+
pixel_data = img.pixel_data.flatten()
237+
self.assertEqual(pixel_data.shape, (100,))
238+
239+
self.assertEqual(len(pixel_data), len(fifties))
240+
for p in range(len(pixel_data)):
241+
self.assertEqual(pixel_data[p], 50)
242+
243+

oss_src/unity/python/sframe/test/test_json.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010

1111
import array
1212
import datetime
13+
import json # Python built-in JSON module
1314
import math
1415
import os
1516
import pytz
1617
import sys
1718
import unittest
1819

1920
from . import util
20-
from .. import _json
21+
from .. import _json # graphlab._json
2122

2223
if sys.version_info.major == 3:
2324
long = int
@@ -74,6 +75,11 @@ def _run_test_case(self, value):
7475
# test that JSON serialization is invertible with respect to both
7576
# value and type.
7677
(data, schema) = _json.to_serializable(value)
78+
79+
# ensure that resulting value is actually naively serializable
80+
data = json.loads(json.dumps(data, allow_nan=False))
81+
schema = json.loads(json.dumps(schema, allow_nan=False))
82+
7783
#print("----------------------------------")
7884
#print("Value: %s" % value)
7985
#print("Serializable Data: %s" % data)
@@ -125,6 +131,7 @@ def test_vec_to_json(self):
125131
array.array('d'),
126132
array.array('d', [1.5]),
127133
array.array('d', [2.1,2.5,3.1]),
134+
array.array('d', [float('-inf'), float('inf')]),
128135
]]
129136

130137
def test_list_to_json(self):
@@ -139,6 +146,7 @@ def test_list_to_json(self):
139146
["hello", 3, None],
140147
[3.14159, None],
141148
[{}, {'x': 1, 'y': 2}],
149+
["hello", float('-inf'), float('inf')],
142150
]]
143151

144152
def test_dict_to_json(self):

0 commit comments

Comments
 (0)