def __set__(self, instance, value):
        if value is None:
            if self.null:
                value = None
            elif self.default is not None:
                value = self.default
                if callable(value):
                    value = value()

        if instance._initialised:
            if self.name not in instance._data \
                    or instance._data[self.name] != value \
                    or type(instance._data[self.name]) != type(value):
                instance._mark_as_changed(self.name)

        value = deepcopy(value)
        if isinstance(value, (dict, string_types)):
            value = mls(value)
        elif isinstance(value, (list, tuple, set, frozenset)):
            try:
                value = mls(**dict(
                    (item["language"], item["value"]) for item in value
                ))
            except (KeyError, TypeError, ValueError):
                pass

        instance._data[self.name] = value
Example #2
0
    def __set__(self, instance, value):
        if value is None:
            if self.null:
                value = None
            elif self.default is not None:
                value = self.default
                if callable(value):
                    value = value()

        if instance._initialised:
            if self.name not in instance._data \
                    or instance._data[self.name] != value \
                    or type(instance._data[self.name]) != type(value):
                instance._mark_as_changed(self.name)

        value = deepcopy(value)
        if isinstance(value, (dict, string_types)):
            value = mls(value)
        elif isinstance(value, (list, tuple, set, frozenset)):
            try:
                value = mls(**dict(
                    (item["language"], item["value"]) for item in value))
            except (KeyError, TypeError, ValueError):
                pass

        instance._data[self.name] = value
Example #3
0
    def test_rshift(self):
        p = mls("Hello, world")
        t = p.translate_to("ru", u("Здравствуй, мир"))
        y = t >> "ru"

        self.assertEqual(text_type(y), u("Здравствуй, мир"))

        z = mls(y)

        self.assertEqual(text_type(z), text_type(y))
        self.assertEqual(z.language, "ru")
        self.assertEqual(z >> "en", "Hello, world")
Example #4
0
    def to_python(self, value):
        if isinstance(value, mls):
            return value

        value = super(MultiLingualField, self).to_python(value)

        return mls(dict((item.language, item.value) for item in value))
Example #5
0
    def test_translate_to(self):
        s = mls("Hello, world")
        t = s.translate_to("ru", u("Здравствуй, мир"))
        p = t.translate_to("en")
        v = s.translate_to("cs")

        self.assertEqual(s, p)
        self.assertEqual(s, v)
    def to_python(self, value):
        if isinstance(value, mls):
            return value

        value = super(MultiLingualField, self).to_python(value)

        return mls(dict(
            (item.language, item.value) for item in value
        ))
Example #7
0
    def setUp(self):
        setlocale(LC_ALL, "en_US.UTF_8")

        connect(
            db=environ.get("TEST_DB", "test"),
            host=environ.get("TEST_HOST", "localhost"),
            port=environ.get("TEST_PORT", 27017),
        )

        Country(
            code="ru", name=mls(ru=u("Россия"), en="Russia", cs="Rusko")
        ).save()
        Country(
            code="cz", name=mls(
                ru=u("Чехия"),
                en="Czech Republic",
                cs=u("Česko")
            )
        ).save()
    def to_mongo(self, value):
        value = deepcopy(value)
        if isinstance(value, (dict, string_types)):
            value = mls(value)

        if isinstance(value, mls):
            return [
                {"language": key, "value": data}
                for key, data in value._mapping.items()
            ]

        return super(MultiLingualField, self).to_mongo(value)
Example #9
0
    def to_mongo(self, value):
        value = deepcopy(value)
        if isinstance(value, (dict, string_types)):
            value = mls(value)

        if isinstance(value, mls):
            return [{
                "language": key,
                "value": data
            } for key, data in value._mapping.items()]

        return super(MultiLingualField, self).to_mongo(value)
Example #10
0
    def test_dict_init(self):
        a = mls({"ru": u("Привет"), "cs": "Ahoj", "en": "Hi"})
        b = a.translate_to("ru_RU")
        c = b.translate_to("cs", "Nazdar")

        self.assertEqual(repr(a), "en'Hi'")
        self.assertEqual(
            repr(b),
            "ru'\u041f\u0440\u0438\u0432\u0435\u0442'" if PY3 else
            "ru'\\xd0\\x9f\\xd1\\x80\\xd0\\xb8\\xd0\\xb2\\xd0\\xb5\\xd1\\x82'"
        )
        self.assertEqual(repr(c), "cs'Nazdar'")
