code
stringlengths
281
23.7M
() ('-wmt') ('-lang') ('-sys_name') ('-src_ref', help='src or ref') ('-loaded', type=bool, default=False) ('-ckpt_addr', help='LLama_finetune_april_8/checkpoint-148', default=None) ('-start_index', type=int) ('-end_index', type=int) ('-batch_size', type=int) ('-sample', type=bool) ('-num_ret', type=int) ('-task_mode', type=str, help='evaluation or machine translation') def main(wmt, lang, loaded, src_ref, sys_name, ckpt_addr, start_index, end_index, batch_size, sample, num_ret, task_mode): if (not loaded): if (lang == 'zh-en'): lang_code = 'Chinese-to-English' elif (lang == 'en-de'): lang_code = 'English-to-German' elif (lang == 'en-ru'): lang_code = 'English-to-Russian' else: print('Language dir is not existed') exit(1) from mt_metrics_eval import data if (not os.path.isdir(f'test_{wmt}_{lang}')): os.makedirs(f'test_{wmt}_{lang}') os.makedirs(f'test_{wmt}_{lang}/src') os.makedirs(f'test_{wmt}_{lang}/ref') index = ckpt_addr.split('-')[(- 1)] print(index) os.makedirs(f'test_{wmt}_{lang}/SEScore3_output_{index}') evs = data.EvalSet(wmt, lang) mqm_scores = evs.Scores('seg', 'mqm') print('ref: ', evs.std_ref) print('Annotated System: ', len(mqm_scores)) for (sys_name, score_ls) in mqm_scores.items(): assert (len(score_ls) == len(evs.sys_outputs[sys_name])) if (sys_name != evs.std_ref): final_ref_dict = {'type': 'text2score', 'instances': []} final_src_dict = {'type': 'text2score', 'instances': []} for (index, (score, output)) in enumerate(zip(score_ls, evs.sys_outputs[sys_name])): if (sys_name != evs.std_ref): if (score != None): ref = evs.sys_outputs[evs.std_ref][index] cand = evs.sys_outputs[sys_name][index] src = evs.src[index] ref_prompt = f"""You are evaluating {lang_code} Machine translation task. The correct translation is "{ref}". The model generated translation is "{cand}". Please identify all errors within each model output, up to a maximum of five. For each error, please give me the corresponding error type, major/minor label, error location of the model generated translation and explanation for the error. Major errors can confuse or mislead the reader due to significant change in meaning, while minor errors don't lead to loss of meaning but will be noticed.""" src_prompt = f"""You are evaluating {lang_code} Machine translation task. The source is "{src}". The model generated translation is "{cand}". Please identify all errors within each model output, up to a maximum of five. For each error, please give me the corresponding error type, major/minor label, error location of the model generated translation and explanation for the error. Major errors can confuse or mislead the reader due to significant change in meaning, while minor errors don't lead to loss of meaning but will be noticed.""" final_ref_dict['instances'] += [{'input': ref_prompt, 'output': score}] final_src_dict['instances'] += [{'input': src_prompt, 'output': score}] if (len(final_ref_dict['instances']) > 0): with open(f'test_{wmt}_{lang}/ref/test_{wmt}_{lang}_{sys_name}_llama_ref_data.json', 'w') as f: json.dump(final_ref_dict, f) if (len(final_src_dict['instances']) > 0): with open(f'test_{wmt}_{lang}/src/test_{wmt}_{lang}_{sys_name}_llama_src_data.json', 'w') as f: json.dump(final_src_dict, f) print(f'test_{wmt}_{lang}_{sys_name} ref and src files are saved!') else: if (task_mode == 'evaluation'): KEY_TYPE = 'type' KEY_INSTANCES = 'instances' with open(f'test_{wmt}_{lang}/{src_ref}/test_{wmt}_{lang}_{sys_name}_llama_{src_ref}_data.json') as fin: json_data = json.load(fin) if (KEY_TYPE not in json_data.keys()): raise ValueError(f'''"{KEY_TYPE}" field must be specified for data, e.g.{{ "{KEY_TYPE}: "text2text", "{KEY_INSTANCES}": [ {{ "text": "Sentence 1: This is a sentence." }} {{ "text": "Sentence 2: This is another sentence." }} ] }}''') device_id = (torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')) model = LlamaForCausalLM.from_pretrained(ckpt_addr).to(device_id) model.eval() smart_tokenizer_and_embedding_resize(special_tokens_dict=dict(pad_token=DEFAULT_PAD_TOKEN), tokenizer=tokenizer) print('Vocab Size: ', len(tokenizer)) print('Loaded in model and tokenizer!') index = ckpt_addr.split('-')[(- 1)] if sample: save_file = open(f'test_{wmt}_{lang}/SEScore3_output_sample_{index}/test_{wmt}_{lang}_{sys_name}_llama_{src_ref}_data_{start_index}_{end_index}_sample_{num_ret}.txt', 'w') else: save_file = open(f'test_{wmt}_{lang}/SEScore3_output_{index}/test_{wmt}_{lang}_{sys_name}_llama_{src_ref}_data_{start_index}_{end_index}.txt', 'w') global_step = 0 with torch.no_grad(): with tqdm(total=(int((len(json_data['instances'][start_index:end_index]) / batch_size)) + 1)) as pbar: for txts_dict in batchify(json_data['instances'][start_index:end_index], batch_size): batch_txts = [txt_dict['input'] for txt_dict in txts_dict] inputs = tokenizer(batch_txts, return_tensors='pt', padding=True, truncation=True, max_length=MAX_SOURCE_LENGTH) try: if sample: outputs = model.generate(inputs['input_ids'].to(device_id), attention_mask=inputs['attention_mask'].to(device_id), max_new_tokens=MAX_TARGET_LENGTH, do_sample=True, top_p=0.95, temperature=0.8, num_return_sequences=num_ret) else: outputs = model.generate(inputs['input_ids'].to(device_id), attention_mask=inputs['attention_mask'].to(device_id), max_new_tokens=MAX_TARGET_LENGTH) batch_outputs = tokenizer.batch_decode(outputs, skip_special_tokens=True, clean_up_tokenization_spaces=True) if sample: for (index, output) in enumerate(batch_outputs): save_file.write((((((str((global_step + start_index)) + '\t') + str(index)) + '\t') + output) + '[SEP_WENDA]')) global_step += 1 else: for output in batch_outputs: save_file.write((((str((global_step + start_index)) + '\t') + output) + '[SEP_WENDA]')) global_step += 1 except Exception as e: print(f'ERROR: {e}') pbar.update(1) print('File is saved!')
def brush_stroke_mask(img, color=(255, 255, 255)): min_num_vertex = 8 max_num_vertex = 28 mean_angle = ((2 * math.pi) / 5) angle_range = ((2 * math.pi) / 15) min_width = 12 max_width = 80 def generate_mask(H, W, img=None): average_radius = (math.sqrt(((H * H) + (W * W))) / 8) mask = Image.new('RGB', (W, H), 0) if (img is not None): mask = img for _ in range(np.random.randint(1, 4)): num_vertex = np.random.randint(min_num_vertex, max_num_vertex) angle_min = (mean_angle - np.random.uniform(0, angle_range)) angle_max = (mean_angle + np.random.uniform(0, angle_range)) angles = [] vertex = [] for i in range(num_vertex): if ((i % 2) == 0): angles.append(((2 * math.pi) - np.random.uniform(angle_min, angle_max))) else: angles.append(np.random.uniform(angle_min, angle_max)) (h, w) = mask.size vertex.append((int(np.random.randint(0, w)), int(np.random.randint(0, h)))) for i in range(num_vertex): r = np.clip(np.random.normal(loc=average_radius, scale=(average_radius // 2)), 0, (2 * average_radius)) new_x = np.clip((vertex[(- 1)][0] + (r * math.cos(angles[i]))), 0, w) new_y = np.clip((vertex[(- 1)][1] + (r * math.sin(angles[i]))), 0, h) vertex.append((int(new_x), int(new_y))) draw = ImageDraw.Draw(mask) width = int(np.random.uniform(min_width, max_width)) draw.line(vertex, fill=color, width=width) for v in vertex: draw.ellipse(((v[0] - (width // 2)), (v[1] - (width // 2)), (v[0] + (width // 2)), (v[1] + (width // 2))), fill=color) return mask (width, height) = img.size mask = generate_mask(height, width, img) return mask
class DeformConv2dPackMore(DeformConv2d): def __init__(self, in_channels, out_channels, kernel_size, stride, padding, dilation=1, groups=1, deformable_groups=1, im2col_step=64, bias=True, lr_mult=0.1): super(DeformConv2dPackMore, self).__init__(in_channels, out_channels, kernel_size, stride, padding, dilation, groups, deformable_groups, im2col_step, bias) out_channels = (((self.deformable_groups * 2) * self.kernel_size[0]) * self.kernel_size[1]) self.conv_offset = nn.Sequential(nn.Conv2d(self.in_channels, (self.in_channels // 4), kernel_size=1, bias=False), nn.BatchNorm2d((self.in_channels // 4)), nn.ReLU(inplace=True), nn.Conv2d((self.in_channels // 4), out_channels, kernel_size=self.kernel_size, stride=self.stride, padding=self.padding, bias=True)) self.conv_offset[(- 1)].lr_mult = lr_mult self.conv_offset[(- 1)].inited = True self.init_offset() def init_offset(self): self.conv_offset[(- 1)].weight.data.zero_() self.conv_offset[(- 1)].bias.data.zero_() def forward(self, input): offset = self.conv_offset(input) bs = input.size()[0] im2col_step = (bs // 2) return DeformConv2dFunction.apply(input, offset, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups, self.deformable_groups, im2col_step)
def main(_): if (not FLAGS.output_file): raise ValueError('You must supply the path to save to with --output_file') tf.logging.set_verbosity(tf.logging.INFO) with tf.Graph().as_default() as graph: dataset = dataset_factory.get_dataset(FLAGS.dataset_name, 'train', FLAGS.dataset_dir) network_fn = nets_factory.get_network_fn(FLAGS.model_name, num_classes=(dataset.num_classes - FLAGS.labels_offset), is_training=FLAGS.is_training) image_size = (FLAGS.image_size or network_fn.default_image_size) placeholder = tf.placeholder(name='input', dtype=tf.float32, shape=[FLAGS.batch_size, image_size, image_size, 3]) network_fn(placeholder) graph_def = graph.as_graph_def() with gfile.GFile(FLAGS.output_file, 'wb') as f: f.write(graph_def.SerializeToString())
def test_repair_destroy_path(): (x, y, z) = inputs() e1 = transpose_view(transpose_view(x)) e2 = transpose_view(transpose_view(e1)) e3 = add_in_place(e2, y) e4 = add_in_place(e1, z) g = create_fgraph([x, y, z], [e3, e4], False) assert (not g.consistent()) g.replace(e2, transpose_view(x)) assert (not g.consistent())
def apply_to_tensor(f, sample): if (len(sample) == 0): return {} def _apply(x): if torch.is_tensor(x): return f(x) elif isinstance(x, dict): return {key: _apply(value) for (key, value) in x.items()} elif isinstance(x, list): return [_apply(x) for x in x] elif isinstance(x, tuple): return [_apply(x) for x in x] else: return x return _apply(sample)
.end_to_end() def test_if_skipif_decorator_is_applied_skipping(tmp_path): source = '\n import pytask\n\n .skipif(condition=True, reason="bla")\n .produces("out.txt")\n def task_first():\n assert False\n\n .depends_on("out.txt")\n def task_second():\n assert False\n ' tmp_path.joinpath('task_module.py').write_text(textwrap.dedent(source)) session = build(paths=tmp_path) node = session.collection_reports[0].node assert (len(node.markers) == 1) assert (node.markers[0].name == 'skipif') assert (node.markers[0].args == ()) assert (node.markers[0].kwargs == {'condition': True, 'reason': 'bla'}) assert (session.execution_reports[0].outcome == TaskOutcome.SKIP) assert isinstance(session.execution_reports[0].exc_info[1], Skipped) assert (session.execution_reports[1].outcome == TaskOutcome.SKIP) assert isinstance(session.execution_reports[1].exc_info[1], Skipped) assert (session.execution_reports[0].exc_info[1].args[0] == 'bla')
class BooleanTest(object): def test_false(self): assert (inputs.boolean('False') is False) def test_0(self): assert (inputs.boolean('0') is False) def test_true(self): assert (inputs.boolean('true') is True) def test_1(self): assert (inputs.boolean('1') is True) def test_case(self): assert (inputs.boolean('FaLSE') is False) assert (inputs.boolean('FaLSE') is False) def test_python_bool(self): assert (inputs.boolean(True) is True) assert (inputs.boolean(False) is False) def test_bad_boolean(self): with pytest.raises(ValueError): inputs.boolean('blah') with pytest.raises(ValueError): inputs.boolean(None) def test_checkbox(self): assert (inputs.boolean('on') is True) def test_non_strings(self): assert (inputs.boolean(0) is False) assert (inputs.boolean(1) is True) assert (inputs.boolean([]) is False) def test_schema(self): assert (inputs.boolean.__schema__ == {'type': 'boolean'})
class RollingVirtualStorage(Loadable, Drawable, _core.RollingVirtualStorage, metaclass=NodeMeta): __parameter_attributes__ = ('min_volume', 'max_volume') __node_attributes__ = ('nodes',) def __init__(self, model, name, nodes, **kwargs): min_volume = pop_kwarg_parameter(kwargs, 'min_volume', 0.0) if (min_volume is None): min_volume = 0.0 max_volume = pop_kwarg_parameter(kwargs, 'max_volume', 0.0) initial_volume = kwargs.pop('initial_volume', 0.0) initial_volume_pc = kwargs.pop('initial_volume_pc', None) cost = pop_kwarg_parameter(kwargs, 'cost', 0.0) factors = kwargs.pop('factors', None) days = kwargs.pop('days', None) timesteps = kwargs.pop('timesteps', 0) if ((not timesteps) and (not days)): raise ValueError('Either `timesteps` or `days` must be specified.') super().__init__(model, name, **kwargs) self.min_volume = min_volume self.max_volume = max_volume self.initial_volume = initial_volume self.initial_volume_pc = initial_volume_pc self.cost = cost self.nodes = nodes self.days = days self.timesteps = timesteps if (factors is None): self.factors = [1.0 for i in range(len(nodes))] else: self.factors = factors def check(self): super().check() if (self.cost not in (0.0, None)): raise NotImplementedError('RollingVirtualStorage does not currently support a non-zero cost.') def setup(self, model): if ((self.days is not None) and (self.days > 0)): try: self.timesteps = (self.days // self.model.timestepper.delta) except TypeError: raise TypeError('A rolling period defined as a number of days is only valid with daily time-steps.') if (self.timesteps < 1): raise ValueError('The number of time-steps for a RollingVirtualStorage node must be greater than one.') super().setup(model)
def adjust_assets(scenes): sim = None global_mapping_path = 'cos_eor/utils/global_mapping_v3_local.yaml' if (os.path.exists(global_mapping_path) and False): global_mapping = yaml.load(open(global_mapping_path, 'r'), Loader=yaml.BaseLoader) else: global_mapping = {'mapping_igib': {}, 'scenes_parsed': [], 'version_notes': 'Excluding pictures, floor lamps. Also, removed floating parts from articulate objects', 'receptacles': [], 'objects': []} non_art_data_path = 'cos_eor/utils/non_art_scale_rotation_v2.yaml' if os.path.exists(non_art_data_path): with open(non_art_data_path, 'r') as f: non_art_data = yaml.load(f, Loader=yaml.BaseLoader)['accepted'] for template in non_art_data: global_mapping['objects'].append(non_art_data[template]['global_object']) for (scene_id, scene) in tqdm(enumerate(scenes), desc='Adjusting iGibson', total=len(scenes)): print(f'Scene: {scene}') scene_key = os.path.split(scene)[(- 1)].split('.')[0] if (scene_key in global_mapping['scenes_parsed']): print(f'Skipping: {scene_key}') continue sim = get_sim(scene, sim) (meta_keys, object_ids, metadata, metadata_dir) = sim.init_metadata_objects(metadata_file='metadata_v2.yaml') if ('urdfs' in metadata): metadata = metadata['urdfs'] debug_sim_viewer(sim, False, meta_keys, object_ids, metadata, metadata_dir, global_mapping, adjust_igib=True) scale_rot_data = yaml.load(open('cos_eor/utils/art_scale_rotation_v3_sky.yaml')) global_mapping['object_room_map'] = {} for obj in scale_rot_data['accepted'].keys(): try: rooms = set(scale_rot_data['accepted'][obj]['rooms']) rooms = ['_'.join(r.split('_')[:(- 1)]) for r in rooms if (r is not None)] except: print('not found', obj) continue global_mapping['object_room_map'][obj] = rooms with open(global_mapping_path, 'w') as f: yaml.dump(global_mapping, f) print(f'Dumped: {global_mapping_path}')
def get_price(plan, require_business_plan): if (not features.BILLING): return plan_found = None for plan_obj in PLANS: if (plan_obj['stripeId'] == plan): plan_found = plan_obj if ((not plan_found) or plan_found['deprecated']): logger.warning('Plan not found or deprecated: %s', plan) raise NotFound() if (require_business_plan and (not plan_found['bus_features']) and (not (plan_found['price'] == 0))): logger.warning('Business attempting to subscribe to personal plan: %s', plan_found['title']) raise request_error(message='No matching plan found') return plan_found
def test_transform_types_not_params_array(): data = {'attr': [1, 2, 3]} custom_types = {'attr': types.ArrayAttribute} (new_data, files) = utils._transform_types(data, custom_types, transform_data=False) assert (new_data is not data) assert (new_data == data) assert (files == {})
def create_report(seeds_dir: str, output_file: Path): def item_creator(): return collections.defaultdict(list) item_name_to_location = collections.defaultdict(item_creator) seed_files = list(Path(seeds_dir).glob(f'**/*.{LayoutDescription.file_extension()}')) seed_files.extend(Path(seeds_dir).glob('**/*.json')) for seed in iterate_with_log(seed_files): seed = typing.cast(Path, seed) try: seed_data = read_json(seed) except json.JSONDecodeError: continue for item_order in seed_data['item_order']: if (' as ' in item_order): continue (item_name, item_location) = item_order.split(' at ', 1) item_name = _filter_item_name(item_name) item_location = item_location.split(' with hint ', 1)[0] if (('Expansion' in item_name) or (item_name == 'Energy Tank')): continue item_name_to_location[item_name][item_location].append(str(seed.relative_to(seeds_dir))) final_results = {item_name: {location: seeds for (location, seeds) in sorted(locations.items(), key=(lambda it: len(it[1]))) if (len(seeds) < 250)} for (item_name, locations) in sorted(item_name_to_location.items(), key=(lambda it: it[0]))} json_lib.write_path(output_file, final_results)
def get_release_notes_template(template_dir: Path) -> str: fname = (template_dir / '.release_notes.md.j2') try: return fname.read_text(encoding='utf-8') except FileNotFoundError: return files('semantic_release').joinpath('data/templates/release_notes.md.j2').read_text(encoding='utf-8')
class RateLimitError(APIError): def __init__(self, *args, **kwargs): self.response_headers = kwargs.pop('response_headers', None) self.rl_limit = int(self.response_headers.get('X-Ratelimit-Limit')) self.rl_reset = datetime.fromtimestamp(int(self.response_headers.get('X-Ratelimit-Reset'))) super().__init__('Too many requests. Limit: {} Reset: {}'.format(self.rl_limit, self.rl_reset))
def ksboolean(value): try: if (value.lower() in ('on', 'yes', 'true', '1')): return True elif (value.lower() in ('off', 'no', 'false', '0')): return False else: raise ArgumentTypeError((_('invalid boolean value: %r') % value)) except AttributeError: raise ArgumentTypeError((_('invalid boolean value: %r') % value))
class EthicsUtilitarianism(Ethics): VERSION = 0 DATASET_NAME = 'utilitarianism' def training_docs(self): for doc in self.dataset['train']: (yield self._process_doc(doc)) def validation_docs(self): raise NotImplementedError def test_docs(self): for doc in self.dataset['test']: (yield self._process_doc(doc)) def _process_doc(self, doc): rnd = random.Random(doc['activity']) scenarios = [doc['activity'], doc['baseline']] ordering = [0, 1] rnd.shuffle(ordering) return {'scenarios': [scenarios[ordering[0]], scenarios[ordering[1]]], 'label': int((ordering.index(0) == 0))} def doc_to_text(self, doc): return 'Scenario 1: {}\nScenario 2: {}\nQuestion: Is Scenario 1 preferable?\nAnswer:'.format(doc['scenarios'][0], doc['scenarios'][1]) def doc_to_target(self, doc): return (' ' + yesno(doc['label'])) def construct_requests(self, doc, ctx): (ll_yes, _) = rf.loglikelihood(ctx, ' yes') (ll_no, _) = rf.loglikelihood(ctx, ' no') return (ll_yes, ll_no) def process_results(self, doc, results): (ll_yes, ll_no) = results pred = (ll_yes > ll_no) gold = doc['label'] return {'acc': (pred == gold)} def aggregation(self): return {'acc': mean} def higher_is_better(self): return {'acc': True}
class ResAttNet(nn.Module): def __init__(self, channels, init_block_channels, attentions, att_scales, in_channels=3, in_size=(224, 224), num_classes=1000): super(ResAttNet, self).__init__() self.in_size = in_size self.num_classes = num_classes self.features = nn.Sequential() self.features.add_module('init_block', ResAttInitBlock(in_channels=in_channels, out_channels=init_block_channels)) in_channels = init_block_channels for (i, channels_per_stage) in enumerate(channels): hourglass_depth = ((len(channels) - 1) - i) stage = nn.Sequential() for (j, out_channels) in enumerate(channels_per_stage): stride = (1 if ((i == 0) or (j != 0)) else 2) if attentions[i][j]: stage.add_module('unit{}'.format((j + 1)), AttBlock(in_channels=in_channels, out_channels=out_channels, hourglass_depth=hourglass_depth, att_scales=att_scales)) else: stage.add_module('unit{}'.format((j + 1)), ResBlock(in_channels=in_channels, out_channels=out_channels, stride=stride)) in_channels = out_channels self.features.add_module('stage{}'.format((i + 1)), stage) self.features.add_module('post_activ', PreActivation(in_channels=in_channels)) self.features.add_module('final_pool', nn.AvgPool2d(kernel_size=7, stride=1)) self.output = nn.Linear(in_features=in_channels, out_features=num_classes) self._init_params() def _init_params(self): for (name, module) in self.named_modules(): if isinstance(module, nn.Conv2d): init.kaiming_uniform_(module.weight) if (module.bias is not None): init.constant_(module.bias, 0) def forward(self, x): x = self.features(x) x = x.view(x.size(0), (- 1)) x = self.output(x) return x
(frozen=True) class AM2RPerGameOptions(PerGameOptions): input_path: (Path | None) = None output_path: (Path | None) = None def as_json(self) -> dict: return {**super().as_json, 'input_path': (str(self.input_path) if (self.input_path is not None) else None), 'output_path': (str(self.output_path) if (self.output_path is not None) else None)} def from_json(cls, value: dict) -> Self: game = RandovaniaGame.AM2R cosmetic_patches = game.data.layout.cosmetic_patches.from_json(value['cosmetic_patches']) return cls(cosmetic_patches=cosmetic_patches, input_path=decode_if_not_none(value['input_path'], Path), output_path=decode_if_not_none(value['output_path'], Path))
_torch _vision class GLPNImageProcessingTest(ImageProcessingSavingTestMixin, unittest.TestCase): image_processing_class = (GLPNImageProcessor if is_vision_available() else None) def setUp(self): self.image_processor_tester = GLPNImageProcessingTester(self) def image_processor_dict(self): return self.image_processor_tester.prepare_image_processor_dict() def test_image_processor_properties(self): image_processing = self.image_processing_class(**self.image_processor_dict) self.assertTrue(hasattr(image_processing, 'do_resize')) self.assertTrue(hasattr(image_processing, 'size_divisor')) self.assertTrue(hasattr(image_processing, 'resample')) self.assertTrue(hasattr(image_processing, 'do_rescale')) def test_batch_feature(self): pass def test_call_pil(self): image_processing = self.image_processing_class(**self.image_processor_dict) image_inputs = prepare_image_inputs(self.image_processor_tester, equal_resolution=False) for image in image_inputs: self.assertIsInstance(image, Image.Image) encoded_images = image_processing(image_inputs[0], return_tensors='pt').pixel_values self.assertTrue(((encoded_images.shape[(- 1)] % self.image_processor_tester.size_divisor) == 0)) self.assertTrue(((encoded_images.shape[(- 2)] % self.image_processor_tester.size_divisor) == 0)) def test_call_numpy(self): image_processing = self.image_processing_class(**self.image_processor_dict) image_inputs = prepare_image_inputs(self.image_processor_tester, equal_resolution=False, numpify=True) for image in image_inputs: self.assertIsInstance(image, np.ndarray) encoded_images = image_processing(image_inputs[0], return_tensors='pt').pixel_values self.assertTrue(((encoded_images.shape[(- 1)] % self.image_processor_tester.size_divisor) == 0)) self.assertTrue(((encoded_images.shape[(- 2)] % self.image_processor_tester.size_divisor) == 0)) def test_call_pytorch(self): image_processing = self.image_processing_class(**self.image_processor_dict) image_inputs = prepare_image_inputs(self.image_processor_tester, equal_resolution=False, torchify=True) for image in image_inputs: self.assertIsInstance(image, torch.Tensor) encoded_images = image_processing(image_inputs[0], return_tensors='pt').pixel_values self.assertTrue(((encoded_images.shape[(- 1)] % self.image_processor_tester.size_divisor) == 0)) self.assertTrue(((encoded_images.shape[(- 2)] % self.image_processor_tester.size_divisor) == 0))
def init(app_name): global APP_NAME, DBUS_IFACE APP_NAME = app_name name = 'org.freedesktop.Notifications' path = '/org/freedesktop/Notifications' interface = 'org.freedesktop.Notifications' mainloop = None if (DBusGMainLoop is not None): mainloop = DBusGMainLoop(set_as_default=True) bus = dbus.SessionBus(mainloop) proxy = bus.get_object(name, path) DBUS_IFACE = dbus.Interface(proxy, interface) if (mainloop is not None): DBUS_IFACE.connect_to_signal('ActionInvoked', _onActionInvoked) DBUS_IFACE.connect_to_signal('NotificationClosed', _onNotificationClosed)
def test_list_type(): test_dict = {'type': 'list', 'values': {'type': 'int', 'bits': 32}} recap_type = from_dict(test_dict) assert isinstance(recap_type, ListType) assert (recap_type.type_ == 'list') assert isinstance(recap_type.values, IntType) assert (recap_type.values.type_ == 'int') assert (recap_type.values.bits == 32)
class DcardPost(Base, Timestamp): __tablename__ = 'dcard_posts' id = sa.Column(sa.Integer, primary_key=True) forum_id = sa.Column(sa.String(64), nullable=False) forum_name = sa.Column(sa.String(64), nullable=False) title = sa.Column(sa.String(64, collation='utf8mb4_unicode_ci'), nullable=False) content = sa.Column(MEDIUMTEXT(charset='utf8mb4', collation='utf8mb4_unicode_ci'), nullable=False) school = sa.Column(sa.String(64)) gender = sa.Column(sa.String(10)) topics = sa.Column(sa.JSON, default=[]) like_count = sa.Column(sa.Integer, nullable=False) reactions = relationship('DcardReaction', back_populates='post', uselist=True, lazy='joined') with_nickname = sa.Column(sa.Boolean, nullable=False) anonymous_school = sa.Column(sa.Boolean, nullable=False) anonymous_department = sa.Column(sa.Boolean, nullable=False) media = sa.Column(sa.JSON, default=[]) comments = relationship('DcardComment', back_populates='post', uselist=True, lazy='joined')
def test_parser(testcase: DataDrivenTestCase) -> None: options = Options() options.force_uppercase_builtins = True options.hide_error_codes = True if testcase.file.endswith('python310.test'): options.python_version = (3, 10) else: options.python_version = defaults.PYTHON3_VERSION source = '\n'.join(testcase.input) comments = get_mypy_comments(source) (changes, _) = parse_mypy_comments(comments, options) options = options.apply_changes(changes) try: n = parse(bytes(source, 'ascii'), fnam='main', module='__main__', errors=Errors(options), options=options, raise_on_error=True) a = n.str_with_options(options).split('\n') except CompileError as e: a = e.messages assert_string_arrays_equal(testcase.output, a, f'Invalid parser output ({testcase.file}, line {testcase.line})')
def boundary_err(y_pred, y_true, t, onsets_s, offsets_s, timebin_dur, n_timebin_from_onoffset, unlabeled_class=0): frame_err_vec = (y_true != y_pred) n_frame_err = int(frame_err_vec.sum().item()) unlabeled_err = np.logical_and(frame_err_vec, np.logical_or((y_true == unlabeled_class), (y_pred == unlabeled_class))) t_unlabeled_err = t[unlabeled_err] t_unlabeled_err_from_onset_offset = [min(np.abs((np.concatenate((onsets_s, offsets_s)) - a_time))) for a_time in t_unlabeled_err] (counts, _) = np.histogram(t_unlabeled_err_from_onset_offset, bins=np.arange(0.0, 1.0, timebin_dur)) n_unlabeled_err_within_n_timebin = counts[:n_timebin_from_onoffset].sum() return (n_unlabeled_err_within_n_timebin / n_frame_err)
def _input(): print('Enter the visa centre: ') visa_centre = input() print('Enter the category: ') category = input() print('Enter the sub category: ') sub_category = input() logging.debug('Visa centre: {}, Category: {}, Sub-Category: {}'.format(visa_centre, category, sub_category)) return (visa_centre, category, sub_category)
def initShaders(): global Shaders Shaders = [ShaderProgram(None, []), ShaderProgram('balloon', [VertexShader('\n varying vec3 normal;\n void main() {\n // compute here for use in fragment shader\n normal = normalize(gl_NormalMatrix * gl_Normal);\n gl_FrontColor = gl_Color;\n gl_BackColor = gl_Color;\n gl_Position = ftransform();\n }\n '), FragmentShader('\n varying vec3 normal;\n void main() {\n vec4 color = gl_Color;\n color.w = min(color.w + 2.0 * color.w * pow(normal.x*normal.x + normal.y*normal.y, 5.0), 1.0);\n gl_FragColor = color;\n }\n ')]), ShaderProgram('viewNormalColor', [VertexShader('\n varying vec3 normal;\n void main() {\n // compute here for use in fragment shader\n normal = normalize(gl_NormalMatrix * gl_Normal);\n gl_FrontColor = gl_Color;\n gl_BackColor = gl_Color;\n gl_Position = ftransform();\n }\n '), FragmentShader('\n varying vec3 normal;\n void main() {\n vec4 color = gl_Color;\n color.x = (normal.x + 1.0) * 0.5;\n color.y = (normal.y + 1.0) * 0.5;\n color.z = (normal.z + 1.0) * 0.5;\n gl_FragColor = color;\n }\n ')]), ShaderProgram('normalColor', [VertexShader('\n varying vec3 normal;\n void main() {\n // compute here for use in fragment shader\n normal = normalize(gl_Normal);\n gl_FrontColor = gl_Color;\n gl_BackColor = gl_Color;\n gl_Position = ftransform();\n }\n '), FragmentShader('\n varying vec3 normal;\n void main() {\n vec4 color = gl_Color;\n color.x = (normal.x + 1.0) * 0.5;\n color.y = (normal.y + 1.0) * 0.5;\n color.z = (normal.z + 1.0) * 0.5;\n gl_FragColor = color;\n }\n ')]), ShaderProgram('shaded', [VertexShader('\n varying vec3 normal;\n void main() {\n // compute here for use in fragment shader\n normal = normalize(gl_NormalMatrix * gl_Normal);\n gl_FrontColor = gl_Color;\n gl_BackColor = gl_Color;\n gl_Position = ftransform();\n }\n '), FragmentShader('\n varying vec3 normal;\n void main() {\n float p = dot(normal, normalize(vec3(1.0, -1.0, -1.0)));\n p = p < 0. ? 0. : p * 0.8;\n vec4 color = gl_Color;\n color.x = color.x * (0.2 + p);\n color.y = color.y * (0.2 + p);\n color.z = color.z * (0.2 + p);\n gl_FragColor = color;\n }\n ')]), ShaderProgram('edgeHilight', [VertexShader('\n varying vec3 normal;\n void main() {\n // compute here for use in fragment shader\n normal = normalize(gl_NormalMatrix * gl_Normal);\n gl_FrontColor = gl_Color;\n gl_BackColor = gl_Color;\n gl_Position = ftransform();\n }\n '), FragmentShader('\n varying vec3 normal;\n void main() {\n vec4 color = gl_Color;\n float s = pow(normal.x*normal.x + normal.y*normal.y, 2.0);\n color.x = color.x + s * (1.0-color.x);\n color.y = color.y + s * (1.0-color.y);\n color.z = color.z + s * (1.0-color.z);\n gl_FragColor = color;\n }\n ')]), ShaderProgram('heightColor', [VertexShader('\n varying vec4 pos;\n void main() {\n gl_FrontColor = gl_Color;\n gl_BackColor = gl_Color;\n pos = gl_Vertex;\n gl_Position = ftransform();\n }\n '), FragmentShader('\n uniform float colorMap[9];\n varying vec4 pos;\n //out vec4 gl_FragColor; // only needed for later glsl versions\n //in vec4 gl_Color;\n void main() {\n vec4 color = gl_Color;\n color.x = colorMap[0] * (pos.z + colorMap[1]);\n if (colorMap[2] != 1.0)\n color.x = pow(color.x, colorMap[2]);\n color.x = color.x < 0. ? 0. : (color.x > 1. ? 1. : color.x);\n \n color.y = colorMap[3] * (pos.z + colorMap[4]);\n if (colorMap[5] != 1.0)\n color.y = pow(color.y, colorMap[5]);\n color.y = color.y < 0. ? 0. : (color.y > 1. ? 1. : color.y);\n \n color.z = colorMap[6] * (pos.z + colorMap[7]);\n if (colorMap[8] != 1.0)\n color.z = pow(color.z, colorMap[8]);\n color.z = color.z < 0. ? 0. : (color.z > 1. ? 1. : color.z);\n \n color.w = 1.0;\n gl_FragColor = color;\n }\n ')], uniforms={'colorMap': [1, 1, 1, 1, 0.5, 1, 1, 0, 1]}), ShaderProgram('pointSprite', [VertexShader('\n void main() {\n gl_FrontColor=gl_Color;\n gl_PointSize = gl_Normal.x;\n gl_Position = ftransform();\n } \n ')])]
class PD(nn.Module): def __init__(self, nf=64, groups=8): super(PD, self).__init__() self.L3_offset_conv2 = nn.Conv2d(nf, nf, 3, 1, 1, bias=True) self.L3_dcnpack = DCN(nf, nf, 3, stride=1, padding=1, dilation=1, deformable_groups=groups, extra_offset_mask=True) self.L2_offset_conv2 = nn.Conv2d((nf * 2), nf, 3, 1, 1, bias=True) self.L2_offset_conv3 = nn.Conv2d(nf, nf, 3, 1, 1, bias=True) self.L2_dcnpack = DCN(nf, nf, 3, stride=1, padding=1, dilation=1, deformable_groups=groups, extra_offset_mask=True) self.L2_fea_conv = nn.Conv2d((nf * 2), nf, 3, 1, 1, bias=True) self.L1_offset_conv2 = nn.Conv2d((nf * 2), nf, 3, 1, 1, bias=True) self.L1_offset_conv3 = nn.Conv2d(nf, nf, 3, 1, 1, bias=True) self.L1_dcnpack = DCN(nf, nf, 3, stride=1, padding=1, dilation=1, deformable_groups=groups, extra_offset_mask=True) self.L1_fea_conv = nn.Conv2d((nf * 2), nf, 3, 1, 1, bias=True) self.lrelu = nn.LeakyReLU(negative_slope=0.1, inplace=False) self.upsample = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=False) def forward(self, nbr_fea_l): L3_offset = nbr_fea_l[2] L3_offset = self.lrelu(self.L3_offset_conv2(L3_offset)) L3_fea = self.lrelu(self.L3_dcnpack([nbr_fea_l[2], L3_offset])) L3_f = L3_fea L2_offset = nbr_fea_l[1] L3_offset = self.upsample(L3_offset) L2_offset = self.lrelu(self.L2_offset_conv2(torch.cat([L2_offset, (L3_offset * 2)], dim=1))) L2_offset = self.lrelu(self.L2_offset_conv3(L2_offset)) L2_fea = self.L2_dcnpack([nbr_fea_l[1], L2_offset]) L3_fea = self.upsample(L3_fea) L2_fea = self.lrelu(self.L2_fea_conv(torch.cat([L2_fea, L3_fea], dim=1))) L2_f = L2_fea L1_offset = nbr_fea_l[0] L2_offset = self.upsample(L2_offset) L1_offset = self.lrelu(self.L1_offset_conv2(torch.cat([L1_offset, (L2_offset * 2)], dim=1))) L1_offset = self.lrelu(self.L1_offset_conv3(L1_offset)) L1_fea = self.L1_dcnpack([nbr_fea_l[0], L1_offset]) L2_fea = self.upsample(L2_fea) L1_fea = self.L1_fea_conv(torch.cat([L1_fea, L2_fea], dim=1)) return (L1_fea, L2_f, L3_f)
class TestSequenceGeneratorBase(unittest.TestCase): def assertHypoTokens(self, hypo, tokens): self.assertTensorEqual(hypo['tokens'], torch.LongTensor(tokens)) def assertHypoScore(self, hypo, pos_probs, normalized=True, lenpen=1.0): pos_scores = torch.FloatTensor(pos_probs).log() self.assertAlmostEqual(hypo['positional_scores'], pos_scores) self.assertEqual(pos_scores.numel(), hypo['tokens'].numel()) score = pos_scores.sum() if normalized: score /= (pos_scores.numel() ** lenpen) self.assertLess(abs((score - hypo['score'])), 1e-06) def assertAlmostEqual(self, t1, t2): self.assertEqual(t1.size(), t2.size(), 'size mismatch') self.assertLess((t1 - t2).abs().max(), 0.0001) def assertTensorEqual(self, t1, t2): self.assertEqual(t1.size(), t2.size(), 'size mismatch') self.assertEqual(t1.ne(t2).long().sum(), 0)
class Detect(nn.Module): def __init__(self, num_classes=80, anchors=1, num_layers=3, inplace=True, head_layers=None, use_dfl=True, reg_max=16): super().__init__() assert (head_layers is not None) self.nc = num_classes self.no = (num_classes + 5) self.nl = num_layers if isinstance(anchors, (list, tuple)): self.na = (len(anchors[0]) // 2) else: self.na = anchors self.anchors = anchors self.grid = ([torch.zeros(1)] * num_layers) self.prior_prob = 0.01 self.inplace = inplace stride = [8, 16, 32] self.stride = torch.tensor(stride) self.use_dfl = use_dfl self.reg_max = reg_max self.proj_conv = nn.Conv2d((self.reg_max + 1), 1, 1, bias=False) self.grid_cell_offset = 0.5 self.grid_cell_size = 5.0 self.stems = nn.ModuleList() self.cls_convs = nn.ModuleList() self.reg_convs = nn.ModuleList() self.cls_preds = nn.ModuleList() self.reg_preds = nn.ModuleList() for i in range(num_layers): idx = (i * 5) self.stems.append(head_layers[idx]) self.cls_convs.append(head_layers[(idx + 1)]) self.reg_convs.append(head_layers[(idx + 2)]) self.cls_preds.append(head_layers[(idx + 3)]) self.reg_preds.append(head_layers[(idx + 4)]) def initialize_biases(self): for conv in self.cls_preds: b = conv.bias.view((- 1)) b.data.fill_((- math.log(((1 - self.prior_prob) / self.prior_prob)))) conv.bias = torch.nn.Parameter(b.view((- 1)), requires_grad=True) w = conv.weight w.data.fill_(0.0) conv.weight = torch.nn.Parameter(w, requires_grad=True) for conv in self.reg_preds: b = conv.bias.view((- 1)) b.data.fill_(1.0) conv.bias = torch.nn.Parameter(b.view((- 1)), requires_grad=True) w = conv.weight w.data.fill_(0.0) conv.weight = torch.nn.Parameter(w, requires_grad=True) self.proj = nn.Parameter(torch.linspace(0, self.reg_max, (self.reg_max + 1)), requires_grad=False) self.proj_conv.weight = nn.Parameter(self.proj.view([1, (self.reg_max + 1), 1, 1]).clone().detach(), requires_grad=False) def forward(self, x, val_loss=None): if self.training: cls_score_list = [] reg_distri_list = [] for i in range(self.nl): x[i] = self.stems[i](x[i]) cls_x = x[i] reg_x = x[i] cls_feat = self.cls_convs[i](cls_x) cls_output = self.cls_preds[i](cls_feat) reg_feat = self.reg_convs[i](reg_x) reg_output = self.reg_preds[i](reg_feat) cls_output = torch.sigmoid(cls_output) cls_score_list.append(cls_output.flatten(2).permute((0, 2, 1))) reg_distri_list.append(reg_output.flatten(2).permute((0, 2, 1))) cls_score_list = torch.cat(cls_score_list, axis=1) reg_distri_list = torch.cat(reg_distri_list, axis=1) return (x, cls_score_list, reg_distri_list) else: cls_score_list = [] reg_dist_list = [] (anchor_points, stride_tensor) = generate_anchors(x, self.stride, self.grid_cell_size, self.grid_cell_offset, device=x[0].device, is_eval=True) for i in range(self.nl): (b, _, h, w) = x[i].shape l = (h * w) x[i] = self.stems[i](x[i]) cls_x = x[i] reg_x = x[i] cls_feat = self.cls_convs[i](cls_x) cls_output = self.cls_preds[i](cls_feat) reg_feat = self.reg_convs[i](reg_x) reg_output = self.reg_preds[i](reg_feat) if self.use_dfl: reg_output = reg_output.reshape([(- 1), 4, (self.reg_max + 1), l]).permute(0, 2, 1, 3) reg_output = self.proj_conv(F.softmax(reg_output, dim=1)) cls_output = torch.sigmoid(cls_output) cls_score_list.append(cls_output.reshape([b, self.nc, l])) reg_dist_list.append(reg_output.reshape([b, 4, l])) cls_score_list = torch.cat(cls_score_list, axis=(- 1)).permute(0, 2, 1) reg_dist_list = torch.cat(reg_dist_list, axis=(- 1)).permute(0, 2, 1) pred_bboxes = dist2bbox(reg_dist_list, anchor_points, box_format='xywh') pred_bboxes *= stride_tensor return torch.cat([pred_bboxes, torch.ones((b, pred_bboxes.shape[1], 1), device=pred_bboxes.device, dtype=pred_bboxes.dtype), cls_score_list], axis=(- 1))
def fallback_version(root: _t.PathT, config: Configuration) -> (ScmVersion | None): if (config.parentdir_prefix_version is not None): (_, parent_name) = os.path.split(os.path.abspath(root)) if parent_name.startswith(config.parentdir_prefix_version): version = tag_to_version(parent_name[len(config.parentdir_prefix_version):], config) if (version is not None): return meta(str(version), preformatted=True, config=config) if (config.fallback_version is not None): log.debug('FALLBACK %s', config.fallback_version) return meta(config.fallback_version, preformatted=True, config=config) return None
('pypyr.venv.EnvBuilderWithExtraDeps') def test_venv_create(mock_builder): context = get_simple_context() mocked_builder = mock_builder.return_value mocked_builder.context = context venv.run_step(Context({'venv': '/arb'})) expected_path = str(Path('/arb').expanduser().resolve()) mocked_builder.create.assert_called_once_with(expected_path) mocked_builder.upgrade_dependencies.assert_called_once_with(context) mocked_builder.pip_install_extras.assert_not_called()
((os.name == 'nt'), 'BNG Console does not work on Windows') _model def test_simulate_network_console(): Monomer('A') Parameter('A_0', 1) Initial(A(), A_0) Parameter('k', 1) Rule('degrade', (A() >> None), k) with BngConsole(model) as bng: bng.generate_network() bng.action('simulate', method='ssa', t_end=20000, n_steps=100, netfile='pysb.net')
def convert_file_size_to_str(total: int) -> str: if (total < (1 << 10)): size = '{:.2f} B'.format(total) elif (total < (1 << 20)): size = '{:.2f} K'.format((total / (1 << 10))) elif (total < (1 << 30)): size = '{:.2f} M'.format((total / (1 << 20))) else: size = '{:.2f} G'.format((total / (1 << 30))) return size
def train(train_loader, model, criterions, optimizer, epoch): model.train() batch_time = AverageMeter() data_time = AverageMeter() loss_protest = AverageMeter() loss_v = AverageMeter() protest_acc = AverageMeter() violence_mse = AverageMeter() visattr_acc = AverageMeter() end = time.time() loss_history = [] for (i, sample) in enumerate(train_loader): (input, target) = (sample['image'], sample['label']) data_time.update((time.time() - end)) if args.cuda: input = input.cuda() for (k, v) in target.items(): target[k] = v.cuda() target_var = {} for (k, v) in target.items(): target_var[k] = Variable(v) input_var = Variable(input) output = model(input_var) (losses, scores, N_protest) = calculate_loss(output, target_var, criterions) optimizer.zero_grad() loss = 0 for l in losses: loss += l loss.backward() optimizer.step() if N_protest: loss_protest.update(losses[0].data[0], input.size(0)) loss_v.update((loss.data[0] - losses[0].data[0]), N_protest) else: loss_protest.update(losses[0].data[0], input.size(0)) loss_history.append(loss.data[0]) protest_acc.update(scores['protest_acc'], input.size(0)) violence_mse.update(scores['violence_mse'], N_protest) visattr_acc.update(scores['visattr_acc'], N_protest) batch_time.update((time.time() - end)) end = time.time() if ((i % args.print_freq) == 0): print('Epoch: [{0}][{1}/{2}] Time {batch_time.val:.2f} ({batch_time.avg:.2f}) Data {data_time.val:.2f} ({data_time.avg:.2f}) Loss {loss_val:.3f} ({loss_avg:.3f}) Protest {protest_acc.val:.3f} ({protest_acc.avg:.3f}) Violence {violence_mse.val:.5f} ({violence_mse.avg:.5f}) Vis Attr {visattr_acc.val:.3f} ({visattr_acc.avg:.3f})'.format(epoch, i, len(train_loader), batch_time=batch_time, data_time=data_time, loss_val=(loss_protest.val + loss_v.val), loss_avg=(loss_protest.avg + loss_v.avg), protest_acc=protest_acc, violence_mse=violence_mse, visattr_acc=visattr_acc)) return loss_history
class GPTNeoXJapaneseTokenizer(PreTrainedTokenizer): vocab_files_names = VOCAB_FILES_NAMES pretrained_vocab_files_map = PRETRAINED_VOCAB_FILES_MAP max_model_input_sizes = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES model_input_names = ['input_ids', 'attention_mask'] def __init__(self, vocab_file, emoji_file, unk_token='<|endoftext|>', pad_token='<|endoftext|>', bos_token='<|startoftext|>', eos_token='<|endoftext|>', do_clean_text=False, **kwargs): super().__init__(unk_token=unk_token, pad_token=pad_token, bos_token=bos_token, eos_token=eos_token, do_clean_text=do_clean_text, **kwargs) if (not os.path.isfile(vocab_file)): raise ValueError(f"Can't find a vocabulary file at path '{vocab_file}'. To load the vocabulary from a Google pretrained model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`") if (not os.path.isfile(emoji_file)): raise ValueError(f"Can't find a emoji file at path '{emoji_file}'. To load the emoji information from a Google pretrained model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`") self.do_clean_text = do_clean_text (self.vocab, self.raw_vocab, self.ids_to_tokens, self.emoji) = load_vocab_and_emoji(vocab_file, emoji_file) self.subword_tokenizer = SubWordJapaneseTokenizer(vocab=self.vocab, ids_to_tokens=self.ids_to_tokens, emoji=self.emoji) def vocab_size(self): return len(self.raw_vocab) def get_vocab(self): return dict(self.raw_vocab, **self.added_tokens_encoder) def _tokenize(self, text): return self.subword_tokenizer.tokenize(text, clean=self.do_clean_text) def _convert_token_to_id(self, token): return self.vocab.get(token, self.vocab.get(self.unk_token)) def _convert_id_to_token(self, index): return self.subword_tokenizer.convert_id_to_token(index) def convert_tokens_to_string(self, tokens): out_string = ''.join(tokens).strip() return out_string def _build_conversation_input_ids(self, conversation: 'Conversation') -> List[int]: input_ids = [] for (is_user, text) in conversation.iter_texts(): input_ids.extend((self.encode(text, add_special_tokens=False) + [self.eos_token_id])) if (len(input_ids) > self.model_max_length): input_ids = input_ids[(- self.model_max_length):] return input_ids def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str]=None) -> Tuple[str]: index = 0 if os.path.isdir(save_directory): vocab_file = os.path.join(save_directory, (((filename_prefix + '-') if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'])) emoji_file = os.path.join(save_directory, (((filename_prefix + '-') if filename_prefix else '') + VOCAB_FILES_NAMES['emoji_file'])) else: vocab_file = ((((filename_prefix + '-') if filename_prefix else '') + save_directory) + VOCAB_FILES_NAMES['vocab_file']) emoji_file = ((((filename_prefix + '-') if filename_prefix else '') + save_directory) + VOCAB_FILES_NAMES['emoji_file']) with open(vocab_file, 'w', encoding='utf-8') as writer: for (token_index, token) in self.ids_to_tokens.items(): if (index != token_index): logger.warning(f'Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive. Please check that the vocabulary is not corrupted!') index = token_index writer.write((','.join(token) + '\n')) index += 1 with open(emoji_file, 'w', encoding='utf-8') as writer: json.dump(self.emoji, writer) return (vocab_file, emoji_file)
def unpack(compressed): uncompressed = np.zeros((compressed.shape[0] * 8), dtype=np.uint8) uncompressed[::8] = ((compressed[:] >> 7) & 1) uncompressed[1::8] = ((compressed[:] >> 6) & 1) uncompressed[2::8] = ((compressed[:] >> 5) & 1) uncompressed[3::8] = ((compressed[:] >> 4) & 1) uncompressed[4::8] = ((compressed[:] >> 3) & 1) uncompressed[5::8] = ((compressed[:] >> 2) & 1) uncompressed[6::8] = ((compressed[:] >> 1) & 1) uncompressed[7::8] = (compressed[:] & 1) return uncompressed
class IASIL2SO2BUFR(BaseFileHandler): def __init__(self, filename, filename_info, filetype_info, **kwargs): super(IASIL2SO2BUFR, self).__init__(filename, filename_info, filetype_info) (start_time, end_time) = self.get_start_end_date() sc_id = self.get_attribute('satelliteIdentifier') self.metadata = {} self.metadata['start_time'] = start_time self.metadata['end_time'] = end_time self.metadata['SpacecraftName'] = data_center_dict[sc_id] def start_time(self): return self.metadata['start_time'] def end_time(self): return self.metadata['end_time'] def platform_name(self): return '{}'.format(self.metadata['SpacecraftName']) def get_start_end_date(self): fh = open(self.filename, 'rb') i = 0 while True: bufr = ec.codes_bufr_new_from_file(fh) if (bufr is None): break ec.codes_set(bufr, 'unpack', 1) year = ec.codes_get(bufr, 'year') month = ec.codes_get(bufr, 'month') day = ec.codes_get(bufr, 'day') hour = ec.codes_get(bufr, 'hour') minute = ec.codes_get(bufr, 'minute') second = ec.codes_get(bufr, 'second') obs_time = datetime(year=year, month=month, day=day, hour=hour, minute=minute, second=second) if (i == 0): start_time = obs_time ec.codes_release(bufr) i += 1 end_time = obs_time fh.close() return (start_time, end_time) def get_attribute(self, key): fh = open(self.filename, 'rb') while True: bufr = ec.codes_bufr_new_from_file(fh) if (bufr is None): break ec.codes_set(bufr, 'unpack', 1) attr = ec.codes_get(bufr, key) ec.codes_release(bufr) fh.close() return attr def get_array(self, key): with open(self.filename, 'rb') as fh: msgCount = 0 while True: bufr = ec.codes_bufr_new_from_file(fh) if (bufr is None): break ec.codes_set(bufr, 'unpack', 1) values = ec.codes_get_array(bufr, key, float) if (len(values) == 1): values = np.repeat(values, 120) if (msgCount == 0): arr = da.from_array([values], chunks=CHUNK_SIZE) else: tmpArr = da.from_array([values], chunks=CHUNK_SIZE) arr = da.concatenate((arr, tmpArr), axis=0) msgCount = (msgCount + 1) ec.codes_release(bufr) if (arr.size == 1): arr = arr[0] return arr def get_dataset(self, dataset_id, dataset_info): arr = self.get_array(dataset_info['key']) arr[(arr == dataset_info['fill_value'])] = np.nan xarr = xr.DataArray(arr, dims=['y', 'x'], name=dataset_info['name']) xarr.attrs['sensor'] = 'IASI' xarr.attrs['platform_name'] = self.platform_name xarr.attrs.update(dataset_info) return xarr
class SawyerCoffeePullV1Policy(Policy): _fully_parsed def _parse_obs(obs): return {'hand_pos': obs[:3], 'mug_pos': obs[3:6], 'unused_info': obs[6:]} def get_action(self, obs): o_d = self._parse_obs(obs) action = Action({'delta_pos': np.arange(3), 'grab_effort': 3}) action['delta_pos'] = move(o_d['hand_pos'], to_xyz=self._desired_pos(o_d), p=10.0) action['grab_effort'] = self._grab_effort(o_d) return action.array def _desired_pos(o_d): pos_curr = o_d['hand_pos'] pos_mug = o_d['mug_pos'] if (np.linalg.norm((pos_curr[:2] - pos_mug[:2])) > 0.06): return (pos_mug + np.array([0.0, 0.0, 0.15])) elif (abs((pos_curr[2] - pos_mug[2])) > 0.04): return pos_mug elif (pos_curr[1] > 0.7): return np.array([0.5, 0.62, 0.1]) else: return np.array([(pos_curr[0] - 0.1), 0.62, 0.1]) def _grab_effort(o_d): pos_curr = o_d['hand_pos'] pos_mug = o_d['mug_pos'] if ((np.linalg.norm((pos_curr[:2] - pos_mug[:2])) > 0.06) or (abs((pos_curr[2] - pos_mug[2])) > 0.06)): return (- 1.0) else: return 0.9
def get_packer_mapping(packers, task): packer_mapping = {} for (rec_id, packer) in packers.items(): obj_keys = [task.sim_obj_id_to_obj_key[obj_key] for obj_key in list(packer.matches.keys())] rec_key = task.sim_obj_id_to_obj_key[rec_id] for obj_key in obj_keys: packer_mapping[obj_key] = rec_key return packer_mapping
def evaluate(args, model, tokenizer, criterion, prefix=''): eval_output_dir = args.output_dir eval_dataset = load_examples(args, tokenizer, evaluate=True) if ((not os.path.exists(eval_output_dir)) and (args.local_rank in [(- 1), 0])): os.makedirs(eval_output_dir) args.eval_batch_size = (args.per_gpu_eval_batch_size * max(1, args.n_gpu)) eval_sampler = SequentialSampler(eval_dataset) eval_dataloader = DataLoader(eval_dataset, sampler=eval_sampler, batch_size=args.eval_batch_size, collate_fn=collate_fn) if ((args.n_gpu > 1) and (not isinstance(model, nn.DataParallel))): model = nn.DataParallel(model) logger.info('***** Running evaluation {} *****'.format(prefix)) logger.info(' Num examples = %d', len(eval_dataset)) logger.info(' Batch size = %d', args.eval_batch_size) eval_loss = 0.0 nb_eval_steps = 0 preds = None out_label_ids = None for batch in tqdm(eval_dataloader, desc='Evaluating'): model.eval() batch = tuple((t.to(args.device) for t in batch)) with torch.no_grad(): batch = tuple((t.to(args.device) for t in batch)) labels = batch[5] inputs = {'input_ids': batch[0], 'input_modal': batch[2], 'attention_mask': batch[1], 'modal_start_tokens': batch[3], 'modal_end_tokens': batch[4]} outputs = model(**inputs) logits = outputs[0] tmp_eval_loss = criterion(logits, labels) eval_loss += tmp_eval_loss.mean().item() nb_eval_steps += 1 if (preds is None): preds = (torch.sigmoid(logits).detach().cpu().numpy() > 0.5) out_label_ids = labels.detach().cpu().numpy() else: preds = np.append(preds, (torch.sigmoid(logits).detach().cpu().numpy() > 0.5), axis=0) out_label_ids = np.append(out_label_ids, labels.detach().cpu().numpy(), axis=0) eval_loss = (eval_loss / nb_eval_steps) result = {'loss': eval_loss, 'macro_f1': f1_score(out_label_ids, preds, average='macro'), 'micro_f1': f1_score(out_label_ids, preds, average='micro')} output_eval_file = os.path.join(eval_output_dir, prefix, 'eval_results.txt') with open(output_eval_file, 'w') as writer: logger.info('***** Eval results {} *****'.format(prefix)) for key in sorted(result.keys()): logger.info(' %s = %s', key, str(result[key])) writer.write(('%s = %s\n' % (key, str(result[key])))) return result
class PluginMethods(PluginActions): def register_function(self, function, inputs, parameters, outputs, name, description, input_descriptions=None, parameter_descriptions=None, output_descriptions=None, citations=None, deprecated=False, examples=None): if (citations is None): citations = () else: citations = tuple(citations) if (examples is None): examples = {} method = qiime2.sdk.Method._init(function, inputs, parameters, outputs, self._plugin_id, name, description, input_descriptions, parameter_descriptions, output_descriptions, citations, deprecated, examples) self[method.id] = method
class RateLimiterBackendTests(): def setUp(self): self.allowance = 10 self.interval = 1 ratelimiter_factory = RateLimiterContextFactory(self.backend_factory, self.allowance, self.interval) self.baseplate_observer = TestBaseplateObserver() baseplate = Baseplate() baseplate.register(self.baseplate_observer) baseplate.add_to_context('ratelimiter', ratelimiter_factory) self.context = baseplate.make_context_object() self.server_span = baseplate.make_server_span(self.context, 'test') def test_ratelimiter_consume(self): user_id = str(uuid4()) with self.server_span: self.context.ratelimiter.consume(user_id, amount=self.allowance) def test_ratelimiter_exceeded(self): user_id = str(uuid4()) with self.server_span: with self.assertRaises(RateLimitExceededException): self.context.ratelimiter.consume(user_id, amount=(self.allowance + 1)) def test_ratelimiter_resets(self): user_id = str(uuid4()) with self.server_span: self.context.ratelimiter.consume(user_id, amount=self.allowance) sleep(self.interval) self.context.ratelimiter.consume(user_id, amount=self.allowance)
def main(): parser = argparse.ArgumentParser() parser.add_argument('path', help='Path to result .json file as produced by 3D evaluation script. Can be downloaded from the evaluation server for test set results.') args = parser.parse_args() if (not os.path.exists(args.path)): raise Exception('Result file not found!') data = prepare_data(args.path) plot_data(data)
class PytestArg(): def __init__(self, request: FixtureRequest) -> None: self._request = request def gethookrecorder(self, hook) -> 'HookRecorder': hookrecorder = HookRecorder(hook._pm) self._request.addfinalizer(hookrecorder.finish_recording) return hookrecorder
class StreamReset(Event): def __init__(self): self.stream_id = None self.error_code = None self.remote_reset = True def __repr__(self): return ('<StreamReset stream_id:%s, error_code:%s, remote_reset:%s>' % (self.stream_id, self.error_code, self.remote_reset))
class tensorboard_log_wrapper(progress_bar): def __init__(self, wrapped_bar, tensorboard_logdir, args): self.wrapped_bar = wrapped_bar self.tensorboard_logdir = tensorboard_logdir self.args = args try: from tensorboardX import SummaryWriter self.SummaryWriter = SummaryWriter self._writers = {} except ImportError: print('tensorboard or required dependencies not found, please see README for using tensorboard. (e.g. pip install tensorboardX)') self.SummaryWriter = None def _writer(self, key): if (self.SummaryWriter is None): return None if (key not in self._writers): self._writers[key] = self.SummaryWriter(os.path.join(self.tensorboard_logdir, key)) self._writers[key].add_text('args', str(vars(self.args))) self._writers[key].add_text('sys.argv', ' '.join(sys.argv)) return self._writers[key] def __iter__(self): return iter(self.wrapped_bar) def log(self, stats, tag='', step=None): self._log_to_tensorboard(stats, tag, step) self.wrapped_bar.log(stats, tag=tag, step=step) def print(self, stats, tag='', step=None): self._log_to_tensorboard(stats, tag, step) self.wrapped_bar.print(stats, tag=tag, step=step) def __exit__(self, *exc): for writer in getattr(self, '_writers', {}).values(): writer.close() return False def _log_to_tensorboard(self, stats, tag='', step=None): writer = self._writer(tag) if (writer is None): return if (step is None): step = stats['num_updates'] for key in (stats.keys() - {'num_updates'}): if isinstance(stats[key], AverageMeter): writer.add_scalar(key, stats[key].val, step) elif isinstance(stats[key], Number): writer.add_scalar(key, stats[key], step)
class TestParticleNumber(PropertyTest): def setUp(self): super().setUp() num_spatial_orbitals = 4 self.prop = ParticleNumber(num_spatial_orbitals) def test_second_q_ops(self): ops = self.prop.second_q_ops()['ParticleNumber'] expected = {'+_0 -_0': 1.0, '+_1 -_1': 1.0, '+_2 -_2': 1.0, '+_3 -_3': 1.0, '+_4 -_4': 1.0, '+_5 -_5': 1.0, '+_6 -_6': 1.0, '+_7 -_7': 1.0} self.assertEqual(dict(ops.items()), expected)
class TestTransformerStretch(unittest.TestCase): def test_default(self): tfm = new_transformer() tfm.stretch(1.1) actual_args = tfm.effects expected_args = ['stretch', '1.100000', '20.000000'] self.assertEqual(expected_args, actual_args) actual_log = tfm.effects_log expected_log = ['stretch'] self.assertEqual(expected_log, actual_log) actual_res = tfm.build(INPUT_FILE, OUTPUT_FILE) expected_res = True self.assertEqual(expected_res, actual_res) tfm_assert_array_to_file_output(INPUT_FILE, OUTPUT_FILE, tfm) def test_factor_valid(self): tfm = new_transformer() tfm.stretch(0.7) actual_args = tfm.effects expected_args = ['stretch', '0.700000', '20.000000'] self.assertEqual(expected_args, actual_args) actual_res = tfm.build(INPUT_FILE, OUTPUT_FILE) expected_res = True self.assertEqual(expected_res, actual_res) tfm_assert_array_to_file_output(INPUT_FILE, OUTPUT_FILE, tfm) def test_factor_extreme(self): tfm = new_transformer() tfm.stretch(0.2) actual_args = tfm.effects expected_args = ['stretch', '0.200000', '20.000000'] self.assertEqual(expected_args, actual_args) actual_res = tfm.build(INPUT_FILE, OUTPUT_FILE) expected_res = True self.assertEqual(expected_res, actual_res) tfm_assert_array_to_file_output(INPUT_FILE, OUTPUT_FILE, tfm) def test_factor_invalid(self): tfm = new_transformer() with self.assertRaises(ValueError): tfm.stretch((- 1)) def test_window_valid(self): tfm = new_transformer() tfm.stretch(0.99, window=10) actual_args = tfm.effects expected_args = ['stretch', '0.990000', '10.000000'] self.assertEqual(expected_args, actual_args) actual_res = tfm.build(INPUT_FILE, OUTPUT_FILE) expected_res = True self.assertEqual(expected_res, actual_res) tfm_assert_array_to_file_output(INPUT_FILE, OUTPUT_FILE, tfm) def test_window_invalid(self): tfm = new_transformer() with self.assertRaises(ValueError): tfm.stretch(0.99, window=0)
def abort_current_continuation(args, env, cont): from pycket.interpreter import return_multi_vals if (not args): raise SchemeException('abort-current-continuation: expected 1 or more args') (tag, args) = (args[0], args[1:]) if (not isinstance(tag, values.W_ContinuationPromptTag)): raise SchemeException('abort-current-continuation: expected prompt-tag for argument 0') (prompt, frames) = find_continuation_prompt(tag, cont, direction='unwind') if (prompt is None): raise SchemeException('abort-current-continuation: no such prompt exists') handler = prompt.handler cont = prompt.get_previous_continuation() assert (cont is not None) if (handler is None): if (not args): raise SchemeException('abort-current-continuation: expected thunk as argument 1') cont = Prompt(tag, None, env, cont) handler = args[0] args = [] if frames: cont = call_handler_cont(handler, args, env, cont) return unwind_frames(frames, env, cont) return handler.call(args, env, cont)
class InceptionResnetV2(nn.Module): def __init__(self, num_classes=1000, in_chans=3, drop_rate=0.0, output_stride=32, global_pool='avg'): super(InceptionResnetV2, self).__init__() self.drop_rate = drop_rate self.num_classes = num_classes self.num_features = 1536 assert (output_stride == 32) self.conv2d_1a = BasicConv2d(in_chans, 32, kernel_size=3, stride=2) self.conv2d_2a = BasicConv2d(32, 32, kernel_size=3, stride=1) self.conv2d_2b = BasicConv2d(32, 64, kernel_size=3, stride=1, padding=1) self.feature_info = [dict(num_chs=64, reduction=2, module='conv2d_2b')] self.maxpool_3a = nn.MaxPool2d(3, stride=2) self.conv2d_3b = BasicConv2d(64, 80, kernel_size=1, stride=1) self.conv2d_4a = BasicConv2d(80, 192, kernel_size=3, stride=1) self.feature_info += [dict(num_chs=192, reduction=4, module='conv2d_4a')] self.maxpool_5a = nn.MaxPool2d(3, stride=2) self.mixed_5b = Mixed_5b() self.repeat = nn.Sequential(Block35(scale=0.17), Block35(scale=0.17), Block35(scale=0.17), Block35(scale=0.17), Block35(scale=0.17), Block35(scale=0.17), Block35(scale=0.17), Block35(scale=0.17), Block35(scale=0.17), Block35(scale=0.17)) self.feature_info += [dict(num_chs=320, reduction=8, module='repeat')] self.mixed_6a = Mixed_6a() self.repeat_1 = nn.Sequential(Block17(scale=0.1), Block17(scale=0.1), Block17(scale=0.1), Block17(scale=0.1), Block17(scale=0.1), Block17(scale=0.1), Block17(scale=0.1), Block17(scale=0.1), Block17(scale=0.1), Block17(scale=0.1), Block17(scale=0.1), Block17(scale=0.1), Block17(scale=0.1), Block17(scale=0.1), Block17(scale=0.1), Block17(scale=0.1), Block17(scale=0.1), Block17(scale=0.1), Block17(scale=0.1), Block17(scale=0.1)) self.feature_info += [dict(num_chs=1088, reduction=16, module='repeat_1')] self.mixed_7a = Mixed_7a() self.repeat_2 = nn.Sequential(Block8(scale=0.2), Block8(scale=0.2), Block8(scale=0.2), Block8(scale=0.2), Block8(scale=0.2), Block8(scale=0.2), Block8(scale=0.2), Block8(scale=0.2), Block8(scale=0.2)) self.block8 = Block8(no_relu=True) self.conv2d_7b = BasicConv2d(2080, self.num_features, kernel_size=1, stride=1) self.feature_info += [dict(num_chs=self.num_features, reduction=32, module='conv2d_7b')] (self.global_pool, self.classif) = create_classifier(self.num_features, self.num_classes, pool_type=global_pool) .ignore def group_matcher(self, coarse=False): module_map = {k: i for (i, (k, _)) in enumerate(flatten_modules(self.named_children(), prefix=()))} module_map.pop(('classif',)) def _matcher(name): if any([name.startswith(n) for n in ('conv2d_1', 'conv2d_2')]): return 0 elif any([name.startswith(n) for n in ('conv2d_3', 'conv2d_4')]): return 1 elif any([name.startswith(n) for n in ('block8', 'conv2d_7')]): return (len(module_map) + 1) else: for k in module_map.keys(): if (k == tuple(name.split('.')[:len(k)])): return module_map[k] return float('inf') return _matcher .ignore def set_grad_checkpointing(self, enable=True): assert (not enable), 'checkpointing not supported' .ignore def get_classifier(self): return self.classif def reset_classifier(self, num_classes, global_pool='avg'): self.num_classes = num_classes (self.global_pool, self.classif) = create_classifier(self.num_features, self.num_classes, pool_type=global_pool) def forward_features(self, x): x = self.conv2d_1a(x) x = self.conv2d_2a(x) x = self.conv2d_2b(x) x = self.maxpool_3a(x) x = self.conv2d_3b(x) x = self.conv2d_4a(x) x = self.maxpool_5a(x) x = self.mixed_5b(x) x = self.repeat(x) x = self.mixed_6a(x) x = self.repeat_1(x) x = self.mixed_7a(x) x = self.repeat_2(x) x = self.block8(x) x = self.conv2d_7b(x) return x def forward_head(self, x, pre_logits: bool=False): x = self.global_pool(x) if (self.drop_rate > 0): x = F.dropout(x, p=self.drop_rate, training=self.training) return (x if pre_logits else self.classif(x)) def forward(self, x): x = self.forward_features(x) x = self.forward_head(x) return x
class ThriftContextFactory(ContextFactory): POOL_PREFIX = 'thrift_client_pool' POOL_LABELS = ['thrift_pool'] max_connections_gauge = Gauge(f'{POOL_PREFIX}_max_size', 'Maximum number of connections in this thrift pool before blocking', POOL_LABELS) active_connections_gauge = Gauge(f'{POOL_PREFIX}_active_connections', 'Number of connections currently in use in this thrift pool', POOL_LABELS) def __init__(self, pool: ThriftConnectionPool, client_cls: Any): self.pool = pool self.client_cls = client_cls self.proxy_cls = type('PooledClientProxy', (_PooledClientProxy,), {fn_name: _build_thrift_proxy_method(fn_name) for fn_name in _enumerate_service_methods(client_cls) if (not (fn_name.startswith('__') and fn_name.endswith('__')))}) def report_runtime_metrics(self, batch: metrics.Client) -> None: pool_name = self.client_cls.__name__ self.max_connections_gauge.labels(pool_name).set(self.pool.size) self.active_connections_gauge.labels(pool_name).set(self.pool.checkedout) batch.gauge('pool.size').replace(self.pool.size) batch.gauge('pool.in_use').replace(self.pool.checkedout) def make_object_for_context(self, name: str, span: Span) -> '_PooledClientProxy': return self.proxy_cls(self.client_cls, self.pool, span, name)
class CellB(nn.Module): def __init__(self, in_planes, out_planes, stride=1): super(CellB, self).__init__() self.stride = stride self.sep_conv1 = SepConv(in_planes, out_planes, kernel_size=7, stride=stride) self.sep_conv2 = SepConv(in_planes, out_planes, kernel_size=3, stride=stride) self.sep_conv3 = SepConv(in_planes, out_planes, kernel_size=5, stride=stride) if (stride == 2): self.conv1 = nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=1, padding=0, bias=False) self.bn1 = nn.BatchNorm2d(out_planes) self.conv2 = nn.Conv2d((2 * out_planes), out_planes, kernel_size=1, stride=1, padding=0, bias=False) self.bn2 = nn.BatchNorm2d(out_planes) def forward(self, x): y1 = self.sep_conv1(x) y2 = self.sep_conv2(x) y3 = F.max_pool2d(x, kernel_size=3, stride=self.stride, padding=1) if (self.stride == 2): y3 = self.bn1(self.conv1(y3)) y4 = self.sep_conv3(x) b1 = F.relu((y1 + y2)) b2 = F.relu((y3 + y4)) y = torch.cat([b1, b2], 1) return F.relu(self.bn2(self.conv2(y)))
class Model(object): def __init__(self): self.history = False def present(self): return (self.conf['state']['imu.rate'] * self.conf['past']) def receive(self, name, value): if ((name in self.conf['sensors']) and self.enabled): self.inputs[name] = norm_sensor(name, value)
class LassoXmlLexer(DelegatingLexer): name = 'XML+Lasso' aliases = ['xml+lasso'] version_added = '1.6' alias_filenames = ['*.xml', '*.lasso', '*.lasso[89]', '*.incl', '*.inc', '*.las'] mimetypes = ['application/xml+lasso'] url = ' def __init__(self, **options): super().__init__(XmlLexer, LassoLexer, **options) def analyse_text(text): rv = (LassoLexer.analyse_text(text) - 0.01) if looks_like_xml(text): rv += 0.4 return rv
class ModelArguments(): model_name_or_path: str = field(metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'}) config_name: Optional[str] = field(default=None, metadata={'help': 'Pretrained config name or path if not the same as model_name'}) tokenizer_name: Optional[str] = field(default=None, metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'}) cache_dir: Optional[str] = field(default=None, metadata={'help': 'Where to store the pretrained models downloaded from huggingface.co'}) use_fast_tokenizer: bool = field(default=True, metadata={'help': 'Whether to use one of the fast tokenizer (backed by the tokenizers library) or not.'}) model_revision: str = field(default='main', metadata={'help': 'The specific model version to use (can be a branch name, tag name or commit id).'}) use_auth_token: bool = field(default=False, metadata={'help': 'Will use the token generated when running `transformers-cli login` (necessary to use this script with private models).'})
def test_search__with_annotations(requests_mock): requests_mock.get(f'{API_V1}/observations', json=SAMPLE_DATA['get_observation_with_ofvs'], status_code=200) requests_mock.get(f'{API_V1}/controlled_terms', json=SAMPLE_DATA['get_controlled_terms'], status_code=200) results = iNatClient().observations.search().all() annotation = results[0].annotations[0] assert isinstance(annotation, Annotation) assert (annotation.controlled_attribute.label == 'Life Stage') assert (annotation.controlled_value.label == 'Adult')
def measure_peak_memory_cpu(function: Callable[([], None)], interval=0.5, device_idx=None) -> int: def get_cpu_memory(process_id: int) -> int: process = psutil.Process(process_id) try: meminfo_attr = ('memory_info' if hasattr(process, 'memory_info') else 'get_memory_info') memory = getattr(process, meminfo_attr)()[0] except psutil.AccessDenied: raise ValueError('Error with Psutil.') return memory if (not is_psutil_available()): logger.warning("Psutil not installed, we won't log CPU memory usage. Install Psutil (pip install psutil) to use CPU memory tracing.") max_memory = 'N/A' else: class MemoryMeasureProcess(Process): def __init__(self, process_id: int, child_connection: Connection, interval: float): super().__init__() self.process_id = process_id self.interval = interval self.connection = child_connection self.num_measurements = 1 self.mem_usage = get_cpu_memory(self.process_id) def run(self): self.connection.send(0) stop = False while True: self.mem_usage = max(self.mem_usage, get_cpu_memory(self.process_id)) self.num_measurements += 1 if stop: break stop = self.connection.poll(self.interval) self.connection.send(self.mem_usage) self.connection.send(self.num_measurements) while True: (child_connection, parent_connection) = Pipe() mem_process = MemoryMeasureProcess(os.getpid(), child_connection, interval) mem_process.start() parent_connection.recv() try: function() parent_connection.send(0) max_memory = parent_connection.recv() num_measurements = parent_connection.recv() except Exception: parent = psutil.Process(os.getpid()) for child in parent.children(recursive=True): os.kill(child.pid, SIGKILL) mem_process.join(0) raise RuntimeError('Process killed. Error in Process') mem_process.join((20 * interval)) if ((num_measurements > 4) or (interval < 1e-06)): break interval /= 10 return max_memory
class BenchmarkMainComplex(): params = [['parallel', 'sequential'], ['basic', 'rule154', 'fig16'], ['local', 'redis']] param_names = ['mode', 'network', 'cache'] timer = timeit.default_timer number = 1 repeat = 1 timeout = 10000 def setup(self, mode, network, cache): if (network == 'basic'): self.network = examples.basic_network() self.state = (0, 1, 1) elif (network == 'rule154'): self.network = examples.rule154_network() self.state = (0, 1, 0, 1, 1) elif (network == 'fig16'): self.network = examples.fig16() self.state = (1, 0, 0, 1, 1, 1, 0) else: raise ValueError(network) self.default_config = copy.copy(config.__dict__) if (mode == 'parallel'): config.PARALLEL_CUT_EVALUATION = True elif (mode == 'sequential'): config.PARALLEL_CUT_EVALUATION = False else: raise ValueError(mode) if (cache == 'local'): config.REDIS_CACHE = False elif (cache == 'redis'): config.REDIS_CACHE = True if (_cache.RedisConn().ping() is False): raise NotImplementedError else: raise ValueError(cache) config.CACHE_SIAS = False def teardown(self, mode, network, cache): config.__dict__.update(self.default_config) def time_major_complex(self, mode, network, cache): compute.major_complex(self.network, self.state)
def evaluate(args): tokenizer = BertTokenizer.from_pretrained('bert-base-uncased', do_lower_case=True) model = BertAbs.from_pretrained('remi/bertabs-finetuned-extractive-abstractive-summarization') model.to(args.device) model.eval() symbols = {'BOS': tokenizer.vocab['[unused0]'], 'EOS': tokenizer.vocab['[unused1]'], 'PAD': tokenizer.vocab['[PAD]']} if args.compute_rouge: reference_summaries = [] generated_summaries = [] import nltk import rouge nltk.download('punkt') rouge_evaluator = rouge.Rouge(metrics=['rouge-n', 'rouge-l'], max_n=2, limit_length=True, length_limit=args.beam_size, length_limit_type='words', apply_avg=True, apply_best=False, alpha=0.5, weight_factor=1.2, stemming=True) args.result_path = '' args.temp_dir = '' data_iterator = build_data_iterator(args, tokenizer) predictor = build_predictor(args, tokenizer, symbols, model) logger.info('***** Running evaluation *****') logger.info(' Number examples = %d', len(data_iterator.dataset)) logger.info(' Batch size = %d', args.batch_size) logger.info('') logger.info('***** Beam Search parameters *****') logger.info(' Beam size = %d', args.beam_size) logger.info(' Minimum length = %d', args.min_length) logger.info(' Maximum length = %d', args.max_length) logger.info(' Alpha (length penalty) = %.2f', args.alpha) logger.info(' Trigrams %s be blocked', ('will' if args.block_trigram else 'will NOT')) for batch in tqdm(data_iterator): batch_data = predictor.translate_batch(batch) translations = predictor.from_batch(batch_data) summaries = [format_summary(t) for t in translations] save_summaries(summaries, args.summaries_output_dir, batch.document_names) if args.compute_rouge: reference_summaries += batch.tgt_str generated_summaries += summaries if args.compute_rouge: scores = rouge_evaluator.get_scores(generated_summaries, reference_summaries) str_scores = format_rouge_scores(scores) save_rouge_scores(str_scores) print(str_scores)
class GraphClassificationDataset(GraphDataset): def __init__(self, graphs, labels): super().__init__(graphs) self.labels = labels assert (len(graphs) == len(labels)) def __getitem__(self, index): return (self.graphs[index], self.labels[index]) def collate_fn(batch): (graphs, labels) = list(zip(*batch)) g = dgl.batch(graphs) labels = torch.tensor(labels).long() return (g, labels)
class UnetBottleneck(nn.Module): def __init__(self, inplanes, planes, kernel_size=3, dilation=1, act_type='relu'): super(UnetBottleneck, self).__init__() self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=kernel_size, stride=1, padding=get_same_padding(kernel_size, dilation), dilation=dilation) self.conv2 = nn.Conv2d(planes, planes, kernel_size=kernel_size, stride=1, padding=get_same_padding(kernel_size, dilation), dilation=dilation) self.act = (get_act(act_type=act_type) if act_type else None) def forward(self, x): out = self.act(self.conv1(x)) out = self.act(self.conv2(out)) return out
def test_opdm_to_ohdm_mapping(): db = opdm_to_ohdm_mapping(6) for dbe in db: assert isinstance(dbe, DualBasisElement) assert (set(dbe.primal_tensors_names) == {'ck', 'kc'}) if (len(dbe.primal_tensors_names) == 4): assert np.allclose(dbe.primal_coeffs, 0.5) assert np.isclose(dbe.dual_scalar, 0.0) elif (len(dbe.primal_tensors_names) == 2): assert np.allclose(dbe.primal_coeffs, 1.0) assert np.isclose(dbe.dual_scalar, 1.0)
def log(s, with_prefix=True, with_timestamp=True, color=None): out = s if with_prefix: out = (_prefix_str + out) if with_timestamp: now = datetime.datetime.now(dateutil.tz.tzlocal()) timestamp = now.strftime('%Y-%m-%d %H:%M:%S.%f %Z') out = ('%s | %s' % (timestamp, out)) if (color is not None): out = colorize(out, color) if (not _log_tabular_only): print(out) for fd in list(_text_fds.values()): fd.write((out + '\n')) fd.flush() sys.stdout.flush()
class VAEAugExperiment(object): def __init__(self, config): self.config = config self.device = self._get_device() self.writer = SummaryWriter(os.path.join(self.config['save_dir'], self.config['vae_mode'], 'tensorboard')) self.nt_xent_criterion = NTXentLoss(self.device, **config['loss']) split_dir = os.path.join(self.config['base_dir'], 'splits.pkl') self.data_dir = os.path.join(self.config['base_dir'], 'preprocessed') with open(split_dir, 'rb') as f: splits = pickle.load(f) k = config['fold'] tr_keys = (splits[k]['train'] + splits[k]['val']) self.val_keys = splits[k]['train'] self.train_loader = NumpyDataSet(self.data_dir, target_size=self.config['img_size'], batch_size=self.config['batch_size'], keys=tr_keys, do_reshuffle=True) self.val_loader = NumpyDataSet(self.data_dir, target_size=self.config['img_size'], batch_size=self.config['val_batch_size'], keys=self.val_keys, do_reshuffle=True) self.transforms = get_transforms(mode='vae') print(len(self.train_loader)) if (self.config['vae_mode'] == 'beta'): self.model = BetaVAE(in_channels=1, latent_dim=256).to(self.device) elif (self.config['vae_mode'] == 'base'): self.model = VanillaVAE(in_channels=1, latent_dim=256).to(self.device) self.optimizer = torch.optim.Adam(self.model.parameters(), 0.0003, weight_decay=eval(self.config['weight_decay'])) self.save_folder = os.path.join('output_experiment', ('infer_vae' + str(datetime.now())[0:16])) if (not os.path.exists(self.save_folder)): os.mkdir(self.save_folder) def _get_device(self): device = ('cuda' if torch.cuda.is_available() else 'cpu') print('Running on:', device) return device def train(self): model_checkpoints_folder = os.path.join(self.writer.log_dir, 'checkpoints') _save_config_file(model_checkpoints_folder) n_iter = 0 valid_n_iter = 0 best_valid_loss = np.inf scheduler = torch.optim.lr_scheduler.ExponentialLR(self.optimizer, gamma=0.95, last_epoch=(- 1)) for epoch_counter in range(self.config['epochs']): print(('=====Training Epoch: %d =====' % epoch_counter)) for (i, data_batch) in enumerate(self.train_loader): self.optimizer.zero_grad() img = data_batch['data'][0].float().to(self.device) label = data_batch['seg'][0].long().to(self.device) new_data = dict() new_data['data'] = data_batch['data'][0].numpy() new_data = self.transforms(**new_data) new_img = new_data['data'].float().to(self.device) results = self.model(new_img, labels=label) results[1] = img train_loss = self.model.loss_function(*results, M_N=1, optimizer_idx=0, batch_idx=i) loss = train_loss['loss'] if ((n_iter % self.config['log_every_n_steps']) == 0): self.writer.add_scalar('train_loss', loss, global_step=n_iter) print('Train:[{0}][{1}][{2}] loss: {loss:.4f}'.format(epoch_counter, i, len(self.train_loader), loss=loss.item())) loss.backward() self.optimizer.step() n_iter += 1 torch.save(self.model.state_dict(), os.path.join(self.config['save_dir'], self.config['vae_mode'], 'b_{}_f{}_vae.pth'.format(self.config['batch_size'], self.config['fold']))) if (epoch_counter >= 10): scheduler.step() self.writer.add_scalar('cosine_lr_decay', scheduler.get_lr()[0], global_step=n_iter) def _load_pre_trained_weights(self, model): try: checkpoints_folder = os.path.join('./runs', self.config['fine_tune_from'], 'checkpoints') state_dict = torch.load(os.path.join(checkpoints_folder, 'model.pth')) model.load_state_dict(state_dict) print('Loaded pre-trained model with success.') except FileNotFoundError: print('Pre-trained weights not found. Training from scratch.') return model def _validate(self, valid_loader): with torch.no_grad(): self.model.eval() valid_loss = 0.0 counter = 0 for (xis, xjs) in valid_loader: xis = xis['data'][0].float().to(self.device) xjs = xjs['data'][0].float().to(self.device) loss = self._step(self.model, xis, xjs, counter) valid_loss += loss.item() counter += 1 valid_loss /= counter return valid_loss def load_checkpoint(self): if (self.config['saved_model_path'] is None): print('checkpoint_dir is empty, please provide directory to load checkpoint.') exit(0) else: checkpoint = torch.load(self.config['saved_model_path']) if ('model' not in checkpoint.keys()): state_dict = checkpoint else: state_dict = checkpoint['model'] self.model.load_state_dict(state_dict, strict=False) print('checkpoint state dict:', state_dict.keys()) print('model state dict:', self.model.state_dict().keys()) def infer(self): self.load_checkpoint() with torch.no_grad(): for k in range(len(self.val_keys)): key = self.val_keys[k:(k + 1)] data_loader = NumpyDataSet(self.data_dir, target_size=self.config['img_size'], batch_size=8, keys=key, do_reshuffle=False, mode='test') feature_map = [] reconstruct_img = [] prediction = [] for (i, data_batch) in enumerate(data_loader): data = data_batch['data'][0].float().to(self.device) labels = data_batch['seg'][0].long().to(self.device) slice_idx = data_batch['slice_idxs'] features = self.model(data, infer=True) img = features[0] mu = features[2] logvar = features[3] std = torch.exp((0.5 * logvar)) eps = torch.randn_like(std) features = mu features = F.normalize(features, p=2, dim=1) for j in range(features.shape[0]): feature_map.append(features[j].cpu().numpy()) reconstruct_img.append(img[j].cpu().numpy()) feature_map = np.stack(feature_map) self.save_data(feature_map, key, 'features') self.save_data(reconstruct_img, key, 'reconstruct') def save_data(self, data, key, mode): if (not os.path.exists(os.path.join(self.save_folder, mode))): os.mkdir(os.path.join(self.save_folder, mode)) save_path = os.path.join(self.save_folder, ((mode + '_') + key[0])) np.save(save_path, data)
.parametrize(('metroids', 'stronger_metroids', 'bosses', 'artifacts'), [(False, False, True, 5), (False, True, False, 15), (True, False, True, 40), (True, True, True, 40)]) def test_msr_artifact_pool_should_throw_on_invalid_config(msr_game_description, metroids, stronger_metroids, bosses, artifacts): configuration = MSRArtifactConfig(prefer_metroids=metroids, prefer_stronger_metroids=stronger_metroids, prefer_bosses=bosses, required_artifacts=artifacts) with pytest.raises(InvalidConfiguration, match='More Metroid DNA than allowed!'): artifact_pool(msr_game_description, configuration)
def set_app_menu(app_menu_list): class InternalMenu(): def __init__(self, title, parent): self.m = AppKit.NSMenu.alloc().init() self.item = AppKit.NSMenuItem.alloc().init() self.item.setSubmenu_(self.m) if (not isinstance(parent, self.__class__)): self.m.setTitle_(title) parent.addItem_(self.item) else: self.item.setTitle_(title) parent.m.addItem_(self.item) def action(self, title: str, action: Callable, command: (str | None)=None): InternalAction(self, title, action, command) return self def separator(self): self.m.addItem_(AppKit.NSMenuItem.separatorItem()) return self def sub_menu(self, title: str): return self.__class__(title, parent=self) class InternalAction(): def __init__(self, parent: InternalMenu, title: str, action: callable, command=None): self.action = action s = selector(self._call_action, signature=b':') if command: item = parent.m.addItemWithTitle_action_keyEquivalent_(title, s, command) else: item = AppKit.NSMenuItem.alloc().init() item.setAction_(s) item.setTitle_(title) parent.m.addItem_(item) item.setTarget_(self) def _call_action(self): Thread(target=self.action).start() def create_submenu(title, line_items, supermenu): m = InternalMenu(title, parent=supermenu) for menu_line_item in line_items: if isinstance(menu_line_item, MenuSeparator): m = m.separator() elif isinstance(menu_line_item, MenuAction): m = m.action(menu_line_item.title, menu_line_item.function) elif isinstance(menu_line_item, Menu): create_submenu(menu_line_item.title, menu_line_item.items, m) os_bar_menu = BrowserView.app.mainMenu() if (os_bar_menu is None): os_bar_menu = AppKit.NSMenu.alloc().init() BrowserView.app.setMainMenu_(os_bar_menu) for app_menu in app_menu_list: create_submenu(app_menu.title, app_menu.items, os_bar_menu)
class PLMInputFeatures(InputFeatures): def __init__(self, *_, perm_mask, target_mapping, **kwargs): super().__init__(**kwargs) self.perm_mask = perm_mask self.target_mapping = target_mapping def pretty_print(self, tokenizer): return (((super().pretty_print(tokenizer) + '\n') + f'''perm_mask = {self.perm_mask} ''') + f'target_mapping = {self.target_mapping}')
class TaskSetTimeHandler(TaskNewHandler): .authenticated async def get(self, taskid): user = self.current_user task = self.check_permission((await self.db.task.get(taskid, fields=('id', 'userid', 'tplid', 'disabled', 'note', 'ontime', 'ontimeflg', 'newontime'))), 'w') newontime = json.loads(task['newontime']) ontime = newontime if ('mode' not in newontime): ontime['mode'] = 'ontime' else: ontime = newontime today_date = time.strftime('%Y-%m-%d', time.localtime()) (await self.render('task_setTime.html', task=task, ontime=ontime, today_date=today_date)) .authenticated async def post(self, taskid): log = u'' try: envs = {} for key in self.request.body_arguments: envs[key] = self.get_body_arguments(key) for env in envs.keys(): if ((envs[env][0] == u'true') or (envs[env][0] == u'false')): envs[env] = (True if (envs[env][0] == u'true') else False) else: envs[env] = u'{0}'.format(envs[env][0]) async with self.db.transaction() as sql_session: if envs['sw']: c = cal() if ('time' in envs): if (len(envs['time'].split(':')) < 3): envs['time'] = (envs['time'] + ':00') tmp = c.calNextTs(envs) if (tmp['r'] == 'True'): (await self.db.task.mod(taskid, disabled=False, newontime=json.dumps(envs), next=tmp['ts'], sql_session=sql_session)) log = u',:{0}'.format(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(tmp['ts']))) else: raise Exception(tmp['r']) else: tmp = json.loads((await self.db.task.get(taskid, fields=('newontime',), sql_session=sql_session))['newontime']) tmp['sw'] = False (await self.db.task.mod(taskid, newontime=json.dumps(tmp), sql_session=sql_session)) except Exception as e: if config.traceback_print: traceback.print_exc() (await self.render('utils_run_result.html', log=str(e), title=u'', flg='danger')) logger_Web_Handler.error('TaskID: %s set Time failed! Reason: %s', taskid, str(e).replace('\\r\\n', '\r\n')) return (await self.render('utils_run_result.html', log=log, title=u'', flg='success')) return
class SelectOnRemove(MappingType): MAPPING = {'prev': (QTabBar.SelectionBehavior.SelectLeftTab, 'Select the tab which came before the closed one (left in horizontal, above in vertical).'), 'next': (QTabBar.SelectionBehavior.SelectRightTab, 'Select the tab which came after the closed one (right in horizontal, below in vertical).'), 'last-used': (QTabBar.SelectionBehavior.SelectPreviousTab, 'Select the previously selected tab.')}
class FP16Optimizer(_FP16OptimizerMixin, optim.FairseqOptimizer): def __init__(self, args, params, fp32_optimizer, fp32_params): super().__init__(args) self.fp16_params = params self.fp32_optimizer = fp32_optimizer self.fp32_params = fp32_params if (getattr(args, 'fp16_scale_window', None) is None): if (len(args.update_freq) > 1): raise ValueError('--fp16-scale-window must be given explicitly when using a custom --update-freq schedule') scale_window = int((((2 ** 14) / args.distributed_world_size) / args.update_freq[0])) else: scale_window = args.fp16_scale_window self.scaler = DynamicLossScaler(init_scale=args.fp16_init_scale, scale_window=scale_window, tolerance=args.fp16_scale_tolerance, threshold=args.threshold_loss_scale) self.min_loss_scale = self.args.min_loss_scale def build_optimizer(cls, args, params): fp32_params = cls.build_fp32_params(params) fp32_optimizer = optim.build_optimizer(args, [fp32_params]) return cls(args, params, fp32_optimizer, fp32_params) def optimizer(self): return self.fp32_optimizer.optimizer def optimizer_config(self): return self.fp32_optimizer.optimizer_config def get_lr(self): return self.fp32_optimizer.get_lr() def set_lr(self, lr): self.fp32_optimizer.set_lr(lr)
def find_extremes(cluster_list, dataset, jaccard_threshold): global _shared_dataset _shared_dataset = dataset extremes_list = [] f = partial(_find_cluster_extremes_shared, jaccard_threshold=jaccard_threshold) with mp.Pool() as pool: for extremes in tqdm(pool.imap_unordered(f, cluster_list), total=len(cluster_list)): extremes_list.append(extremes) return extremes_list
class DefinitionWideFormat(sphinx_jsonschema.wide_format.WideFormat): def _objecttype(self, schema): rows = self._simpletype(schema) rows.extend(self._objectproperties(schema, 'definition')) rows.extend(self._objectproperties(schema, 'properties')) rows.extend(self._objectproperties(schema, 'patternProperties')) rows.extend(self._bool_or_object(schema, 'additionalProperties')) rows.extend(self._kvpairs(schema, self.KV_OBJECT)) return rows def _dispatch(self, schema, label=None): rows = [] self.nesting += 1 if ('type' in schema): if ('object' in schema['type']): rows = self._objecttype(schema) elif ('array' in schema['type']): rows = self._arraytype(schema) else: rows = self._simpletype(schema) elif ('description' in schema): rows.append(self._line(self._cell(schema['description']))) if ('$ref' in schema): rows.append(self._line(self._cell(((':ref:`' + schema['$ref']) + '`').replace('#/definitions/', '')))) for k in self.COMBINATORS: if (k in schema): items = [] for s in schema[k]: items.extend(self._dispatch(s, self._cell('-'))) rows.extend(self._prepend(self._cell(k), items)) for k in self.SINGLEOBJECTS: if (k in schema): rows.extend(self._dispatch(schema[k], self._cell(k))) rows.extend(self._objectproperties(schema, 'definitions')) if (label is not None): rows = self._prepend(label, rows) self.nesting -= 1 return rows
class SimclrInfoNCECriterion(nn.Module): def __init__(self, buffer_params, temperature: float): super(SimclrInfoNCECriterion, self).__init__() self.use_gpu = (get_cuda_device_index() > (- 1)) self.temperature = temperature self.num_pos = 2 self.buffer_params = buffer_params self.criterion = nn.CrossEntropyLoss() self.dist_rank = get_rank() self.pos_mask = None self.neg_mask = None self.precompute_pos_neg_mask() logging.info(f'Creating Info-NCE loss on Rank: {self.dist_rank}') def precompute_pos_neg_mask(self): total_images = self.buffer_params.effective_batch_size world_size = self.buffer_params.world_size batch_size = (total_images // world_size) orig_images = (batch_size // self.num_pos) rank = self.dist_rank pos_mask = torch.zeros(batch_size, total_images) neg_mask = torch.zeros(batch_size, total_images) all_indices = np.arange(total_images) pos_members = (orig_images * np.arange(self.num_pos)) orig_members = torch.arange(orig_images) for anchor in np.arange(self.num_pos): for img_idx in range(orig_images): delete_inds = (((batch_size * rank) + img_idx) + pos_members) neg_inds = torch.tensor(np.delete(all_indices, delete_inds)).long() neg_mask[(((anchor * orig_images) + img_idx), neg_inds)] = 1 for pos in np.delete(np.arange(self.num_pos), anchor): pos_inds = (((batch_size * rank) + (pos * orig_images)) + orig_members) pos_mask[(torch.arange((anchor * orig_images), ((anchor + 1) * orig_images)).long(), pos_inds.long())] = 1 self.pos_mask = (pos_mask.cuda(non_blocking=True) if self.use_gpu else pos_mask) self.neg_mask = (neg_mask.cuda(non_blocking=True) if self.use_gpu else neg_mask) def forward(self, embedding: torch.Tensor): assert (embedding.ndim == 2) assert (embedding.shape[1] == int(self.buffer_params.embedding_dim)) batch_size = embedding.shape[0] T = self.temperature num_pos = self.num_pos assert ((batch_size % num_pos) == 0), 'Batch size should be divisible by num_pos' embeddings_buffer = self.gather_embeddings(embedding) similarity = torch.exp((torch.mm(embedding, embeddings_buffer.t()) / T)) pos = torch.sum((similarity * self.pos_mask), 1) neg = torch.sum((similarity * self.neg_mask), 1) loss = (- torch.mean(torch.log((pos / (pos + neg))))) return loss def __repr__(self): num_negatives = (self.buffer_params.effective_batch_size - 2) T = self.temperature num_pos = self.num_pos repr_dict = {'name': self._get_name(), 'temperature': T, 'num_negatives': num_negatives, 'num_pos': num_pos, 'dist_rank': self.dist_rank} return pprint.pformat(repr_dict, indent=2) def gather_embeddings(embedding: torch.Tensor): if (torch.distributed.is_available() and torch.distributed.is_initialized()): embedding_gathered = gather_from_all(embedding) else: embedding_gathered = embedding return embedding_gathered
class ReadInputRegistersRequest(ReadRegistersRequestBase): function_code = 4 function_code_name = 'read_input_registers' def __init__(self, address=None, count=None, slave=0, **kwargs): super().__init__(address, count, slave, **kwargs) def execute(self, context): if (not (1 <= self.count <= 125)): return self.doException(merror.IllegalValue) if (not context.validate(self.function_code, self.address, self.count)): return self.doException(merror.IllegalAddress) values = context.getValues(self.function_code, self.address, self.count) if isinstance(values, ExceptionResponse): return values return ReadInputRegistersResponse(values)
def deconv2D_layer(l0, name=None, filters=32, kernel_size=3, strides=2, padding='same', activation='relu', kernel_initializer='he_normal'): l = Conv2DTranspose(filters=filters, name=name, kernel_size=kernel_size, strides=strides, padding=padding, activation=activation, kernel_initializer=kernel_initializer)(l0) return l
def test_create_poetry_with_local_config(fixture_dir: FixtureDirGetter) -> None: poetry = Factory().create_poetry(fixture_dir('with_local_config')) assert (not poetry.config.get('virtualenvs.in-project')) assert (not poetry.config.get('virtualenvs.create')) assert (not poetry.config.get('virtualenvs.options.always-copy')) assert (not poetry.config.get('virtualenvs.options.no-pip')) assert (not poetry.config.get('virtualenvs.options.no-setuptools')) assert (not poetry.config.get('virtualenvs.options.system-site-packages'))
def DFOM(pattern, ItemS): count = 0 Nettree = [[[] for i in range(SeqNum)] for k in range(len(pattern))] unit = [[] for k in range(len(pattern))] for i in range(len(pattern)): unit[i] = ItemS[str(pattern[i])] for i in range(SeqNum): for m in range(len(unit[0][i])): bbb = 0 Nettree[0][i].append(unit[0][i][m]) for j in range(1, len(unit)): n = 1 if (unit[j][i] == []): bbb = 1 break else: for k in range(len(unit[j][i])): if (Nettree[j][i] == []): aaa = (- 1) else: aaa = Nettree[j][i][(- 1)] if ((unit[j][i][k] > Nettree[(j - 1)][i][(- 1)]) and (unit[j][i][k] > aaa)): Nettree[j][i].append(unit[j][i][k]) if (j == (len(unit) - 1)): count += 1 n = 0 break if n: bbb = 1 break if bbb: break return count
def main(): try: session = saga.Session() print('Connecting...') js = saga.job.Service('pbs+ssh://sierra.futuregrid.org', session=session) print('CONNECTED') jd = saga.job.Description() jd.queue = 'batch' jd.environment = {'RUNTIME': '5'} jd.wall_time_limit = 1 jd.executable = '/bin/sleep' jd.arguments = ['$RUNTIME'] sleepjob = js.create_job(jd) print(('Job ID : %s' % sleepjob.id)) print(('Job State : %s' % sleepjob.state)) print('\n...starting job...\n') sleepjob.run() print(('Job ID : %s' % sleepjob.id)) print(('Job State : %s' % sleepjob.state)) print('\nListing active jobs: ') for job in js.list(): print((' * %s' % job)) sleebjob_clone = js.get_job(sleepjob.id) print('\n...waiting for job...\n') sleebjob_clone.wait() print(('Job State : %s' % sleebjob_clone.state)) print(('Exitcode : %s' % sleebjob_clone.exit_code)) print(('Exec. hosts : %s' % sleebjob_clone.execution_hosts)) print(('Create time : %s' % sleebjob_clone.created)) print(('Start time : %s' % sleebjob_clone.started)) print(('End time : %s' % sleebjob_clone.finished)) return 0 except saga.SagaException as ex: print(('An exception occured: (%s) %s ' % (ex.type, str(ex)))) print((' \n*** Backtrace:\n %s' % ex.traceback)) return (- 1)
def main(): parser = argparse.ArgumentParser(description='Convert model keys') parser.add_argument('src', help='src detectron model path') parser.add_argument('dst', help='save path') parser.add_argument('depth', type=int, help='ResNet model depth') args = parser.parse_args() convert(args.src, args.dst, args.depth)
.parametrize('const_shape', [(), (1,), (5,), (1, 5), (2, 5)]) .parametrize('op, np_op', [(pt.pow, np.power), (pt.add, np.add)]) def test_local_inline_composite_constants(op, np_op, const_shape): const = np.full(shape=const_shape, fill_value=2.5).astype(config.floatX) x = vector('x') y = vector('y') out = (pt.exp(op(x, const)) + y) fn = pytensor.function([x, y], out, mode=get_default_mode().including('specialize', 'fusion')) [node] = [node for node in fn.maker.fgraph.apply_nodes if isinstance(node.op, Elemwise)] assert isinstance(node.op.scalar_op, Composite) assert (len(node.inputs) == 2) x_test_value = np.arange(5).astype(config.floatX) y_test_value = np.ones(5).astype(config.floatX) np.testing.assert_allclose(fn(x_test_value, y_test_value), (np.exp(np_op(x_test_value, const)) + y_test_value))
class Conv8(nn.Module): def __init__(self): super(Conv8, self).__init__() builder = get_builder() self.convs = nn.Sequential(builder.conv3x3(3, 64, first_layer=True), nn.ReLU(), builder.conv3x3(64, 64), nn.ReLU(), nn.MaxPool2d((2, 2)), builder.conv3x3(64, 128), nn.ReLU(), builder.conv3x3(128, 128), nn.ReLU(), nn.MaxPool2d((2, 2)), builder.conv3x3(128, 256), nn.ReLU(), builder.conv3x3(256, 256), nn.ReLU(), nn.MaxPool2d((2, 2)), builder.conv3x3(256, 512), nn.ReLU(), builder.conv3x3(512, 512), nn.ReLU(), nn.MaxPool2d((2, 2))) self.linear = nn.Sequential(builder.conv1x1(((512 * 2) * 2), 256), nn.ReLU(), builder.conv1x1(256, 256), nn.ReLU(), builder.conv1x1(256, 10)) def forward(self, x): out = self.convs(x) out = out.view(out.size(0), ((512 * 2) * 2), 1, 1) out = self.linear(out) return out.squeeze()
class ChannelPutSchema(BaseSchema): token_address = AddressField(required=True) partner_address = AddressField(required=True) reveal_timeout = IntegerToStringField(missing=None) settle_timeout = IntegerToStringField(missing=None) total_deposit = IntegerToStringField(default=None, missing=None)
class Effect7074(BaseEffect): type = 'passive' def handler(fit, container, context, projectionRange, **kwargs): level = (container.level if ('skill' in context) else 1) fit.modules.filteredItemBoost((lambda mod: mod.item.requiresSkill('Small Disintegrator Specialization')), 'damageMultiplier', (container.getModifiedItemAttr('damageMultiplierBonus') * level), **kwargs)
class Effect5622(BaseEffect): type = 'passive' def handler(fit, ship, context, projectionRange, **kwargs): fit.modules.filteredItemBoost((lambda mod: (mod.item.group.name == 'Missile Launcher Torpedo')), 'speed', ship.getModifiedItemAttr('shipBonusMB'), skill='Minmatar Battleship', **kwargs)
class RequiredImgAsset(RequiredAssetMixin, BaseRequiredImgAsset, BenefitFeature): class Meta(BaseRequiredImgAsset.Meta, BenefitFeature.Meta): verbose_name = 'Require Image' verbose_name_plural = 'Require Images' def __str__(self): return f'Require image' def as_form_field(self, **kwargs): help_text = kwargs.pop('help_text', self.help_text) label = kwargs.pop('label', self.label) required = kwargs.pop('required', False) return forms.ImageField(required=required, help_text=help_text, label=label, widget=forms.ClearableFileInput, **kwargs)
class DomainGeometricParameters(BaseParameters): def __init__(self, domain, main_param): self.domain = domain self.main_param = main_param if (self.domain != 'separator'): self.prim = ParticleGeometricParameters(domain, 'primary', main_param) self.sec = ParticleGeometricParameters(domain, 'secondary', main_param) self.phase_params = {'primary': self.prim, 'secondary': self.sec} else: self.phase_params = {} def _set_parameters(self): for phase in self.phase_params.values(): phase._set_parameters() if (self.domain == 'separator'): self.L = pybamm.Parameter('Separator thickness [m]') self.b_e = pybamm.Parameter('Separator Bruggeman coefficient (electrolyte)') return Domain = self.domain.capitalize() self.L_cc = pybamm.Parameter(f'{Domain} current collector thickness [m]') self.L = pybamm.Parameter(f'{Domain} electrode thickness [m]') self.L_tab = pybamm.Parameter(f'{Domain} tab width [m]') self.centre_y_tab = pybamm.Parameter(f'{Domain} tab centre y-coordinate [m]') self.centre_z_tab = pybamm.Parameter(f'{Domain} tab centre z-coordinate [m]') self.A_tab = (self.L_tab * self.L_cc) self.b_e = pybamm.Parameter(f'{Domain} electrode Bruggeman coefficient (electrolyte)') self.b_s = pybamm.Parameter(f'{Domain} electrode Bruggeman coefficient (electrode)')
def initial_wavefunction(particle): return (((np.exp((((- 1) / (4 * ( ** 2))) * (((particle.x + (100 * A)) ** 2) + (1 * ((particle.y - 250) ** 2))))) / np.sqrt(((2 * np.pi) * ( ** 2)))) * np.exp(((p1_x0 * particle.x) * 1j))) + ((np.exp((((- 1) / (4 * ( ** 2))) * (((particle.x + (100 * A)) ** 2) + (1 * ((particle.y + 250) ** 2))))) / np.sqrt(((2 * np.pi) * ( ** 2)))) * np.exp(((p2_x0 * particle.x) * 1j))))
def plot(genotype, filename): g = Digraph(format='pdf', edge_attr=dict(fontsize='20', fontname='times'), node_attr=dict(style='filled', shape='rect', align='center', fontsize='20', height='0.5', width='0.5', penwidth='2', fontname='times'), engine='dot') g.body.extend(['rankdir=LR']) g.node('x_{t}', fillcolor='darkseagreen2') g.node('h_{t-1}', fillcolor='darkseagreen2') g.node('0', fillcolor='lightblue') g.edge('x_{t}', '0', fillcolor='gray') g.edge('h_{t-1}', '0', fillcolor='gray') steps = len(genotype) for i in range(1, (steps + 1)): g.node(str(i), fillcolor='lightblue') for (i, (op, j)) in enumerate(genotype): g.edge(str(j), str((i + 1)), label=op, fillcolor='gray') g.node('h_{t}', fillcolor='palegoldenrod') for i in range(1, (steps + 1)): g.edge(str(i), 'h_{t}', fillcolor='gray') g.render(filename, view=False)
class Mlp(nn.Module): def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.0): super().__init__() out_features = (out_features or in_features) hidden_features = (hidden_features or in_features) self.fc1 = nn.Conv2d(in_features, hidden_features, 1) self.act = act_layer() self.fc2 = nn.Conv2d(hidden_features, out_features, 1) self.drop = nn.Dropout(drop) self.apply(self._init_weights) def _init_weights(self, m): if isinstance(m, nn.Conv2d): trunc_normal_(m.weight, std=0.02) if (m.bias is not None): nn.init.constant_(m.bias, 0) def forward(self, x): x = self.fc1(x) x = self.act(x) x = self.drop(x) x = self.fc2(x) x = self.drop(x) return x
def render_notebook_cells(nbspec: NotebookSpec) -> NbCells: return NbCells(title_cell=_md_nbnode('\n'.join(_get_title_lines(title=nbspec.title, mod=nbspec.module)), cqid='title_cell'), top_imports=_code_nbnode(_IMPORTS, cqid='top_imports'), gate_cells={gspec.cqid: _GateCells(md=_md_nbnode('\n'.join(get_markdown_docstring_lines(cls=gspec.gate_cls)), cqid=f'{gspec.cqid}.md'), py=_code_nbnode(_get_code_for_demoing(gspec), cqid=f'{gspec.cqid}.py')) for gspec in nbspec.gate_specs})
def get_extract_label(art_sents, abs_sents): extracted = [] scores = [] indices = list(range(len(art_sents))) for abst in abs_sents: rouges = list(map(compute_rouge_l(reference=abst, mode='r'), art_sents)) ext = max(indices, key=(lambda i: rouges[i])) indices.remove(ext) extracted.append(ext) scores.append(rouges[ext]) if (not indices): break return (extracted, scores)
class Testuser(): def test_min_threshold_dist_from_shapefile(self): f = examples.get_path('columbus.shp') min_d = user.min_threshold_dist_from_shapefile(f) assert (min_d == pytest.approx(0.)) def test_build_lattice_shapefile(self): of = 'lattice.shp' user.build_lattice_shapefile(20, 20, of) w = Rook.from_shapefile(of) assert (w.n == 400) os.remove('lattice.dbf') os.remove('lattice.shp') os.remove('lattice.shx')
class PFSTS_With_Ksappend(ParserTest): def __init__(self, *args, **kwargs): ParserTest.__init__(self, *args, **kwargs) self.ks = '\nlang en_US\nkeyboard us\nautopart\n' self.ksappend = '\ntimezone America/New_York\n' def setUp(self): ParserTest.setUp(self) (handle, self._ksappendPath) = tempfile.mkstemp(prefix='ksappend-', text=True) s = self.ksappend.encode('utf-8') os.write(handle, s) os.close(handle) def tearDown(self): ParserTest.tearDown(self) os.unlink(self._ksappendPath) def runTest(self): processed = preprocessFromStringToString(((self.ks + '%ksappend ') + self._ksappendPath)) self.assertEqual(processed.decode(), (self.ks + self.ksappend))
class Fraction(): def __init__(self, fraction=None, chntext=None): self.fraction = fraction self.chntext = chntext def chntext2fraction(self): (denominator, numerator) = self.chntext.split('') return ((chn2num(numerator) + '/') + chn2num(denominator)) def fraction2chntext(self): (numerator, denominator) = self.fraction.split('/') return ((num2chn(denominator) + '') + num2chn(numerator))
def evaluate_single_sub(sub_id): truth = nib.load(os.path.joint('../data/preprocessed/HGG', sub_id, 'truth.nii.gz')).get_data() prediction = nib.load(os.path.joint('prediction', sub_id, (sub_id + '.nii.gz'))).get_data() masking_functions = (get_whole_tumor_mask, get_tumor_core_mask, get_enhancing_tumor_mask) return [dice_coefficient(func(truth), func(prediction)) for func in masking_functions]
def cast_tensor_type(inputs, src_type, dst_type): if isinstance(inputs, nn.Module): return inputs elif isinstance(inputs, torch.Tensor): return inputs.to(dst_type) elif isinstance(inputs, str): return inputs elif isinstance(inputs, np.ndarray): return inputs elif isinstance(inputs, abc.Mapping): return type(inputs)({k: cast_tensor_type(v, src_type, dst_type) for (k, v) in inputs.items()}) elif isinstance(inputs, abc.Iterable): return type(inputs)((cast_tensor_type(item, src_type, dst_type) for item in inputs)) else: return inputs
def compute_KMM(Xsamples, sigma, noise, l_vec): m = len(Xsamples) Xbar = np.multiply(Xsamples, np.array(np.sqrt(([l_vec] * m)))) Qbar = np.array(([np.sum(np.multiply(Xbar, Xbar), axis=1)] * m)).T distance = ((Qbar + Qbar.T) - (2 * np.dot(Xbar, Xbar.T))) result = ((sigma * np.exp(((- 0.5) * distance))) + (np.eye(m) * (noise + (sigma * (10 ** (- 10)))))) return result
def load_from_file(filename): vehicles_loaded = 0 try: with open(filename, 'r') as f: lines = f.readlines() for line in lines: index = 0 index = line.split(',') if (len(index) < 1): continue vehicletype = int(index[0]) spawn_x = float(index[1]) spawn_y = float(index[2]) spawn_z = float(index[3]) spawn_rot = float(index[4]) color_1 = int(index[5]) color_2 = int(index[6].split(';')[0]) if ((vehicletype < 400) or (vehicletype > 611)): continue if ((index[0] == (- 1)) or (index[1] == (- 1)) or (index[2] == (- 1)) or (index[3] == (- 1)) or (index[4] == (- 1))): continue add_static_vehicle_ex(vehicletype, spawn_x, spawn_y, spawn_z, spawn_rot, color_1, color_2, 1800) vehicles_loaded += 1 f.close() print(f'Loaded {int(vehicles_loaded)} vehicles from: {filename}') except IOError as error: print(error)
def test_python_tags(wheelpath): newname = tags(str(wheelpath), python_tags='py3') assert (TESTWHEEL_NAME.replace('py2.py3', 'py3') == newname) output_file = (wheelpath.parent / newname) with WheelFile(str(output_file)) as f: output = f.read((f.dist_info_path + '/WHEEL')) assert (output == b'Wheel-Version: 1.0\nGenerator: bdist_wheel (0.30.0)\nRoot-Is-Purelib: false\nTag: py3-none-any\n\n') output_file.unlink() newname = tags(str(wheelpath), python_tags='py2.py3') assert (TESTWHEEL_NAME == newname) newname = tags(str(wheelpath), python_tags='+py4', remove=True) assert (not wheelpath.exists()) assert (TESTWHEEL_NAME.replace('py2.py3', 'py2.py3.py4') == newname) output_file = (wheelpath.parent / newname) output_file.unlink()