Best Python code snippet using unittest-xml-reporting_python
seq_embed.py
Source:seq_embed.py
1"""2x-vector categorical embeddings3"""4from __future__ import absolute_import5from __future__ import print_function6from __future__ import division7from six.moves import xrange8import logging9import numpy as np10from keras import backend as K11from keras import optimizers12from keras import objectives13from keras.layers import Input, Concatenate, MaxPooling1D14from keras.models import Model, load_model, model_from_json15from .. import objectives as hyp_obj16from ..keras_utils import *17from ..layers import *18from ..losses import categorical_mbr19from ...hyp_model import HypModel20class SeqEmbed(HypModel):21 def __init__(self, enc_net, pt_net,22 loss='categorical_crossentropy',23 pooling='mean+std',24 left_context=0,25 right_context=0,26 begin_context=None,27 end_context=None,28 enc_downsampling=None,29 **kwargs):30 super(SeqEmbed, self).__init__(**kwargs)31 self.enc_net = enc_net32 self.pt_net = pt_net33 self.pooling = pooling34 self.loss = loss35 self.model = None36 self.pool_net = None37 38 self.left_context = left_context39 self.right_context = right_context40 self.begin_context = left_context if begin_context is None else begin_context41 self.end_context = right_context if end_context is None else end_context42 self._enc_downsampling = enc_downsampling43 self.max_seq_length = None44 45 @property46 def x_dim(self):47 return self.enc_net.get_input_shape_at(0)[-1]48 @property49 def num_classes(self):50 return self.pt_net.get_output_shape_at(0)[-1]51 52 @property53 def pool_in_dim(self):54 return self.enc_net.get_output_shape_at(0)[-1]55 56 @property57 def pool_out_dim(self):58 return self.pt_net.get_input_shape_at(0)[-1]59 60 @property61 def in_length(self):62 if self.max_seq_length is None:63 return self.enc_net.get_input_shape_at(0)[-2]64 return self.max_seq_length65 66 @property67 def pool_in_length(self):68 pool_length = self.enc_net.get_output_shape_at(0)[-2]69 if pool_length is None:70 in_length = self.in_length71 if in_length is None:72 return None73 x = Input(shape=(in_length, self.x_dim))74 net = Model(x, self.enc_net(x))75 pool_length = net.get_output_shape_at(0)[-2]76 return pool_length77 78 @property79 def enc_downsampling(self):80 if self._enc_downsampling is None:81 assert self.in_length is not None82 assert self.pool_in_length is not None83 r = self.in_length/self.pool_in_length84 assert np.ceil(r) == np.floor(r)85 self._enc_downsampling = int(r)86 return self._enc_downsampling87 88 def _apply_pooling(self, x, mask):89 90 if self.pooling == 'mean+std':91 pool = Concatenate(axis=-1, name='pooling')(92 GlobalWeightedMeanStdPooling1D(name='mean--std')([x, mask]))93 elif self.pooling == 'mean+logvar':94 pool = Concatenate(axis=-1, name='pooling')(95 GlobalWeightedMeanLogVarPooling1D(name='mean--logvar')([x, mask]))96 elif self.pooling == 'mean':97 pool = GlobalWeightedAveragePooling1D(name='pooling')([x, mask])98 else:99 raise ValueError('Invalid pooling %s' % self.pooling)100 return pool101 102 def compile(self, metrics=None, **kwargs):103 if self.loss == 'categorical_mbr':104 loss = categorical_mbr105 else:106 loss = self.loss107 108 if metrics is None:109 self.model.compile(loss=loss, **kwargs)110 else:111 self.model.compile(loss=loss,112 metrics=metrics,113 weighted_metrics=metrics, **kwargs)114 115 def freeze_enc_net(self):116 self.enc_net.trainable = False117 118 def freeze_enc_net_layers(self, layers):119 for layer_name in layers:120 self.enc_net.get_layer(layer_name).trainable = False121 122 def freeze_pt_net_layers(self, layers):123 for layer_name in layers:124 self.pt_net.get_layer(layer_name).trainable = False125 126 def build(self, max_seq_length=None):127 if max_seq_length is None:128 max_seq_length = self.enc_net.get_input_shape_at(0)[-2]129 self.max_seq_length = max_seq_length130 x = Input(shape=(max_seq_length, self.x_dim,))131 mask = CreateMask(0)(x)132 frame_embed = self.enc_net(x)133 dec_ratio = int(max_seq_length/frame_embed._keras_shape[1])134 if dec_ratio > 1:135 mask = MaxPooling1D(dec_ratio, padding='same')(mask)136 137 pool = self._apply_pooling(frame_embed, mask)138 y = self.pt_net(pool)139 self.model = Model(x, y)140 self.model.summary()141 142 143 def build_embed(self, layers):144 frame_embed = Input(shape=(None, self.pool_in_dim,))145 mask = Input(shape=(None,))146 pool = self._apply_pooling(frame_embed, mask)147 148 outputs = []149 for layer_name in layers:150 embed_i = Model(self.pt_net.get_input_at(0),151 self.pt_net.get_layer(layer_name).get_output_at(0))(pool)152 outputs.append(embed_i)153 self.pool_net = Model([frame_embed, mask], outputs)154 self.pool_net.summary()155 156 def predict_embed(self, x, **kwargs):157 in_seq_length = self.in_length158 pool_seq_length = self.pool_in_length159 r = self.enc_downsampling160 161 assert np.ceil(self.left_context/r) == np.floor(self.left_context/r)162 assert np.ceil(self.right_context/r) == np.floor(self.right_context/r)163 assert np.ceil(self.begin_context/r) == np.floor(self.begin_context/r)164 assert np.ceil(self.end_context/r) == np.floor(self.end_context/r) 165 pool_begin_context = int(self.begin_context/r)166 pool_end_context = int(self.end_context/r)167 pool_left_context = int(self.left_context/r)168 pool_right_context = int(self.right_context/r)169 in_length = x.shape[-2]170 pool_length = int(in_length/r)171 in_shift = in_seq_length - self.left_context - self.right_context172 pool_shift = int(in_shift/r)173 174 y = np.zeros((pool_length, self.pool_in_dim), dtype=float_keras())175 mask = np.ones((1, pool_length), dtype=float_keras())176 mask[0,:pool_begin_context] = 0177 mask[0,pool_length - pool_end_context:] = 0178 num_batches = max(int(np.ceil((in_length-in_seq_length)/in_shift+1)), 1)179 x_i = np.zeros((1,in_seq_length, x.shape[-1]), dtype=float_keras())180 j_in = 0181 j_out = 0182 for i in xrange(num_batches):183 k_in = min(j_in+in_seq_length, in_length)184 k_out = min(j_out+pool_seq_length, pool_length)185 l_in = k_in - j_in186 l_out = k_out - j_out187 x_i[0,:l_in] = x[j_in:k_in]188 y_i = self.enc_net.predict(x_i, batch_size=1, **kwargs)[0]189 y[j_out:k_out] = y_i[:l_out]190 j_in += in_shift191 j_out += pool_shift192 if i==0:193 j_out += pool_left_context194 logging.debug(pool_seq_length)195 logging.debug(pool_left_context)196 logging.debug(pool_right_context)197 logging.debug(pool_begin_context)198 logging.debug(pool_end_context)199 logging.debug('embed2 %d %d %d' % (pool_length, j_out-pool_shift, j_out-pool_shift+l_out))200 y = np.expand_dims(y, axis=0)201 embeds = self.pool_net.predict([y, mask], batch_size=1, **kwargs)202 return np.hstack(tuple(embeds))203 204 @property205 def embed_dim(self):206 if self.pool_net is None:207 return None208 embed_dim=0209 for node in xrange(len(self.pool_net._inbound_nodes)):210 output_shape = self.pool_net.get_output_shape_at(node)211 if isinstance(output_shape, list):212 for shape in output_shape:213 embed_dim += shape[-1]214 else:215 embed_dim += output_shape[-1]216 return embed_dim217 218 def build_eval(self):219 frame_embed = Input(shape=(None, self.pool_in_dim,))220 mask = Input(shape=(None,))221 pool = self._apply_pooling(frame_embed, mask)222 223 score = self.pt_net(pool)224 self.pool_net = Model([frame_embed, mask], score)225 self.pool_net.summary()226 227 def predict_eval(self, x, **kwargs):228 return np.log(self.predict_embed(x, **kwargs)+1e-10)229 230 231 def fit(self, x, y, **kwargs):232 self.model.fit(x, y, **kwargs)233 234 def fit_generator(self, generator, steps_per_epoch, **kwargs):235 self.model.fit_generator(generator, steps_per_epoch, **kwargs)236 237 def get_config(self):238 config = { 'pooling': self.pooling,239 'loss': self.loss,240 'left_context': self.left_context,241 'right_context': self.right_context,242 'begin_context': self.begin_context,243 'end_context': self.end_context}244 base_config = super(SeqEmbed, self).get_config()245 return dict(list(base_config.items()) + list(config.items()))246 247 def save(self, file_path):248 file_model = '%s.json' % (file_path)249 with open(file_model, 'w') as f:250 f.write(self.to_json())251 252 file_model = '%s.enc.h5' % (file_path)253 self.enc_net.save(file_model)254 file_model = '%s.pt.h5' % (file_path)255 self.pt_net.save(file_model)256 257 @classmethod258 def load(cls, file_path):259 file_config = '%s.json' % (file_path) 260 config = SeqEmbed.load_config(file_config)261 262 file_model = '%s.enc.h5' % (file_path)263 enc_net = load_model(file_model, custom_objects=get_keras_custom_obj())264 file_model = '%s.pt.h5' % (file_path)265 pt_net = load_model(file_model, custom_objects=get_keras_custom_obj())266 filter_args = ('loss', 'pooling',267 'left_context', 'right_context',268 'begin_context', 'end_context', 'name')269 kwargs = {k: config[k] for k in filter_args if k in config }270 return cls(enc_net, pt_net, **kwargs)271 272 273 274 @staticmethod275 def filter_args(prefix=None, **kwargs):276 if prefix is None:277 p = ''278 else:279 p = prefix + '_'280 valid_args = ('pooling', 'left_context', 'right_context',281 'begin_context', 'end_context')282 return dict((k, kwargs[p+k])283 for k in valid_args if p+k in kwargs)284 285 286 @staticmethod287 def add_argparse_args(parser, prefix=None):288 if prefix is None:289 p1 = '--'290 p2 = ''291 else:292 p1 = '--' + prefix + '-'293 p2 = prefix + '_'294 parser.add_argument(p1+'pooling', dest=p2+'pooling', default='mean+std',295 choices=['mean+std', 'mean+logvar', 'mean'])296 parser.add_argument(p1+'left-context', dest=(p2+'left_context'),297 default=0, type=int)298 parser.add_argument(p1+'right-context', dest=(p2+'right_context'),299 default=0, type=int)300 parser.add_argument(p1+'begin-context', dest=(p2+'begin_context'),301 default=None, type=int)302 parser.add_argument(p1+'end-context', dest=(p2+'end_context'),...
builder_test.py
Source:builder_test.py
...64 """65 def setUp(self):66 self.builder = builder.TestXMLBuilder()67 self.doc = self.builder._xml_doc68 self.builder.begin_context('testsuites', 'name')69 self.valid_chars = u'вÑбоÑ'70 self.invalid_chars = '\x01'71 self.invalid_chars_replace = u'\ufffd'72 def test_root_has_no_parent(self):73 self.assertIsNone(self.builder.current_context().parent)74 def test_current_context_tag(self):75 self.assertEqual(self.builder.context_tag(), 'testsuites')76 def test_begin_nested_context(self):77 root = self.builder.current_context()78 self.builder.begin_context('testsuite', 'name')79 self.assertEqual(self.builder.context_tag(), 'testsuite')80 self.assertIs(self.builder.current_context().parent, root)81 def test_end_inexistent_context(self):82 self.builder = builder.TestXMLBuilder()83 self.assertFalse(self.builder.end_context())84 self.assertEqual(len(self.doc.childNodes), 0)85 def test_end_root_context(self):86 root = self.builder.current_context()87 self.assertTrue(self.builder.end_context())88 self.assertIsNone(self.builder.current_context())89 # No contexts left90 self.assertFalse(self.builder.end_context())91 doc_children = self.doc.childNodes92 self.assertEqual(len(doc_children), 1)93 self.assertEqual(len(doc_children[0].childNodes), 0)94 self.assertEqual(doc_children[0].tagName, root.element_tag())95 def test_end_nested_context(self):96 self.builder.begin_context('testsuite', 'name')97 self.builder.current_context()98 self.assertTrue(self.builder.end_context())99 # Only updates the document when all contexts end100 self.assertEqual(len(self.doc.childNodes), 0)101 def test_end_all_context_stack(self):102 root = self.builder.current_context()103 self.builder.begin_context('testsuite', 'name')104 nested = self.builder.current_context()105 self.assertTrue(self.builder.end_context())106 self.assertTrue(self.builder.end_context())107 # No contexts left108 self.assertFalse(self.builder.end_context())109 root_child = self.doc.childNodes110 self.assertEqual(len(root_child), 1)111 self.assertEqual(root_child[0].tagName, root.element_tag())112 nested_child = root_child[0].childNodes113 self.assertEqual(len(nested_child), 1)114 self.assertEqual(nested_child[0].tagName, nested.element_tag())115 def test_append_valid_unicode_cdata_section(self):116 self.builder.append_cdata_section('tag', self.valid_chars)117 self.builder.end_context()118 root_child = self.doc.childNodes[0]119 cdata_container = root_child.childNodes[0]120 self.assertEqual(cdata_container.tagName, 'tag')121 cdata = cdata_container.childNodes[0]122 self.assertEqual(cdata.data, self.valid_chars)123 def test_append_invalid_unicode_cdata_section(self):124 self.builder.append_cdata_section('tag', self.invalid_chars)125 self.builder.end_context()126 root_child = self.doc.childNodes[0]127 cdata_container = root_child.childNodes[0]128 cdata = cdata_container.childNodes[0]129 self.assertEqual(cdata.data, self.invalid_chars_replace)130 def test_append_cdata_closing_tags_into_cdata_section(self):131 self.builder.append_cdata_section('tag', ']]>')132 self.builder.end_context()133 root_child = self.doc.childNodes[0]134 cdata_container = root_child.childNodes[0]135 self.assertEqual(len(cdata_container.childNodes), 2)136 self.assertEqual(cdata_container.childNodes[0].data, ']]')137 self.assertEqual(cdata_container.childNodes[1].data, '>')138 def test_append_tag_with_valid_unicode_values(self):139 self.builder.append('tag', self.valid_chars, attr=self.valid_chars)140 self.builder.end_context()141 root_child = self.doc.childNodes[0]142 tag = root_child.childNodes[0]143 self.assertEqual(tag.tagName, 'tag')144 self.assertEqual(tag.getAttribute('attr'), self.valid_chars)145 self.assertEqual(tag.childNodes[0].data, self.valid_chars)146 def test_append_tag_with_invalid_unicode_values(self):147 self.builder.append('tag', self.invalid_chars, attr=self.invalid_chars)148 self.builder.end_context()149 root_child = self.doc.childNodes[0]150 tag = root_child.childNodes[0]151 self.assertEqual(tag.tagName, 'tag')152 self.assertEqual(tag.getAttribute('attr'), self.invalid_chars_replace)153 self.assertEqual(tag.childNodes[0].data, self.invalid_chars_replace)154 def test_increment_root_context_counter(self):155 self.builder.increment_counter('tests')156 self.builder.end_context()157 root_child = self.doc.childNodes[0]158 self.assertEqual(root_child.tagName, 'testsuites')159 self.assertEqual(root_child.getAttribute('tests'), '1')160 def test_increment_nested_context_counter(self):161 self.builder.increment_counter('tests')162 self.builder.begin_context('testsuite', 'name')163 self.builder.increment_counter('tests')164 self.builder.end_context()165 self.builder.end_context()166 root_child = self.doc.childNodes[0]167 nested_child = root_child.childNodes[0]168 self.assertEqual(root_child.tagName, 'testsuites')169 self.assertEqual(nested_child.getAttribute('tests'), '1')170 self.assertEqual(root_child.getAttribute('tests'), '2')171 def test_finish_nested_context(self):172 self.builder.begin_context('testsuite', 'name')173 tree = ET.fromstring(self.builder.finish())174 self.assertEqual(tree.tag, 'testsuites')...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!