Example #11
0
    def __init__(self, options):
        gr.top_block.__init__(self)
        ##################################################
        # Variables
        ##################################################
        self.samp_rate = samp_rate = 10000000
        self.f = f = 2510000000

        ##################################################
        # Blocks
        ##################################################
        self.uhd_usrp_sink_0 = uhd.usrp_sink(
            ",".join(("", "")),
            uhd.stream_args(
                cpu_format="fc32",
                channels=range(1),
            ),
        )
        self.uhd_usrp_sink_0.set_samp_rate(samp_rate)
        self.uhd_usrp_sink_0.set_center_freq(2510000000, 0)
        self.uhd_usrp_sink_0.set_gain(90, 0)
        self.blocks_float_to_complex_0 = blocks.float_to_complex(1)

        mcode = True
        nbits = 11
        if mcode == True:
            v = []
            for i in mls.mls(nbits):
                v.append(int(i))
            v = np.array(v)
        else:
            v = np.random.randint(0, 2, np.power(nbits, 2))
        z = np.zeros(np.power(nbits, 2))
        v = v * 2 - 1

        #v = nm.repmat(v,10,1).flatten("F")
        #z = nm.repmat(z,1,,1).flatten("F")

        self.blocks_vector_source_x_0 = blocks.vector_source_f((v), True, 1,
                                                               [])
        self.blocks_vector_source_x_1 = blocks.vector_source_f((z), True, 1,
                                                               [])

        ##################################################
        # Connections
        ##################################################
        self.connect((self.blocks_vector_source_x_0, 0),
                     (self.blocks_float_to_complex_0, 0))
        self.connect((self.blocks_vector_source_x_1, 0),
                     (self.blocks_float_to_complex_0, 1))
        self.connect((self.blocks_float_to_complex_0, 0),
                     (self.uhd_usrp_sink_0, 0))
    def __init__(self, options):
        gr.top_block.__init__(self)
        ##################################################
        # Variables
        ##################################################
        self.samp_rate = samp_rate = 10000000
        self.f = f = 2510000000

        ##################################################
        # Blocks
        ##################################################
        self.uhd_usrp_sink_0 = uhd.usrp_sink(
        	",".join(("", "")),
        	uhd.stream_args(
        		cpu_format="fc32",
        		channels=range(1),
        	),
        )
        self.uhd_usrp_sink_0.set_samp_rate(samp_rate)
        self.uhd_usrp_sink_0.set_center_freq(2510000000, 0)
        self.uhd_usrp_sink_0.set_gain(90, 0)
        self.blocks_float_to_complex_0 = blocks.float_to_complex(1)

        mcode = True
        nbits = 11
        if mcode == True:
            v = []
            for i in mls.mls(nbits): v.append(int(i))
            v = np.array(v)
        else:
            v = np.random.randint(0,2,np.power(nbits,2))
        z = np.zeros(np.power(nbits,2))
        v = v*2-1

        #v = nm.repmat(v,10,1).flatten("F")
        #z = nm.repmat(z,1,,1).flatten("F")

        self.blocks_vector_source_x_0 = blocks.vector_source_f((v), True, 1, [])
        self.blocks_vector_source_x_1 = blocks.vector_source_f((z), True, 1, [])

        ##################################################
        # Connections
        ##################################################
        self.connect((self.blocks_vector_source_x_0, 0), (self.blocks_float_to_complex_0, 0))    
        self.connect((self.blocks_vector_source_x_1, 0), (self.blocks_float_to_complex_0, 1))    
        self.connect((self.blocks_float_to_complex_0, 0), (self.uhd_usrp_sink_0, 0))    
Example #13
0
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 25 23:35:08 2014

@author: sean
"""
import numpy as np
import scipy as sp
import mls
X = np.loadtxt('../data/threshold_pca_image.txt')
y = np.loadtxt('../data/labels.txt')
y_mat = sp.sparse.coo_matrix((np.ones((X.shape[0], )),(np.arange(X.shape[0]), y)),shape=(X.shape[0],10))

#y_m = y_mat.todense()
w = mls.mls(X, y_mat, 1)
Example #14
0
all_train=ct.transform(images)


seed=42
n_folds=3

kf=KFold(images.shape[0], n_folds=n_folds, shuffle=True, random_state=seed)
nrows=image_labels.shape[0]
class_dummy = scipy.sparse.coo_matrix((np.ones((nrows,)),
                                       (np.arange(nrows),
                                        image_labels))).tocsr()
train_index,test_index=iter(kf).next()

X_train=all_train[train_index,:]
y_train=class_dummy[train_index,:]
w=mls.mls(X_train, y_train, 1)
label_probs, label_tags=mls.multinom(X_train,w)
acc_train=1-(label_tags.squeeze()!=image_labels[train_index]).sum()/float(image_labels[train_index].shape[0])
print acc_train

label_probs_test, label_tags_test=mls.multinom(all_train[test_index,:],w)
acc_test=1-(label_tags_test.squeeze()!=image_labels[test_index]).sum()/float(image_labels[test_index].shape[0])

""" pipeline:
gcn-> each patch has mean zero, norm 1 
pca(whiten) -> each pixel has mean zero, std 1 over the patches (not within patch)

#mbkmeans=MiniBatchKMeans(n_clusters=n_elements, init='k-means++',  batch_size=1000 )
#mbkmeans.fit(pca_patches)
#mbkmeans_filename= '../data/mbkmeans.pkl'
#joblib.dump(mbkmeans,mbkmeans_filename)
Example #15
0
    def test_from_mls(self):
        p = mls("Hello, world")
        t = p.translate_to("ru", u("Здравствуй, мир"))

        w = mls(t, language="en")
        self.assertEqual(w, "Hello, world")
Example #16
0
    def test_lshift(self):
        x = mls()
        x <<= "Hello"

        self.assertEqual(str(x), u("Hello"))
        self.assertTrue("ll" in x)
Example #17
0
    def test_empty(self):
        x = mls()

        self.assertEqual(str(x), u(""))
        self.assertEqual(x.language, "en")