content
stringlengths
0
894k
type
stringclasses
2 values
from .annotation import Sentence from nltk.tokenize.punkt import PunktParameters from nltk.tokenize.punkt import PunktSentenceTokenizer from .ru.processor_tokenizer_ru import _ru_abbrevs from .en.processor_tokenizer_nltk_en import _en_abbrevs from itertools import combinations class ProcessorSentenceSplitter: """Performs sentence splitting using simple rules. Simple wrapper around NLTK component. Suitable for european languages. """ def __init__(self, delay_init = False): self.sent_tokeniser_ = None if not delay_init: self.init() def init(self): if self.sent_tokeniser_ is None: punkt_param = PunktParameters() punkt_param.abbrev_types = self.compile_abbreviations() self.sent_tokeniser_ = PunktSentenceTokenizer(punkt_param) def __call__(self, tokens): assert self.sent_tokeniser_ sents = self.sent_tokeniser_.sentences_from_tokens((e.text for e in tokens)) curr = 0 res_sents = list() for sent in sents: res_sents.append(Sentence(curr, curr + len(sent))) curr += len(sent) return res_sents def compile_abbreviations(self): def get_dot_pairs(alphabet): return ['.'.join(abbrev) for abbrev in list(combinations(alphabet, 2))] def clean_regexps(regexps): return [''.join(abbrev.lower().split('.')[:-1]).replace('\\', '').replace(u'\xad', '').replace(' ', '.').replace('?', ' ').lower() for abbrev in regexps] ru_abbrevs = get_dot_pairs('цукенгшзхфвапролджэячсмитбю') ru_abbrevs += clean_regexps(_ru_abbrevs) en_abbrevs = get_dot_pairs('qwertyuiopasdfghjklzxcvbnm') en_abbrevs += clean_regexps(_en_abbrevs) return list(set(ru_abbrevs + en_abbrevs))
python
import datetime from django.db import IntegrityError from django.db.models import Min, Max from django.utils.timezone import make_aware from zabiegi import models def integruj_jednostki(): for w in ( models.WykazStrona1.objects.all() .exclude(dane_operacji_jednostka_wykonująca_kod=None) .values_list("dane_operacji_jednostka_wykonująca_kod", flat=True) .distinct() ): models.Jednostka.objects.get_or_create(kod=w) def integruj_procedury(): for kod, nazwa in ( models.WykazStrona1.objects.all() .exclude(procedury_medyczne_kod_procedury=None) .values_list("procedury_medyczne_kod_procedury", "procedury_medyczne_nazwa") .distinct() ): try: models.Procedura.objects.get_or_create(kod=kod, nazwa=nazwa) except IntegrityError: n = models.Procedura.objects.get(kod=kod) raise ValueError( f"Procedura juz istnieje {kod}, probowano nazwy {nazwa}, jest {n.nazwa}" ) def integruj_lekarzy(): for ( personel_uczestniczący_imiona, personel_uczestniczący_nazwisko, personel_uczestniczący_kod, ) in ( models.WykazStrona1.objects.exclude(personel_uczestniczący_kod=None) .values_list( "personel_uczestniczący_imiona", "personel_uczestniczący_nazwisko", "personel_uczestniczący_kod", ) .distinct() ): models.Lekarz.objects.get_or_create( kod=personel_uczestniczący_kod, nazwisko=personel_uczestniczący_nazwisko, imiona=personel_uczestniczący_imiona, ) def integruj_pacjentow(): for (dane_pacjenta_identyfikator_pacjenta_mip, dane_pacjenta_data_urodzenia,) in ( models.WykazStrona1.objects.exclude( dane_pacjenta_identyfikator_pacjenta_mip=None ) .values_list( "dane_pacjenta_identyfikator_pacjenta_mip", "dane_pacjenta_data_urodzenia" ) .distinct() ): models.Pacjent.objects.get_or_create( mip=dane_pacjenta_identyfikator_pacjenta_mip, data_urodzenia=dane_pacjenta_data_urodzenia, ) def integruj_znieczulenia(): for w in models.WykazStrona1.objects.exclude(l_p=None): poczatek = datetime.datetime.combine( w.element_operacji_data_wykonania.date(), w.element_operacji_czas_wykonania.time(), ) poczatek = make_aware(poczatek) koniec = None if w.element_operacji_czas_zakończenia is not None: koniec = datetime.datetime.combine( w.element_operacji_data_wykonania.date(), w.element_operacji_czas_zakończenia.time(), ) koniec = make_aware(koniec) if koniec < poczatek: koniec += datetime.timedelta(days=1) jednostka = models.Jednostka.objects.get( kod=w.dane_operacji_jednostka_wykonująca_kod ) pacjent = models.Pacjent.objects.get( mip=w.dane_pacjenta_identyfikator_pacjenta_mip ) z, created = models.Znieczulenie.objects.get_or_create( nr=w.dane_operacji_księga_nr, poczatek=poczatek, koniec=koniec, czas_trwania=w.element_operacji_czas_trwania_w_minutach, jednostka=jednostka, pacjent=pacjent, ) lekarz = models.Lekarz.objects.get(kod=w.personel_uczestniczący_kod) if lekarz not in z.lekarze.all(): z.lekarze.add(lekarz) procedura = models.Procedura.objects.get(kod=w.procedury_medyczne_kod_procedury) if procedura not in z.procedury.all(): z.procedury.add(procedura) z.save() def pokaz_statystyki(): z = models.Znieczulenie.objects.all().aggregate( min=Min("poczatek"), max=Max("poczatek") ) print(f"Analizowany okres od: {z['min'].date()} do {z['max'].date()}") print(f"Liczba znieczuleń ogółem: {models.Znieczulenie.objects.count()}") print(f"Znieczulenia wg procedury: ") for p in models.Procedura.objects.all(): print( f"{p.nazwa},{models.Znieczulenie.objects.filter(procedury__kod=p.kod).count()}" ) print("Znieczulenia wg jednostek i procedur:") print(",".join([p.nazwa for p in models.Procedura.objects.all()])) for j in models.Jednostka.objects.all(): row = [] for p in models.Procedura.objects.all(): row.append( models.Znieczulenie.objects.filter(jednostka=j, procedury=p).count() ) print(f"{j.kod}," + ",".join([str(x) for x in row])) print("Znieczulenia wg miesiąca i jednostki") print(",".join(["lip", "sie", "wrz", "paź", "lis", "gru"])) for j in models.Jednostka.objects.all(): row = [] for miesiac in range(7, 13): row.append( models.Znieczulenie.objects.filter( jednostka=j, poczatek__month=miesiac ).count() ) print(f"{j.kod}," + ",".join([str(x) for x in row])) def integruj_wszystko(): integruj_jednostki() integruj_procedury() integruj_lekarzy() integruj_pacjentow() integruj_znieczulenia()
python
#!/usr/local/bin/python # # Copyright (c) 2009-2014 Sippy Software, Inc. All rights reserved. # # All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation and/or # other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import socket, sys, getopt from functools import reduce def cli_client(address, argv, tcp = False): if not tcp: s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) else: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(address) command = reduce(lambda x, y: x + ' ' + y, argv) s.send(command.encode('ascii') + b'\nquit\n') while True: data = s.recv(1024) if len(data) == 0: break sys.stdout.write(data.decode('ascii')) def usage(): print('usage: rtp_cluster_client.py [-s cmdfile]') sys.exit(1) if __name__ == '__main__': try: opts, args = getopt.getopt(sys.argv[1:], 's:') except getopt.GetoptError: usage() if len(args) == 0: usage() cmdfile = 'unix:/var/run/rtp_cluster.sock' for o, a in opts: if o == '-s': cmdfile = a.strip() continue if cmdfile.startswith('tcp:'): parts = cmdfile[4:].split(':', 1) if len(parts) == 1: address = (parts[0], 12345) else: address = (parts[0], int(parts[1])) cli_client(address, args, tcp = True) else: if cmdfile.startswith('unix:'): cmdfile = cmdfile[5:] cli_client(cmdfile, args)
python
from datasets import load_dataset cord = load_dataset("katanaml/cord") # labels = cord['train'].features['ner_tags'].feature.names # id2label = {v: k for v, k in enumerate(labels)} label2id = {k: v for v, k in enumerate(labels)} # from PIL import Image from transformers import LayoutLMv2Processor from datasets import Features, Sequence, ClassLabel, Value, Array2D, Array3D processor = LayoutLMv2Processor.from_pretrained("microsoft/layoutlmv2-base-uncased", revision="no_ocr") # we need to define custom features features = Features({ 'image': Array3D(dtype="int64", shape=(3, 224, 224)), 'input_ids': Sequence(feature=Value(dtype='int64')), 'attention_mask': Sequence(Value(dtype='int64')), 'token_type_ids': Sequence(Value(dtype='int64')), 'bbox': Array2D(dtype="int64", shape=(512, 4)), 'labels': Sequence(ClassLabel(names=labels)), }) def preprocess_data(examples): images = [Image.open(path).convert("RGB") for path in examples['image_path']] words = examples['words'] boxes = examples['bboxes'] word_labels = examples['ner_tags'] encoded_inputs = processor(images, words, boxes=boxes, word_labels=word_labels, padding="max_length", truncation=True) return encoded_inputs train_dataset = cord['train'].map(preprocess_data, batched=True, remove_columns=cord['train'].column_names, features=features) test_dataset = cord['test'].map(preprocess_data, batched=True, remove_columns=cord['test'].column_names, features=features) # train_dataset.set_format(type="torch") test_dataset.set_format(type="torch") # from torch.utils.data import DataLoader train_dataloader = DataLoader(train_dataset, batch_size=4, shuffle=True) test_dataloader = DataLoader(test_dataset, batch_size=1) # batch = next(iter(train_dataloader)) for k, v in batch.items(): print(k, v.shape) # from transformers import LayoutLMv2ForTokenClassification, TrainingArguments, Trainer from datasets import load_metric import numpy as np model = LayoutLMv2ForTokenClassification.from_pretrained('microsoft/layoutlmv2-base-uncased', num_labels=len(label2id)) # Set id2label and label2id model.config.id2label = id2label model.config.label2id = label2id # Metrics metric = load_metric("seqeval") return_entity_level_metrics = True def compute_metrics(p): predictions, labels = p predictions = np.argmax(predictions, axis=2) # Remove ignored index (special tokens) true_predictions = [ [id2label[p] for (p, l) in zip(prediction, label) if l != -100] for prediction, label in zip(predictions, labels) ] true_labels = [ [id2label[l] for (p, l) in zip(prediction, label) if l != -100] for prediction, label in zip(predictions, labels) ] results = metric.compute(predictions=true_predictions, references=true_labels) if return_entity_level_metrics: # Unpack nested dictionaries final_results = {} for key, value in results.items(): if isinstance(value, dict): for n, v in value.items(): final_results[f"{key}_{n}"] = v else: final_results[key] = value return final_results else: return { "precision": results["overall_precision"], "recall": results["overall_recall"], "f1": results["overall_f1"], "accuracy": results["overall_accuracy"], } class CordTrainer(Trainer): def get_train_dataloader(self): return train_dataloader def get_test_dataloader(self, test_dataset): return test_dataloader args = TrainingArguments( output_dir="layoutlmv2-finetuned-cord", # name of directory to store the checkpoints max_steps=10, # we train for a maximum of 1,000 batches warmup_ratio=0.1, # we warmup a bit fp16=False, # we use mixed precision (less memory consumption), False when on CPU # push_to_hub=False, # after training, we'd like to push our model to the hub # push_to_hub_model_id=f"layoutlmv2-finetuned-cord", # this is the name we'll use for our model on the hub ) # Initialize our Trainer trainer = CordTrainer( model=model, args=args, compute_metrics=compute_metrics, ) # trainer.train() # predictions, labels, metrics = trainer.predict(test_dataset) # print(metrics) def process_document(image): print('PROCESS DOCUMENT') return image
python
# Generated by Django 3.2.6 on 2021-09-18 04:13 from django.db import migrations class Migration(migrations.Migration): dependencies = [ ('registration', '0011_auto_20210917_0032'), ] operations = [ migrations.DeleteModel( name='Links', ), migrations.RemoveField( model_name='data', name='state', ), ]
python
# coding: utf-8 import scipy import json import re import traceback import allennlp from allennlp.predictors.predictor import Predictor import sys from allennlp.commands.elmo import ElmoEmbedder from spacy.lang.en import English import numpy as np # import tensorflow as tf import torch from hyperpara import * import dgl from tqdm import tqdm # class Logger(object): # def __init__(self, filename='default.log', stream=sys.stdout): # self.terminal = stream # self.log = open(filename, 'w') # # def write(self, message): # self.terminal.write(message) # self.log.write(message) # # def flush(self): # pass # # sys.stdout = Logger(args.graph_gen_out_file+'_generation.log', sys.stdout) # sys.stderr = Logger(args.graph_gen_out_file+'_generation_err.log', sys.stderr) # Setting for Elmo Embedder - CHANGE THE PATH # options_file = args.project_address+'mlp_project/src/elmo_2x4096_512_2048cnn_2xhighway_options.json' # weight_file = args.project_address+'mlp_project/src/elmo_2x4096_512_2048cnn_2xhighway_weights' options_file = '/home/watsonzhouanda/multihop/src/elmo_2x4096_512_2048cnn_2xhighway_options.json' weight_file = '/home/watsonzhouanda/multihop/src/elmo_2x4096_512_2048cnn_2xhighway_weights' # Initialization for each module nlp = English() ee = ElmoEmbedder( options_file=options_file, weight_file=weight_file) # predictor = Predictor.from_path(args.project_address+'mlp_project/src/coref-model-2018.02.05.tar.gz') predictor = Predictor.from_path('/home/watsonzhouanda/multihop/src/coref-model-2018.02.05.tar.gz') print('Pre-trained modules init', flush=True) def compute_coref(s): try: ''' { "document": [tokenised document text] "clusters": [ [ [start_index, end_index], [start_index, end_index] ], [ [start_index, end_index], [start_index, end_index], [start_index, end_index], ], .... ] } ''' ret = predictor.predict(s) return ret['clusters'], ret['document'] except RuntimeError: return [], [str(w) for w in nlp(s)] MODE = args.graph_gen_mode num_data = args.graph_gen_size # DATA_ADD = args.project_address+"mlp_project/dataset/qangaroo_v1.1/"+args.dataset+"/" # in_file = DATA_ADD+MODE+".json" # GRAPH_ADD = args.project_address+"mlp_project/graph/" DATA_ADD = '/home/watsonzhouanda/multihop/dataset/qangaroo_v1.1/'+args.dataset+"/" in_file = DATA_ADD+MODE+".json" GRAPH_ADD = '/home/watsonzhouanda/multihop/graph/' with open(in_file, 'r') as f: data = json.load(f) # print('Dataset loaded! with size:', len(data[30000:43738]), flush=True) print('Dataset loaded! with size:', len(data), flush=True) def regex(text): text = text.replace(u'\xa0', ' ') text = text.translate(str.maketrans({key: ' {0} '.format(key) for key in '"!&()*+,/:;<=>?[]^`{|}~'})) text = re.sub('\s{2,}', ' ', text).replace('\n', '') return text def check(s, wi, c): return sum([s[wi + j].lower() == c_ for j, c_ in enumerate(c) if wi + j < len(s)]) == len(c) # c_i, c: entity in entitise set {s} U C_q (entites in canddiate answers and query) # s_i, s: tokenized support document in supports # wi, w: word in document s # Turns (tokenized docu, word_i in original doc, candidate i) def ind(si, wi, ci, c): return [[si, wi + i, ci] for i in range(len(c))] graph_set = [] if num_data == -1: num_data = len(data) print("Note: Now you are generating the full graph dataset! in "+args.dataset, flush=True) else: print("Note: Now you are generating tiny graph dataset! in "+args.dataset, flush=True) rm_list = [] # for i_d, d in enumerate(tqdm(data[30000:43738])): for i_d, d in enumerate(tqdm(data)): # # # Test mode # if i_d != 2: # continue g = dgl.DGLGraph() try: # Processing the query and candidate entities, find C_q U {s} d['candidates_orig'] = list(d['candidates']) # record the original candidate d['candidates'] = [c for c in d['candidates'] if c not in nlp.Defaults.stop_words] d['candidates'] = [[str(w) for w in c] for c in nlp.pipe(d['candidates'])] d['query'] = [str(w) for w in nlp.tokenizer(d['query'])][1:] # discard the sample accroding to De Cao if (len(d['query']) > max_query_size) or (len(d['candidates']) > max_candidates): rm_list.append((i_d, d['id'])) print("Discard sample because query or candidates length over limitation, ID:",(i_d, d['id']), flush=True) graph_set.append(dgl.DGLGraph()) continue entities_set = d['candidates'] + [d['query']] # C_q U {s} # Document level coreference prediction # First preprocess the document d['supports'] = [regex(s) for s in d['supports']] coref_temp = [compute_coref(support_doc) for support_doc in d['supports']] entities_span_in_docs = [e for _, e in coref_temp] # [tokenised document text for each document], entities span S_q coref_cluster_in_docs = [e for e, _ in coref_temp] # [corefernt spans for each cluster in each document] d['coref'] = [[[[f, []] for f in e] for e in s] for s in coref_cluster_in_docs] #[support_doc_id, cluster_id, span_id] # c_i, c: entity in entitise set {s} U C_q (entites in canddiate answers and query) # s_i, s: tokenized support document in supports # wi, w: word in document s # shape: [num_supports, i in entities set, tuple] # tuple: (#doc, position in doc, id of c in entities set) exact_match_doc2entity_set = [[ind(si, wi, ci, c) for wi, w in enumerate(s) for ci, c in enumerate(entities_set) if check(s, wi, c)] for si, s in enumerate(entities_span_in_docs)] exact_match_entity_spans = [] # [cid, start, end, doc_id] for support_doc_id in range(len(exact_match_doc2entity_set)): if len(exact_match_doc2entity_set[support_doc_id]) == 0: continue for c_i, exact_matched_entities in enumerate(exact_match_doc2entity_set[support_doc_id]): for loc_i, loc in enumerate(exact_matched_entities): # print(loc) doc_id = loc[0] doc_ent_loc = loc[1] id_in_entities = loc[2] # span.append(d['supports'][doc_id][doc_ent_loc]) # entity_in_supdoc_id = torch.Tensor(exact_matched_entities[0][0]) doc_id = torch.tensor(exact_matched_entities[0][0], dtype=torch.int32).unsqueeze(0) entities_id = exact_matched_entities[0][-1] # print([entities_id, exact_matched_entities[0][1],exact_matched_entities[-1][1],support_doc_id]) exact_match_entity_spans.append([entities_id, exact_matched_entities[0][1],exact_matched_entities[-1][1],support_doc_id]) # Compute coreference # print("--------------------------") # print("NEXT WE START ADDING COREFERENCE NODES!") # print("--------------------------") # Find the nodes that entities in entities_set has corefrent in coreference prediction coref_nodes = [] for sc, sm in zip(d['coref'], exact_match_doc2entity_set): # overloop (entity id, loc, doc_id) u = [] # doc for ni, n in enumerate(sm): # overloop each match entities (entity id, loc, doc_id) k = [] for cli, cl in enumerate(sc): # overloop coref clusters coref_loc = [[co[0], co[1]] for co, cll in cl] x = [(n[0][1] <= co[0] <= n[-1][1]) or (co[0] <= n[0][1] <= co[1]) for co, cll in cl] # i: entity id for i, v in filter(lambda y: y[1], enumerate(x)): k.append((cli, i)) # De cao's : cluster - entities - loc start - loc end # cl[i][1].append(ni) u.append(k) coref_nodes.append(u) # remove one entity with multiple coref for sli, sl in enumerate(coref_nodes): # loop sup document for ni, n in enumerate(sl): # loop entities to coref if len(n) > 1: for e0, e1 in n: i = d['coref'][sli][e0][e1][1].index(ni) del d['coref'][sli][e0][e1][1][i] sl[ni] = [] # remove one coref with multiple entity for ms, cs in zip(coref_nodes, d['coref']): for cli, cl in enumerate(cs): for eli, (el, li) in enumerate(cl): if len(li) > 1: for e in li: i = ms[e].index((cli, eli)) del ms[e][i] cl[eli][1] = [] ## Check here d['edges_coref'] = [] for si, (ms, cs) in enumerate(zip(exact_match_doc2entity_set, d['coref'])): tmp = [] for cl in cs: cand = {ms[n[0]][0][-1] for p, n in cl if n} if len(cand) == 1: cl_ = [] for (p0, p1), _ in cl: if not _: cl_.append(len(ms)) ms.append([[si, i, list(cand)[0]] for i in range(p0, p1 + 1)]) else: cl_.append(_[0]) tmp.append(cl_) d['edges_coref'].append(tmp) # print("coref_nodes:", coref_nodes) nodes_id_name = [] c = 0 for e in [[[x[-1] for x in c][0] for c in s] for s in exact_match_doc2entity_set]: u = [] for f in e: u.append((c, f)) c +=1 nodes_id_name.append(u) mask_ = [[x[:-1] for x in f] for e in exact_match_doc2entity_set for f in e] # print("len mask",len(mask_)) # print(mask_) record_of_loc_span = [] for node_i, node in enumerate(mask_): node_span = [] loc_span = [] doc_id = -1 for i, unit in enumerate(node): doc_id, loc = unit[0], unit[1] node_span.append(entities_span_in_docs[doc_id][loc]) loc_span.append(loc) item = (doc_id, loc_span, node_span) record_of_loc_span.append(item) candidates, _ = ee.batch_to_embeddings(entities_span_in_docs) # select out the words (entities) we want d['nodes_elmo'] = [(candidates.transpose(2, 1)[torch.tensor(m,dtype=torch.float).T.tolist()]) for m in mask_] # change second and first dimension for e in d['nodes_elmo']: t0, t1 = e[:,2,512:].clone(), e[:,1,512:].clone() e[:,1,512:], e[:,2,512:] = t0, t1 filt = lambda c: torch.stack([c.mean(0)[0], c[0][1], c[-1][2]]) nodes_embed = torch.stack([filt(a) for a in d['nodes_elmo']]) # print("nodes_id_name: ", nodes_id_name) # [[(node id, entity id)] for all docu] # g = dgl.DGLGraph() # Now we initalize the node in the graph wid = 0 for doc_id, nodes_in_doc in enumerate(nodes_id_name): if nodes_in_doc == []: continue for node_id, e_id in nodes_in_doc: doc_id, loc_span, word_span = record_of_loc_span[wid] loc_start = torch.tensor([loc_span[0]], dtype=torch.int) loc_end = torch.tensor([loc_span[-1]], dtype=torch.int) # print("Add node now:", doc_id, loc_start, loc_end) doc_id = torch.tensor([doc_id], dtype=torch.int32) e_id = torch.tensor([e_id], dtype=torch.int32) # embed_entities = torch.tensor([nodes_embed[wid]]) # print(nodes_embed[wid].shape) embed_entities = nodes_embed[wid].unsqueeze(0) # print(embed_entities.shape) wid+=1 g.add_nodes(1, {"n_embed": embed_entities, "d_id": doc_id, "loc_start":loc_start, "loc_end":loc_end, "e_id": e_id}) # Check Graph # print(g) # print(g.ndata['d_id'],g.ndata['loc_start'],g.ndata['loc_end']) # print(g.ndata['d_id']) # print(g.ndata['e_id']) # print(g.ndata['n_embed'].shape) d['nodes_candidates_id'] = [[x[-1] for x in f][0] for e in exact_match_doc2entity_set for f in e] # print(d['nodes_candidates_id']) # discard the sample according to De Cao if len(d['nodes_candidates_id']) > max_nodes or len(d['nodes_candidates_id']) <= 0: rm_list.append((i_d, d['id'])) print("Discard sample because num of nodes is zero or larger than limid. ID:",(i_d, d['id']), flush=True) graph_set.append(dgl.DGLGraph()) continue edges_in, edges_out = [], [] for e0 in nodes_id_name: for f0, w0 in e0: for f1, w1 in e0: if f0 != f1: # DOC-BASED edges_in.append((f0, f1)) for e1 in nodes_id_name: for f1, w1 in e1: # Exact match if e0 != e1 and w0 == w1: edges_out.append((f0, f1)) edges_coref = [] for nins, cs in zip (nodes_id_name, d['edges_coref']): for cl in cs: for e0 in cl: for e1 in cl: if e0 != e1: edges_coref.append((nins[e0][0], nins[e1][0])) d['edges_DOC_BASED'] = edges_in d['edges_MATCH'] = edges_out d['edges_COREF'] = edges_coref d['edges_n_COMPLETE'] = d['edges_DOC_BASED'] + d['edges_MATCH'] + d['edges_COREF'] # print("existing: ",d['edges_n_COMPLETE']) d['edges_COMPLETE'] = [] # nodes_id_list = [i for i in g.nodes().data.cpu().numpy()] nodes_id_list = np.arange(len(record_of_loc_span)) for i in nodes_id_list: for j in nodes_id_list: if i == j: # ignore same node, no self-loopo continue if (i,j) not in d['edges_n_COMPLETE']: d['edges_COMPLETE'].append((i, j)) # print(d['edges_COMPLETE']) all_edges = [d['edges_DOC_BASED']] + [d['edges_MATCH']] + [d['edges_COREF']] + [d['edges_COMPLETE']] # Calculate probability weight edge_prob_record = [] for graph_i, subgraph_edges in enumerate(all_edges): edge_prob_in_graph = {} for start_node in nodes_id_list: out_count = len([a for a in subgraph_edges if a[0] == start_node]) if out_count: edge_prob_in_graph[start_node] = 1/out_count edge_prob_record.append(edge_prob_in_graph) for i, rel_graph in enumerate(all_edges): for (src, tgt) in rel_graph: edge_type = torch.tensor([i], dtype=torch.int) p_weight = edge_prob_record[i][src] edge_weight = torch.tensor([p_weight], dtype=torch.float16) g.add_edges(src, tgt, data={'rel_type': edge_type, 'e_weight': edge_weight}) graph_set.append(g) if i_d == 0: dgl.save_graphs(GRAPH_ADD+args.graph_gen_out_file+'.dgl', graph_set) if i_d+1 % 500 == 0: dgl.save_graphs(GRAPH_ADD+args.graph_gen_out_file+'.dgl', graph_set) # file = open('removed_samples_id_30000_43738.txt','w') file = open('dev_removed_samples_id.txt','w') file.write(str(rm_list)) file.close() except: traceback.print_exc() print("Discard sample because of error: ",(i_d, d['id']), "; add an empty graph to graph set.", flush=True) graph_set.append(dgl.DGLGraph()) rm_list.append((i_d, d['id'])) continue # print(graph_set) dgl.save_graphs(GRAPH_ADD+args.graph_gen_out_file+'.dgl', graph_set) # file = open('removed_samples_id_30000_43738.txt','w') file = open('dev_removed_samples_id.txt','w') file.write(str(rm_list)) file.close()
python
import stoked import numpy as np from functools import partial import matplotlib.pyplot as plt def harmonic_force(time, position, orientation, stiffness): return -stiffness*position nm = 1e-9 us = 1e-6 stiffness = 2e-6 radius = 25*nm N = 15 initial = np.random.uniform(-300*nm, 300*nm, size=(N,2)) Q = 1e-18 bd = stoked.brownian_dynamics(position=initial, temperature=300, drag=stoked.drag_sphere(radius=radius, viscosity=8e-4), dt=.2*us, force=partial(harmonic_force, stiffness=stiffness), interactions=stoked.electrostatics(Q)) trajectory = bd.run(10000).position fig, ax = plt.subplots() ax.plot(trajectory[...,0]/nm, trajectory[...,1]/nm, lw=.5) ax.set(aspect='equal', xlabel='x (nm)', ylabel='y (nm)') plt.show()
python
# Test the unicode support! 👋 ᚴ=2 assert ᚴ*8 == 16 ᚴ="👋" c = ᚴ*3 assert c == '👋👋👋' import unicodedata assert unicodedata.category('a') == 'Ll' assert unicodedata.category('A') == 'Lu' assert unicodedata.name('a') == 'LATIN SMALL LETTER A' assert unicodedata.lookup('LATIN SMALL LETTER A') == 'a' assert unicodedata.bidirectional('a') == 'L' assert unicodedata.normalize('NFC', 'bla') == 'bla' # testing unicodedata.ucd_3_2_0 for idna assert "abcСĤ".encode("idna") == b'xn--abc-7sa390b' # TODO: fix: assert "abc䄣IJ".encode("idna") == b'xn--abcij-zb5f' # from CPython tests assert "python.org".encode("idna") == b"python.org" assert "python.org.".encode("idna") == b"python.org." assert "pyth\xf6n.org".encode("idna") == b"xn--pythn-mua.org" assert "pyth\xf6n.org.".encode("idna") == b"xn--pythn-mua.org." assert b"python.org".decode("idna") == "python.org" assert b"python.org.".decode("idna") == "python.org." assert b"xn--pythn-mua.org".decode("idna") == "pyth\xf6n.org" assert b"xn--pythn-mua.org.".decode("idna") == "pyth\xf6n.org." # TODO: add east_asian_width and mirrored # assert unicodedata.ucd_3_2_0.east_asian_width('\u231a') == 'N' # assert not unicodedata.ucd_3_2_0.mirrored("\u0f3a")
python
from pathlib import Path from typing import Union import click import matplotlib.pyplot as plt from ertk.dataset import read_features @click.command() @click.argument("input", type=click.Path(exists=True, dir_okay=False, path_type=Path)) @click.argument("instance", type=str, default="2") def main(input: Path, instance: str): """Displays plot of INSTANCE in INPUT. INSTANCE can either be a numeric index, a range of indices using numpy slice notation or a named instance. """ data = read_features(input) if instance.isdigit(): idx: Union[int, slice] = int(instance) else: _i = instance.find(":") if _i != -1: start = int(instance[:_i]) end = int(instance[_i + 1 :]) idx = slice(start, end) else: idx = data.names.index(instance) arr = data.features[idx] names = data.names[idx] print(names) plt.figure() plt.imshow(arr, aspect="equal", origin="upper", interpolation="nearest") plt.xlabel("Features") plt.ylabel("Instance" if len(names) > 1 else "Time") plt.show() if __name__ == "__main__": main()
python
import nbconvert, git, yaml, inspect; from pathlib import Path class FrontMatters(nbconvert.exporters.MarkdownExporter): def from_notebook_node(self, nb, resources=None, **kw): nb, resources = super().from_notebook_node(nb, resources, **kw) md = dict(resources['metadata']) md['author'] = author_from_repo(Path(md['path'], f"{md['name']}.ipynb")) md['layout'] = 'post' return '---\n'.join(( '', yaml.safe_dump(md, default_flow_style=False), nb)), resources def author_from_repo(file, dir='.'): repo = git.Repo(dir) return repo.blame('HEAD~0', file)[0][0].author.name try: c.NbConvertApp.export_format = f"jupyter_nbconvert_config.FrontMatters" c.FilesWriter.build_directory = "_posts" except: ...
python
import random from arcade import Sprite, load_texture, check_for_collision_with_list from activities import explore, backtrack, follow_the_food, find_the_food from path import Path class Ant(Sprite): def __init__(self, x, y, arena, colony, scale=1, activity="wander"): super().__init__(center_x=x, center_y=y, scale=scale) self.arena = arena self.colony = colony self.speed = 1 self.textures = { "black": load_texture("graphics/ant_black.png"), "green": load_texture("graphics/ant_green.png"), "red": load_texture("graphics/ant_red.png"), "blue": load_texture("graphics/ant_blue.png"), "black_green": load_texture("graphics/ant_black_green.png"), } self.set_activity(explore) self.back_track_path = Path((x, y)) self.food_search_timer = 0 # Used to get a limited number of turns to find food at end of promising path def move(self): if self.activity in (explore, find_the_food): # Ant is exploring the environment in search of food explore(self) if check_for_collision_with_list(self, self.arena.wall_list): # Hit a wall, backup backtrack(self) food_list = check_for_collision_with_list(self, self.arena.food_list) if food_list: # Food found! Take it and back to the colony self.arena.food_list.remove(food_list[0]) # assert self.back_track_path.is_valid() self.colony.found_food(self.back_track_path) self.set_activity(backtrack) self.food_search_timer = 0 elif self.food_search_timer: # Ant followed the path to food and is now at the end of it. Where is it? self.food_search_timer -= 1 if not self.food_search_timer: # Searched at the end of the path but no food in sight. Report and continue exploring # assert self.path_to_food.is_valid() self.colony.no_food_at(self.path_to_food) self.set_activity(explore) elif random.random() < 0.001: self.set_activity(backtrack) self.texture = self.textures["black_green"] elif self.activity == backtrack: # Ant has found food and is tracing back it's steps to the colony if not backtrack(self): # No more backtracking left. We're back at the colony. self.colony.deliver_food() self.path_to_food = self.colony.get_path_to_follow() if self.path_to_food: # assert self.path_to_food.is_valid() # Colony has instructed this ant to follow a path to food self.set_activity(follow_the_food) else: # Colony has instructed this ant to go and find food self.set_activity(explore) elif self.activity == follow_the_food: # Ant is following a path to where food should be if not follow_the_food(self): # End of the path, explore and get 10 turns to find the food self.back_track_path = self.path_to_food.reverse() # assert self.back_track_path.is_valid() # assert self.back_track_path.is_valid() self.food_search_timer = 10 self.set_activity(explore) self.texture = self.textures["blue"] self.update() def set_activity(self, activity): self.activity = activity self.texture = self.textures[self.activity.color] # if activity == explore: # self.texture = self.textures['black'] # else: # self.texture = self.textures['green'] def move_to(self, coo): dx = coo[0] - self.center_x dy = coo[1] - self.center_y if dx < 0: self.angle = 90 elif dx > 0: self.angle = 270 elif dy > 0: self.angle = 0 else: self.angle = 180 self.speed = abs(dx) + abs(dy) self.center_x = coo[0] self.center_y = coo[1]
python
# -*- coding: utf-8 -*- """ Created on Fri Dec 29 13:52:01 2017 @author: User """ import re aPara = "this is a list of words. and here is another" aList = [] print(aPara.split()) # splits into words print(len(aPara.split())) # count of words for item in re.split('[.]', aPara): #splits into sentences print(item) aList.append((len(item.split()))) print(aList) print(re.split('[.]', aPara)) print(len(re.split('[.]', aPara))) # number of sentences
python
## Copyright 2015-2019 Ilgar Lunin, Pedro Cabrera ## Licensed under the Apache License, Version 2.0 (the "License"); ## you may not use this file except in compliance with the License. ## You may obtain a copy of the License at ## http://www.apache.org/licenses/LICENSE-2.0 ## Unless required by applicable law or agreed to in writing, software ## distributed under the License is distributed on an "AS IS" BASIS, ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ## See the License for the specific language governing permissions and ## limitations under the License. from Qt.QtWidgets import * from Qt import QtCore, QtGui from PyFlow.Input import InputActionType from PyFlow.UI.Widgets.KeyboardModifiersCapture import KeyboardModifiersCaptureWidget from PyFlow.UI.Widgets.KeyCapture import KeyCaptureWidget from PyFlow.UI.Widgets.MouseButtonCapture import MouseButtonCaptureWidget class InputActionWidget(QWidget): """docstring for InputActionWidget.""" def __init__(self, parent=None, inputActionRef=None): super(InputActionWidget, self).__init__(parent) self.currentActionRef = inputActionRef self.layout = QHBoxLayout(self) self.layout.setContentsMargins(0, 0, 0, 0) modifiersLabel = QLabel() modifiersLabel.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Preferred) modifiersLabel.setPixmap(QtGui.QPixmap(":/shift-32.png")) self.modifiersWidget = KeyboardModifiersCaptureWidget() self.modifiersWidget.captured.connect(self.updateActionModifiers) self.layout.addWidget(modifiersLabel) self.layout.addWidget(self.modifiersWidget) if self.actionType == InputActionType.Keyboard: keyLabel = QLabel() keyLabel.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Preferred) keyLabel.setPixmap(QtGui.QPixmap(":/keyboard-32.png")) self.keyCapture = KeyCaptureWidget() self.keyCapture.captured.connect(self.updateActionKey) self.layout.addWidget(keyLabel) self.layout.addWidget(self.keyCapture) if self.actionType == InputActionType.Mouse: mouseLabel = QLabel("Mouse:") mouseLabel.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Preferred) mouseLabel.setPixmap(QtGui.QPixmap(":/mouse-32.png")) self.mouseCapture = MouseButtonCaptureWidget() self.mouseCapture.captured.connect(self.updateActionMouse) self.layout.addWidget(mouseLabel) self.layout.addWidget(self.mouseCapture) def updateActionMouse(self, value): if self.currentActionRef is not None: self.currentActionRef.setMouseButton(value) def updateActionKey(self, value): if self.currentActionRef is not None: self.currentActionRef.setKey(value) def updateActionModifiers(self, value): if self.currentActionRef is not None: self.currentActionRef.setModifiers(value) def setAction(self, inputAction): self.modifiersWidget.currentModifiers = inputAction.getModifiers() try: self.keyCapture.currentKey = inputAction.getKey() except: pass try: self.mouseCapture.currentButton = inputAction.getMouseButton() except: pass def getModifiers(self): return self.modifiersWidget.currentModifiers def getKey(self): try: return self.keyCapture.currentKey except: return None def getMouseButton(self): try: return self.mouseCapture.currentButton except: return None @property def actionType(self): return self.currentActionRef.actionType
python
import argparse from typing import Optional from typing import Sequence BLACKLIST = [ b'\x64\x6e\x63', #dnc ] def main(argv: Optional[Sequence[str]] = None) -> int: parser = argparse.ArgumentParser() parser.add_argument('filenames', nargs='*', help='Filenames to check') args = parser.parse_args(argv) bad_files = [] for filename in args.filenames: with open(filename, 'rb') as f: content = f.read() if any(line in content for line in BLACKLIST): bad_files.append(filename) if bad_files: for bad_file in bad_files: print(f'do not commit tag found: {bad_file}') return 1 else: return 0 if __name__ == '__main__': exit(main())
python
# coding=utf-8 """update_xml_verses.py """ from __future__ import print_function import sys, re,codecs sys.path.append('../step3e') # the Entry object from transcode import xml_header,read_entries class Edit(object): def __init__(self,lines): self.lines = lines assert lines[0] == '<edit>' assert lines[-1] == '</edit>' m = re.search(r'^<info L="(.*?)" page="(.*?)" gtypes="(.*?)"/>$',lines[1]) assert m != None self.L = m.group(1) self.page = m.group(2) #self.gtypestr = m.group(3) #self.gtypes = self.gtypestr.split(',') self.parse_edit_groups() def parse_edit_groups(self): # based on Entry.parse_groups groupelts = 'HS,S,D,F,V1,V2,V3,V4,V5'.split(',') groupbegs = ['<%s' % groupelt for groupelt in groupelts] groupends = ['</%s>' % groupelt for groupelt in groupelts] groups = [] ngroup = -1 groupelt = None gtypes = [] # edit gtypes for iline,line in enumerate(self.lines): if groupelt == None: for i,groupbeg in enumerate(groupbegs): if line.startswith(groupbeg): groupelt = groupelts[i] groupend = groupends[i] group = [line] break elif line.startswith(groupend): group.append(line) groups.append(group) ngroup = ngroup + 1 gtypes.append(groupelt) groupelt = None group = [] else: group.append(line) self.groups = groups self.gtypes = gtypes def generate_edits(lines): group = None for iline,line in enumerate(lines): line = line.strip() if line == '<edit>': group = [line] elif line == '</edit>': group.append(line) entry = Edit(group) yield entry group = None L = None elif group != None: group.append(line) else: pass # outside of a group, e.g. ;---------- lines def get_edits(filein): with codecs.open(filein,"r","utf-8") as f: lines = [line.rstrip('\r\n') for line in f] edits = list(generate_edits(lines)) print(len(edits),"edits from",filein) return edits def entry_dict(entries): d = {} for entry in entries: L = entry.L if L in d: print('entry_dict: unexpected duplicate',entry.infoline) d[L] = entry return d def edit_entry(entry,edit): """ """ newgroups = [] found = False info = entry.info assert info == edit.lines[1] gtypes = entry.gtypes groups = entry.groups egroups = edit.groups egtypes = edit.gtypes for igtype,gtype in enumerate(gtypes): group = groups[igtype] if not (igtype < len(egtypes)): newgroups.append(group) continue egtype = egtypes[igtype] assert egtype == gtype egroup = egroups[igtype] newgroups.append(egroup) entry.newgroups = newgroups def compute_entry_lines(entry,newgroups): newlines = [] newlines.append('<entry>') # assume no change needed in info newlines.append(entry.info) for newgroup in newgroups: for newline in newgroup: newlines.append(newline) newlines.append('</entry>') if len(entry.lines) != len(newlines): print('newlines anomaly',entry.info) print('entry.lines:') for line in entry.lines: print(' ',line) print('entry.newlines:') for line in newlines: print(' ',line) exit(1) return newlines def write_entries(entries,xmlroot,version,fileout): head = xml_header(xmlroot,version) head.append('') body = [] for entry in entries: groups = entry.groups newgroups = entry.newgroups if newgroups == None: newlines = entry.lines else: newlines = compute_entry_lines(entry,newgroups) #lines = entry.lines for line in newlines: body.append(line) body.append('') tail = ['</%s>'%xmlroot] linesout = head + body + tail with codecs.open(fileout,"w","utf-8") as f: for line in linesout: f.write(line+'\n') print(len(linesout),"lines written to",fileout) if __name__=="__main__": filein = sys.argv[1] # old boesp.xml filein1 = sys.argv[2] # corrected verses fileout = sys.argv[3] # new boesp.xml xmlroot = 'boesp' version = "1.4" # this must agree with step0/boesp.dtd entries = read_entries(filein) edits = get_edits(filein1) d = entry_dict(entries) # also, add 'newgroups' attribute to each entry # so we can tell which entries have been edited. for entry in entries: entry.newgroups = None for iedit,edit in enumerate(edits): L = edit.L if L not in d: print('edit entry not found',L) else: entry = d[L] edit_entry(entry,edit) # if True: if iedit == 0: print(edit.L) for line in edit.lines: print(line) write_entries(entries,xmlroot,version,fileout)
python
# # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for filesystemio.""" from __future__ import absolute_import import io import logging import multiprocessing import os import sys import threading import unittest from builtins import range from apache_beam.io import filesystemio class FakeDownloader(filesystemio.Downloader): def __init__(self, data): self._data = data self.last_read_size = -1 @property def size(self): return len(self._data) def get_range(self, start, end): self.last_read_size = end - start return self._data[start:end] class FakeUploader(filesystemio.Uploader): def __init__(self): self.data = '' self.last_write_size = -1 self.finished = False def last_error(self): return None def put(self, data): assert not self.finished self.data += data.tobytes() self.last_write_size = len(data) def finish(self): self.finished = True class TestDownloaderStream(unittest.TestCase): def test_file_attributes(self): downloader = FakeDownloader(data=None) stream = filesystemio.DownloaderStream(downloader) self.assertEqual(stream.mode, 'r') self.assertTrue(stream.readable()) self.assertFalse(stream.writable()) self.assertTrue(stream.seekable()) def test_read_empty(self): downloader = FakeDownloader(data=b'') stream = filesystemio.DownloaderStream(downloader) self.assertEqual(stream.read(), b'') def test_read(self): data = 'abcde' downloader = FakeDownloader(data) stream = filesystemio.DownloaderStream(downloader) # Read size is exactly what was passed to read() (unbuffered). self.assertEqual(stream.read(1), data[0]) self.assertEqual(downloader.last_read_size, 1) self.assertEqual(stream.read(), data[1:]) self.assertEqual(downloader.last_read_size, len(data) - 1) @unittest.skipIf(sys.version_info[0] == 3 and os.environ.get('RUN_SKIPPED_PY3_TESTS') != '1', 'This test still needs to be fixed on Python 3' 'TODO: BEAM-5627') def test_read_buffered(self): data = 'abcde' downloader = FakeDownloader(data) buffer_size = 2 stream = io.BufferedReader(filesystemio.DownloaderStream(downloader), buffer_size) # Verify that buffering works and is reading ahead. self.assertEqual(stream.read(1), data[0]) self.assertEqual(downloader.last_read_size, buffer_size) self.assertEqual(stream.read(), data[1:]) @unittest.skipIf(sys.version_info[0] == 3 and os.environ.get('RUN_SKIPPED_PY3_TESTS') != '1', 'This test still needs to be fixed on Python 3' 'TODO: BEAM-5627') class TestUploaderStream(unittest.TestCase): def test_file_attributes(self): uploader = FakeUploader() stream = filesystemio.UploaderStream(uploader) self.assertEqual(stream.mode, 'w') self.assertFalse(stream.readable()) self.assertTrue(stream.writable()) self.assertFalse(stream.seekable()) def test_write_empty(self): uploader = FakeUploader() stream = filesystemio.UploaderStream(uploader) data = '' stream.write(memoryview(data)) self.assertEqual(uploader.data, data) def test_write(self): data = 'abcde' uploader = FakeUploader() stream = filesystemio.UploaderStream(uploader) # Unbuffered writes. stream.write(memoryview(data[0])) self.assertEqual(uploader.data[0], data[0]) self.assertEqual(uploader.last_write_size, 1) stream.write(memoryview(data[1:])) self.assertEqual(uploader.data, data) self.assertEqual(uploader.last_write_size, len(data) - 1) def test_write_buffered(self): data = 'abcde' uploader = FakeUploader() buffer_size = 2 stream = io.BufferedWriter(filesystemio.UploaderStream(uploader), buffer_size) # Verify that buffering works: doesn't write to uploader until buffer is # filled. stream.write(data[0]) self.assertEqual(-1, uploader.last_write_size) stream.write(data[1:]) stream.close() self.assertEqual(data, uploader.data) class TestPipeStream(unittest.TestCase): def _read_and_verify(self, stream, expected, buffer_size): data_list = [] bytes_read = 0 seen_last_block = False while True: data = stream.read(buffer_size) self.assertLessEqual(len(data), buffer_size) if len(data) < buffer_size: # Test the constraint that the pipe stream returns less than the buffer # size only when at the end of the stream. if data: self.assertFalse(seen_last_block) seen_last_block = True if not data: break data_list.append(data) bytes_read += len(data) self.assertEqual(stream.tell(), bytes_read) self.assertEqual(b''.join(data_list), expected) def test_pipe_stream(self): block_sizes = list(4**i for i in range(0, 12)) data_blocks = list(os.urandom(size) for size in block_sizes) expected = b''.join(data_blocks) buffer_sizes = [100001, 512 * 1024, 1024 * 1024] for buffer_size in buffer_sizes: parent_conn, child_conn = multiprocessing.Pipe() stream = filesystemio.PipeStream(child_conn) child_thread = threading.Thread( target=self._read_and_verify, args=(stream, expected, buffer_size)) child_thread.start() for data in data_blocks: parent_conn.send_bytes(data) parent_conn.close() child_thread.join() if __name__ == '__main__': logging.getLogger().setLevel(logging.INFO) unittest.main()
python
from subprocess import call from core import kde from core.action import has_dependency class IconTheme4(kde.KDE4Action): """Change KDE's icon theme.""" def arguments(self): return [ ('theme', 'Icon theme name.') ] def execute(self, theme): kde.writeconfig("Icons", "Theme", theme, file = "kdeglobals") return True class IconTheme5(kde.KDE5Action): """Change KDE's icon theme.""" def arguments(self): return [ ('theme', 'Icon theme name.') ] def execute(self, theme): if (has_dependency("kwriteconfig")): kde.writeconfig4("Icons", "Theme", theme, file = "kdeglobals") kde.writeconfig("Icons", "Theme", theme, file = "kdeglobals") # clear&&dbus-monitor "type=signal,interface='org.kde.KGlobalSettings'" # clear&&dbus-monitor "type=signal,path=/KIconLoader" for x in range(0, 6): call("dbus-send --session --type=signal /KIconLoader org.kde.KIconLoader.iconChanged int32:%d" % x, shell=True) call("dbus-send --session --type=signal /KGlobalSettings org.kde.KGlobalSettings.notifyChange int32:4 int32:%d" % x, shell=True) call("dbus-send --session --type=signal /KWin org.kde.KWin.reloadConfig", shell=True) return True
python
import logging import progressbar from django.core.management.base import BaseCommand from contentcuration.models import Channel from contentcuration.utils.nodes import generate_diff from contentcuration.utils.nodes import get_diff logging.basicConfig() logger = logging.getLogger('command') class Command(BaseCommand): def add_arguments(self, parser): parser.add_argument("--force", action="store_true", dest="force", default=False) def handle(self, *args, **options): # Set up variables for restoration process logger.info("\n\n********** GENERATING STAGED DIFFS **********") channels_with_staged_changes = Channel.objects.exclude(staging_tree=None) bar = progressbar.ProgressBar(max_value=channels_with_staged_changes.count()) for i, c in enumerate(channels_with_staged_changes): if options["force"] or not get_diff(c.staging_tree, c.main_tree): generate_diff(c.staging_tree.pk, c.main_tree.pk) bar.update(i) logger.info("\n\nDONE")
python
import os import re import json import random import codecs import argparse from template_config import * from nltk import word_tokenize from collections import defaultdict from transformers.tokenization_roberta import RobertaTokenizer ADD_INDEX_ID = 0.7 ADD_INDEX_NAME = 0.3 OP_VAL_EQUAL = 0.4 USE_TABLE_1 = 0.5 USE_1_FOR_INTEGER = 0.5 SEP_TOKEN = "</s>" tokenizer = RobertaTokenizer.from_pretrained("roberta-base") MAX_TOKEN_LEN = 150 MAX_COL_NUM = 25 OPS = ["=", ">", "<", ">=", "<=", "!=", "LIKE"] # read NL-SQL templates def read_NL_SQL_template(nlsql_templates_file): templates = [] with open(nlsql_templates_file) as fp: lines = fp.readlines() template_one = {} for line in lines: if "\n" == line: templates.append(template_one) elif "SQL Pattern:" in line: template_one = {} sps = line.strip().replace("SQL Pattern: ", "").split("|||") template_one["questions"] = [] if len(sps) == 1: template_one["SQL pattern"] = sps[0] template_one["SQL constraints"] = [] elif len(sps) == 2: template_one["SQL pattern"] = sps[0] template_one["SQL constraints"] = [x.strip() for x in sps[1].split("|") if x != " "] else: print("\n======Error warning!!!!") elif "count: " in line: sql_count = int(line.strip().replace("count: ", "")) template_one["count"] = sql_count elif "question: " in line: sps = line.strip().replace("question: ", "").split("|||") question = sps[0] if len(sps) == 2: q_constraints = [x.strip() for x in sps[1].split("|") if x != " "] else: q_constraints = [] template_one["questions"].append((question, q_constraints)) return templates # Sieve through the templates and get valid single-table questions def get_templates_for_one_table(templates): templates_one_table = [] for template in templates: sql_constraints = template['SQL constraints'] sql_pattern = template["SQL pattern"] questions = template["questions"] skip = False for constraint in sql_constraints: if "id" in constraint or "T1" in constraint: skip = True questions_after = [] if not skip: for q, qc in questions: if "TABLE1" not in q: questions_after.append((q, qc)) if len(questions_after) > 0: template_one = {} template_one['SQL constraints'] = sql_constraints template_one['SQL pattern'] = sql_pattern template_one["questions"] = questions_after templates_one_table.append(template_one) return templates_one_table # Read json file def read_json(file): with open(file) as json_file: res = json.load(json_file) return res # Unify and combine tables as databases def create_dbs(tables): dbs = [] cur_cols = [] db_one = [] ahd_cols = [] for i, tab in enumerate(tables): # if i % 100000 == 0: # print("processed: ", i) if len(db_one) <= random.choice([0, 1]) and len(ahd_cols) < MAX_COL_NUM: db_one.append(tab) cur_cols.extend([col+"."+tab["name"] for col in tab["columns"]]) if i+1 < len(tables): ahd_cols = cur_cols + [col+"."+tables[i+1]["name"] for col in tables[i+1]["columns"]] else: break else: if len(cur_cols) == len(list(set(cur_cols))): if len(db_one) > 1: db_one_new = [] for tab in db_one: if tab["columns"][0] == "id": tab["columns"] = tab["columns"][1:] tab["column_types"] = tab["column_types"][1:] tab["columns_original"] = tab["columns_original"][1:] tab["values"] = tab["values"][1:] if random.random() < ADD_INDEX_ID: index_col = "id" if random.random() < ADD_INDEX_NAME: index_col = "name" if index_col not in tab["columns"]: tabn_str = "_".join(tab["name"].split(" ")) tab["columns"] = [tab["columns"][0]] + [tabn_str +" "+ index_col] + tab["columns"][1:] val_add = 1 if index_col == "name": val_add = "value" tab["values"] = [tab["values"][0]] + [val_add] + tab["values"][1:] tab["column_types"] = [tab["column_types"][0]] + ["text"] + tab["column_types"][1:] tab["columns_original"] = [tab["columns_original"][0]] + [index_col] + tab["columns_original"][1:] db_one_new.append(tab) dbs.append(db_one_new) else: dbs.append(db_one) db_one = [] cur_cols = [] ahd_cols = [] return dbs def get_sql_slots(sql_pattern): sql_tokens = sql_pattern.split(" ") columns = {} ops = {} values = {} aggs = {} dasc = False slots = [] val_pros = [] for i, tok in enumerate(sql_tokens): if "{" in tok and "}" in tok and "FROM" not in tok: if tok not in slots: slots.append(tok) if "AGG" in tok: if i + 2 < len(sql_tokens) and "(" == sql_tokens[i+1]: if "COLUMN" in sql_tokens[i+2]: if sql_tokens[i+2] not in columns.keys(): columns[sql_tokens[i+2]] = ["number"] else: columns[sql_tokens[i+2]].append("number") aggs[tok] = sql_tokens[i+2] else: print("\nTemplate Error: AGG format is wrong!!!") print(sql_pattern) elif "COLUMN" in tok: if tok not in columns.keys(): columns[tok] = [] elif "OP" in tok: if i - 1 >= 0 and "COLUMN" in sql_tokens[i-1]: ops[tok] = [sql_tokens[i-1]] if i + 1 < len(sql_tokens) and "VALUE" in sql_tokens[i+1]: ops[tok].append(sql_tokens[i+1]) val_pros.append(sql_tokens[i+1]) elif i - 2 >= 0 and ")" == sql_tokens[i-1] and ("COLUMN" in sql_tokens[i-2] or "*" == sql_tokens[i-2]): ops[tok] = [sql_tokens[i-2]] if i + 1 < len(sql_tokens) and "VALUE" in sql_tokens[i+1]: ops[tok].append(sql_tokens[i+1]) val_pros.append(sql_tokens[i+1]) else: print("\nTemplate Error: OP format is wrong!!!") print(sql_pattern) elif "VALUE" in tok and tok not in val_pros: """ OP} {VALUE0} LIMIT {VALUE0} {COLUMN1} BETWEEN {VALUE0} AND {VALUE1} HAVING COUNT ( * ) {OP1} {VALUE1} = {VALUE1} """ if i - 2 >= 0 and ("BETWEEN" == sql_tokens[i-1] or "AND" == sql_tokens[i-1]): values[tok] = "number" if "BETWEEN" == sql_tokens[i-1]: columns[sql_tokens[i-2]].append("number") elif i - 1 >= 0 and "LIMIT" == sql_tokens[i-1]: values[tok] = "integer" elif i - 1 >= 0 and "=" == sql_tokens[i-1]: assert "COLUMN" in sql_tokens[i-2] columns[sql_tokens[i-2]].append(tok) else: print("\nTemplate Error: VALUE format is wrong!!!") print(sql_pattern) elif "DASC" in tok: dasc = True return (list(set(slots)), columns, ops, values, aggs, dasc) def get_q_slots(question): q_toks = question.strip().split(" ") q_slots = list(set([tok for tok in q_toks if "TABLE" in tok or "SC" in tok or ("{" in tok and "}" in tok)])) return q_slots def process_constraints(constraints, columns, slots): slot_values = {} skip_db_with_one_table = False for constraint in constraints: if "P0==" == constraint: assert "{OP0}" in slots slot_values["{OP0}"] = "=" elif "P1==" == constraint: assert "{OP1}" in slots slot_values["{OP1}"] = "=" elif "P0=P1==" == constraint: assert "{OP0}" in slots and "{OP1}" in slots slot_values["{OP0}"] = "=" slot_values["{OP1}"] = "=" elif "P0=P1=P2==" == constraint: assert "{OP0}" in slots and "{OP1}" in slots and "{OP2}" in slots slot_values["{OP0}"] = "=" slot_values["{OP1}"] = "=" slot_values["{OP2}"] = "=" elif "P0=>" == constraint: assert "{OP0}" in slots slot_values["{OP0}"] = ">" elif "P0=<" == constraint: assert "{OP0}" in slots slot_values["{OP0}"] = "<" elif "{AGG0}=MIN" == constraint: assert "{AGG0}" in slots slot_values["{AGG0}"] = "MIN" elif "{AGG0}=MAX" == constraint: assert "{AGG0}" in slots slot_values["{AGG0}"] = "MAX" elif "C0-id" == constraint: skip_db_with_one_table = True assert "{COLUMN0}" in slots and "{COLUMN0}" in columns.keys() columns["{COLUMN0}"].append("id") elif "C1-id" == constraint: skip_db_with_one_table = True assert "{COLUMN1}" in slots and "{COLUMN1}" in columns.keys() columns["{COLUMN1}"].append("id") elif "C2-id" == constraint: skip_db_with_one_table = True assert "{COLUMN2}" in slots and "{COLUMN2}" in columns.keys() columns["{COLUMN2}"].append("id") elif "C3-T1" == constraint: skip_db_with_one_table = True assert "{COLUMN3}" in slots and "{COLUMN3}" in columns.keys() columns["{COLUMN3}"].append("T1") elif "T0-T1-JOIN" == constraint or 'T0-T1-NO-JOIN' == constraint: skip_db_with_one_table = True columns["{COLUMN0}"].append("T0") if "{COLUMN1}" in columns.keys(): columns["{COLUMN1}"].append("T1") return (slot_values, columns, skip_db_with_one_table) # helper function def gen_col_info(col_str, columns, columns_inf): col_conds = columns[col_str] value_slot = [cc for cc in col_conds if "VALUE" in cc] col = "" value_val = None if "id" in col_conds: has_id = False for c, t, v in columns_inf: if "id" in col or "name" in col: has_id = True col, ctype, values = c, t, v break if not has_id: col, ctype, value = columns_inf[0] elif "number" in col_conds: for colinfo in columns_inf[1:]: if colinfo[1] == "real": col, ctype, value = colinfo if col == "": col, ctype, value = random.choice(columns_inf[1:]) if len(value_slot) > 0: assert len(value_slot) < 3 if len(value_slot) == 1: value_val = [(value_slot[0], value)] else: value_val = [(value_slot[0], value), (value_slot[1], value)] return (col, value_val) def replace_dict(inp, dicts): for rep_in, rep_out in dicts.items(): inp = inp.replace(rep_in, str(rep_out)) return inp def get_labels(sql_pattern): STRUCT_KEYWORDS = ["WHERE", "GROUP_BY", "HAVING", "ORDER_BY", "SELECT"] EXTRA_OPS = ["NOT_IN", "IN", "BETWEEN", "="] COUNT = "COUNT" OTHER_KEYWORDS = ["LIMIT"] #AGG, OP, DASC, OR, = NEST_KEYWORDS = ["EXCEPT", "UNION", "INTERSECT"] sql_tokens = sql_pattern.replace("GROUP BY", "GROUP_BY").replace("ORDER BY", "ORDER_BY").replace("NOT IN", "NOT_IN").split(" ") columns = {} cur_nest = "" cur_struct = "" cur_len = len(sql_tokens) select_count = 0 for i, tok in enumerate(sql_tokens): if tok in NEST_KEYWORDS: if cur_nest == "" or cur_nest == "OP_SEL": cur_nest = tok else: cur_nest = cur_nest + " " + tok elif tok in STRUCT_KEYWORDS: cur_struct = tok if tok == "SELECT": select_count += 1 if select_count > 1 and cur_nest == "": cur_nest = "OP_SEL" elif "COLUMN" in tok or "*" == tok: if tok not in columns.keys(): columns[tok] = [] # SELECT {COLUMN0} # SELECT {COLUMN0} , {COLUMN1} # SELECT {AGG0} ( {COLUMN0} ) # SELECT {COLUMN0} {FROM} WHERE {COLUMN1} {OP} ( SELECT {AGG0} ( {COLUMN1} ) {FROM} ) AND {COLUMN2} {OP0} {VALUE0} if cur_struct == "SELECT": if "," == sql_tokens[i-1] or "SELECT" == sql_tokens[i-1]: columns[tok].append(cur_nest + " " + cur_struct) elif "(" == sql_tokens[i-1]: columns[tok].append(cur_nest + " " + cur_struct + " " + sql_tokens[i-2]) else: print("\nWarning: unexcepted SELECT format") print(sql_pattern) # WHERE {COLUMN} {OP} # WHERE {COLUMN2} {OP0} # WHERE OR {COLUMN2} {OP0} # WHERE {COLUMN2} BETWEEN elif cur_struct == "WHERE": assert "OP" in sql_tokens[i+1] or sql_tokens[i+1] in EXTRA_OPS last_tok = sql_tokens[i-1] if "OR" == last_tok or (i+3 < cur_len and "OR" == sql_tokens[i+3]): columns[tok].append(cur_nest + " " + cur_struct + " OR " + sql_tokens[i+1]) elif "WHERE" == last_tok or "AND" == last_tok: columns[tok].append(cur_nest + " " + cur_struct + " " + sql_tokens[i+1]) else: print("\nWarning: unexcepted WHERE format") # GROUP BY {COLUMN0} , {COLUMN0} elif cur_struct == "GROUP_BY": columns[tok].append(cur_nest + " " + cur_struct) # HAVING COUNT ( * ) {OP0} # HAVING {AGG0} ( {COLUMN2} ) {OP0} elif cur_struct == "HAVING": last_tok = sql_tokens[i-1] if last_tok != "(" and not ("AGG" in sql_tokens[i-2] or COUNT == sql_tokens[i-2]): print("\nWarning: unexcepted HAVING format") columns[tok].append(cur_nest + " " + cur_struct + " " + sql_tokens[i-2] + " " + sql_tokens[i+2]) # ORDER BY COUNT ( * ) {DASC} LIMIT # ORDER BY COUNT ( * ) {DASC} # ORDER BY {COLUMN1} {DASC} LIMIT # ORDER BY {COLUMN1} LIMIT # ORDER BY {COLUMN1} , {COLUMN1} {DASC} LIMIT # ORDER BY {COLUMN1} {DASC} if no DASC then is ASC elif cur_struct == "ORDER_BY": last_tok = sql_tokens[i-1] if last_tok == "(": dasc_tok = "{DASC}" limit_tok = "" if sql_tokens[i+2] != "{DASC}": dasc_tok = "ASC" if sql_tokens[i+2] == "LIMIT": limit_tok = "LIMIT" elif i+3 < cur_len and sql_tokens[i+3] == "LIMIT": limit_tok = "LIMIT" columns[tok].append(cur_nest + " " + cur_struct + " " + sql_tokens[i-2] + " " + dasc_tok + " " + limit_tok) elif last_tok == "ORDER_BY" or last_tok == ",": dasc_tok = "ASC" limit_tok = "" # small dirty pass if i+1 < cur_len and sql_tokens[i+1] == "{DASC}": dasc_tok = "{DASC}" if i+2 < cur_len and sql_tokens[i+2] == "LIMIT": limit_tok = "LIMIT" elif i+1 < cur_len and sql_tokens[i+1] == "LIMIT": limit_tok = "LIMIT" columns[tok].append(cur_nest + " " + cur_struct + " " + dasc_tok + " " + limit_tok) else: print("\n------------Warning: unexcepted COLUMN label format") column_labels = {} for col, labels in columns.items(): label_str = " ".join([l.strip() for l in labels]) column_labels[col] = label_str return column_labels def populate_one(db, templates, templates_one, sql_components): """ 'P0=P1==', 'P0=P1=P2==', 'P0==', 'P1==', 'P0=>', 'P0=<', '{AGG0}=MAX', '{AGG0}=MIN' 'T0-T1-JOIN', 'T0-T1-NO-JOIN', 'C0-id',, 'C2-id', , 'C1-id', 'C3-T1' """ if len(db) > 1: template = random.choice(templates) else: template = random.choice(templates_one) sql_constraints = template['SQL constraints'] sql_pattern = template["SQL pattern"] question, q_constraints = random.choice(template["questions"]) constraints = list(set(sql_constraints + q_constraints)) slots, columns, ops, vals, aggs, dasc = get_sql_slots(sql_pattern) slot_values, columns, skip_db_with_one_table = process_constraints(constraints, columns, slots) q_slots = get_q_slots(question) q_slot_values = {} # 1 process ops - update columns and values constraints for op, colv in ops.items(): if colv[0] == "*": if op not in slot_values.keys(): op_val = random.choice([">", "<", ">=", "<=", "="]) slot_values[op] = op_val if len(colv) == 2: slot_values[colv[1]] = random.randint(1, 10) else: if colv[0] not in columns.keys(): print("\n-----colv[0] not in columns.keys(): ") print(columns.keys()) print(ops) assert colv[0] in columns.keys() if op not in slot_values.keys(): if random.random() < OP_VAL_EQUAL: op_val = "=" else: op_val = random.choice(OPS) slot_values[op] = op_val if op_val in [">", "<", ">=", "<="]: columns[colv[0]].append("number") if len(colv) == 2: columns[colv[0]].append(colv[1]) # 2 process columns random.shuffle(db) table_0, table_1 = None, None table_label_0 = "" table_label_1 = "" use_table_1 = False if "{COLUMN0}" in columns.keys() or "{TABLE0}" in q_slots: table_label_0 = "SELECT" if len(db) >= 2: table_0, table_1 = db[:2] if "{TABLE1}" in q_slots: table_label_1 = "SELECT" if "{TABLE0}" in q_slots: # p<0.5 from T0, T1 AND to SELECT T1 * # otherwise all from T0 AND to SELECT T1 * if random.random() < USE_TABLE_1: use_table_1 = True else: # p<0.4 all from T0 # AND to SELECT T1 * if random.random() < 0.6: use_table_1 = True if "{COLUMN1}" in columns.keys(): table_label_1 = "SELECT" else: # p<0.5 from T0, T1 AND to SELECT T1 * # otherwise all from T0, NOT to SELECT T1 * if random.random() < USE_TABLE_1: use_table_1 = True if "{COLUMN1}" in columns.keys(): table_label_1 = "SELECT" else: table_0, table_1 = db[0], db[0] T0 = table_0["name"] T1 = table_1["name"] columns_inf_0 = list(zip(table_0["columns"], table_0["column_types"], table_0["values"]))[1:] if use_table_1: columns_inf_1 = list(zip(table_1["columns"], table_1["column_types"], table_1["values"]))[1:] if "{COLUMN0}" in columns.keys(): col_0, value_0 = gen_col_info("{COLUMN0}", columns, columns_inf_0) slot_values["{COLUMN0}"] = col_0 if value_0 is not None: for k, v in value_0: slot_values[k] = v if len(columns_inf_0) > 2: columns_inf_0 = [(col, ctype, val) for col, ctype, val in columns_inf_0 if col != col_0] if use_table_1: columns_input = columns_inf_1 else: columns_input = columns_inf_0 if "{COLUMN1}" in columns.keys(): col_1, value_1 = gen_col_info("{COLUMN1}", columns, columns_input) slot_values["{COLUMN1}"] = col_1 if value_1 is not None: for k, v in value_1: slot_values[k] = v columns_input_org = columns_input if len(columns_input) > 3: columns_input = [(col, ctype, val) for col, ctype, val in columns_input if col != col_1] if len(columns_input) < 2: columns_input = columns_input_org if "{COLUMN2}" in columns.keys(): col_2, value_2 = gen_col_info("{COLUMN2}", columns, columns_input) slot_values["{COLUMN2}"] = col_2 if value_2 is not None: for k, v in value_2: slot_values[k] = v columns_input_org = columns_input if len(columns_input) > 2: columns_input = [(col, ctype, val) for col, ctype, val in columns_input if col != col_2] if len(columns_input) < 2: columns_input = columns_input_org if "{COLUMN3}" in columns.keys(): col_3, value_3 = gen_col_info("{COLUMN3}", columns, columns_input) slot_values["{COLUMN3}"] = col_3 if value_3 is not None: for k, v in value_3: slot_values[k] = v # 3 aggs for agg in aggs.keys(): if agg not in slot_values.keys(): slot_values[agg] = random.choice(["MAX", "MIN", "SUM", "AVG"]) # 4 values NUM = 1 for val, cond in vals.items(): assert val not in slot_values.keys() if cond == "integer": if random.random() < USE_1_FOR_INTEGER: slot_values[val] = 1 else: NUM = random.randint(2, 10) slot_values[val] = NUM else: slot_values[val] = random.randint(0, 100) # 5 dasc - true if dasc == True: slot_values["{DASC}"] = random.choice(["ASC", "DESC"]) # 6 check if all sql slot values are done if len(slots) != len(slot_values): print("\nlen(slots) != len(slot_values)") print("sql_pattern: ", sql_pattern) print("slots: ", slots) print("slot_values: ", slot_values.keys()) assert len(slots) == len(slot_values) # 7 for the questions slots: for qs in q_slots: if qs == "{TABLE0}": q_slot_values["{TABLE0}"] = T0 elif qs == "{TABLE1}": q_slot_values["{TABLE1}"] = T1 elif "SC" in qs: sc = slot_values["{DASC}"] if "SC" == qs: q_slot_values[qs] = random.choice(sql_components["SC"][sc]) elif "SC_COL_LIMIT" == qs: if NUM > 1: sc = sc + "_NUM" q_slot_values[qs] = random.choice(sql_components["SC_COL_LIMIT"][sc]).replace("[NUM]", str(NUM)) else: q_slot_values[qs] = random.choice(sql_components["SC_COL_LIMIT"][sc]) elif "SC_COL_COUNT_LIMIT" in qs: sc_type = qs.replace("SC_COL_COUNT_LIMIT", "") if NUM > 1: sc = sc + "_NUM" + sc_type q_slot_values[qs] = random.choice(sql_components["SC_COL_COUNT_LIMIT"][sc]).replace("[NUM]", str(NUM)) else: sc = sc + sc_type q_slot_values[qs] = random.choice(sql_components["SC_COL_COUNT_LIMIT"][sc]) else: if "-" not in qs: print("qs wrong", qs) assert "-" in qs if "C1" in qs: sc_col = slot_values["{COLUMN1}"] elif "C2" in qs: sc_col = slot_values["{COLUMN2}"] q_slot_values[qs] = random.choice(sql_components["SC_COL"][sc]).replace("[COL]", sc_col) else: if qs not in slot_values.keys(): print("qs not in sv: ", qs) print("sql_pattern: ", sql_pattern) print("slot_values: ", slot_values) assert qs in slot_values.keys() if "OP" in qs: q_slot_values[qs] = random.choice(sql_components["OP"][slot_values[qs]]) elif "AGG" in qs: q_slot_values[qs] = random.choice(sql_components["AGG"][slot_values[qs]]) elif "COLUMN" in qs: q_slot_values[qs] = " ".join(slot_values[qs].split(" ")[1:6]) elif "VALUE" in qs: q_slot_values[qs] = " ".join(str(slot_values[qs]).split(" ")[:5]) else: print("\nWarning: some q slot type not considered!") print(qs) # 8 check if all question slots are processed assert len(q_slots) == len(q_slot_values) # 9 generate final SQL-question pair question_gen = replace_dict(question, q_slot_values) # 10 generate column labels slot_values_new = {} for sl, vl in slot_values.items(): if "COLUMN" in sl: slot_values_new[sl] = "_=_".join(vl.split(" ")) else: slot_values_new[sl] = vl column_labels = get_labels(sql_pattern) column_lables_real = {} for col, label in column_labels.items(): if col != "*": col = slot_values[col] for slot, value in slot_values.items(): label = label.replace(slot, str(value)) column_lables_real[col] = label # also add labels for table column * if table_label_0 != "": column_lables_real[table_0["columns"][0]] = table_label_0 if table_label_1 != "": column_lables_real[table_1["columns"][0]] = table_label_1 sql_gen = replace_dict(sql_pattern.replace(" {FROM}", ""), slot_values_new) return (sql_gen, question_gen, column_lables_real) # augmentation for one db def augment_db(db, templates, templates_one_table, sql_components, aug_limit): count = 1 augment_pairs = [] while count < aug_limit or (count == int(aug_limit)+1 and random.random()<aug_limit+1-count): sql_gen, question_gen, column_lables = populate_one(db, templates, templates_one_table, sql_components) augment_pairs.append((question_gen, sql_gen, column_lables)) count += 1 return augment_pairs def augment_all_dbs(dbs, templates, templates_one_table, sql_components, aug_limit): augment_data = {} for idx, db in enumerate(dbs): # if idx % 10000 == 0: # print("processed: ", idx) db_cols = ["*"] db_values = [""] for tab in db: db_cols.extend(tab["columns"]) db_values.extend(tab["values"]) assert len(db_cols) == len(db_values) schema_str = " </s> ".join(db_cols) values_str = " </s> ".join([str(k) for k in db_values]) schema_str = schema_str + " |-| " + values_str augment_pairs = augment_db(db, templates, templates_one_table, sql_components, aug_limit) augment_data[schema_str] = augment_pairs return augment_data # Return the mapping of all the labels to an integer def get_label_map(data): label_dict = defaultdict(int) for schema_str, example_list in data.items(): for example in example_list: (question, sql, col_labels) = example for val in col_labels.values(): label_dict[val] += 1 label_list = sorted(label_dict.items(), key=lambda kv: kv[1], reverse=True) label_map = {} count = 1 for label, _ in label_list: label_map[label] = count count += 1 return label_map def map_labels(data, label_map, is_dev=False): data_new = {} skip_count = 0 count = 0 for schema_str, exs in data.items(): count += 1 # if count % 100000 == 0: # print("processed: ", count) data_new[schema_str] = [] for ex in exs: skip = False label_dict = ex[2] label_dict_new = {} for col, label in label_dict.items(): if label in label_map.keys(): label_dict_new[col] = label_map[label] else: skip = True skip_count += 1 #else just skip if not skip: data_new[schema_str].append((ex[0], ex[1], ex[2], label_dict_new)) # print("skip_count: ", skip_count) return data_new def write_final_file(augment_data): data_json = [] skip_count = 0 line_count = 0 dup_count = 0 pro_count = 0 for schema_str, exs in augment_data.items(): for ex in exs: line_count += 1 # if line_count % 100000 == 0: # print("processed: ", line_count) question, sql, label_strs, label_ints = ex col_str, val_str = schema_str.split(" |-| ") colns = col_str.split(" </s> ") values = val_str.split(" </s> ") assert len(colns) == len(values) cols = [] label_num = len(label_ints) label_count = 0 for idx, coln in enumerate(colns): col = {} col["name"] = coln col["value"] = values[idx] if coln != "*": col["name"] = " ".join(coln.split(" ")[1:]) col["label_int"] = 0 if coln in label_ints.keys(): col["label_int"] = label_ints[coln] label_count += 1 cols.append(col) assert label_count >= label_num if label_count > label_num: dup_count += 1 col_list = [] label_list = [] value_list = [] col_count = 0 for i, col in enumerate(cols): if col_count > 40 and col["label_int"] == 0: continue col_list.append(col["name"]) value_list.append(col["value"]) col_count += 1 label_list.append(int(col["label_int"])) assert len(col_list) == len(value_list) label_str = " ".join([str(k) for k in label_list]) q_col_str = "<s> " + question.lower() + " </s> " + " </s> ".join(col_list).strip() + " </s> " caption = q_col_str + " ||| " + label_str tokens = tokenizer.tokenize(q_col_str) if len(tokens) > MAX_TOKEN_LEN: continue data_json.append({"question": question.lower(), "columns": col_list, "rows": [value_list], "column_labels": label_list }) pro_count += 1 print("total line: ", line_count) print("skiped line: ", skip_count) print("dup line: ", dup_count) print("pro line: ", pro_count) return data_json if __name__=="__main__": parser = argparse.ArgumentParser() parser.add_argument("table_file", help="Please provide a processed table file") parser.add_argument("nlsql_templates_file", help="Please provide a template file") parser.add_argument("sql_components_file", help="Please provide the SQL component file") parser.add_argument("output", help="Please provide the output path") parser.add_argument("size", type=int, help="Please provide the output path") args = parser.parse_args() # read input files table_file = args.table_file nlsql_templates_file = args.nlsql_templates_file sql_components_file = args.sql_components_file templates = read_NL_SQL_template(nlsql_templates_file) sql_components = read_json(sql_components_file) all_tables = read_json(table_file) table_dbs = create_dbs(all_tables) single_table_templates = get_templates_for_one_table(templates) sample_size_per_db = 1.0 * args.size / len(table_dbs) augment_data = augment_all_dbs(table_dbs, templates, single_table_templates, sql_components, sample_size_per_db) label_map = get_label_map(augment_data) augment_data = map_labels(augment_data, label_map) json_data = write_final_file(augment_data) with open(args.output, "w") as f: json.dump(json_data, f)
python
""" Tests for exact diffuse initialization Notes ----- These tests are against four sources: - Koopman (1997) - The R package KFAS (v1.3.1): test_exact_diffuse_filtering.R - Stata: test_exact_diffuse_filtering_stata.do - Statsmodels state space models using approximate diffuse filtering Koopman (1997) provides analytic results for a few cases that we can test against. More comprehensive tests are available against the R package KFAS, which also uses the Durbin and Koopman (2012) univariate diffuse filtering method. However, there are apparently some bugs in the KFAS output (see notes below), so some tests are run against Stata. KFAS v1.3.1 appears to have the following bugs: - Incorrect filtered covariance matrix (in their syntax, kf$Ptt). These matrices are not even symmetric, so they are clearly wrong. - Loglikelihood computation appears to be incorrect for the diffuse part of the state. See the section with "Note: Apparent loglikelihood discrepancy" in the R file. It appears that KFAS does not include the constant term (-0.5 * log(2 pi)) for the diffuse observations, whereas the loglikelihood function as given in e.g. section 7.2.5 of Durbin and Koopman (2012) shows that it should be included. To confirm this, we also check against the loglikelihood value computed by Stata. Stata uses the DeJong diffuse filtering method, which gives almost identical results but does imply some numerical differences for output at the 6th or 7th decimal place. Finally, we have tests against the same model using approximate (rather than exact) diffuse filtering. These will by definition have some discrepancies in the diffuse observations. Author: Chad Fulton License: Simplified-BSD """ from __future__ import division, absolute_import, print_function import numpy as np import pandas as pd import pytest import os from statsmodels.tools.tools import Bunch from statsmodels import datasets from statsmodels.tsa.statespace.initialization import Initialization from statsmodels.tsa.statespace.kalman_smoother import KalmanSmoother from statsmodels.tsa.statespace.mlemodel import MLEModel from statsmodels.tsa.statespace.varmax import VARMAX from statsmodels.tsa.statespace.dynamic_factor import DynamicFactor from statsmodels.tsa.statespace.structural import UnobservedComponents from numpy.testing import assert_equal, assert_allclose import pytest from . import kfas_helpers current_path = os.path.dirname(os.path.abspath(__file__)) macrodata = datasets.macrodata.load_pandas().data macrodata.index = pd.PeriodIndex(start='1959Q1', end='2009Q3', freq='Q') # - Model definitions -------------------------------------------------------- def model_local_level(endog=None, params=None, direct=False): if endog is None: y1 = 10.2394 endog = np.r_[y1, [1] * 9] if params is None: params = [1.993, 8.253] sigma2_y, sigma2_mu = params if direct: mod = None # Construct the basic representation ssm = KalmanSmoother(k_endog=1, k_states=1, k_posdef=1) ssm.bind(endog) init = Initialization(ssm.k_states, initialization_type='diffuse') ssm.initialize(init) # ssm.filter_univariate = True # should not be required # Fill in the system matrices for a local level model ssm['design', :] = 1 ssm['obs_cov', :] = sigma2_y ssm['transition', :] = 1 ssm['selection', :] = 1 ssm['state_cov', :] = sigma2_mu else: mod = UnobservedComponents(endog, 'llevel') mod.update(params) ssm = mod.ssm ssm.initialize(Initialization(ssm.k_states, 'diffuse')) return mod, ssm def model_local_linear_trend(endog=None, params=None, direct=False): if endog is None: y1 = 10.2394 y2 = 4.2039 y3 = 6.123123 endog = np.r_[y1, y2, y3, [1] * 7] if params is None: params = [1.993, 8.253, 2.334] sigma2_y, sigma2_mu, sigma2_beta = params if direct: mod = None # Construct the basic representation ssm = KalmanSmoother(k_endog=1, k_states=2, k_posdef=2) ssm.bind(endog) init = Initialization(ssm.k_states, initialization_type='diffuse') ssm.initialize(init) # ssm.filter_univariate = True # should not be required # Fill in the system matrices for a local level model ssm['design', 0, 0] = 1 ssm['obs_cov', 0, 0] = sigma2_y ssm['transition'] = np.array([[1, 1], [0, 1]]) ssm['selection'] = np.eye(2) ssm['state_cov'] = np.diag([sigma2_mu, sigma2_beta]) else: mod = UnobservedComponents(endog, 'lltrend') mod.update(params) ssm = mod.ssm ssm.initialize(Initialization(ssm.k_states, 'diffuse')) return mod, ssm def model_common_level(endog=None, params=None, restricted=False): if endog is None: y11 = 10.2394 y21 = 8.2304 endog = np.column_stack([np.r_[y11, [1] * 9], np.r_[y21, [1] * 9]]) if params is None: params = [0.1111, 3.2324] theta, sigma2_mu = params # sigma2_1 = 1 # sigma_12 = 0 # sigma2_2 = 1 if not restricted: # Construct the basic representation ssm = KalmanSmoother(k_endog=2, k_states=2, k_posdef=1) ssm.bind(endog.T) init = Initialization(ssm.k_states, initialization_type='diffuse') ssm.initialize(init) # ssm.filter_univariate = True # should not be required # Fill in the system matrices for a common trend model ssm['design'] = np.array([[1, 0], [theta, 1]]) ssm['obs_cov'] = np.eye(2) ssm['transition'] = np.eye(2) ssm['selection', 0, 0] = 1 ssm['state_cov', 0, 0] = sigma2_mu else: # Construct the basic representation ssm = KalmanSmoother(k_endog=2, k_states=1, k_posdef=1) ssm.bind(endog.T) init = Initialization(ssm.k_states, initialization_type='diffuse') ssm.initialize(init) # ssm.filter_univariate = True # should not be required # Fill in the system matrices for a local level model ssm['design'] = np.array([[1, theta]]).T ssm['obs_cov'] = np.eye(2) ssm['transition', :] = 1 ssm['selection', :] = 1 ssm['state_cov', :] = sigma2_mu return ssm def model_var1(endog=None, params=None, measurement_error=False, init=None): if endog is None: endog = (np.log( macrodata[['realgdp','realcons']]).iloc[:21].diff().iloc[1:] * 400) if params is None: params = np.r_[0.5, 0.3, 0.2, 0.4, 2**0.5, 0, 3**0.5] if measurement_error: params = np.r_[params, 4, 5] # Model mod = VARMAX(endog, order=(1, 0), trend='nc', measurement_error=measurement_error) mod.update(params) ssm = mod.ssm if init is None: init = Initialization(ssm.k_states, 'diffuse') ssm.initialize(init) return mod, ssm def model_dfm(endog=None, params=None, factor_order=2): if endog is None: endog = (np.log( macrodata[['realgdp','realcons']]).iloc[:21].diff().iloc[1:] * 400) if params is None: params = np.r_[0.5, 1., 1.5, 2., 0.9, 0.1] # Model mod = DynamicFactor(endog, k_factors=1, factor_order=factor_order) mod.update(params) ssm = mod.ssm ssm.filter_univariate = True init = Initialization(ssm.k_states, 'diffuse') ssm.initialize(init) return mod, ssm # - Analytic tests (Koopman, 1997) ------------------------------------------- class TestLocalLevelAnalytic(object): @classmethod def setup_class(cls, **kwargs): cls.mod, cls.ssm = model_local_level(**kwargs) cls.res = cls.ssm.smooth() def test_results(self): ssm = self.ssm res = self.res y1 = ssm.endog[0, 0] sigma2_y = ssm['obs_cov', 0, 0] sigma2_mu = ssm['state_cov', 0, 0] # Basic initialization variables assert_allclose(res.predicted_state_cov[0, 0, 0], 0) assert_allclose(res.predicted_diffuse_state_cov[0, 0, 0], 1) # Output of the exact diffuse initialization, see Koopman (1997) assert_allclose(res.forecasts_error[0, 0], y1) assert_allclose(res.forecasts_error_cov[0, 0, 0], sigma2_y) assert_allclose(res.forecasts_error_diffuse_cov[0, 0, 0], 1) assert_allclose(res.kalman_gain[0, 0, 0], 1) assert_allclose(res.predicted_state[0, 1], y1) assert_allclose(res.predicted_state_cov[0, 0, 1], sigma2_y + sigma2_mu) assert_allclose(res.predicted_diffuse_state_cov[0, 0, 1], 0) # Miscellaneous assert_equal(res.nobs_diffuse, 1) class TestLocalLevelAnalyticDirect(TestLocalLevelAnalytic): @classmethod def setup_class(cls): super(TestLocalLevelAnalyticDirect, cls).setup_class(direct=True) class TestLocalLinearTrendAnalytic(object): @classmethod def setup_class(cls, **kwargs): cls.mod, cls.ssm = model_local_linear_trend(**kwargs) cls.res = cls.ssm.smooth() def test_results(self): ssm = self.ssm res = self.res y1, y2, y3 = ssm.endog[0, :3] sigma2_y = ssm['obs_cov', 0, 0] sigma2_mu, sigma2_beta = np.diagonal(ssm['state_cov']) # Basic initialization variables assert_allclose(res.predicted_state_cov[..., 0], np.zeros((2, 2))) assert_allclose(res.predicted_diffuse_state_cov[..., 0], np.eye(2)) # Output of the exact diffuse initialization, see Koopman (1997) q_mu = sigma2_mu / sigma2_y q_beta = sigma2_beta / sigma2_y assert_allclose(res.forecasts_error[0, 0], y1) assert_allclose(res.kalman_gain[:, 0, 0], [1, 0]) assert_allclose(res.predicted_state[:, 1], [y1, 0]) P2 = sigma2_y * np.array([[1 + q_mu, 0], [0, q_beta]]) assert_allclose(res.predicted_state_cov[:, :, 1], P2) assert_allclose(res.predicted_diffuse_state_cov[0, 0, 1], np.ones((2, 2))) # assert_allclose(res.kalman_gain[:, 0, 1], [2, 1]) assert_allclose(res.predicted_state[:, 2], [2 * y2 - y1, y2 - y1]) P3 = sigma2_y * np.array([[5 + 2 * q_mu + q_beta, 3 + q_mu + q_beta], [3 + q_mu + q_beta, 2 + q_mu + 2 * q_beta]]) assert_allclose(res.predicted_state_cov[:, :, 2], P3) assert_allclose(res.predicted_diffuse_state_cov[:, :, 2], np.zeros((2, 2))) # Miscellaneous assert_equal(res.nobs_diffuse, 2) class TestLocalLinearTrendAnalyticDirect(TestLocalLinearTrendAnalytic): @classmethod def setup_class(cls): super(TestLocalLinearTrendAnalyticDirect, cls).setup_class(direct=True) class TestLocalLinearTrendAnalyticMissing(TestLocalLinearTrendAnalytic): @classmethod def setup_class(cls): y1 = 10.2394 y2 = np.nan y3 = 6.123123 endog = np.r_[y1, y2, y3, [1] * 7] super(TestLocalLinearTrendAnalyticMissing, cls).setup_class( endog=endog) def test_results(self): ssm = self.ssm res = self.res y1, y2, y3 = ssm.endog[0, :3] sigma2_y = ssm['obs_cov', 0, 0] sigma2_mu, sigma2_beta = np.diagonal(ssm['state_cov']) # Test output q_mu = sigma2_mu / sigma2_y q_beta = sigma2_beta / sigma2_y a4 = [1.5 * y3 - 0.5 * y1, 0.5 * y3 - 0.5 * y1] assert_allclose(res.predicted_state[:, 3], a4) P4 = sigma2_y * np.array([ [2.5 + 1.5 * q_mu + 1.25 * q_beta, 1 + 0.5 * q_mu + 1.25 * q_beta], [1 + 0.5 * q_mu + 1.25 * q_beta, 0.5 + 0.5 * q_mu + 2.25 * q_beta]]) assert_allclose(res.predicted_state_cov[:, :, 3], P4) # Miscellaneous assert_equal(res.nobs_diffuse, 3) def test_common_level_analytic(): # Analytic test using results from Koopman (1997), section 5.3 mod = model_common_level() y11, y21 = mod.endog[:, 0] theta = mod['design', 1, 0] sigma2_mu = mod['state_cov', 0, 0] # Perform filtering res = mod.smooth() # Basic initialization variables assert_allclose(res.predicted_state_cov[..., 0], np.zeros((2, 2))) assert_allclose(res.predicted_diffuse_state_cov[..., 0], np.eye(2)) # Output of the exact diffuse initialization, see Koopman (1997) # Note: since Koopman (1997) did not apply the univariate method, # forecast errors and covariances, and the Kalman gain won't match # assert_allclose(res.forecasts_error[:, 0], [y11, y21]) # assert_allclose(res.forecasts_error_cov[:, :, 0], np.eye(2)) # F_inf1 = np.array([[1, theta], # [theta, 1 + theta**2]]) # assert_allclose(res.forecasts_error_diffuse_cov[:, :, 0], F_inf1) # K0 = np.array([[1, 0], # [-theta, 1]]) # assert_allclose(res.kalman_gain[..., 0], K0) assert_allclose(res.predicted_state[:, 1], [y11, y21 - theta * y11]) P2 = np.array([[1 + sigma2_mu, -theta], [-theta, 1 + theta**2]]) assert_allclose(res.predicted_state_cov[..., 1], P2) assert_allclose(res.predicted_diffuse_state_cov[..., 1], np.zeros((2, 2))) # Miscellaneous assert_equal(res.nobs_diffuse, 1) def test_common_level_restricted_analytic(): # Analytic test using results from Koopman (1997), section 5.3, # with the restriction mu_bar = 0 mod = model_common_level(restricted=True) y11, y21 = mod.endog[:, 0] theta = mod['design', 1, 0] sigma2_mu = mod['state_cov', 0, 0] # Perform filtering res = mod.smooth() # Basic initialization variables assert_allclose(res.predicted_state_cov[..., 0], 0) assert_allclose(res.predicted_diffuse_state_cov[..., 0], 1) # Output of the exact diffuse initialization, see Koopman (1997) phi = 1 / (1 + theta**2) # Note: since Koopman (1997) did not apply the univariate method, # forecast errors and covariances, and the Kalman gain won't match # assert_allclose(res.forecasts_error[:, 0], [y11, y21]) # assert_allclose(res.forecasts_error_cov[0, 0, 0], np.eye(2)) # F_inf1 = np.array([[1, theta], # [theta, theta**2]]) # assert_allclose(res.forecasts_error_diffuse_cov[0, 0, 0], F_inf1) # assert_allclose(res.kalman_gain[..., 0], phi * np.array([1, theta])) assert_allclose(res.predicted_state[:, 1], phi * (y11 + theta * y21)) # Note: Koopman (1997) actually has phi + sigma2_mu**0.5, but that appears # to be a typo assert_allclose(res.predicted_state_cov[..., 1], phi + sigma2_mu) assert_allclose(res.predicted_diffuse_state_cov[..., 1], 0) # Miscellaneous assert_equal(res.nobs_diffuse, 1) class CheckSSMResults(object): atol = 1e-14 rtol = 1e-07 atol_diffuse = 1e-7 rtol_diffuse = None def check_object(self, actual, desired, rtol_diffuse): # Short-circuit the test if desired is set to None (which allows us to # skip testing some objects where appropriate) if actual is None or desired is None: return # Optionally apply a different relative tolerance to the periods in the # diffuse observations. # This is especially useful when testing against approximate diffuse # initialization. By definition, the first few observations will be # quite different between the exact and approximate approach for many # quantities. # Note that the absolute tolerance is also pretty low (1e-7), mostly # for comparison against zero values in the approximate case d = None if rtol_diffuse is None: rtol_diffuse = self.rtol_diffuse if rtol_diffuse is not None: d = self.d if rtol_diffuse != np.inf: assert_allclose(actual.T[:d], desired.T[:d], rtol=rtol_diffuse, atol=self.atol_diffuse) assert_allclose(actual.T[d:], desired.T[d:], rtol=self.rtol, atol=self.atol) # - Filtered results tests ----------------------------------------------- def test_forecasts(self, rtol_diffuse=None): actual = self.results_a.forecasts desired = self.results_a.forecasts self.check_object(actual, desired, rtol_diffuse) def test_forecasts_error(self, rtol_diffuse=None): actual = self.results_a.forecasts_error desired = self.results_a.forecasts_error self.check_object(actual, desired, rtol_diffuse) def test_forecasts_error_cov(self, rtol_diffuse=None): actual = self.results_a.forecasts_error_cov desired = self.results_b.forecasts_error_cov self.check_object(actual, desired, rtol_diffuse) def test_filtered_state(self, rtol_diffuse=1e-5): # Note: we do want to check the diffuse values here, with a reduced # tolerance. See the note before the smoothed values for additional # details. actual = self.results_a.filtered_state desired = self.results_b.filtered_state self.check_object(actual, desired, rtol_diffuse) def test_filtered_state_cov(self, rtol_diffuse=None): actual = self.results_a.filtered_state_cov desired = self.results_b.filtered_state_cov self.check_object(actual, desired, rtol_diffuse) def test_predicted_state(self, rtol_diffuse=None): actual = self.results_a.predicted_state desired = self.results_b.predicted_state self.check_object(actual, desired, rtol_diffuse) def test_predicted_state_cov(self, rtol_diffuse=None): actual = self.results_a.predicted_state_cov desired = self.results_b.predicted_state_cov self.check_object(actual, desired, rtol_diffuse) def test_kalman_gain(self, rtol_diffuse=None): actual = self.results_a.kalman_gain desired = self.results_b.kalman_gain self.check_object(actual, desired, rtol_diffuse) def test_loglike(self, rtol_diffuse=None): if np.isscalar(self.results_b.llf_obs): actual = np.sum(self.results_a.llf_obs) desired = self.results_b.llf_obs assert_allclose(actual, desired) else: actual = self.results_a.llf_obs desired = self.results_b.llf_obs self.check_object(actual, desired, rtol_diffuse) # - Smoothed output tests ------------------------------------------------ # Note: for smoothed states, we do want to check some of the diffuse values # even in the approximate case, but with reduced precision. Note also that # there are cases that demonstrate the numerical error associated with the # approximate method, and so some specific tests are overridden in certain # cases, since they would not pass. def test_smoothed_state(self, rtol_diffuse=1e-5): actual = self.results_a.smoothed_state desired = self.results_b.smoothed_state self.check_object(actual, desired, rtol_diffuse) def test_smoothed_state_cov(self, rtol_diffuse=1e-5): actual = self.results_a.smoothed_state_cov desired = self.results_b.smoothed_state_cov self.check_object(actual, desired, rtol_diffuse) def test_smoothed_state_autocov(self, rtol_diffuse=None): actual = self.results_a.smoothed_state_autocov desired = self.results_b.smoothed_state_autocov self.check_object(actual, desired, rtol_diffuse) def test_smoothed_measurement_disturbance(self, rtol_diffuse=1e-5): actual = self.results_a.smoothed_measurement_disturbance desired = self.results_b.smoothed_measurement_disturbance self.check_object(actual, desired, rtol_diffuse) def test_smoothed_measurement_disturbance_cov(self, rtol_diffuse=1e-5): actual = self.results_a.smoothed_measurement_disturbance_cov desired = self.results_b.smoothed_measurement_disturbance_cov self.check_object(actual, desired, rtol_diffuse) def test_smoothed_state_disturbance(self, rtol_diffuse=1e-5): actual = self.results_a.smoothed_state_disturbance desired = self.results_b.smoothed_state_disturbance self.check_object(actual, desired, rtol_diffuse) def test_smoothed_state_disturbance_cov(self, rtol_diffuse=1e-5): actual = self.results_a.smoothed_state_disturbance_cov desired = self.results_b.smoothed_state_disturbance_cov self.check_object(actual, desired, rtol_diffuse) # - Smoothed intermediate tests ------------------------------------------ # This isn't computed in the univariate method or by KFAS # def test_smoothing_error(self, rtol_diffuse=None): # actual = self.results_a.smoothing_error # desired = self.results_b.smoothing_error # self.check_object(actual, desired, rtol_diffuse) def test_scaled_smoothed_estimator(self, rtol_diffuse=1e-5): actual = self.results_a.scaled_smoothed_estimator desired = self.results_b.scaled_smoothed_estimator self.check_object(actual, desired, rtol_diffuse) def test_scaled_smoothed_estimator_cov(self, rtol_diffuse=1e-5): actual = self.results_a.scaled_smoothed_estimator_cov desired = self.results_b.scaled_smoothed_estimator_cov self.check_object(actual, desired, rtol_diffuse) # - Diffuse objects tests ------------------------------------------------ # Note: these can't be checked against the approximate diffuse method. def test_forecasts_error_diffuse_cov(self, rtol_diffuse=None): actual = self.results_a.forecasts_error_diffuse_cov desired = self.results_b.forecasts_error_diffuse_cov self.check_object(actual, desired, rtol_diffuse) def test_predicted_diffuse_state_cov(self, rtol_diffuse=None): actual = self.results_a.predicted_diffuse_state_cov desired = self.results_b.predicted_diffuse_state_cov self.check_object(actual, desired, rtol_diffuse) # We don't currently store this array # def test_kalman_gain_diffuse(self, rtol_diffuse=None): # actual = self.results_a. # desired = self.results_b. # self.check_object(actual, desired, rtol_diffuse) def test_scaled_smoothed_diffuse_estimator(self, rtol_diffuse=None): actual = self.results_a.scaled_smoothed_diffuse_estimator desired = self.results_b.scaled_smoothed_diffuse_estimator self.check_object(actual, desired, rtol_diffuse) def test_scaled_smoothed_diffuse1_estimator_cov(self, rtol_diffuse=None): actual = self.results_a.scaled_smoothed_diffuse1_estimator_cov desired = self.results_b.scaled_smoothed_diffuse1_estimator_cov self.check_object(actual, desired, rtol_diffuse) def test_scaled_smoothed_diffuse2_estimator_cov(self, rtol_diffuse=None): actual = self.results_a.scaled_smoothed_diffuse2_estimator_cov desired = self.results_b.scaled_smoothed_diffuse2_estimator_cov self.check_object(actual, desired, rtol_diffuse) # - Simulation smoother results tests ------------------------------------ # def test_simulation_smoothed_state(self): # assert_allclose( # self.sim_a.simulated_state, # self.sim_a.simulated_state) # def test_simulation_smoothed_measurement_disturbance(self): # assert_allclose( # self.sim_a.simulated_measurement_disturbance, # self.sim_a.simulated_measurement_disturbance) # def test_simulation_smoothed_state_disturbance(self): # assert_allclose( # self.sim_a.simulated_state_disturbance, # self.sim_a.simulated_state_disturbance) class CheckApproximateDiffuseMixin(object): """ Test the exact diffuse initialization against the approximate diffuse initialization. By definition, the first few observations will be quite different between the exact and approximate approach for many quantities, so we do not test them here. """ approximate_diffuse_variance = 1e6 @classmethod def setup_class(cls, *args, **kwargs): init_approx = kwargs.pop('init_approx', None) super(CheckApproximateDiffuseMixin, cls).setup_class(*args, **kwargs) # Get the approximate diffuse results kappa = cls.approximate_diffuse_variance if init_approx is None: init_approx = Initialization(cls.ssm.k_states, 'approximate_diffuse', approximate_diffuse_variance=kappa) cls.ssm.initialize(init_approx) cls.results_b = cls.ssm.smooth() # Instruct the tests not to test against the first d values cls.rtol_diffuse = np.inf def test_initialization_approx(self): kappa = self.approximate_diffuse_variance assert_allclose(self.results_b.initial_state_cov, np.eye(self.ssm.k_states) * kappa) assert_equal(self.results_b.initial_diffuse_state_cov, None) class CheckKFASMixin(object): """ Test against values from KFAS """ @classmethod def setup_class(cls, *args, **kwargs): kwargs.setdefault('filter_univariate', True) super(CheckKFASMixin, cls).setup_class(*args, **kwargs) # Get the KFAS results objects cls.results_b = kfas_helpers.parse(cls.results_path, cls.ssm) # Set some attributes that KFAS does not compute cls.results_b.smoothed_state_autocov = None # Remove the Kalman gain matrix since KFAS computes it using the # non-univariate method cls.results_b.kalman_gain = None # Remove the filtered_state_cov since KFAS v1.3.1 has a bug for these # matrices (they are not even symmetric) cls.results_b.filtered_state_cov = None # KFAS v1.3.1 seems to compute the loglikelihood incorrectly, so we # correct for it here # (we need to add back in the constant term for all of the non-missing # diffuse observations for which Finf is nonsingular) Finf = cls.results_b.forecasts_error_diffuse_cov.T Finf_nonsingular_obs = np.c_[[np.diag(Finf_t) for Finf_t in Finf]] > 0 nonmissing = ~np.isnan(cls.ssm.endog).T constant = (-0.5 * np.log(2 * np.pi) * (Finf_nonsingular_obs * nonmissing).sum(axis=1)) cls.results_b.llf_obs += constant[:cls.results_a.nobs_diffuse].sum() # - VAR(1) ------------------------------------------------------------------- class CheckVAR1(CheckSSMResults): @classmethod def setup_class(cls, **kwargs): filter_univariate = kwargs.pop('filter_univariate', False) cls.mod, cls.ssm = model_var1(**kwargs) if filter_univariate: cls.ssm.filter_univariate = True cls.results_a = cls.ssm.smooth() cls.d = cls.results_a.nobs_diffuse def test_nobs_diffuse(self): assert_allclose(self.d, 1) def test_initialization(self): assert_allclose(self.results_a.initial_state_cov, 0) assert_allclose(self.results_a.initial_diffuse_state_cov, np.eye(2)) class TestVAR1_Approx(CheckApproximateDiffuseMixin, CheckVAR1): pass class TestVAR1_KFAS(CheckKFASMixin, CheckVAR1): results_path = os.path.join( current_path, 'results', 'results_exact_initial_var1_R.csv') # - VAR(1) + Measurement error ----------------------------------------------- class CheckVAR1MeasurementError(CheckVAR1): @classmethod def setup_class(cls, **kwargs): kwargs['measurement_error'] = True super(CheckVAR1MeasurementError, cls).setup_class(**kwargs) class TestVAR1MeasurementError_Approx(CheckApproximateDiffuseMixin, CheckVAR1MeasurementError): # Note: somewhat fragile, we need to increase the approximate variance to # 1e9 for the tests to pass at the appropriate level of precision, but # we can't increase too much more than this because then we start get # numerical errors (e.g. 1e10 is fine but 1e11 doesn't pass) approximate_diffuse_variance = 1e9 def test_smoothed_measurement_disturbance_cov(self, rtol_diffuse=None): # Note: this test would fail here with most rtol, because # this is an example where the numerical errors associated with the # approximate method result in noticeable errors # term: (x is the exact method, y is the approximate method) # x: array([[[3.355072, 0. ], # [0. , 4.221227]]]) # y: array([[[ 3.355072, -0.600856], # [-0.600856, 4.221227]]]) super(TestVAR1MeasurementError_Approx, self).test_smoothed_measurement_disturbance_cov( rtol_diffuse=rtol_diffuse) class TestVAR1MeasurementError_KFAS(CheckKFASMixin, CheckVAR1MeasurementError): results_path = os.path.join(current_path, 'results', 'results_exact_initial_var1_measurement_error_R.csv') # - VAR(1) + Missing data ---------------------------------------------------- class CheckVAR1Missing(CheckVAR1): @classmethod def setup_class(cls, **kwargs): endog = (np.log( macrodata[['realgdp','realcons']]).iloc[:21].diff().iloc[1:] * 400) endog.iloc[0:5, 0] = np.nan endog.iloc[8:12, :] = np.nan kwargs['endog'] = endog super(CheckVAR1Missing, cls).setup_class(**kwargs) def test_nobs_diffuse(self): assert_allclose(self.d, 2) class TestVAR1Missing_Approx(CheckApproximateDiffuseMixin, CheckVAR1Missing): # Note: somewhat fragile, we need to increase the approximate variance to # 1e10 for the tests to pass at the appropriate level of precision, but # we can't increase it any more than this because then we start get # numerical errors (e.g. 1e11 doesn't pass) approximate_diffuse_variance = 1e10 def test_smoothed_state_cov(self, rtol_diffuse=None): # Note: this test would fail here with essentially any rtol, because # this is an example where the numerical errors associated with the # approximate method result in extreme errors: here a negative variance # term: (x is the exact method, y is the approximate method) # x: array([[[ 5.601218e+01, 0.000000e+00], # [ 0.000000e+00, 0.000000e+00]], # ... # y: array([[[-12.083676, 0. ], # [ 0. , 0. ]], super(TestVAR1Missing_Approx, self).test_smoothed_state_cov( rtol_diffuse=rtol_diffuse) class TestVAR1Missing_KFAS(CheckKFASMixin, CheckVAR1Missing): results_path = os.path.join( current_path, 'results', 'results_exact_initial_var1_missing_R.csv') def test_forecasts_error_cov(self): # TODO: fails for the general version of forecasts_error_cov because # (1) the routines in kalman_filter.py fill in values for all variables # regardless of missing status and also it uses the multivariate # approach rather than the univariate approach, and (2) KFAS fills in # values for all variables regardless of missing status (but does use # the univariate method). # Here we remove the off-diagonal elements so that the test passes (but # note that this is **not** a general solution since it depends on # which variables are missing). bak = self.results_a.forecasts_error_cov[:] self.results_a.forecasts_error_cov[0, 1, :] = 0 self.results_a.forecasts_error_cov[1, 0, :] = 0 super(TestVAR1Missing_KFAS, self).test_forecasts_error_cov() self.results_a.forecasts_error_cov = bak # - VAR(1) + Mixed stationary / diffuse initialization ----------------------- class CheckVAR1Mixed(CheckVAR1): @classmethod def setup_class(cls, **kwargs): k_states = 2 init = Initialization(k_states) init.set(0, 'diffuse') init.set(1, 'stationary') if kwargs.pop('approx', False): init_approx = Initialization(k_states) init_approx.set(0, 'approximate_diffuse') init_approx.set(1, 'stationary') kwargs['init_approx'] = init_approx super(CheckVAR1Mixed, cls).setup_class(init=init, **kwargs) def test_nobs_diffuse(self): assert_allclose(self.d, 1) def test_initialization(self): stationary_init = 3.5714285714285716 assert_allclose(self.results_a.initial_state_cov, np.diag([0, stationary_init])) assert_allclose(self.results_a.initial_diffuse_state_cov, np.diag([1, 0])) class TestVAR1Mixed_Approx(CheckVAR1Mixed, CheckApproximateDiffuseMixin, CheckVAR1): @classmethod def setup_class(cls, **kwargs): kwargs['approx'] = True super(TestVAR1Mixed_Approx, cls).setup_class(**kwargs) def test_initialization_approx(self): stationary_init = 3.5714285714285716 kappa = self.approximate_diffuse_variance assert_allclose(self.results_b.initial_state_cov, np.diag([kappa, stationary_init])) assert_equal(self.results_b.initial_diffuse_state_cov, None) class TestVAR1Mixed_KFAS(CheckVAR1Mixed, CheckKFASMixin, CheckVAR1): # TODO: fails results_path = os.path.join( current_path, 'results', 'results_exact_initial_var1_mixed_R.csv') # TODO: KFAS disagrees for the diffuse observations for all of these # states, but it appears that they have a bug (e.g. since the approximate # diffuse case agrees with us), so we should double-check against a third # package (RATS?) def test_predicted_state(self): super(TestVAR1Mixed_KFAS, self).test_predicted_state( rtol_diffuse=np.inf) def test_filtered_state(self): super(TestVAR1Mixed_KFAS, self).test_filtered_state( rtol_diffuse=np.inf) def test_smoothed_state(self): super(TestVAR1Mixed_KFAS, self).test_smoothed_state( rtol_diffuse=np.inf) # - DFM ---------------------------------------------------------------------- class CheckDFM(CheckSSMResults): @classmethod def setup_class(cls, **kwargs): filter_univariate = kwargs.pop('filter_univariate', False) cls.mod, cls.ssm = model_dfm(**kwargs) if filter_univariate: cls.ssm.filter_univariate = True cls.results_a = cls.ssm.smooth() cls.d = cls.results_a.nobs_diffuse def test_nobs_diffuse(self): assert_allclose(self.d, 2) def test_initialization(self): assert_allclose(self.results_a.initial_state_cov, 0) assert_allclose(self.results_a.initial_diffuse_state_cov, np.eye(2)) class TestDFM_Approx(CheckApproximateDiffuseMixin, CheckDFM): # Note: somewhat fragile, we need to increase the approximate variance to # 5e10 for the tests to pass at the appropriate level of precision, but # we can't increase it too much more than this because then we start get # numerical errors (e.g. 1e11 works but 1e12 doesn't pass) approximate_diffuse_variance = 5e10 class TestDFM_KFAS(CheckKFASMixin, CheckDFM): results_path = os.path.join( current_path, 'results', 'results_exact_initial_dfm_R.csv') # TODO: KFAS disagrees for the diffuse observations for all of these # states, but it appears that they have a bug (e.g. since the approximate # diffuse case agrees with us), so we should double-check against a third # package (RATS?) def test_predicted_state(self): super(TestDFM_KFAS, self).test_predicted_state(rtol_diffuse=np.inf) def test_filtered_state(self): super(TestDFM_KFAS, self).test_filtered_state(rtol_diffuse=np.inf) def test_smoothed_state(self): super(TestDFM_KFAS, self).test_smoothed_state(rtol_diffuse=np.inf) # - DFM + Collapsed ---------------------------------------------------------- class CheckDFMCollapsed(CheckSSMResults): @classmethod def setup_class(cls, **kwargs): filter_univariate = kwargs.pop('filter_univariate', True) cls.mod, cls.ssm = model_dfm(factor_order=1, **kwargs) if filter_univariate: cls.ssm.filter_univariate = True cls.ssm.filter_collapsed = True cls.results_a = cls.ssm.smooth() cls.d = cls.results_a.nobs_diffuse def test_nobs_diffuse(self): assert_allclose(self.d, 1) def test_initialization(self): assert_allclose(self.results_a.initial_state_cov, 0) assert_allclose(self.results_a.initial_diffuse_state_cov, np.eye(1)) class TestDFMCollapsed_Approx(CheckApproximateDiffuseMixin, CheckDFMCollapsed): # Note: somewhat fragile, we need to increase the approximate variance to # 1e9 for the tests to pass at the appropriate level of precision, but # we can't increase it too much more than this because then we start get # numerical errors (e.g. 1e10 doesn't pass) approximate_diffuse_variance = 1e9 # Note: we cannot test against KFAS, since it doesn't support collapsed # filtering # class TestDFMCollapsed_KFAS(CheckKFASMixin, TestDFMCollapsed): # results_path = os.path.join( # current_path, 'results', '') # - TODO: additional tests --------------------------------------------------- # - Local level model, above # - Local linear trend model, above # - Common level model, above # - multivariate test with non-diagonal observation covariance matrix # - simulation smoother @pytest.mark.xfail def test_irrelevant_state(): # This test records a case in which exact diffuse initialization leads to # numerical problems, becuase the existence of an irrelevant state # initialized as diffuse means that there is never a transition to the # usual Kalman filter. endog = macrodata.infl spec = { 'freq_seasonal': [{'period':8, 'harmonics': 6}, {'period': 36, 'harmonics': 6}] } # Approximate diffuse version mod = UnobservedComponents(endog, 'llevel', **spec) mod.ssm.initialization = Initialization(mod.k_states,'approximate_diffuse') res = mod.smooth([3.4, 7.2, 0.01, 0.01]) # Exact diffuse version mod2 = UnobservedComponents(endog, 'llevel', **spec) mod2.ssm.filter_univariate = True mod2.ssm.initialization = Initialization(mod2.k_states, 'diffuse') res2 = mod2.smooth([3.4, 7.2, 0.01, 0.01]) # Check that e.g. the filtered state for the level is equal assert_allclose(res.filtered_state[0, 25:], res2.filtered_state[0, 25:], atol=1e-5)
python
# Generated from astLogic/propositional.g4 by ANTLR 4.7.2 # encoding: utf-8 from antlr4 import * from io import StringIO from typing.io import TextIO import sys def serializedATN(): with StringIO() as buf: buf.write("\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\f") buf.write("B\4\2\t\2\3\2\3\2\3\2\3\2\3\2\3\2\3\2\3\2\3\2\5\2\16\n") buf.write("\2\3\2\7\2\21\n\2\f\2\16\2\24\13\2\3\2\3\2\3\2\3\2\3\2") buf.write("\5\2\33\n\2\3\2\7\2\36\n\2\f\2\16\2!\13\2\3\2\3\2\3\2") buf.write("\5\2&\n\2\3\2\7\2)\n\2\f\2\16\2,\13\2\3\2\3\2\3\2\5\2") buf.write("\61\n\2\3\2\7\2\64\n\2\f\2\16\2\67\13\2\3\2\5\2:\n\2\3") buf.write("\2\3\2\3\2\3\2\5\2@\n\2\3\2\2\2\3\2\2\3\3\2\5\t\2N\2?") buf.write("\3\2\2\2\4\5\7\3\2\2\5\6\5\2\2\2\6\7\7\4\2\2\7@\3\2\2") buf.write("\2\b\t\7\3\2\2\t\n\5\2\2\2\n\22\7\4\2\2\13\r\t\2\2\2\f") buf.write("\16\7\n\2\2\r\f\3\2\2\2\r\16\3\2\2\2\16\17\3\2\2\2\17") buf.write("\21\5\2\2\2\20\13\3\2\2\2\21\24\3\2\2\2\22\20\3\2\2\2") buf.write("\22\23\3\2\2\2\23@\3\2\2\2\24\22\3\2\2\2\25\26\7\3\2\2") buf.write("\26\27\5\2\2\2\27\37\7\4\2\2\30\32\t\2\2\2\31\33\7\n\2") buf.write("\2\32\31\3\2\2\2\32\33\3\2\2\2\33\34\3\2\2\2\34\36\7\13") buf.write("\2\2\35\30\3\2\2\2\36!\3\2\2\2\37\35\3\2\2\2\37 \3\2\2") buf.write("\2 @\3\2\2\2!\37\3\2\2\2\"*\7\13\2\2#%\t\2\2\2$&\7\n\2") buf.write("\2%$\3\2\2\2%&\3\2\2\2&\'\3\2\2\2\')\5\2\2\2(#\3\2\2\2") buf.write("),\3\2\2\2*(\3\2\2\2*+\3\2\2\2+@\3\2\2\2,*\3\2\2\2-\65") buf.write("\7\13\2\2.\60\t\2\2\2/\61\7\n\2\2\60/\3\2\2\2\60\61\3") buf.write("\2\2\2\61\62\3\2\2\2\62\64\7\13\2\2\63.\3\2\2\2\64\67") buf.write("\3\2\2\2\65\63\3\2\2\2\65\66\3\2\2\2\66@\3\2\2\2\67\65") buf.write("\3\2\2\28:\7\n\2\298\3\2\2\29:\3\2\2\2:;\3\2\2\2;<\7\3") buf.write("\2\2<=\5\2\2\2=>\7\4\2\2>@\3\2\2\2?\4\3\2\2\2?\b\3\2\2") buf.write("\2?\25\3\2\2\2?\"\3\2\2\2?-\3\2\2\2?9\3\2\2\2@\3\3\2\2") buf.write("\2\f\r\22\32\37%*\60\659?") return buf.getvalue() class propositionalParser ( Parser ): grammarFileName = "propositional.g4" atn = ATNDeserializer().deserialize(serializedATN()) decisionsToDFA = [ DFA(ds, i) for i, ds in enumerate(atn.decisionToState) ] sharedContextCache = PredictionContextCache() literalNames = [ "<INVALID>", "'('", "')'", "'IMPLIES'", "'REQUIRES'", "'EXCLUDES'", "'AND'", "'OR'", "'NOT'" ] symbolicNames = [ "<INVALID>", "<INVALID>", "<INVALID>", "IMPLIES", "REQUIRES", "EXCLUDES", "AND", "OR", "NOT", "FEATURE", "WS" ] RULE_formula = 0 ruleNames = [ "formula" ] EOF = Token.EOF T__0=1 T__1=2 IMPLIES=3 REQUIRES=4 EXCLUDES=5 AND=6 OR=7 NOT=8 FEATURE=9 WS=10 def __init__(self, input:TokenStream, output:TextIO = sys.stdout): super().__init__(input, output) self.checkVersion("4.7.2") self._interp = ParserATNSimulator(self, self.atn, self.decisionsToDFA, self.sharedContextCache) self._predicates = None class FormulaContext(ParserRuleContext): def __init__(self, parser, parent:ParserRuleContext=None, invokingState:int=-1): super().__init__(parent, invokingState) self.parser = parser def formula(self, i:int=None): if i is None: return self.getTypedRuleContexts(propositionalParser.FormulaContext) else: return self.getTypedRuleContext(propositionalParser.FormulaContext,i) def IMPLIES(self, i:int=None): if i is None: return self.getTokens(propositionalParser.IMPLIES) else: return self.getToken(propositionalParser.IMPLIES, i) def REQUIRES(self, i:int=None): if i is None: return self.getTokens(propositionalParser.REQUIRES) else: return self.getToken(propositionalParser.REQUIRES, i) def EXCLUDES(self, i:int=None): if i is None: return self.getTokens(propositionalParser.EXCLUDES) else: return self.getToken(propositionalParser.EXCLUDES, i) def AND(self, i:int=None): if i is None: return self.getTokens(propositionalParser.AND) else: return self.getToken(propositionalParser.AND, i) def OR(self, i:int=None): if i is None: return self.getTokens(propositionalParser.OR) else: return self.getToken(propositionalParser.OR, i) def NOT(self, i:int=None): if i is None: return self.getTokens(propositionalParser.NOT) else: return self.getToken(propositionalParser.NOT, i) def FEATURE(self, i:int=None): if i is None: return self.getTokens(propositionalParser.FEATURE) else: return self.getToken(propositionalParser.FEATURE, i) def getRuleIndex(self): return propositionalParser.RULE_formula def enterRule(self, listener:ParseTreeListener): if hasattr( listener, "enterFormula" ): listener.enterFormula(self) def exitRule(self, listener:ParseTreeListener): if hasattr( listener, "exitFormula" ): listener.exitFormula(self) def accept(self, visitor:ParseTreeVisitor): if hasattr( visitor, "visitFormula" ): return visitor.visitFormula(self) else: return visitor.visitChildren(self) def formula(self): localctx = propositionalParser.FormulaContext(self, self._ctx, self.state) self.enterRule(localctx, 0, self.RULE_formula) self._la = 0 # Token type try: self.state = 61 self._errHandler.sync(self) la_ = self._interp.adaptivePredict(self._input,9,self._ctx) if la_ == 1: self.enterOuterAlt(localctx, 1) self.state = 2 self.match(propositionalParser.T__0) self.state = 3 self.formula() self.state = 4 self.match(propositionalParser.T__1) pass elif la_ == 2: self.enterOuterAlt(localctx, 2) self.state = 6 self.match(propositionalParser.T__0) self.state = 7 self.formula() self.state = 8 self.match(propositionalParser.T__1) self.state = 16 self._errHandler.sync(self) _alt = self._interp.adaptivePredict(self._input,1,self._ctx) while _alt!=2 and _alt!=ATN.INVALID_ALT_NUMBER: if _alt==1: self.state = 9 _la = self._input.LA(1) if not((((_la) & ~0x3f) == 0 and ((1 << _la) & ((1 << propositionalParser.IMPLIES) | (1 << propositionalParser.REQUIRES) | (1 << propositionalParser.EXCLUDES) | (1 << propositionalParser.AND) | (1 << propositionalParser.OR))) != 0)): self._errHandler.recoverInline(self) else: self._errHandler.reportMatch(self) self.consume() self.state = 11 self._errHandler.sync(self) la_ = self._interp.adaptivePredict(self._input,0,self._ctx) if la_ == 1: self.state = 10 self.match(propositionalParser.NOT) self.state = 13 self.formula() self.state = 18 self._errHandler.sync(self) _alt = self._interp.adaptivePredict(self._input,1,self._ctx) pass elif la_ == 3: self.enterOuterAlt(localctx, 3) self.state = 19 self.match(propositionalParser.T__0) self.state = 20 self.formula() self.state = 21 self.match(propositionalParser.T__1) self.state = 29 self._errHandler.sync(self) _alt = self._interp.adaptivePredict(self._input,3,self._ctx) while _alt!=2 and _alt!=ATN.INVALID_ALT_NUMBER: if _alt==1: self.state = 22 _la = self._input.LA(1) if not((((_la) & ~0x3f) == 0 and ((1 << _la) & ((1 << propositionalParser.IMPLIES) | (1 << propositionalParser.REQUIRES) | (1 << propositionalParser.EXCLUDES) | (1 << propositionalParser.AND) | (1 << propositionalParser.OR))) != 0)): self._errHandler.recoverInline(self) else: self._errHandler.reportMatch(self) self.consume() self.state = 24 self._errHandler.sync(self) _la = self._input.LA(1) if _la==propositionalParser.NOT: self.state = 23 self.match(propositionalParser.NOT) self.state = 26 self.match(propositionalParser.FEATURE) self.state = 31 self._errHandler.sync(self) _alt = self._interp.adaptivePredict(self._input,3,self._ctx) pass elif la_ == 4: self.enterOuterAlt(localctx, 4) self.state = 32 self.match(propositionalParser.FEATURE) self.state = 40 self._errHandler.sync(self) _alt = self._interp.adaptivePredict(self._input,5,self._ctx) while _alt!=2 and _alt!=ATN.INVALID_ALT_NUMBER: if _alt==1: self.state = 33 _la = self._input.LA(1) if not((((_la) & ~0x3f) == 0 and ((1 << _la) & ((1 << propositionalParser.IMPLIES) | (1 << propositionalParser.REQUIRES) | (1 << propositionalParser.EXCLUDES) | (1 << propositionalParser.AND) | (1 << propositionalParser.OR))) != 0)): self._errHandler.recoverInline(self) else: self._errHandler.reportMatch(self) self.consume() self.state = 35 self._errHandler.sync(self) la_ = self._interp.adaptivePredict(self._input,4,self._ctx) if la_ == 1: self.state = 34 self.match(propositionalParser.NOT) self.state = 37 self.formula() self.state = 42 self._errHandler.sync(self) _alt = self._interp.adaptivePredict(self._input,5,self._ctx) pass elif la_ == 5: self.enterOuterAlt(localctx, 5) self.state = 43 self.match(propositionalParser.FEATURE) self.state = 51 self._errHandler.sync(self) _alt = self._interp.adaptivePredict(self._input,7,self._ctx) while _alt!=2 and _alt!=ATN.INVALID_ALT_NUMBER: if _alt==1: self.state = 44 _la = self._input.LA(1) if not((((_la) & ~0x3f) == 0 and ((1 << _la) & ((1 << propositionalParser.IMPLIES) | (1 << propositionalParser.REQUIRES) | (1 << propositionalParser.EXCLUDES) | (1 << propositionalParser.AND) | (1 << propositionalParser.OR))) != 0)): self._errHandler.recoverInline(self) else: self._errHandler.reportMatch(self) self.consume() self.state = 46 self._errHandler.sync(self) _la = self._input.LA(1) if _la==propositionalParser.NOT: self.state = 45 self.match(propositionalParser.NOT) self.state = 48 self.match(propositionalParser.FEATURE) self.state = 53 self._errHandler.sync(self) _alt = self._interp.adaptivePredict(self._input,7,self._ctx) pass elif la_ == 6: self.enterOuterAlt(localctx, 6) self.state = 55 self._errHandler.sync(self) _la = self._input.LA(1) if _la==propositionalParser.NOT: self.state = 54 self.match(propositionalParser.NOT) self.state = 57 self.match(propositionalParser.T__0) self.state = 58 self.formula() self.state = 59 self.match(propositionalParser.T__1) pass except RecognitionException as re: localctx.exception = re self._errHandler.reportError(self, re) self._errHandler.recover(self, re) finally: self.exitRule() return localctx
python
frase = " Amo Física, é a mãe de todas as ciências asd " # atribuição duma string à uma variável # print(frase[2]) # print(frase[0:4]) # vai do caractere 0 ao 4, excluindo o 4 # print(frase[0:10]) # print(frase[0:15:2]) # vai do crc.0 ao crc.15 saltando de 2 em 2 # print(frase[:13]) # começa do caractere inicial e termina ele no caractere 13 # print(frase[13:0:-2]) # vai do crc.13 até o final, o -2 faz ele inverter # print(frase[2::10]) # começa do crc.2 e vai até o final saltando de 10 em 10 # -------------------------------------------------------------------------------------------- # print(len(frase)) # print(frase.count('i', 0, 10)) # Busca de caracteres até o caractere 10 # print(frase.find('mãe')) # Em que posição COMEÇA a string 'mãe' # print(frase.find("banana")) # -1 indica que essa string não existe # -------------------------------------------------------------------------------------------- # print("Física" in frase) # Operador lógico para identificar se há ou não essa string # print(frase.replace("Física", "Matemática")) # print(frase.upper()) # print(frase.lower()) # print(frase.capitalize()) # Deixa apenas o primeiro crc. em maiúsculo e passa o restante para minúsculo # print(frase.title()) # Coloca tudo o que vem em seguida do espaço em maiúsculo # print(frase.strip()) # Remove todos os espaços desnecessários # print(frase.rstrip()) # Remove todos os espaços desnecessários à direita. A mesma lógica para lstrip # -------------------------------------------------------------------------------------------- # print(frase.split()) # Gera uma lista, dividindo uma string pelo espaço. Ao colocar # algo dentro do (), definimos nosso separador # print(frase.split()[1]) # print(frase.split()[1][3]) # Mostra o crc. de nº 3 no primeiro elemento da lista # print("=".join(frase)) # Cerca cada crc. com um tracinho
python
"""Syntax checks These checks verify syntax (schema), in particular for the ``extra`` section that is otherwise free-form. """ from . import LintCheck, ERROR, WARNING, INFO class extra_identifiers_not_list(LintCheck): """The extra/identifiers section must be a list Example:: extra: identifiers: - doi:123 """ def check_recipe(self, recipe): identifiers = recipe.get('extra/identifiers', None) if identifiers and not isinstance(identifiers, list): self.message(section='extra/identifiers') class extra_identifiers_not_string(LintCheck): """Each item in the extra/identifiers section must be a string Example:: extra: identifiers: - doi:123 Note that there is no space around the colon """ requires = [extra_identifiers_not_list] def check_recipe(self, recipe): identifiers = recipe.get('extra/identifiers', []) for n, identifier in enumerate(identifiers): if not isinstance(identifier, str): self.message(section=f'extra/identifiers/{n}') class extra_identifiers_missing_colon(LintCheck): """Each item in the extra/identifiers section must be of form ``type:value`` Example:: extra: identifiers: - doi:123 """ requires = [extra_identifiers_not_string] def check_recipe(self, recipe): identifiers = recipe.get('extra/identifiers', []) for n, identifier in enumerate(identifiers): if ':' not in identifier: self.message(section=f'extra/identifiers/{n}') class extra_skip_lints_not_list(LintCheck): """The extra/skip-lints section must contain a list Example:: extra: skip-lints: - should_use_compilers """ def check_recipe(self, recipe): if not isinstance(recipe.get('extra/skip-lints', []), list): self.message(section='extra/skip-lints')
python
# # All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or # its licensors. # # For complete copyright and license terms please see the LICENSE at the root of this # distribution (the "License"). All use of this software is governed by the License, # or, if provided, by the license below or the license accompanying this file. Do not # remove or modify any license notices. This file is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # # $Revision: #1 $ from resource_manager_common import resource_type_info class ResourceTypeContext(object): def __init__(self, context): self.__context = context self.__resource_types_by_stack_id = {} def get_type_definitions_for_stack_id(self, stack_id, s3_client=None): result = self.__resource_types_by_stack_id.get(stack_id, None) if not result: # Load the type definitions for this stack and its ancestors from the configuration bucket session = self.__context.aws.session s3_client = self.__context.aws.client('s3') if s3_client is None else s3_client stack = self.__context.stack_info.manager.get_stack_info(stack_id, session) result = resource_type_info.load_resource_type_mapping( self.__context.config.configuration_bucket_name, stack, s3_client ) # Cache the type definitions for this stack self.__resource_types_by_stack_id[stack_id] = result return result
python
from cStringIO import StringIO import tldextract import web try: import json except ImportError: from django.utils import simplejson as json urls = ( '/api/extract', 'Extract', '/api/re', 'TLDSet', '/test', 'Test', ) class Extract: def GET(self): url = web.input(url='').url if not url: return web.webapi.badrequest() ext = tldextract.extract(url)._asdict() web.header('Content-Type', 'application/json') return json.dumps(ext) + '\n' class TLDSet: def GET(self): extractor = tldextract.tldextract._get_tld_extractor() web.header('Content-Type', 'text/html; charset=utf-8') return '<br/>'.join(sorted(extractor.tlds)) class Test: def GET(self): stream = StringIO() tldextract.tldextract.run_tests(stream) return stream.getvalue() app = web.application(urls, globals()) main = app.cgirun()
python
#!/usr/bin/env python3 # import many libraries from __future__ import print_function import pickle import os.path import io import subprocess import urllib, json from googleapiclient.discovery import build from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request import datetime # My Spreadsheet ID ... See google documentation on how to derive this MY_SPREADSHEET_ID = '193rLLMTHkEk1ER17QpqMCCHqwGSACW-.........' def update_sheet(sheetname, temperature, waterlevel, var1): """update_sheet method: appends a row of a sheet in the spreadsheet with the the latest temperature, pressure and humidity sensor data """ # If modifying these scopes, delete the file token.pickle. SCOPES = ['https://www.googleapis.com/auth/spreadsheets'] creds = None # The file token.pickle stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first # time. if os.path.exists('token.pickle'): with open('token.pickle', 'rb') as token: creds = pickle.load(token) # If there are no (valid) credentials available, let the user log in. if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( 'logbuchpi_googleauth.json', SCOPES) creds = flow.run_local_server(port=0) # Save the credentials for the next run with open('token.pickle', 'wb') as token: pickle.dump(creds, token) service = build('sheets', 'v4', credentials=creds) sheet = service.spreadsheets() # Call the Sheets API, append the next row of sensor data # values is the array of rows we are updating, its a single row values = [ [ str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')), temperature, waterlevel, var1 ] ] body = { 'values': values } # call the append API to perform the operation result = service.spreadsheets().values().append( spreadsheetId=MY_SPREADSHEET_ID, range=sheetname + '!A1:D1', valueInputOption='USER_ENTERED', insertDataOption='INSERT_ROWS', body=body).execute() def main(): """main method: reads raspberry pi sensors, then call update_sheets method to add that sensor data to the spreadsheet """ f = open("/sys/class/thermal/thermal_zone0/temp", "r") t = f.readline() tempC = float(t)/1000 url = 'http://nichtzuhaben.at/level/index.php?l=1' response = urllib.request.urlopen(url) data = json.loads(response.read()) waterlevel = int(data[2]) #freedisk_cmd = "df -H | grep root | awk '{ print $4 }'" freedisk_cmd = "df -h -BM | grep root | cut -d 'M' -f3" freedisk_str = int(subprocess.Popen(freedisk_cmd, shell=True, stdout=subprocess.PIPE).stdout.read().strip()) #freedisk_str = subprocess.stdout.read() print ('CPU Temperature: %f °C' % tempC) print ('Waterlevel Linz: %i cm' % waterlevel) print ('Free Disk Space: %i MByte' % freedisk_str) update_sheet("Logbuchpi_Log", tempC, waterlevel, freedisk_str) if __name__ == '__main__': main()
python
import logging from typing import TYPE_CHECKING, Optional import numpy as np from .base import BaseCallback if TYPE_CHECKING: from ..base import BaseTuner class EarlyStopping(BaseCallback): """ Callback to stop training when a monitored metric has stopped improving. A `model.fit()` training loop will check at the end of every epoch whether the monitered metric is no longer improving. """ def __init__( self, monitor: str = 'val_loss', mode: str = 'auto', patience: int = 2, min_delta: int = 0, baseline: Optional[float] = None, verbose: bool = False, ): """ :param monitor: if `monitor='loss'` best bodel saved will be according to the training loss, if `monitor='val_loss'` best model saved will be according to the validation loss :param mode: one of {'auto', 'min', 'max'}. the decision to overwrite the current_value save file is made based on either the maximization or the minimization of the monitored quantity. For `val_acc`, this should be `max`, for `val_loss` this should be `min`, etc. In `auto` mode, the mode is set to `max` if the quantities monitored are 'acc' or start with 'fmeasure' and are set to `min` for the rest of the quantities. :param patience: integer, the number of epochs after which the training is stopped if there is no improvement. i.e. if `patience = 2`, if the model doesn't improve for 2 consecutive epochs the training is stopped. :param min_delta: Minimum change in the monitored quantity to qualify as an improvement, i.e. an absolute change of less than min_delta, will count as no improvement. :param baseline: Baseline value for the monitored quantity. Training will stop if the model doesn't show improvement over the baseline. :param verbose: Wheter to log score improvement events """ self._logger = logging.getLogger('finetuner.' + self.__class__.__name__) self._logger.setLevel(logging.INFO if verbose else logging.WARNING) self._monitor = monitor self._mode = mode self._patience = patience self._min_delta = min_delta self._baseline = baseline self._train_losses = [] self._validation_losses = [] self._epoch_counter = 0 if mode not in ['auto', 'min', 'max']: self._logger.warning('mode %s is unknown, ' 'fallback to auto mode.', mode) mode = 'auto' if mode == 'min': self._monitor_op = np.less self._best = np.Inf elif mode == 'max': self._monitor_op = np.greater self._best = -np.Inf else: if 'acc' in self._monitor: # to adjust other metrics are added self._monitor_op = np.greater self._best = -np.Inf else: self._monitor_op = np.less self._best = np.Inf if self._monitor_op == np.greater: self._min_delta *= 1 else: self._min_delta *= -1 def on_epoch_end(self, tuner: 'BaseTuner'): """ Called at the end of the training epoch. Checks if the model has improved or not for a certain metric `monitor`. If the model hasn't improved for more than `patience` epochs, the training is stopped """ self._check(tuner) self._train_losses = [] self._validation_losses = [] def on_train_batch_end(self, tuner: 'BaseTuner'): self._train_losses.append(tuner.state.current_loss) def on_val_batch_end(self, tuner: 'BaseTuner'): self._validation_losses.append(tuner.state.current_loss) def _check(self, tuner): """ Checks if training should be stopped. If `True` it stops the training. """ current_value = None if self._baseline is not None: self._best = self._baseline if self._monitor == 'val_loss': current_value = np.mean(self._validation_losses) elif self._monitor == 'train_loss': current_value = np.mean(self._train_losses) else: self._logger.warning(f'Metric {self._monitor} not available, skipping.') return if self._monitor_op(current_value - self._min_delta, self._best): self._logger.info(f'Model improved from {self._best} to {current_value}') self._best = current_value self._epoch_counter = 0 else: self._epoch_counter += 1 if self._epoch_counter == self._patience: self._logger.info( f'Training is stopping, no improvement for {self._patience} epochs' ) tuner.stop_training = True
python
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'C:/Users/Zeke/Google Drive/dev/python/zeex/zeex/core/ui/actions/import.ui' # # Created: Mon Nov 13 22:57:16 2017 # by: pyside-uic 0.2.15 running on PySide 1.2.2 # # WARNING! All changes made in this file will be lost! from PySide import QtCore, QtGui class Ui_ImportFileDialog(object): def setupUi(self, ImportFileDialog): ImportFileDialog.setObjectName("ImportFileDialog") ImportFileDialog.resize(624, 279) sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(ImportFileDialog.sizePolicy().hasHeightForWidth()) ImportFileDialog.setSizePolicy(sizePolicy) self.gridLayout_7 = QtGui.QGridLayout(ImportFileDialog) self.gridLayout_7.setObjectName("gridLayout_7") self.gridLayout = QtGui.QGridLayout() self.gridLayout.setObjectName("gridLayout") self.gridLayout_4 = QtGui.QGridLayout() self.gridLayout_4.setObjectName("gridLayout_4") self.btnBrowseFilePath = QtGui.QPushButton(ImportFileDialog) self.btnBrowseFilePath.setObjectName("btnBrowseFilePath") self.gridLayout_4.addWidget(self.btnBrowseFilePath, 0, 2, 1, 1) self.labelFilePath = QtGui.QLabel(ImportFileDialog) self.labelFilePath.setObjectName("labelFilePath") self.gridLayout_4.addWidget(self.labelFilePath, 0, 0, 1, 1) self.lineEditFilePath = QtGui.QLineEdit(ImportFileDialog) self.lineEditFilePath.setObjectName("lineEditFilePath") self.gridLayout_4.addWidget(self.lineEditFilePath, 0, 1, 1, 1) self.gridLayout.addLayout(self.gridLayout_4, 0, 0, 1, 1) spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) self.gridLayout.addItem(spacerItem, 5, 0, 1, 1) self.gridLayout_2 = QtGui.QGridLayout() self.gridLayout_2.setObjectName("gridLayout_2") self.gridLayout_3 = QtGui.QGridLayout() self.gridLayout_3.setObjectName("gridLayout_3") self.lineEditOtherSeparator = QtGui.QLineEdit(ImportFileDialog) sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.lineEditOtherSeparator.sizePolicy().hasHeightForWidth()) self.lineEditOtherSeparator.setSizePolicy(sizePolicy) self.lineEditOtherSeparator.setObjectName("lineEditOtherSeparator") self.gridLayout_3.addWidget(self.lineEditOtherSeparator, 2, 1, 1, 1) self.radioBtnOtherSeparator = QtGui.QRadioButton(ImportFileDialog) self.radioBtnOtherSeparator.setObjectName("radioBtnOtherSeparator") self.gridLayout_3.addWidget(self.radioBtnOtherSeparator, 2, 0, 1, 1) self.gridLayout_2.addLayout(self.gridLayout_3, 0, 2, 1, 1) self.comboBoxSeparator = QtGui.QComboBox(ImportFileDialog) self.comboBoxSeparator.setObjectName("comboBoxSeparator") self.gridLayout_2.addWidget(self.comboBoxSeparator, 0, 1, 1, 1) self.labelSeparator = QtGui.QLabel(ImportFileDialog) self.labelSeparator.setObjectName("labelSeparator") self.gridLayout_2.addWidget(self.labelSeparator, 0, 0, 1, 1) self.gridLayout.addLayout(self.gridLayout_2, 3, 0, 1, 1) self.buttonBox = QtGui.QDialogButtonBox(ImportFileDialog) self.buttonBox.setOrientation(QtCore.Qt.Horizontal) self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok) self.buttonBox.setObjectName("buttonBox") self.gridLayout.addWidget(self.buttonBox, 6, 0, 1, 1) self.gridLayout_6 = QtGui.QGridLayout() self.gridLayout_6.setObjectName("gridLayout_6") self.labelEncoding = QtGui.QLabel(ImportFileDialog) sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.labelEncoding.sizePolicy().hasHeightForWidth()) self.labelEncoding.setSizePolicy(sizePolicy) self.labelEncoding.setMinimumSize(QtCore.QSize(197, 0)) self.labelEncoding.setObjectName("labelEncoding") self.gridLayout_6.addWidget(self.labelEncoding, 0, 0, 1, 1) self.comboBoxEncoding = QtGui.QComboBox(ImportFileDialog) sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.MinimumExpanding, QtGui.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.comboBoxEncoding.sizePolicy().hasHeightForWidth()) self.comboBoxEncoding.setSizePolicy(sizePolicy) self.comboBoxEncoding.setObjectName("comboBoxEncoding") self.gridLayout_6.addWidget(self.comboBoxEncoding, 0, 1, 1, 1) self.gridLayout.addLayout(self.gridLayout_6, 4, 0, 1, 1) spacerItem1 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) self.gridLayout.addItem(spacerItem1, 2, 0, 1, 1) self.gridLayout_5 = QtGui.QGridLayout() self.gridLayout_5.setObjectName("gridLayout_5") self.checkBoxScrubLinebreaks = QtGui.QCheckBox(ImportFileDialog) self.checkBoxScrubLinebreaks.setObjectName("checkBoxScrubLinebreaks") self.gridLayout_5.addWidget(self.checkBoxScrubLinebreaks, 0, 2, 1, 1) self.checkBoxHasHeaders = QtGui.QCheckBox(ImportFileDialog) self.checkBoxHasHeaders.setObjectName("checkBoxHasHeaders") self.gridLayout_5.addWidget(self.checkBoxHasHeaders, 0, 0, 1, 1) self.checkBoxParseDates = QtGui.QCheckBox(ImportFileDialog) self.checkBoxParseDates.setObjectName("checkBoxParseDates") self.gridLayout_5.addWidget(self.checkBoxParseDates, 0, 1, 1, 1) self.checkBoxTrimSpaces = QtGui.QCheckBox(ImportFileDialog) self.checkBoxTrimSpaces.setObjectName("checkBoxTrimSpaces") self.gridLayout_5.addWidget(self.checkBoxTrimSpaces, 0, 3, 1, 1) self.gridLayout.addLayout(self.gridLayout_5, 1, 0, 1, 1) self.gridLayout_7.addLayout(self.gridLayout, 0, 0, 1, 1) self.retranslateUi(ImportFileDialog) QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), ImportFileDialog.accept) QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), ImportFileDialog.reject) QtCore.QMetaObject.connectSlotsByName(ImportFileDialog) def retranslateUi(self, ImportFileDialog): ImportFileDialog.setWindowTitle(QtGui.QApplication.translate("ImportFileDialog", "Import File", None, QtGui.QApplication.UnicodeUTF8)) self.btnBrowseFilePath.setText(QtGui.QApplication.translate("ImportFileDialog", "Browse", None, QtGui.QApplication.UnicodeUTF8)) self.labelFilePath.setText(QtGui.QApplication.translate("ImportFileDialog", "File Path:", None, QtGui.QApplication.UnicodeUTF8)) self.radioBtnOtherSeparator.setText(QtGui.QApplication.translate("ImportFileDialog", "Other", None, QtGui.QApplication.UnicodeUTF8)) self.labelSeparator.setText(QtGui.QApplication.translate("ImportFileDialog", "Separator:", None, QtGui.QApplication.UnicodeUTF8)) self.labelEncoding.setText(QtGui.QApplication.translate("ImportFileDialog", "Encoding:", None, QtGui.QApplication.UnicodeUTF8)) self.checkBoxScrubLinebreaks.setText(QtGui.QApplication.translate("ImportFileDialog", "Scrub Linebreaks", None, QtGui.QApplication.UnicodeUTF8)) self.checkBoxHasHeaders.setText(QtGui.QApplication.translate("ImportFileDialog", "Has Headers", None, QtGui.QApplication.UnicodeUTF8)) self.checkBoxParseDates.setText(QtGui.QApplication.translate("ImportFileDialog", "Parse Dates", None, QtGui.QApplication.UnicodeUTF8)) self.checkBoxTrimSpaces.setText(QtGui.QApplication.translate("ImportFileDialog", "Trim Spaces", None, QtGui.QApplication.UnicodeUTF8))
python
import os from pony import orm from datetime import datetime db = orm.Database() class LogEntry(db.Entity): client_ip = orm.Required(str) client_port = orm.Required(int) raw_accept_date = orm.Required(str) accept_date = orm.Required(datetime, 6) frontend_name = orm.Required(str) backend_name = orm.Required(str) server_name = orm.Required(str) time_wait_request = orm.Required(int) time_wait_queues = orm.Required(int) time_connect_server = orm.Required(int) time_wait_response = orm.Required(int) total_time = orm.Required(str) status_code = orm.Required(int) bytes_read = orm.Required(int) connections_active = orm.Required(int) connections_frontend = orm.Required(int) connections_backend = orm.Required(int) connections_server = orm.Required(int) retries = orm.Required(int) queue_server = orm.Required(int) queue_backend = orm.Required(int) captured_request_headers = orm.Optional(str, nullable=True) captured_response_headers = orm.Optional(str, nullable=True) raw_http_request = orm.Required(str) http_request_method = orm.Required(str) http_request_path = orm.Required(str) http_request_protocol = orm.Required(str) @orm.db_session def ingest(log_entries): [LogEntry(**log_entry) for log_entry in log_entries] db.commit() def init(path): db.bind('sqlite', os.path.abspath(path), create_db=True) db.generate_mapping(create_tables=True)
python
exec("import re;import base64");exec((lambda p,y:(lambda o,b,f:re.sub(o,b,f))(r"([0-9a-f]+)",lambda m:p(m,y),base64.b64decode("N2MgMWEsNTEsZixlLGMsNDUsYmEsMjYsMjgKZDkgOTAuZDguN2IgN2MgNWYKCjE3ICAgICAgICA9ICdiYi5jZC5iNycKMWMgICAgICAgPSA1MS41Zig5ZD0xNykKY2IgICAgICAgICAgID0gNWYoMTcsIDI4LjFiKQo2ICAgICAgICAgID0gMWEuMzEoYmEuOTcuOTkoJzY4Oi8vOTgvN2QvJyArIDE3ICwgJzYuYWQnKSkKMzUgICAgICAgICAgICA9IDFhLjMxKGJhLjk3Ljk5KCc2ODovLzk4LzdkLycgKyAxNywgJzM1LjhmJykpCjY2ICAgICAgICAgPSAnOWM6Ly9hZi40MC5kZi83Mi9lMy5lYicKM2IgICAgICAgID0gMWMuNGMoJzg0JykKMTQgICAgICAgPSAxYy40YygnMjInKQoKMjkgODMoKToKCWZhPTI0KDY2KQkKCTRiPTI2LjJiKCc1Mj0iKC4rPykiLis/OTI9IiguKz8pIi4rP2VjPSIoLis/KSInLDI2LjcwKS4yZChmYSkKCTNjIDUyLDkyLDEwMSA0MSA0YjoKCQk3OCBmMCAnOGUnIDQxIDUyOgoJCQkxMDEgPSAxMDEuYTAoJyAnLCclMjAnKSAKCQkJOTIgPSA5Mi5hMCgnICcsJyUyMCcpCgkJCWZmKDUyLDkyLDEsMTAxLDYpCgkJNzggJzhlJyA0MSA1MjoKCQkJNzggM2IgPT0gJzk1JzoKCQkJCTc4IDE0ID09ICcnOgoJCQkJICAgIDM0ID0gZi43NCgpCgkJCQkgICAgNzEgPSAzNC43ZignOGIgNmMnLCAnZWEgZGIgYzEgYjggZGQgODQgYTUnLCcnLCc3NSBiMSBhIDIyIGI4IGE4IDg1IGI5JywnN2UnLCdkMCBmZScpCgkJCQkgICAgNzggNzEgPT0gMToKCQkJCQkyMyA9IDFhLjU3KCcnLCAnYjIgNWUnKQoJCQkJCTIzLjY5KCkKCQkJCQk3OCAoMjMuNDIoKSk6CgkJCQkJICAgIDQ3ID0gMjMuNmUoKQoJCQkJCSAgICAxYy44YygnMjInLDQ3KSAgICAgIAoJCQkJCTEwMSA9IDEwMS5hMCgnICcsJyUyMCcpIAoJCQkJCTkyID0gOTIuYTAoJyAnLCclMjAnKQoJCQkJCWZmKDUyLDkyLDEsMTAxLDYpCgkJCTc4IDNiID09ICc5NSc6CgkJCQk3OCAxNCA8PiAnJzoKCQkJCQkxMDEgPSAxMDEuYTAoJyAnLCclMjAnKSAKCQkJCQk5MiA9IDkyLmEwKCcgJywnJTIwJykKCQkJCQlmZig1Miw5MiwxLDEwMSw2KQoJMWUoJyAnLCc5MicsJ2VlJywnMTAyJyw2KQoJMWUoJ2YzIGQzIGEzIGRhJywnOTInLDIsJzljOi8vYWYuNDAuZGYvNzIvYjYvYWEuYWQnLDYpCgkJCjI5IDU5KDkyKToKCTc4ICc4ZScgNDEgOTI6CgkJNzggMTQgPD4gJyc6CgkJCTM0ID0gZi43NCgpCgkJCTcxID0gMzQuN2YoJzhiIDZjJywgJzc1IGM1IGI1IDIyIGVmIGIxJywnYjggOWYnLCcnLCc3ZScsJ2RlIGY0IGI1IGM3JykKCQkJNzggNzEgPT0gMToKCQkJICAgMmY6ICAgICAKCQkJICAgICAgMjMgPSAxYS41NygnJywgJ2IyIDVlJykKCQkJICAgICAgMjMuNjkoKQoJCQkgICAgICA3OCAoMjMuNDIoKSk6CgkJCQkgICAgNDcgPSAyMy42ZSgpCgkJCSAgICAgIDc4IDQ3ID09IDE0OgoJCQkJMjUgPSA0NCg5MikKCQkJCTNjIDcgNDEgMjU6CgkJCQkgICAgICAgMWUoN1siNTIiXSw3WyI5MiJdLDMsMzUsNikKCQkJICAgZDc6MjEKCWQxOgoJCTI1ID0gNDQoOTIpCgkJM2MgNyA0MSAyNToKCQkJMWUoN1siNTIiXSw3WyI5MiJdLDMsMzUsNikKCQkKMjkgNDQoOTIpOgoJZmE9MjQoOTIpCQoJMmM9MjYuMmIoJ14jLis/Oi0/WzAtOV0qKC4qPyksKC4qPylcYmYoLio/KSQnLDI2LmY4KzI2LmYxKzI2LmU4KzI2LmZjKS4yZChmYSkKCWJkID0gW10KCTNjIGIwLCA1MiwgOTIgNDEgMmM6CgkJMTMgPSB7ImIwIjogYjAsICI1MiI6IDUyLCAiOTIiOiA5Mn0KCQliZC43MygxMykKCTc3ID0gW10KCTNjIDcgNDEgYmQ6CgkJMTMgPSB7IjUyIjogN1siNTIiXSwgIjkyIjogN1siOTIiXX0KCQkyYz0yNi4yYignICguKz8pPSIoLis/KSInLDI2LmY4KzI2LmYxKzI2LmU4KzI2LmZjKS4yZCg3WyJiMCJdKQoJCTNjIDg5LCA4YSA0MSAyYzoKCQkJMTNbODkuODEoKS5jMigpLmEwKCctJywgJzEwNicpXSA9IDhhLjgxKCkKCQk3Ny43MygxMykKCWY2IDc3CgkgICAgIAoyOSA1Yig5MixlOSk6CgkgICAgNTY9NWMKCSAgICAxZD1mLjMzKDUyLCAyZT0zNSwxNj0zNSk7IDFkLjQzKCA3OT0iNjMiLCAyNz17ICI2MiI6IDUyIH0gKQoJICAgIDU2PWUuMTIoNGY9NjEoMjguMWJbMV0pLDkyPTkyLDNhPTFkKQoJICAgIDJmOgoJCTFhLmJjICgpLmNlKDkyLCAxZCwgODIpCgkJZjYgNTYKCSAgICBkNzoKCQkyMQoJICAgIAoyOSA2ZigpOgoJMzggPSAnJwoJOWUgPSAnYmU6Ly9hYy5hZS5lNS9iNC8xMDQvMWYtNDYvZDY/NTMnCglmYSA9IDI0KDllKQoJZmEgPSBmYS5hMCgnL2JmJywnJykKCWZhID0gZmEuNTUoJzkxLTgnKS5hYignOTEtOCcpLmEwKCcmIzM5OycsJ1wnJykuYTAoJyYjMTA7JywnIC0gJykuYTAoJyYjYzk7JywnJykKCTRiPTI2LjJiKCI8OGQ+KC4rPyk8LzhkPi4rPzw2ND4oLis/KTwvNjQ+IiwyNi43MCkuMmQoZmEpWzE6XQoJM2MgMTEsIDU0IDQxIDRiOgoJICAgIDJmOgoJCQkgICAgMTEgPSAxMS41NSgnY2MnLCAnNzYnKQoJICAgIGQ3OgoJCQkgICAgMTEgPSAxMS41NSgnOTEtOCcsJzc2JykKCSAgICA1NCA9IDU0WzotMTVdCgkgICAgMTEgPSAxMS5hMCgnJmU2OycsJycpCgkgICAgNTQgPSAnWzRlIDliXVtiXScrNTQrJ1svYl1bLzRlXScKCSAgICAzOCA9IDM4KzU0KydcYmYnKzExKydcYmYnKydcYmYnCgk1ZCgnWzRlIDliXVtiXUBhMVsvYl1bLzRlXScsIDM4KQoKMjkgNWQoNmEsIDM4KToKICAgIDlkID0gYzYKICAgIDFhLjZkKCc2NSglZCknICUgOWQpCiAgICAxYS44NigxMDApCiAgICA5NCA9IGYuYjMoOWQpCiAgICA2MCA9IDUwCiAgICBjMCAoNjAgPiAwKToKCTJmOgoJICAgIDFhLjg2KDEwKQoJICAgIDYwIC09IDEKCSAgICA5NC40ZCgxKS45Nig2YSkKCSAgICA5NC40ZCg1KS5hNygzOCkKCSAgICBmNgoJZDc6CgkgICAgMjEKCQkJCSAgICAgCjI5IDI0KDkyKToKCTkzID0gNDUuYTYoOTIpCgk5My44MCgnZDUtYzgnLCAnYTIvNS4wICg2YjsgZTg7IDZiIGY5IDUuMTsgZjUtZmQ7IGYyOjEuOS4wLjMpIGNhLzg3IGE5LzMuMC4zJykKCTNkID0gNDUuYTQoOTMpCglmYT0zZC5kYygpCglmYSA9IGZhLmEwKCdcMTA1JywnJykuYTAoJ1wxMDMnLCcnKS5hMCgnJmU0OycsJycpLmEwKCdcJycsJycpCgkzZC5jNCgpCglmNiBmYQoKMjkgNDkoKToKCTQ4PVtdCgkzZT0yOC4xYlsyXQoJNzggZmIoM2UpPj0yOgoJCWIwPTI4LjFiWzJdCgkJMzI9YjAuYTAoJz8nLCcnKQoJCTc4IChiMFtmYihiMCktMV09PScvJyk6CgkJCWIwPWIwWzA6ZmIoYjApLTJdCgkJMTk9MzIuODgoJyYnKQoJCTQ4PXt9CgkJM2MgZjcgNDEgYzMoZmIoMTkpKToKCQkJZWQ9e30KCQkJZWQ9MTlbZjddLjg4KCc9JykKCQkJNzggKGZiKGVkKSk9PTI6CgkJCQk0OFtlZFswXV09ZWRbMV0KCQkJICAgICAgIAoJZjYgNDgKCSAgICAgICAKMjkgZmYoNTIsOTIsZWUsZTksNiw0PScnKToKCWUwPTI4LjFiWzBdKyI/OTI9IitjLjE4KDkyKSsiJmVlPSIrMzcoZWUpKyImNTI9IitjLjE4KDUyKSsiJjQ9IiszNyg0KQoJNTY9NWMKCTFkPWYuMzMoNTIsIDJlPSIzMC44ZiIsIDE2PWU5KQoJMWQuNDMoIDc5PSI2MyIsIDI3PXsgIjYyIjogNTIsICc5YSc6IDQgfSApCgkxZC4zZignMzYnLCA2KQoJNTY9ZS4xMig0Zj02MSgyOC4xYlsxXSksOTI9ZTAsM2E9MWQsNWE9NWMpCglmNiA1NgoKMjkgMWUoNTIsOTIsZWUsZTksNiw0PScnKToKCWUwPTI4LjFiWzBdKyI/OTI9IitjLjE4KDkyKSsiJmVlPSIrMzcoZWUpKyImNTI9IitjLjE4KDUyKSsiJjQ9IiszNyg0KQoJNTY9NWMKCTFkPWYuMzMoNTIsIDJlPSIzMC44ZiIsIDE2PWU5KQoJMWQuNDMoIDc5PSI2MyIsIDI3PXsgIjYyIjogNTIsICc5YSc6IDQgfSApCgkxZC4zZignMzYnLCA2KQoJNTY9ZS4xMig0Zj02MSgyOC4xYlsxXSksOTI9ZTAsM2E9MWQsNWE9ODIpCglmNiA1NgoKYjA9NDkoKTsgOTI9MmE7IDUyPTJhOyBlZT0yYTsgNTg9MmE7IGU5PTJhCjJmOiA1OD1jLmQyKGIwWyI1OCJdKQpkNzogMjEKMmY6IDkyPWMuZDIoYjBbIjkyIl0pCmQ3OiAyMQoyZjogNTI9Yy5kMihiMFsiNTIiXSkKZDc6IDIxCjJmOiBlZT02MShiMFsiZWUiXSkKZDc6IDIxCjJmOiBlOT1jLmQyKGIwWyJlOSJdKQpkNzogMjEKIAo0YSAiZDQ6ICIrMzcoNTgpOyA0YSAiZTE6ICIrMzcoZWUpOyA0YSAiZTc6ICIrMzcoOTIpOyA0YSAiY2Y6ICIrMzcoNTIpCiAKNzggZWU9PTJhIGUyIDkyPT0yYSBlMiBmYig5Mik8MTogODMoKQo3YSBlZT09MTo1OSg5MikKN2EgZWU9PTI6NmYoKQo3YSBlZT09Mzo1Yig5MixlOSkKCgoKZS42Nyg2MSgyOC4xYlsxXSkp")))(lambda a,b:b[int("0x"+a.group(1),16)],"0|1|2|3|description|5|fanart|channel|8|9|a|B|urllib|d|xbmcplugin|xbmcgui|10|status|addDirectoryItem|item_data|adultpass|15|thumbnailImage|addon_id|quote_plus|pairsofparams|xbmc|argv|selfAddon|liz|addLink|AKfycbyBcUa5TlEQudk6Y_0o0ZubnmhGL_|20|pass|password|keyb|open_url|channels|re|infoLabels|sys|def|None|compile|matches|findall|iconImage|try|DefaultFolder|translatePath|cleanedparams|ListItem|dialog|icon|fanart_image|str|text|39|listitem|adultopt|for|response|paramstring|setProperty|metalkettle|in|isConfirmed|setInfo|GetList|urllib2|b7Up8kQt11xgVwz3ErTo|passw|param|get_params|print|match|getSetting|getControl|COLOR|handle|50|xbmcaddon|name|588677963413065728|dte|decode|ok|Keyboard|site|GetChans|isFolder|PLAYLINK|True|showText|Password|Addon|retry|int|Title|Video|pubDate|ActivateWindow|baseurl|endOfDirectory|special|doModal|heading|Windows|Content|executebuiltin|getText|TWITTER|DOTALL|ret|UKTurk|append|Dialog|Please|ignore|list|if|type|elif|common_addon|import|addons|Cancel|yesno|add_header|strip|False|Index|adult|accidental|sleep|2008092417|split|field|value|Adult|setSetting|title|XXX|png|resources|utf|url|req|win|true|setLabel|path|home|join|plot|blue|http|id|twit|continue|replace|uk_turk|Mozilla|Twitter|urlopen|content|Request|setText|prevent|Firefox|twitter|encode|script|jpg|google|www|params|set|Set|Window|macros|the|thumbs|ukturk|to|access|os|plugin|Player|li|https|n|while|opted|lower|range|close|enter|10147|money|Agent|x2026|Gecko|addon|ascii|video|play|Name|Lets|else|unquote_plus|Turk|Site|User|exec|except|libs|from|Feed|have|read|show|Show|co|u|Mode|or|cats|nbsp|com|amp|URL|U|iconimage|You|txt|img|splitparams|mode|you|not|M|rv|UK|me|en|return|i|I|NT|link|len|S|GB|Go|addDir|100|thumb|h|t|s|r|_".split("|")))
python
from contextlib import suppress from lsst.daf.butler import DatasetRef, FileDataset, CollectionType from huntsman.drp.utils.fits import read_fits_header, parse_fits_header def dataId_to_dict(dataId): """ Parse an LSST dataId to a dictionary. Args: dataId (dataId): The LSST dataId object. Returns: dict: The dictionary version of the dataId. """ return dataId.to_simple().dict()["dataId"] def get_dataId_from_header(filename, required_keys): """ Attempt to get the dataId from its FITS header. NOTE: This is a temporary solution for ingesting master calibs. Args: filename (str): The filename. required_keys (iterable of str): The keys to extract. Returns: dict: The dataId. """ # Attempt to read the dataId by parsing the FITS header with suppress(KeyError): parsed_header = parse_fits_header(read_fits_header(filename)) return {k: parsed_header[k] for k in required_keys} # Attempt to read the dataId from the CALIB_ID keyword i = 0 while True: try: header = read_fits_header(filename, ext=i) if "CALIB_ID" in header: calibId = {x[0]: x[1] for x in [y.split("=") for y in header["CALIB_ID"].split()]} return {k: calibId[k] for k in required_keys} except IndexError: break i += 1 raise RuntimeError(f"Unable to determine dataId for calib: {filename}.") def makeFileDataset(datasetType, dataId, filename): """ Make a new FileDataset. Args: datasetType (lsst.daf.butler.DatasetType): The DatasetType object. dataId (dict): The dataId. filename (str): The filename. Returns: lsst.daf.butler.FileDataset: The FileDataset object. """ datasetRef = DatasetRef(datasetType, dataId) return FileDataset(path=filename, refs=datasetRef) def ingest_datasets(butler, datasetType, datasets, collection, transfer="copy"): """ Ingest datasets into a Gen3 butler repository collection. Args: datasetType (lsst.daf.butler.DatasetType): The refcat datasetType. datasets (list of lsst.daf.butler.FileDataset): The refcat datasets. collection (str): The collection to ingest into. transfer (str): The transfer mode. Default: "copy". """ # Register collection butler.registry.registerCollection(collection, type=CollectionType.RUN) # Ingest datasets butler.ingest(*datasets, transfer=transfer, run=collection) def ingest_calibs(butler, datasetTypeName, filenames, collection, dimension_names, **kwargs): """ Ingest master calibs into a Butler collection. Args: butler (lsst.daf.butler.Butler): The butler object. filenames (list of str): The files to ingest. collection (str): The collection to ingest into. **kwargs: Parsed to ingest_datasets. """ datasetType = butler.registry.getDatasetType(datasetTypeName) datasets = [] for filename in filenames: dataId = get_dataId_from_header(filename, required_keys=dimension_names) datasets.append(makeFileDataset(datasetType, dataId=dataId, filename=filename)) ingest_datasets(butler, datasetType, datasets, collection, **kwargs)
python
from .. import initializations from ..layers.core import MaskedLayer from .. import backend as K import numpy as np class LeakyReLU(MaskedLayer): '''Special version of a Rectified Linear Unit that allows a small gradient when the unit is not active (`f(x) = alpha*x for x < 0`). # Input shape Arbitrary. Use the keyword argument `input_shape` (tuple of integers, does not include the samples axis) when using this layer as the first layer in a model. # Output shape Same shape as the input. # Arguments alpha: float >= 0. Negative slope coefficient. ''' def __init__(self, alpha=0.3, **kwargs): super(LeakyReLU, self).__init__(**kwargs) self.alpha = alpha def get_output(self, train): X = self.get_input(train) return K.relu(X, alpha=self.alpha) def get_config(self): config = {"name": self.__class__.__name__, "alpha": self.alpha} base_config = super(LeakyReLU, self).get_config() return dict(list(base_config.items()) + list(config.items())) class PReLU(MaskedLayer): ''' # Input shape Arbitrary. Use the keyword argument `input_shape` (tuple of integers, does not include the samples axis) when using this layer as the first layer in a model. # Output shape Same shape as the input. # Arguments: init: initialization function for the weights. weights: initial weights, as a list of a single numpy array. # References: - [Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification](http://arxiv.org/pdf/1502.01852v1.pdf) ''' def __init__(self, init='zero', weights=None, **kwargs): self.init = initializations.get(init) self.initial_weights = weights super(PReLU, self).__init__(**kwargs) def build(self): input_shape = self.input_shape[1:] self.alphas = self.init(input_shape) self.params = [self.alphas] if self.initial_weights is not None: self.set_weights(self.initial_weights) del self.initial_weights def get_output(self, train): X = self.get_input(train) pos = K.relu(X) neg = self.alphas * (X - abs(X)) * 0.5 return pos + neg def get_config(self): config = {"name": self.__class__.__name__, "init": self.init.__name__} base_config = super(PReLU, self).get_config() return dict(list(base_config.items()) + list(config.items())) class ELU(MaskedLayer): ''' # Input shape Arbitrary. Use the keyword argument `input_shape` (tuple of integers, does not include the samples axis) when using this layer as the first layer in a model. # Output shape Same shape as the input. # Arguments alpha: scale for the negative factor. # References - [Fast and Accurate Deep Network Learning by Exponential Linear Units (ELUs)](http://arxiv.org/pdf/1511.07289v1.pdf) ''' def __init__(self, alpha=1.0, **kwargs): super(ELU, self).__init__(**kwargs) self.alpha = alpha def get_output(self, train): X = self.get_input(train) pos = K.relu(X) neg = (X - abs(X)) * 0.5 return pos + self.alpha * (K.exp(neg) - 1.) def get_config(self): config = {"name": self.__class__.__name__, "alpha": self.alpha} base_config = super(ELU, self).get_config() return dict(list(base_config.items()) + list(config.items())) class ParametricSoftplus(MaskedLayer): '''Parametric Softplus of the form: alpha * log(1 + exp(beta * X)) # Input shape Arbitrary. Use the keyword argument `input_shape` (tuple of integers, does not include the samples axis) when using this layer as the first layer in a model. # Output shape Same shape as the input. # Arguments alpha_init: float. Initial value of the alpha weights. beta_init: float. Initial values of the beta weights. weights: initial weights, as a list of 2 numpy arrays. # References: - [Inferring Nonlinear Neuronal Computation Based on Physiologically Plausible Inputs](http://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1003143) ''' def __init__(self, alpha_init=0.2, beta_init=5.0, weights=None, **kwargs): self.alpha_init = alpha_init self.beta_init = beta_init self.initial_weights = weights super(ParametricSoftplus, self).__init__(**kwargs) def build(self): input_shape = self.input_shape[1:] self.alphas = K.variable(self.alpha_init * np.ones(input_shape)) self.betas = K.variable(self.beta_init * np.ones(input_shape)) self.params = [self.alphas, self.betas] if self.initial_weights is not None: self.set_weights(self.initial_weights) del self.initial_weights def get_output(self, train): X = self.get_input(train) return K.softplus(self.betas * X) * self.alphas def get_config(self): config = {"name": self.__class__.__name__, "alpha_init": self.alpha_init, "beta_init": self.beta_init} base_config = super(ParametricSoftplus, self).get_config() return dict(list(base_config.items()) + list(config.items())) class ThresholdedLinear(MaskedLayer): '''Thresholded Linear Activation. # Input shape Arbitrary. Use the keyword argument `input_shape` (tuple of integers, does not include the samples axis) when using this layer as the first layer in a model. # Output shape Same shape as the input. # Arguments theta: float >= 0. Threshold location of activation. # References [Zero-Bias Autoencoders and the Benefits of Co-Adapting Features](http://arxiv.org/pdf/1402.3337.pdf) ''' def __init__(self, theta=1.0, **kwargs): super(ThresholdedLinear, self).__init__(**kwargs) self.theta = theta def get_output(self, train): X = self.get_input(train) return K.switch(K.abs(X) < self.theta, 0, X) def get_config(self): config = {"name": self.__class__.__name__, "theta": self.theta} base_config = super(ThresholdedLinear, self).get_config() return dict(list(base_config.items()) + list(config.items())) class ThresholdedReLU(MaskedLayer): '''Thresholded Rectified Activation. # Input shape Arbitrary. Use the keyword argument `input_shape` (tuple of integers, does not include the samples axis) when using this layer as the first layer in a model. # Output shape Same shape as the input. # Arguments theta: float >= 0. Threshold location of activation. # References [Zero-Bias Autoencoders and the Benefits of Co-Adapting Features](http://arxiv.org/pdf/1402.3337.pdf) ''' def __init__(self, theta=1.0, **kwargs): super(ThresholdedReLU, self).__init__(**kwargs) self.theta = theta def get_output(self, train): X = self.get_input(train) return K.switch(X > self.theta, X, 0) def get_config(self): config = {"name": self.__class__.__name__, "theta": self.theta} base_config = super(ThresholdedReLU, self).get_config() return dict(list(base_config.items()) + list(config.items())) class Quorum(MaskedLayer): ''' # Input shape Arbitrary. Use the keyword argument `input_shape` (tuple of integers, does not include the samples axis) when using this layer as the first layer in a model. # Output shape Same shape as the input. # Arguments: # References: ''' def __init__(self, activation_fns, activation_weights_init=None, trainable=True, threshold=None, **kwargs): self.activation_fns = activation_fns self.activation_weights_init = activation_weights_init self.trainable = trainable self.threshold = threshold assert(len(self.activation_fns) > 0),("Must have at least one " "activation function!") if self.activation_weights_init is None: starting_weight = 1. / len(self.activation_fns) self.activation_weights_init = [starting_weight for x in xrange(len(self.activation_fns))] assert (len(self.activation_fns) == len(self.activation_weights_init)),("Must have the same number " "of activation functions " "and weights!") super(Quorum, self).__init__(**kwargs) def build(self): input_shape = self.input_shape[1:] self.activation_weights = [K.variable(init_val * np.ones(input_shape)) for init_val in self.activation_weights_init] if self.trainable: self.params = self.activation_weights def get_output(self, train): X = self.get_input(train) Y_ = X for (fn, w) in zip(self.activation_fns, self.activation_weights): if self.threshold: print("Threshold!") Y_ = Y_ + K.clip(w, 0, self.threshold) * fn(X) else: print("Not threshold!") Y_ = Y_ + w * fn(X) return Y_ def get_config(self): config = {"name": self.__class__.__name__, "fns": self.activation_fns, "weights": self.activation_weights} base_config = super(Quorum, self).get_config() return dict(list(base_config.items()) + list(config.items()))
python
#!/usr/bin/env python # Licensed to Cloudera, Inc. under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. Cloudera, Inc. licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from builtins import next, str import json import logging import re import sys import time from django import forms from django.core.paginator import Paginator, EmptyPage, InvalidPage from django.contrib import messages from django.db.models import Q from django.http import HttpResponse, QueryDict from django.shortcuts import redirect from django.utils.html import escape from django.utils.translation import ugettext as _ from django.urls import reverse from desktop.appmanager import get_apps_dict from desktop.conf import ENABLE_DOWNLOAD, REDIRECT_WHITELIST from desktop.context_processors import get_app_name from desktop.lib.django_util import JsonResponse from desktop.lib.django_util import copy_query_dict, format_preserving_redirect, render from desktop.lib.django_util import login_notrequired, get_desktop_uri_prefix from desktop.lib.exceptions_renderable import PopupException from desktop.models import Document, _get_apps from desktop.lib.parameterization import find_variables from desktop.views import serve_403_error from notebook.models import escape_rows from useradmin.models import User import beeswax.forms import beeswax.design from beeswax import common, data_export, models from beeswax.management.commands import beeswax_install_examples from beeswax.models import QueryHistory, SavedQuery, Session from beeswax.server import dbms from beeswax.server.dbms import expand_exception, get_query_server_config, QueryServerException from desktop.auth.backend import is_admin LOG = logging.getLogger(__name__) # For scraping Job IDs from logs HADOOP_JOBS_RE = re.compile("Starting Job = ([a-z0-9_]+?),") SPARK_APPLICATION_RE = re.compile("Running with YARN Application = (?P<application_id>application_\d+_\d+)") TEZ_APPLICATION_RE = re.compile("Executing on YARN cluster with App id ([a-z0-9_]+?)\)") TEZ_QUERY_RE = re.compile("\(queryId=([a-z0-9_-]+?)\)") def index(request): return execute_query(request) """ Design views """ def save_design(request, form, type_, design, explicit_save): """ save_design(request, form, type_, design, explicit_save) -> SavedQuery A helper method to save the design: * If ``explicit_save``, then we save the data in the current design. * If the user clicked the submit button, we do NOT overwrite the current design. Instead, we create a new "auto" design (iff the user modified the data). This new design is named after the current design, with the AUTO_DESIGN_SUFFIX to signify that it's different. Need to return a SavedQuery because we may end up with a different one. Assumes that form.saveform is the SaveForm, and that it is valid. """ authorized_get_design(request, design.id) assert form.saveform.is_valid() sub_design_form = form # Beeswax/Impala case if type_ == models.HQL: design_cls = beeswax.design.HQLdesign elif type_ == models.IMPALA: design_cls = beeswax.design.HQLdesign elif type_ == models.SPARK: from spark.design import SparkDesign design_cls = SparkDesign sub_design_form = form.query else: raise ValueError(_('Invalid design type %(type)s') % {'type': type_}) design_obj = design_cls(sub_design_form, query_type=type_) name = form.saveform.cleaned_data['name'] desc = form.saveform.cleaned_data['desc'] return _save_design(request.user, design, type_, design_obj, explicit_save, name, desc) def _save_design(user, design, type_, design_obj, explicit_save, name=None, desc=None): # Design here means SavedQuery old_design = design new_data = design_obj.dumps() # Auto save if (1) the user didn't click "save", and (2) the data is different. # Create an history design if the user is executing a shared design. # Don't generate an auto-saved design if the user didn't change anything. if explicit_save and (not design.doc.exists() or design.doc.get().can_write_or_exception(user)): design.name = name design.desc = desc design.is_auto = False elif design_obj != old_design.get_design(): # Auto save iff the data is different if old_design.id is not None: # Clone iff the parent design isn't a new unsaved model design = old_design.clone(new_owner=user) if not old_design.is_auto: design.name = old_design.name + models.SavedQuery.AUTO_DESIGN_SUFFIX else: design.name = models.SavedQuery.DEFAULT_NEW_DESIGN_NAME design.is_auto = True design.name = design.name[:64] design.type = type_ design.data = new_data design.save() LOG.info('Saved %s design "%s" (id %s) for %s' % (explicit_save and '' or 'auto ', design.name, design.id, design.owner)) if design.doc.exists(): design.doc.update(name=design.name, description=design.desc) else: Document.objects.link(design, owner=design.owner, extra=design.type, name=design.name, description=design.desc) if design.is_auto: design.doc.get().add_to_history() return design def delete_design(request): if request.method == 'POST': ids = request.POST.getlist('designs_selection') designs = dict([(design_id, authorized_get_design(request, design_id, owner_only=True)) for design_id in ids]) if None in list(designs.values()): LOG.error('Cannot delete non-existent design(s) %s' % ','.join([key for key, name in list(designs.items()) if name is None])) return list_designs(request) for design in list(designs.values()): if request.POST.get('skipTrash', 'false') == 'false': design.doc.get().send_to_trash() else: design.doc.all().delete() design.delete() return redirect(reverse(get_app_name(request) + ':list_designs')) else: return render('confirm.mako', request, {'url': request.path, 'title': _('Delete design(s)?')}) def restore_design(request): if request.method == 'POST': ids = request.POST.getlist('designs_selection') designs = dict([(design_id, authorized_get_design(request, design_id)) for design_id in ids]) if None in list(designs.values()): LOG.error('Cannot restore non-existent design(s) %s' % ','.join([key for key, name in list(designs.items()) if name is None])) return list_designs(request) for design in list(designs.values()): design.doc.get().restore_from_trash() return redirect(reverse(get_app_name(request) + ':list_designs')) else: return render('confirm.mako', request, {'url': request.path, 'title': _('Restore design(s)?')}) def clone_design(request, design_id): """Clone a design belonging to any user""" design = authorized_get_design(request, design_id) if design is None: LOG.error('Cannot clone non-existent design %s' % (design_id,)) return list_designs(request) copy = design.clone(request.user) copy.save() name = copy.name + '-copy' design.doc.get().copy(content_object=copy, name=name, owner=request.user) messages.info(request, _('Copied design: %(name)s') % {'name': design.name}) return format_preserving_redirect(request, reverse(get_app_name(request) + ':execute_design', kwargs={'design_id': copy.id})) def list_designs(request): """ View function for show all saved queries. We get here from /beeswax/list_designs?filterargs, with the options being: page=<n> - Controls pagination. Defaults to 1. user=<name> - Show design items belonging to a user. Default to all users. type=<type> - <type> is "hql", for saved query type. Default to show all. sort=<key> - Sort by the attribute <key>, which is one of: "date", "name", "desc", and "type" (design type) Accepts the form "-date", which sort in descending order. Default to "-date". text=<frag> - Search for fragment "frag" in names and descriptions. """ DEFAULT_PAGE_SIZE = 20 app_name = get_app_name(request) # Extract the saved query list. prefix = 'q-' querydict_query = _copy_prefix(prefix, request.GET) # Manually limit up the user filter. querydict_query[prefix + 'type'] = app_name # Get search filter input if any search_filter = request.GET.get('text', None) if search_filter is not None: querydict_query[prefix + 'text'] = search_filter paginator, page, filter_params = _list_designs(request.user, querydict_query, DEFAULT_PAGE_SIZE, prefix) designs_json = [] if page: designs_json = [query.id for query in page.object_list] return render('list_designs.mako', request, { 'page': page, 'paginator': paginator, 'filter_params': filter_params, 'prefix': prefix, 'user': request.user, 'designs_json': json.dumps(designs_json) }) def list_trashed_designs(request): DEFAULT_PAGE_SIZE = 20 app_name = get_app_name(request) user = request.user # Extract the saved query list. prefix = 'q-' querydict_query = _copy_prefix(prefix, request.GET) # Manually limit up the user filter. querydict_query[prefix + 'type'] = app_name # Get search filter input if any search_filter = request.GET.get('text', None) if search_filter is not None: querydict_query[prefix + 'text'] = search_filter paginator, page, filter_params = _list_designs(user, querydict_query, DEFAULT_PAGE_SIZE, prefix, is_trashed=True) designs_json = [] if page: designs_json = [query.id for query in page.object_list] return render('list_trashed_designs.mako', request, { 'page': page, 'paginator': paginator, 'filter_params': filter_params, 'prefix': prefix, 'user': request.user, 'designs_json': json.dumps(designs_json) }) def my_queries(request): """ View a mix of history and saved queries. It understands all the GET params in ``list_query_history`` (with a ``h-`` prefix) and those in ``list_designs`` (with a ``q-`` prefix). The only thing it disallows is the ``user`` filter, since this view only shows what belongs to the user. """ DEFAULT_PAGE_SIZE = 30 app_name = get_app_name(request) # Extract the history list. prefix = 'h-' querydict_history = _copy_prefix(prefix, request.GET) # Manually limit up the user filter. querydict_history[prefix + 'user'] = request.user querydict_history[prefix + 'type'] = app_name hist_paginator, hist_page, hist_filter = _list_query_history( request.user, querydict_history, DEFAULT_PAGE_SIZE, prefix ) # Extract the saved query list. prefix = 'q-' querydict_query = _copy_prefix(prefix, request.GET) # Manually limit up the user filter. querydict_query[prefix + 'user'] = request.user querydict_query[prefix + 'type'] = app_name query_paginator, query_page, query_filter = _list_designs(request.user, querydict_query, DEFAULT_PAGE_SIZE, prefix) designs_json = [] if query_page: designs_json = [query.id for query in query_page.object_list] filter_params = hist_filter filter_params.update(query_filter) return render('my_queries.mako', request, { 'request': request, 'h_page': hist_page, 'h_paginator': hist_paginator, 'q_page': query_page, 'q_paginator': query_paginator, 'filter_params': filter_params, 'designs_json': json.dumps(designs_json) }) def list_query_history(request): """ View the history of query (for the current user). We get here from /beeswax/query_history?filterargs, with the options being: page=<n> - Controls pagination. Defaults to 1. user=<name> - Show history items from a user. Default to current user only. Also accepts ':all' to show all history items. type=<type> - <type> is "beeswax|impala", for design type. Default to show all. design_id=<id> - Show history for this particular design id. sort=<key> - Sort by the attribute <key>, which is one of: "date", "state", "name" (design name), and "type" (design type) Accepts the form "-date", which sort in descending order. Default to "-date". auto_query=<bool> - Show auto generated actions (drop table, read data, etc). Default True """ DEFAULT_PAGE_SIZE = 100 prefix = 'q-' share_queries = is_admin(request.user) querydict_query = request.GET.copy() if not share_queries: querydict_query[prefix + 'user'] = request.user.username app_name = get_app_name(request) querydict_query[prefix + 'type'] = app_name paginator, page, filter_params = _list_query_history(request.user, querydict_query, DEFAULT_PAGE_SIZE, prefix) filter = request.GET.get(prefix + 'search') and request.GET.get(prefix + 'search') or '' if request.GET.get('format') == 'json': resp = { 'queries': [massage_query_history_for_json(app_name, query_history) for query_history in page.object_list] } return JsonResponse(resp) return render('list_history.mako', request, { 'request': request, 'page': page, 'paginator': paginator, 'filter_params': filter_params, 'share_queries': share_queries, 'prefix': prefix, 'filter': filter, }) def massage_query_history_for_json(app_name, query_history): return { 'id': query_history.id, 'design_id': query_history.design.id, 'query': escape(query_history.query), 'timeInMs': time.mktime(query_history.submission_date.timetuple()), 'timeFormatted': query_history.submission_date.strftime("%x %X"), 'designUrl': reverse(app_name + ':execute_design', kwargs={'design_id': query_history.design.id}), 'resultsUrl': not query_history.is_failure() and reverse( app_name + ':watch_query_history', kwargs={'query_history_id': query_history.id} ) or "" } def download(request, id, format, user_agent=None): if not ENABLE_DOWNLOAD.get(): return serve_403_error(request) try: query_history = authorized_get_query_history(request, id, must_exist=True) db = dbms.get(request.user, query_history.get_query_server_config()) LOG.debug('Download results for query %s: [ %s ]' % (query_history.server_id, query_history.query)) return data_export.download(query_history.get_handle(), format, db, user_agent=user_agent) except Exception as e: if not hasattr(e, 'message') or not e.message: message = e else: message = e.message raise PopupException(message, detail='') """ Queries Views """ def execute_query(request, design_id=None, query_history_id=None): """ View function for executing an arbitrary query. """ action = 'query' if query_history_id: query_history = authorized_get_query_history(request, query_history_id, must_exist=True) design = query_history.design try: if query_history.server_id and query_history.server_guid: handle, state = _get_query_handle_and_state(query_history) if 'on_success_url' in request.GET: if request.GET.get('on_success_url') and any( [regexp.match(request.GET.get('on_success_url')) for regexp in REDIRECT_WHITELIST.get()] ): action = 'watch-redirect' else: action = 'watch-results' else: action = 'editor-results' except QueryServerException as e: if 'Invalid query handle' in e.message or 'Invalid OperationHandle' in e.message: query_history.save_state(QueryHistory.STATE.expired) LOG.warn("Invalid query handle", exc_info=sys.exc_info()) action = 'editor-expired-results' else: raise e else: # Check perms. authorized_get_design(request, design_id) app_name = get_app_name(request) query_type = SavedQuery.TYPES_MAPPING[app_name] design = safe_get_design(request, query_type, design_id) query_history = None current_app, other_apps, apps_list = _get_apps(request.user, '') doc = design and design.id and design.doc.get() context = { 'design': design, 'apps': apps_list, 'query': query_history, # Backward 'query_history': query_history, 'autocomplete_base_url': reverse(get_app_name(request) + ':api_autocomplete_databases', kwargs={}), 'autocomplete_base_url_hive': reverse('beeswax:api_autocomplete_databases', kwargs={}), 'can_edit_name': design and design.id and not design.is_auto, 'doc_id': doc and doc.id or -1, 'can_edit': doc and doc.can_write(request.user), 'action': action, 'on_success_url': request.GET.get('on_success_url'), 'has_metastore': 'metastore' in get_apps_dict(request.user) } return render('execute.mako', request, context) def view_results(request, id, first_row=0): """ Returns the view for the results of the QueryHistory with the given id. The query results MUST be ready. To display query results, one should always go through the execute_query view. If the result set has has_result_set=False, display an empty result. If ``first_row`` is 0, restarts (if necessary) the query read. Otherwise, just spits out a warning if first_row doesn't match the servers conception. Multiple readers will produce a confusing interaction here, and that's known. It understands the ``context`` GET parameter. (See execute_query().) """ first_row = int(first_row) start_over = (first_row == 0) results = type('Result', (object,), { 'rows': 0, 'columns': [], 'has_more': False, 'start_row': 0, }) data = [] fetch_error = False error_message = '' log = '' columns = [] app_name = get_app_name(request) query_history = authorized_get_query_history(request, id, must_exist=True) query_server = query_history.get_query_server_config() db = dbms.get(request.user, query_server) handle, state = _get_query_handle_and_state(query_history) context_param = request.GET.get('context', '') query_context = parse_query_context(context_param) # Update the status as expired should not be accessible expired = state == models.QueryHistory.STATE.expired # Retrieve query results or use empty result if no result set try: if query_server['server_name'] == 'impala' and not handle.has_result_set: downloadable = False else: results = db.fetch(handle, start_over, 100) # Materialize and HTML escape results data = escape_rows(results.rows()) # We display the "Download" button only when we know that there are results: downloadable = first_row > 0 or data log = db.get_log(handle) columns = results.data_table.cols() except Exception as ex: LOG.exception('error fetching results') fetch_error = True error_message, log = expand_exception(ex, db, handle) # Handle errors error = fetch_error or results is None or expired context = { 'error': error, 'message': error_message, 'query': query_history, 'results': data, 'columns': columns, 'expected_first_row': first_row, 'log': log, 'hadoop_jobs': app_name != 'impala' and parse_out_jobs(log), 'query_context': query_context, 'can_save': False, 'context_param': context_param, 'expired': expired, 'app_name': app_name, 'next_json_set': None, 'is_finished': query_history.is_finished() } if not error: download_urls = {} if downloadable: for format in common.DL_FORMATS: download_urls[format] = reverse(app_name + ':download', kwargs=dict(id=str(id), format=format)) results.start_row = first_row context.update({ 'id': id, 'results': data, 'has_more': results.has_more, 'next_row': results.start_row + len(data), 'start_row': results.start_row, 'expected_first_row': first_row, 'columns': columns, 'download_urls': download_urls, 'can_save': query_history.owner == request.user, 'next_json_set': reverse(get_app_name(request) + ':view_results', kwargs={ 'id': str(id), 'first_row': results.start_row + len(data) } ) + ('?context=' + context_param or '') + '&format=json' }) context['columns'] = massage_columns_for_json(columns) if 'save_form' in context: del context['save_form'] if 'query' in context: del context['query'] return JsonResponse(context) def configuration(request): app_name = get_app_name(request) query_server = get_query_server_config(app_name) session = Session.objects.get_session(request.user, query_server['server_name']) if session: properties = json.loads(session.properties) # Redact passwords for key, value in list(properties.items()): if 'password' in key.lower(): properties[key] = '*' * len(value) else: properties = {} return render("configuration.mako", request, {'configuration': properties}) """ Other views """ def install_examples(request): response = {'status': -1, 'message': ''} if request.method == 'POST': try: dialect = get_app_name(request) if dialect == 'beeswax': dialect = 'hive' db_name = request.POST.get('db_name', 'default') connector_id = request.POST.get('connector_id') beeswax_install_examples.Command().handle(dialect=dialect, db_name=db_name, user=request.user, request=request) response['status'] = 0 except Exception as err: LOG.exception(err) response['message'] = str(err) else: response['message'] = _('A POST request is required.') return JsonResponse(response) @login_notrequired def query_done_cb(request, server_id): """ A callback for query completion notification. When the query is done, BeeswaxServer notifies us by sending a GET request to this view. """ message_template = '<html><head></head>%(message)s<body></body></html>' message = {'message': 'error'} try: query_history = QueryHistory.objects.get(server_id=server_id + '\n') # Update the query status query_history.set_to_available() # Find out details about the query if not query_history.notify: message['message'] = 'email_notify is false' return HttpResponse(message_template % message) design = query_history.design user = query_history.owner subject = _("Beeswax query completed.") if design: subject += ": %s" % (design.name,) link = "%s%s" % ( get_desktop_uri_prefix(), reverse(get_app_name(request) + ':watch_query_history', kwargs={'query_history_id': query_history.id}) ) body = _( "%(subject)s. See the results here: %(link)s\n\nQuery:\n%(query)s") % { 'subject': subject, 'link': link, 'query': query_history.query } user.email_user(subject, body) message['message'] = 'sent' except Exception as ex: msg = "Failed to send query completion notification via e-mail: %s" % (ex) LOG.error(msg) message['message'] = msg return HttpResponse(message_template % message) """ Utils """ def massage_columns_for_json(cols): massaged_cols = [] for column in cols: massaged_cols.append({ 'name': column.name, 'type': column.type, 'comment': column.comment }) return massaged_cols def authorized_get_design(request, design_id, owner_only=False, must_exist=False): if design_id is None and not must_exist: return None try: design = SavedQuery.objects.get(id=design_id) except SavedQuery.DoesNotExist: if must_exist: raise PopupException(_('Design %(id)s does not exist.') % {'id': design_id}) else: return None if owner_only: design.doc.get().can_write_or_exception(request.user) else: design.doc.get().can_read_or_exception(request.user) return design def authorized_get_query_history(request, query_history_id, owner_only=False, must_exist=False): if query_history_id is None and not must_exist: return None try: query_history = QueryHistory.get(id=query_history_id) except QueryHistory.DoesNotExist: if must_exist: raise PopupException(_('QueryHistory %(id)s does not exist.') % {'id': query_history_id}) else: return None # Some queries don't have a design so are not linked to Document Model permission if query_history.design is None or not query_history.design.doc.exists(): if not is_admin(request.user) and request.user != query_history.owner: raise PopupException(_('Permission denied to read QueryHistory %(id)s') % {'id': query_history_id}) else: query_history.design.doc.get().can_read_or_exception(request.user) return query_history def safe_get_design(request, design_type, design_id=None): """ Return a new design, if design_id is None, Return the design with the given id and type. If the design is not found, display a notification and return a new design. """ design = None if design_id is not None: design = authorized_get_design(request, design_id) if design is None: design = SavedQuery(owner=request.user, type=design_type) return design def make_parameterization_form(query_str): """ Creates a django form on the fly with arguments from the query. """ variables = find_variables(query_str) if len(variables) > 0: class Form(forms.Form): for name in sorted(variables): locals()[name] = forms.CharField(widget=forms.TextInput(attrs={'required': True})) return Form else: return None def execute_directly(request, query, query_server=None, design=None, on_success_url=None, on_success_params=None, **kwargs): """ execute_directly(request, query_msg, tablename, design) -> HTTP response for execution This method wraps around dbms.execute_query() to take care of the HTTP response after the execution. query The HQL model Query object. query_server To which Query Server to submit the query. Dictionary with keys: ['server_name', 'server_host', 'server_port']. design The design associated with the query. on_success_url Where to go after the query is done. The URL handler may expect an option "context" GET param. (See ``watch_query``.) For advanced usage, on_success_url can be a function, in which case the on complete URL is the return of: on_success_url(history_obj) -> URL string Defaults to the view results page. on_success_params Optional params to pass to the on_success_url (in additional to "context"). Note that this may throw a Beeswax exception. """ if design is not None: authorized_get_design(request, design.id) db = dbms.get(request.user, query_server) database = query.query.get('database', 'default') db.use(database) query_history = db.execute_query(query, design) watch_url = reverse(get_app_name(request) + ':watch_query_history', kwargs={'query_history_id': query_history.id}) # Prepare the GET params for the watch_url get_dict = QueryDict(None, mutable=True) # (1) on_success_url if on_success_url: if callable(on_success_url): on_success_url = on_success_url(query_history) get_dict['on_success_url'] = on_success_url # (2) misc if on_success_params: get_dict.update(on_success_params) return format_preserving_redirect(request, watch_url, get_dict) def _list_designs(user, querydict, page_size, prefix="", is_trashed=False): """ _list_designs(user, querydict, page_size, prefix, is_trashed) -> (page, filter_param) A helper to gather the designs page. It understands all the GET params in ``list_designs``, by reading keys from the ``querydict`` with the given ``prefix``. """ DEFAULT_SORT = ('-', 'date') # Descending date SORT_ATTR_TRANSLATION = dict( date='last_modified', name='name', desc='description', type='extra', ) # Trash and security if is_trashed: db_queryset = Document.objects.trashed_docs(SavedQuery, user) else: db_queryset = Document.objects.available_docs(SavedQuery, user) # Filter by user filter_username = querydict.get(prefix + 'user') if filter_username: try: db_queryset = db_queryset.filter(owner=User.objects.get(username=filter_username)) except User.DoesNotExist: # Don't care if a bad filter term is provided pass # Design type d_type = querydict.get(prefix + 'type') if d_type and d_type in list(SavedQuery.TYPES_MAPPING.keys()): db_queryset = db_queryset.filter(extra=str(SavedQuery.TYPES_MAPPING[d_type])) # Text search frag = querydict.get(prefix + 'text') if frag: db_queryset = db_queryset.filter(Q(name__icontains=frag) | Q(description__icontains=frag)) # Ordering sort_key = querydict.get(prefix + 'sort') if sort_key: if sort_key[0] == '-': sort_dir, sort_attr = '-', sort_key[1:] else: sort_dir, sort_attr = '', sort_key if sort_attr not in SORT_ATTR_TRANSLATION: LOG.warn('Bad parameter to list_designs: sort=%s' % (sort_key,)) sort_dir, sort_attr = DEFAULT_SORT else: sort_dir, sort_attr = DEFAULT_SORT db_queryset = db_queryset.order_by(sort_dir + SORT_ATTR_TRANSLATION[sort_attr]) designs = [job.content_object for job in db_queryset.all() if job.content_object and job.content_object.is_auto == False] pagenum = int(querydict.get(prefix + 'page', 1)) paginator = Paginator(designs, page_size, allow_empty_first_page=True) try: page = paginator.page(pagenum) except EmptyPage: page = None # We need to pass the parameters back to the template to generate links keys_to_copy = [prefix + key for key in ('user', 'type', 'sort', 'text')] filter_params = copy_query_dict(querydict, keys_to_copy) return paginator, page, filter_params def _get_query_handle_and_state(query_history): """ Front-end wrapper to handle exceptions. Expects the query to be submitted. """ handle = query_history.get_handle() if handle is None: raise PopupException(_("Failed to retrieve query state from the Query Server.")) state = dbms.get(query_history.owner, query_history.get_query_server_config()).get_state(handle) if state is None: raise PopupException(_("Failed to contact Server to check query status.")) return (handle, state) def parse_query_context(context): """ parse_query_context(context) -> ('table', <table_name>) -or- ('design', <design_obj>) """ if not context: return None pair = context.split(':', 1) if len(pair) != 2 or pair[0] not in ('table', 'design'): LOG.error("Invalid query context data: %s" % (context,)) return None if pair[0] == 'design': # Translate design id to design obj pair[1] = models.SavedQuery.get(int(pair[1])) return pair def parse_out_jobs(log, engine='mr', with_state=False): """ Ideally, Hive would tell us what jobs it has run directly from the Thrift interface. with_state: If True, will return a list of dict items with 'job_id', 'started', 'finished' """ ret = [] if engine.lower() == 'mr': start_pattern = HADOOP_JOBS_RE elif engine.lower() == 'spark': start_pattern = SPARK_APPLICATION_RE elif engine.lower() == 'tez': start_pattern = TEZ_APPLICATION_RE elif engine.lower() == 'impala': return ret else: raise ValueError(_('Cannot parse job IDs for execution engine %(engine)s') % {'engine': engine}) for match in start_pattern.finditer(log): job_id = match.group(1) if with_state: if job_id not in list(job['job_id'] for job in ret): ret.append({'job_id': job_id, 'started': True, 'finished': False}) end_pattern = 'Ended Job = %s' % job_id if end_pattern in log: job = next((job for job in ret if job['job_id'] == job_id), None) if job is not None: job['finished'] = True else: ret.append({'job_id': job_id, 'started': True, 'finished': True}) else: if job_id not in ret: ret.append(job_id) return ret def parse_out_queries(log, engine=None, with_state=False): """ Ideally, Hive would tell us what jobs it has run directly from the Thrift interface. with_state: If True, will return a list of dict items with 'job_id', 'started', 'finished' """ ret = [] if engine.lower() == 'tez': start_pattern = TEZ_QUERY_RE else: return ret for match in start_pattern.finditer(log): job_id = match.group(1) if with_state: if job_id not in list(job['job_id'] for job in ret): ret.append({'job_id': job_id, 'started': False, 'finished': False}) start_pattern = 'Executing command(queryId=%s' % job_id end_pattern = 'Completed executing command(queryId=%s' % job_id if start_pattern in log: job = next((job for job in ret if job['job_id'] == job_id), None) if job is not None: job['started'] = True else: ret.append({'job_id': job_id, 'started': True, 'finished': False}) if end_pattern in log: job = next((job for job in ret if job['job_id'] == job_id), None) if job is not None: job['finished'] = True else: ret.append({'job_id': job_id, 'started': True, 'finished': True}) else: if job_id not in ret: ret.append(job_id) return ret def _copy_prefix(prefix, base_dict): """Copy keys starting with ``prefix``""" querydict = QueryDict(None, mutable=True) for key, val in base_dict.items(): if key.startswith(prefix): querydict[key] = val return querydict def _list_query_history(user, querydict, page_size, prefix=""): """ _list_query_history(user, querydict, page_size, prefix) -> (page, filter_param) A helper to gather the history page. It understands all the GET params in ``list_query_history``, by reading keys from the ``querydict`` with the given ``prefix``. """ DEFAULT_SORT = ('-', 'date') # Descending date SORT_ATTR_TRANSLATION = dict( date='submission_date', state='last_state', name='design__name', type='design__type', ) db_queryset = models.QueryHistory.objects.select_related() # Filtering # # Queries without designs are the ones we submitted on behalf of the user, # (e.g. view table data). Exclude those when returning query history. if querydict.get(prefix + 'auto_query', 'on') != 'on': db_queryset = db_queryset.exclude(design__isnull=False, design__is_auto=True) user_filter = querydict.get(prefix + 'user', user.username) if user_filter != ':all': db_queryset = db_queryset.filter(owner__username=user_filter) # Design id design_id = querydict.get(prefix + 'design_id') if design_id: if design_id.isdigit(): db_queryset = db_queryset.filter(design__id=int(design_id)) else: raise PopupException(_('list_query_history requires design_id parameter to be an integer: %s') % design_id) # Search search_filter = querydict.get(prefix + 'search') if search_filter: db_queryset = db_queryset.filter( Q(design__name__icontains=search_filter) | Q(query__icontains=search_filter) | Q(owner__username__icontains=search_filter) ) # Design type d_type = querydict.get(prefix + 'type') if d_type: if d_type not in list(SavedQuery.TYPES_MAPPING.keys()): LOG.warn('Bad parameter to list_query_history: type=%s' % (d_type,)) else: db_queryset = db_queryset.filter(design__type=SavedQuery.TYPES_MAPPING[d_type]) # If recent query recent = querydict.get('recent') if recent: db_queryset = db_queryset.filter(is_cleared=False) # Ordering sort_key = querydict.get(prefix + 'sort') if sort_key: sort_dir, sort_attr = '', sort_key if sort_key[0] == '-': sort_dir, sort_attr = '-', sort_key[1:] if sort_attr not in SORT_ATTR_TRANSLATION: LOG.warn('Bad parameter to list_query_history: sort=%s' % (sort_key,)) sort_dir, sort_attr = DEFAULT_SORT else: sort_dir, sort_attr = DEFAULT_SORT db_queryset = db_queryset.order_by(sort_dir + SORT_ATTR_TRANSLATION[sort_attr], '-id') # Get the total return count before slicing total_count = db_queryset.count() # Slicing (must be the last filter applied) pagenum = int(querydict.get(prefix + 'page', 1)) if pagenum < 1: pagenum = 1 db_queryset = db_queryset[page_size * (pagenum - 1) : page_size * pagenum] paginator = Paginator(db_queryset, page_size, allow_empty_first_page=True) try: page = paginator.page(pagenum) except EmptyPage: page = None # We do slicing ourselves, rather than letting the Paginator handle it, in order to # update the last_state on the running queries if page: for history in page.object_list: _update_query_state(history.get_full_object()) # We need to pass the parameters back to the template to generate links keys_to_copy = [prefix + key for key in ('user', 'type', 'sort', 'design_id', 'auto_query', 'search')] filter_params = copy_query_dict(querydict, keys_to_copy) return paginator, page, filter_params def _update_query_state(query_history): """ Update the last_state for a QueryHistory object. Returns success as True/False. This only occurs iff the current last_state is submitted or running, since the other states are stable, more-or-less. Note that there is a transition from available/failed to expired. That occurs lazily when the user attempts to view results that have expired. """ if query_history.last_state <= models.QueryHistory.STATE.running.value: try: state_enum = dbms.get(query_history.owner, query_history.get_query_server_config()).get_state(query_history.get_handle()) if state_enum is None: # Error was logged at the source return False except Exception as e: LOG.error(e) state_enum = models.QueryHistory.STATE.failed query_history.save_state(state_enum) return True def get_db_choices(request): app_name = get_app_name(request) query_server = get_query_server_config(app_name) db = dbms.get(request.user, query_server) dbs = db.get_databases() return [(db, db) for db in dbs] WHITESPACE = re.compile("\s+", re.MULTILINE) def collapse_whitespace(s): return WHITESPACE.sub(" ", s).strip()
python
from datetime import datetime from unittest import TestCase import xml.etree.ElementTree as ET from youtube_discussion_tree_api.utils import Node from youtube_discussion_tree_api._xml import _create_argument, _create_pair, _serialize_tree import os class TestXmlTreeConstruction(TestCase): def test_create_argument(self): argument_list = ET.Element("argument-list") node = Node( id = "comment1", author_name = "Ororo", author_id = "author1", text = "Hello, I love turtles and dogs", like_count = 10000000, parent_id = None, published_at = "12-12-2012" ) _create_argument(argument_list, node, None) self.assertEqual(node.id, argument_list.find("arg").get("id")) def test_create_pair(self): argument_pair = argument_list = ET.Element("argument-list") node = Node( id = "comment1", author_name = "Ororo", author_id = "author1", text = "Hello, I love turtles and dogs", like_count = 10000000, parent_id = "Turtle", published_at = "12-12-2012" ) _create_pair(argument_pair, node, 0) self.assertEqual('0', argument_list.find("pair").get("id")) self.assertEqual(node.id, argument_list.find("pair").find("t").get("id")) self.assertEqual(node.parent_id, argument_list.find("pair").find("h").get("id")) def test_serialize_tree(self): nodes = [ Node( id = "comment1", author_name = "Ororo", author_id = "author1", text = "Hello, I love turtles and dogs", like_count = 10000000, parent_id = None, published_at = "12-12-2012" ), Node( id = "comment2", author_name = "Horno Microondas", author_id = "author2", text = "Cats are the best animals in the whole world", like_count = 10000000, parent_id = "comment1", published_at = "12-12-2012" ), Node( id = "comment3", author_name = "Kekino", author_id = "author3", text = "I'm more of a dogs person, they are so cute", like_count = 10000000, parent_id = "comment1", published_at = "12-12-2012" ) ] _serialize_tree("./youtube_discussion_tree_api/tests/output.xml", nodes, None) self.assertTrue(os.path.isfile("./youtube_discussion_tree_api/tests/output.xml")) tree = ET.parse('./youtube_discussion_tree_api/tests/output.xml') self.assertEqual("entailment-corpus",tree.findall(".")[0].tag) self.assertTrue(tree.find("./argument-list") != None) self.assertTrue(tree.find("./argument-pairs") != None) self.assertTrue(3,len(tree.findall("./argument-list/arg"))) self.assertTrue(3,len(tree.findall("./argument-pairs/pairs")))
python
# -*- coding: utf-8 -*- import asyncio import logging import os import sys from pathlib import Path import subprocess import numpy as np import pandas as pd import flask import dash from dash.dependencies import Input, Output, State import dash_core_components as dcc import dash_html_components as html import plotly.graph_objs as go from plotly import subplots from simcore_sdk import node_ports logging.basicConfig(stream=sys.stderr, level=logging.INFO) logger = logging.getLogger() DEVEL_MODE = False if DEVEL_MODE: IN_OUT_PARENT_DIR = Path(Path(os.path.dirname( os.path.realpath(__file__))).parent).parent / 'validation' else: IN_OUT_PARENT_DIR = Path('/home/jovyan') INPUT_DIR = IN_OUT_PARENT_DIR / 'input' OUTPUT_DIR = IN_OUT_PARENT_DIR / 'output' DEFAULT_PATH = '/' base_pathname = os.environ.get('SIMCORE_NODE_BASEPATH', DEFAULT_PATH) if base_pathname != DEFAULT_PATH: base_pathname = "/{}/".format(base_pathname.strip('/')) print('url_base_pathname', base_pathname) server = flask.Flask(__name__) app = dash.Dash(__name__, server=server, url_base_pathname=base_pathname ) app.css.append_css({ "external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css" }) osparc_style = { 'color': '#bfbfbf', 'backgroundColor': '#202020', 'gridColor': '#444', } flex_columns = { 'display': 'flex' } flex_column = { 'flex': 1, 'min-width': 0 } unflex_column = { 'flex': 0, 'min-width': '220px', 'color': osparc_style['color'], 'backgroundColor': osparc_style['backgroundColor'] } centered_text = { 'text-align': 'center', 'color': osparc_style['color'], 'backgroundColor': osparc_style['backgroundColor'] } tab_style = { 'padding': '5px', 'color': osparc_style['color'], 'backgroundColor': osparc_style['backgroundColor'] } options_layout = { # 'border': '1px solid', # 'border-radius': '5px', 'margin-top': '50px' } dcc_input = { 'color': osparc_style['color'], 'backgroundColor': osparc_style['gridColor'] } dcc_input_button = { 'height': '40px', 'width': '100%', 'color': dcc_input['color'], 'backgroundColor': dcc_input['backgroundColor'] } dcc_input_label = { 'width': '120px', 'float': 'left' } dcc_input_number = { 'height': '30px', 'width': '100px', 'color': dcc_input['color'], 'backgroundColor': dcc_input['backgroundColor'] } dcc_input_pair = { 'overflow': 'hidden', 'margin-top': '2px', 'margin-bottom': '2px', 'color': osparc_style['color'], 'backgroundColor': osparc_style['backgroundColor'] } def get_empty_input_graph(): fig = subplots.make_subplots(rows=4, cols=1, shared_xaxes=True, vertical_spacing=0.05 ) fig['layout']['xaxis'].update( title='Conduction Velocity (m/s)', gridcolor=osparc_style['gridColor'] ) fig['layout']['yaxis'].update( title='Vmax(uV)', gridcolor=osparc_style['gridColor'] ) fig['layout']['yaxis2'].update( title='M coeff', gridcolor=osparc_style['gridColor'] ) fig['layout']['yaxis3'].update( title='B coeff (mA)', gridcolor=osparc_style['gridColor'] ) fig['layout']['yaxis4'].update( title='tau_SD(ms)', gridcolor=osparc_style['gridColor'] ) margin = 10 y_label_padding = 50 x_label_padding = 30 fig['layout']['margin'].update( l=margin+y_label_padding, r=margin, b=margin+x_label_padding, t=margin, ) fig['layout'].update( autosize=True, height=800, showlegend=False, plot_bgcolor=osparc_style['backgroundColor'], paper_bgcolor=osparc_style['backgroundColor'], font=dict( color=osparc_style['color'] ) ) return fig def get_empty_output_1_graph(fixed_tst=True, plot_vs_qst=False, plot_vs_tCNAP=False): margin = 10 label_padding = 30 layout = go.Layout( scene=dict( xaxis=dict( title='CV (m/s)', gridcolor=osparc_style['gridColor'], zerolinecolor='rgb(255, 255, 255)', showbackground=True, backgroundcolor=osparc_style['backgroundColor'], type='log', autorange=True ), yaxis=dict( title='I_st (mA)', gridcolor=osparc_style['gridColor'], zerolinecolor='rgb(255, 255, 255)', showbackground=True, backgroundcolor=osparc_style['backgroundColor'] ), zaxis=dict( title='V_pred (uV)', gridcolor=osparc_style['gridColor'], zerolinecolor='rgb(255, 255, 255)', showbackground=True, backgroundcolor=osparc_style['backgroundColor'] ) ), showlegend=False, margin=dict( l=margin+label_padding, r=margin, b=margin, t=margin ), height=400, plot_bgcolor=osparc_style['backgroundColor'], paper_bgcolor=osparc_style['backgroundColor'], font=dict( color=osparc_style['color'] ) ) if plot_vs_tCNAP: layout['scene']['xaxis'].update( title='t_CNAP (ms)', type='linear' ) if not fixed_tst: layout['scene']['yaxis'].update( title='t_st (mA)' ) if plot_vs_qst: layout['scene']['yaxis'].update( title='Q_st (nC)' ) fig = { 'layout': layout, 'data': [] } return fig def get_empty_output_2_graph(fixed_tst=True, plot_vs_qst=False, plot_vs_tCNAP=False): margin = 10 y_label_padding = 50 x_label_padding = 30 layout = go.Layout( scene=dict( xaxis=dict( title='CV (m/s)', type='log', autorange=True ), yaxis=dict( title='I_st (mA)' ) ), margin=dict( l=margin+y_label_padding, r=margin, b=margin+x_label_padding, t=margin ), height=400, plot_bgcolor=osparc_style['backgroundColor'], paper_bgcolor=osparc_style['backgroundColor'], font=dict( color=osparc_style['color'] ) ) if plot_vs_tCNAP: layout['scene']['xaxis'].update( title='t_CNAP (ms)', type='linear' ) if not fixed_tst: layout['scene']['yaxis'].update( title='t_st (mA)' ) if plot_vs_qst: layout['scene']['yaxis'].update( title='Q_st (nC)' ) return { 'layout': layout, 'data': [] } empty_input_graph = get_empty_input_graph() empty_output_1_graph = get_empty_output_1_graph() empty_output_2_graph = get_empty_output_2_graph() app.layout = html.Div(children=[ html.Div([ # Four input graphs on the left html.Div([ html.H4( children='Learned Model Input Parameters', style=centered_text ), dcc.Graph(id='graph-ins', figure=empty_input_graph) ], style=flex_column), # Controls in the middle html.Div([ html.Div( children='Minimal description of how the solver works.', style=centered_text ), html.Div([ html.H5('Input options'), html.Label('Select a Nerve Profile'), dcc.Dropdown( id='input-nerve-profile', options=[ {'label': 'Subject 1: Cervical Vagus', 'value': 0}, {'label': 'Subject 2: Cervical Vagus', 'value': 1}, {'label': 'Subject 2: Gastric Vagus', 'value': 2} ], value=0, style=dcc_input ), html.Label('Plot Options'), dcc.Checklist( id='input-plot-options', options=[ {'label': 'Plot against Charge-Phase', 'value': 'charge_phase_cb'}, {'label': 'Plot CNAP versus Time (ms)', 'value': 'time_cb'} ], values=[] ), html.Button('Load', id='load-input-button', style=dcc_input_button) ], style=options_layout), html.Div([ html.H5('Sweep Pulse'), dcc.Tabs( id="sweep-pulse-tabs", value='current', children=[ dcc.Tab( label='Current', value='current', style=tab_style, selected_style=tab_style, children=[ html.Div([ html.Div([ html.Label('Starting tst (mA):') ], style=dcc_input_label), dcc.Input( id='current_in_1', type='number', value=0, style=dcc_input_number ) ], style=dcc_input_pair), html.Div([ html.Div([ html.Label('Ending tst (mA):'), ], style=dcc_input_label), dcc.Input( id='current_in_2', type='number', value=1, style=dcc_input_number ) ], style=dcc_input_pair), html.Div([ html.Div([ html.Label('Step Size (mA):') ], style=dcc_input_label), dcc.Input( id='current_in_3', type='number', value=0.01, style=dcc_input_number ) ], style=dcc_input_pair), html.Div([ html.Div([ html.Label('Fixed Ist (ms):') ], style=dcc_input_label), dcc.Input( id='current_in_4', type='number', value=0.4, style=dcc_input_number ) ], style=dcc_input_pair), html.Button( 'Predict CNAPs', id='predict-current-button', style=dcc_input_button), ] ), dcc.Tab( label='Duration', value='duration', style=tab_style, selected_style=tab_style, children=[ html.Div([ html.Div([ html.Label('Starting Ist (mA):') ], style=dcc_input_label), dcc.Input( id='duration_in_1', type='number', value=0, style=dcc_input_number ) ], style=dcc_input_pair), html.Div([ html.Div([ html.Label('Ending Ist (mA):'), ], style=dcc_input_label), dcc.Input( id='duration_in_2', type='number', value=1, style=dcc_input_number ) ], style=dcc_input_pair), html.Div([ html.Div([ html.Label('Step Size (mA):') ], style=dcc_input_label), dcc.Input( id='duration_in_3', type='number', value=0.01, style=dcc_input_number ) ], style=dcc_input_pair), html.Div([ html.Div([ html.Label('Fixed tst (ms):') ], style=dcc_input_label), dcc.Input( id='duration_in_4', type='number', value=0.6, style=dcc_input_number ) ], style=dcc_input_pair), html.Button( 'Predict CNAPs', id='predict-duration-button', style=dcc_input_button), ] ) ], ), html.Div(id='tabs-content') ], style=options_layout) ], style=unflex_column), # Two output graphs on the right html.Div([ html.H4( id='output-label', children='Predicted Compound Nerve Action Potentials', style=centered_text ), dcc.Graph(id='graph-out1', figure=empty_output_1_graph), dcc.Graph(id='graph-out2', figure=empty_output_2_graph) ], style=flex_column), ], style=flex_columns) ], style=osparc_style) def get_selected_checkboxes(string_from_components): checked = [0, 0] if ('charge_phase_cb' in string_from_components): checked[0] = 1 if ('time_cb' in string_from_components): checked[1] = 1 return checked def create_learned_model_input(path, plot_vs_tcnap): column_names = ['t_ms', 'CV', 'Vmax', 'M_mod', 'B_mod', 'tauSD'] data = pd.read_csv(path, sep=',', names=column_names) # dpi = 96 # height = 1024 # width = 1024 # fontsize = 16 # plt.figure(figsize=(width / dpi, height / dpi), dpi=dpi) return { "plot_vs_tcnap": plot_vs_tcnap, "x_axis": { "t_ms": data.t_ms, "CV": data.CV }, "y_axis": { "Vmax": [i*-1e12 for i in data.Vmax], "M_mod": data.M_mod, "B_mod": data.B_mod, "tauSD": data.tauSD, } } def create_predicted_compound_nerve_action(cv_path, t_path, ist_path, tst_path, qst_path, vpred_path, lpred_path, fixed_tst, plot_vs_qst, plot_vs_tCNAP): # pylint:disable=too-many-arguments data_cv = pd.read_csv(cv_path, sep=',', header=None) data_tcnap = pd.read_csv(t_path, sep=',', header=None) data_ist = None data_tst = None if fixed_tst: data_ist = pd.read_csv(ist_path, sep=',', header=None) else: data_tst = pd.read_csv(tst_path, sep=',', header=None) data_CAP = pd.read_csv(qst_path, sep=',', header=None) data_vpred = pd.read_csv(vpred_path, sep=',', header=None) data_lpred = pd.read_csv(lpred_path, sep=',', header=None) # dpi = 96 # height = 1024 # width = 800 # fontsize = 16 data_cv[data_cv > 100] = None x_axis = data_cv if plot_vs_tCNAP: x_axis = data_tcnap y_axis = data_ist if not fixed_tst: y_axis = data_tst if plot_vs_qst: y_axis = data_CAP x_axis = x_axis.values[:, 0] y_axis = y_axis.values[0, :] return { "fixed_tst": fixed_tst, "plot_vs_qst": plot_vs_qst, "plot_vs_tCNAP": plot_vs_tCNAP, "3d": { "x": y_axis, "y": x_axis, "z": data_vpred.values.T, }, "heatmap": { "x": x_axis, "y": y_axis, "z": data_lpred.values.T, } } async def _upload_data(output_files): ports = await node_ports.ports() for idx, path in enumerate(output_files): if path.exists(): await (await ports.outputs)[idx].set(path) def push_output_data(): input_path = OUTPUT_DIR / 'input.csv' cv_path = OUTPUT_DIR / 'CV_plot.csv' t_path = OUTPUT_DIR / 't_plot.csv' ist_path = OUTPUT_DIR / 'Ist_plot.csv' tst_path = OUTPUT_DIR / 'tst_plot.csv' qst_path = OUTPUT_DIR / 'CAP_plot.csv' vpred_path = OUTPUT_DIR / 'V_pred_plot.csv' lpred_path = OUTPUT_DIR / 'Lpred_plot.csv' output_files = [input_path, cv_path, t_path, ist_path, tst_path, qst_path, vpred_path, lpred_path] for p in output_files: logger.info('file %s', str(p)) logger.info('exsits %s', p.exists()) asyncio.get_event_loop().run_until_complete(_upload_data(output_files)) # ports = node_ports.ports() # tasks = asyncio.gather(*[ports.outputs[idx].set(path) for idx, path in enumerate(output_files)]) # paths_to_outputs = asyncio.get_event_loop().run_until_complete( tasks ) # assert all( p.exists() for p in paths_to_outputs ) # return paths_to_outputs def run_solver(*args): if DEVEL_MODE: return subprocess.call(["execute_cnap.sh", *args], cwd=OUTPUT_DIR) def create_input_files(model_id, plot_vs_tCNAP): # !execute_cnap.sh $model_id 0 0.0 1.0 0.5 0.4 run_solver(str(model_id), "0", "0.0", "1.0", "0.5", "0.4") path = OUTPUT_DIR / 'input.csv' return create_learned_model_input(path, plot_vs_tCNAP) def build_input_graphs(data): marker_size = 2 line_width = 1 plot_vs_tcnap = data["plot_vs_tcnap"] if (plot_vs_tcnap): x_data = data["x_axis"]["t_ms"] else: x_data = data["x_axis"]["CV"] trace1 = go.Scatter( x=x_data, y=data["y_axis"]["Vmax"], mode='lines+markers', marker=dict( size=marker_size ), line=dict( width=line_width ) ) trace2 = go.Scatter( x=x_data, y=data["y_axis"]["M_mod"], mode='lines+markers', marker=dict( size=marker_size ), line=dict( width=line_width ) ) trace3 = go.Scatter( x=x_data, y=data["y_axis"]["B_mod"], mode='lines+markers', marker=dict( size=marker_size ), line=dict( width=line_width ) ) trace4 = go.Scatter( x=x_data, y=data["y_axis"]["tauSD"], mode='lines+markers', marker=dict( size=marker_size ), line=dict( width=line_width ) ) fig = get_empty_input_graph() fig.append_trace(trace1, 1, 1) fig.append_trace(trace2, 2, 1) fig.append_trace(trace3, 3, 1) fig.append_trace(trace4, 4, 1) if (plot_vs_tcnap): fig['layout']['xaxis'].update( autorange=True ) else: fig['layout']['xaxis'].update( type='log', autorange=True ) return fig # When pressing 'Load' this callback will be triggered. # Also, its output will trigger the rebuilding of the four input graphs. @app.callback( Output('graph-ins', 'figure'), [Input('load-input-button', 'n_clicks')], state=[ State(component_id='input-nerve-profile', component_property='value'), State(component_id='input-plot-options', component_property='values') ] ) def read_input_file(_n_clicks, input_nerve_profile, input_plot_options): model_id = input_nerve_profile + 1 selected_cb = get_selected_checkboxes(input_plot_options) data = create_input_files(model_id, selected_cb[1]) push_output_data() return build_input_graphs(data) # When pressing 'Predict' this callback will be triggered. # Also, its output will trigger the rebuilding of the two output graphs. @app.callback( Output('output-label', 'children'), [ Input('predict-current-button', 'n_clicks_timestamp'), Input('predict-duration-button', 'n_clicks_timestamp') ] ) def update_output_label(button_current_ts, button_duration_ts): if button_current_ts is None: button_current_ts = 0 if button_duration_ts is None: button_duration_ts = 0 base_text = 'Predicted Compound Nerve Action Potentials' if button_current_ts < button_duration_ts: return base_text + ' (Duration)' return base_text + ' (Current)' def build_graph_out_1(data): fig = get_empty_output_1_graph() if not data: return fig fig = get_empty_output_1_graph( data["fixed_tst"], data["plot_vs_qst"], data["plot_vs_tCNAP"]) dummy_wireframe = False if dummy_wireframe: x = np.linspace(-5, 5, 50) y = np.linspace(-5, 5, 50) xGrid, yGrid = np.meshgrid(y, x) R = np.sqrt(xGrid ** 2 + yGrid ** 2) z = np.sin(R) # Creating the plot lines = [] line_marker = dict(color='#0066FF', width=2) for i, j, k in zip(xGrid, yGrid, z): lines.append(go.Scatter3d( x=i, y=j, z=k, mode='lines', line=line_marker)) fig['data'] = lines return fig data_3d = data["3d"] x = data_3d["x"] y = data_3d["y"] xGrid, yGrid = np.meshgrid(y, x) z = data_3d["z"] # Creating the plot lines = [] line_marker = dict(color='#0066FF', width=2) for i, j, k in zip(xGrid, yGrid, z): lines.append(go.Scatter3d( x=i, y=j, z=k, mode='lines', line=line_marker)) fig['data'] = lines return fig def build_graph_out_2(data): fig = get_empty_output_2_graph() if not data: return fig fig = get_empty_output_2_graph( data["fixed_tst"], data["plot_vs_qst"], data["plot_vs_tCNAP"]) data_heatmap = data["heatmap"] x = data_heatmap["x"] y = data_heatmap["y"] z = data_heatmap["z"] data = go.Heatmap(x=x, y=y, z=z) fig['data'] = [data] return fig @app.callback( [ Output('graph-out1', 'figure'), Output('graph-out2', 'figure'), ], [ Input('predict-current-button', 'n_clicks_timestamp'), Input('predict-duration-button', 'n_clicks_timestamp') ], state=[ State(component_id='input-nerve-profile', component_property='value'), State(component_id='input-plot-options', component_property='values'), State(component_id='current_in_1', component_property='value'), State(component_id='current_in_2', component_property='value'), State(component_id='current_in_3', component_property='value'), State(component_id='current_in_4', component_property='value'), State(component_id='duration_in_1', component_property='value'), State(component_id='duration_in_2', component_property='value'), State(component_id='duration_in_3', component_property='value'), State(component_id='duration_in_4', component_property='value') ] ) def predict( # pylint:disable=too-many-arguments button_current_ts, button_duration_ts, input_nerve_profile, input_plot_options, current_1, current_2, current_3, current_4, duration_1, duration_2, duration_3, duration_4): if button_current_ts is None: button_current_ts = 0 if button_duration_ts is None: button_duration_ts = 0 if button_current_ts == 0 and button_duration_ts == 0: return [get_empty_output_1_graph(), get_empty_output_2_graph()] model_id = input_nerve_profile + 1 selected_cb = get_selected_checkboxes(input_plot_options) plot_vs_qst = selected_cb[0] plot_vs_tCNAP = selected_cb[1] cv_path = OUTPUT_DIR / 'CV_plot.csv' t_path = OUTPUT_DIR / 't_plot.csv' ist_path = OUTPUT_DIR / 'Ist_plot.csv' tst_path = OUTPUT_DIR / 'tst_plot.csv' qst_path = OUTPUT_DIR / 'CAP_plot.csv' vpred_path = OUTPUT_DIR / 'V_pred_plot.csv' lpred_path = OUTPUT_DIR / 'Lpred_plot.csv' data = None if button_current_ts > button_duration_ts: sweep_param = 1 fixed_tst = True print("Current clicked.", model_id, sweep_param, plot_vs_qst, plot_vs_tCNAP, current_1, current_2, current_3, current_4) # !execute_cnap.sh $model_id $sweep_param $start_ist.value $end_ist.value $step_size_current.value $fixed_tst.value run_solver(str(model_id), str(sweep_param), str(current_1), str(current_2), str(current_3), str(current_4)) data = create_predicted_compound_nerve_action(cv_path=cv_path, t_path=t_path, ist_path=ist_path, tst_path=tst_path, qst_path=qst_path, vpred_path=vpred_path, lpred_path=lpred_path, fixed_tst=fixed_tst, plot_vs_qst=plot_vs_qst, plot_vs_tCNAP=plot_vs_tCNAP) else: sweep_param = 0 fixed_tst = False print("Duration clicked.", model_id, sweep_param, plot_vs_qst, plot_vs_tCNAP, duration_1, duration_2, duration_3, duration_4) # !execute_cnap.sh $model_id $sweep_param $start_ist.value $end_ist.value $step_size_current.value $fixed_tst.value run_solver(str(model_id), str(sweep_param), str(duration_1), str(duration_2), str(duration_3), str(duration_4)) data = create_predicted_compound_nerve_action(cv_path=cv_path, t_path=t_path, ist_path=ist_path, tst_path=tst_path, qst_path=qst_path, vpred_path=vpred_path, lpred_path=lpred_path, fixed_tst=fixed_tst, plot_vs_qst=plot_vs_qst, plot_vs_tCNAP=plot_vs_tCNAP) graph1 = build_graph_out_1(data) graph2 = build_graph_out_2(data) return [graph1, graph2] class AnyThreadEventLoopPolicy(asyncio.DefaultEventLoopPolicy): """Event loop policy that allows loop creation on any thread.""" def get_event_loop(self) -> asyncio.AbstractEventLoop: try: return super().get_event_loop() except (RuntimeError, AssertionError): # "There is no current event loop in thread %r" loop = self.new_event_loop() self.set_event_loop(loop) return loop if __name__ == '__main__': # the following line is needed for async calls asyncio.set_event_loop_policy(AnyThreadEventLoopPolicy()) app.run_server(debug=DEVEL_MODE, port=8888, host="0.0.0.0")
python
""" This week’s question: Implement a simple version of autocomplete, where given an input string s and a dictionary of words dict, return the word(s) in dict that partially match s (or an empty string if nothing matches). Example: let dict = ['apple', 'banana', 'cranberry', 'strawberry'] $ simpleAutocomplete('app') $ ['apple'] $ simpleAutocomplete('berry') $ ['cranberry', 'strawberry'] $ simpleAutocomplete('fart') $ [] """ class AutoComplete: def __init__(self, words): self.words = words def simple_autocomplete(self, s): """ >> a = AutoComplete(['apple', 'banana', 'cranberry', 'strawberry']) >> a.simple_autocomplete('app') ['apple'] >> a.simple_autocomplete('berry') ['cranberry', 'strawberry'] >> a.simple_autocomplete('fart') [] """ return [word for word in self.words if word.contains(s)] if __name__ == "__main__": import doctest doctest.testmod()
python
import re from setuptools import setup, find_packages INIT_FILE = 'dimensigon/__init__.py' with open("README.md", "r") as fh: long_description = fh.read() def find_version(): with open(INIT_FILE) as fp: for line in fp: # __version__ = '0.1.0' match = re.search(r"__version__\s*=\s*(['\"])([^\1]+)\1", line) if match: return match.group(2) assert False, 'cannot find version' def find_author_email(): with open(INIT_FILE) as fp: m_author, m_email = None, None for line in fp: if not m_author: m_author = re.search(r"__author__\s*=\s*(['\"])([^\1]*)\1", line) if not m_email: m_email = re.search(r"__email__\s*=\s*(['\"])([^\1]*)\1", line) if m_author and m_email: return m_author.group(2), m_email.group(2) assert False, 'cannot find author or email' def find_licence(): with open(INIT_FILE) as fp: for line in fp: match = re.search(r"__license__\s*=\s*(['\"])([^\1]*)\1", line) if match: return match.group(2) assert False, 'cannot find license' def required_packages(): with open('requirements.txt') as fp: return [line.strip() for line in fp if line.strip()] author, email = find_author_email() setup( name='dimensigon', version=find_version(), package_dir={"": "."}, packages=find_packages(where=".", exclude=["contrib", "docs", "tests*", "tasks"]), url='https://github.com/dimensigon/dimensigon', license=find_licence(), author=author, author_email=email, description="Distributed Management and orchestration through RESTful, Mesh Networking and with a flair of IoT.", long_description=long_description, long_description_content_type="text/markdown", test_suite="tests", install_requires=required_packages(), classifiers=[ "Programming Language :: Python :: 3.6", "License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)", "Operating System :: POSIX", ], entry_points={'console_scripts': ["dshell=dimensigon.dshell.batch.dshell:main", "dimensigon=dimensigon.__main__:main"]}, python_requires='>=3.6', )
python
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ app.modules.logs ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 日志模块 """ from flask_smorest import Blueprint blp = Blueprint("Log", __name__, url_prefix="/logs", description="日志模块")
python
import pytest import numpy as np from whatlies.language import SpacyLanguage from whatlies.transformers import Pca words = [ "prince", "princess", "nurse", "doctor", "banker", "man", "woman", "cousin", "neice", "king", "queen", "dude", "guy", "gal", "fire", "dog", "cat", "mouse", "red", "blue", "green", "yellow", "water", "person", "family", "brother", "sister", ] # I'm loading in the spaCy model globally because it is much faster this way. lang = SpacyLanguage("en_core_web_md") @pytest.fixture def embset(): return lang[words] def test_set_title_works(embset): ax = embset.plot_3d(annot=True, title="foobar") assert ax.title._text == "foobar" def test_correct_points_plotted(embset): embset_plt = embset.transform(Pca(3)) ax = embset_plt.plot_3d(annot=True) offset = ax.collections[0]._offsets3d assert np.all(np.array(offset).T == embset_plt.to_X()) def test_correct_points_plotted_mapped(embset): embset_plt = embset.transform(Pca(3)) ax = embset_plt.plot_3d("king", "red", "dog", annot=True) offset = ax.collections[0]._offsets3d king, red, dog = [v for v in np.array(offset)] assert np.all(king == np.array([embset_plt[w] > embset_plt["king"] for w in words])) assert np.all(red == np.array([embset_plt[w] > embset_plt["red"] for w in words])) assert np.all(dog == np.array([embset_plt[w] > embset_plt["dog"] for w in words])) def test_basic_dimensions_3d_chart(embset): embset_plt = embset.transform(Pca(3)) ax = embset_plt.plot_3d(annot=True, title="foobar") assert ax.xaxis.get_label_text() == "Dimension 0" assert ax.yaxis.get_label_text() == "Dimension 1" assert ax.zaxis.get_label_text() == "Dimension 2" assert [t.get_text() for t in ax.texts] == words def test_named_dimensions_3d_chart(embset): ax = embset.transform(Pca(3)).plot_3d("king", "queen", "prince", annot=True) assert ax.xaxis.get_label_text() == "king" assert ax.yaxis.get_label_text() == "queen" assert ax.zaxis.get_label_text() == "prince" assert [t.get_text() for t in ax.texts] == words def test_named_dimensions_3d_chart_rename(embset): ax = embset.transform(Pca(3)).plot_3d( "king", "queen", "prince", annot=True, x_label="x", y_label="y" ) assert ax.xaxis.get_label_text() == "x" assert ax.yaxis.get_label_text() == "y" assert ax.zaxis.get_label_text() == "prince" assert [t.get_text() for t in ax.texts] == words
python
import functools import numpy as np import psyneulink as pnl import psyneulink.core.components.functions.transferfunctions input_layer = pnl.TransferMechanism( size=3, name='Input Layer' ) action_selection = pnl.TransferMechanism( size=3, function=psyneulink.core.components.functions.transferfunctions.SoftMax( output=pnl.ALL, gain=1.0), output_ports={pnl.NAME: 'SELECTED ACTION', pnl.VARIABLE:[(pnl.INPUT_PORT_VARIABLES, 0), (pnl.OWNER_VALUE, 0)], pnl.FUNCTION: psyneulink.core.components.functions.selectionfunctions.OneHot(mode=pnl.PROB).function}, # output_ports={pnl.NAME: "SOFT_MAX", # pnl.VARIABLE: (pnl.OWNER_VALUE,0), # pnl.FUNCTION: pnl.SoftMax(output=pnl.PROB,gain=1.0)}, name='Action Selection' ) p = pnl.Pathway( pathway=([input_layer, action_selection], pnl.Reinforcement), ) actions = ['left', 'middle', 'right'] reward_values = [10, 0, 0] first_reward = 0 # Must initialize reward (won't be used, but needed for declaration of lambda function) action_selection.output_port.value = [0, 0, 1] # Get reward value for selected action) def reward(context=None): """Return the reward associated with the selected action""" return [reward_values[int(np.nonzero(action_selection.output_port.parameters.value.get(context))[0])]] def print_header(comp): print("\n\n**** Time: ", comp.scheduler.get_clock(comp).simple_time) def show_weights(comp): comparator = action_selection.output_port.efferents[0].receiver.owner learn_mech = action_selection.output_port.efferents[1].receiver.owner print( '\n' '\naction_selection value: {} ' '\naction_selection output: {} ' '\ncomparator sample: {} ' '\ncomparator target: {} ' '\nlearning mech act in: {} ' '\nlearning mech act out: {} ' '\nlearning mech error in: {} ' '\nlearning mech error out: {} ' '\nlearning mech learning_sig: {} ' '\npredicted reward: {} '.format( action_selection.parameters.value.get(comp), action_selection.output_port.parameters.value.get(comp), comparator.input_ports[pnl.SAMPLE].parameters.value.get(comp), comparator.input_ports[pnl.TARGET].parameters.value.get(comp), learn_mech.input_ports[pnl.ACTIVATION_INPUT].parameters.value.get(comp), learn_mech.input_ports[pnl.ACTIVATION_OUTPUT].parameters.value.get(comp), learn_mech.input_ports[pnl.ERROR_SIGNAL].parameters.value.get(comp), learn_mech.output_ports[pnl.ERROR_SIGNAL].parameters.value.get(comp), learn_mech.output_ports[pnl.LEARNING_SIGNAL].parameters.value.get(comp), action_selection.output_port.parameters.value.get(comp)[np.nonzero(action_selection.output_port.parameters.value.get(comp))][0] ) ) input_list = {input_layer: [[1, 1, 1]]} c = pnl.Composition(pathways=[p]) print('reward prediction weights: \n', action_selection.input_port.path_afferents[0].matrix) print('target_mechanism weights: \n', action_selection.output_port.efferents[0].matrix) c.show_graph(show_learning=pnl.ALL) c.learn( num_trials=10, inputs=input_list, # FIX: PROPER FORMAT FOR ASSIGNING TARGET AS FUNCTION? targets={action_selection:reward}, call_before_trial=functools.partial(print_header, c), call_after_trial=functools.partial(show_weights, c) )
python
""" This class provides functionality for managing a generig sqlite or mysql database: * reading specific fields (with the possibility to filter by field values) * storing calculated values in the dataset Created on May 11 2018 @author: Jerónimo Arenas García """ from __future__ import print_function # For python 2 copmatibility import os import pandas as pd import MySQLdb import sqlite3 import numpy as np from tabulate import tabulate import copy import ipdb class BaseDMsql(object): """ Data manager base class. """ def __init__(self, db_name, db_connector, path2project=None, db_server=None, db_user=None, db_password=None): """ Initializes a DataManager object Args: db_name :Name of the DB db_connector :Connector. Available options are mysql or sqlite path2project :Path to the project folder (sqlite only) db_server :Server (mysql only) db_user :User (mysql only) db_password :Password (mysql only) """ # Store paths to the main project folders and files self._path2project = copy.copy(path2project) self.dbname = db_name self.connector = db_connector self.server = db_server self.user = db_user self.password = db_password # Other class variables self.dbON = False # Will switch to True when the db was connected. # Connector to database self._conn = None # Cursor of the database self._c = None # Try connection try: if self.connector == 'mysql': self._conn = MySQLdb.connect(self.server, self.user, self.password, self.dbname) self._c = self._conn.cursor() print("MySQL database connection successful") self.dbON = True self._conn.set_character_set('utf8') elif self.connector == 'sqlite3': # sqlite3 # sqlite file will be in the root of the project, we read the # name from the config file and establish the connection db_fname = os.path.join(self._path2project, self.dbname + '.db') print("---- Connecting to {}".format(db_fname)) self._conn = sqlite3.connect(db_fname) self._c = self._conn.cursor() self.dbON = True else: print("---- Unknown DB connector {}".format(self.connector)) except: print("---- Error connecting to the database") def __del__(self): """ When destroying the object, it is necessary to commit changes in the database and close the connection """ try: self._conn.commit() self._conn.close() except: print("---- Error closing database") def resetDBtables(self, tables=None): """ Delete existing database, and regenerate empty tables Args: tables: If string, name of the table to reset. If list, list of tables to reset If None (default), all tables are deleted, and all tables (inlcuding those that might not exist previously) """ # If tables is None, all tables are deleted an re-generated if tables is None: # Delete all existing tables for table in self.getTableNames(): self._c.execute("DROP TABLE " + table) # Create tables. No tables as specifies in order to create tables # that did not exist previously also. self.createDBtables() else: # It tables is not a list, make the appropriate list if type(tables) is str: tables = [tables] # Remove all selected tables (if exist in the database). for table in set(tables) & set(self.getTableNames()): self._c.execute("DROP TABLE " + table) # All deleted tables are created again self.createDBtables(tables) self._conn.commit() return def resetDB(self): """ Deletes existing database, and regenerate empty tables """ if self.connector == 'mysql': # In mysql we simply drop all existing tables for tablename in self.getTableNames(): self._c.execute("DROP TABLE " + tablename) self._conn.commit() else: # If sqlite3, we need to delete the file, and start over try: self._conn.commit() self._conn.close() except: print("Error closing database") # Delete sqlite3 file db_fname = os.path.join(self._path2project, self.dbname + '.db') os.remove(db_fname) try: self._conn = sqlite3.connect(db_fname) self._c = self._conn.cursor() except: print("Error connecting to the database") self.createDBtables() def addTableColumn(self, tablename, columnname, columntype): """ Add a new column to the specified table. Args: tablename :Table to which the column will be added columnname :Name of new column columntype :Type of new column. Note that, for mysql, if type is TXT or VARCHAR, the character set if forzed to be utf8. """ # Check if the table exists if tablename in self.getTableNames(): # Check that the column does not already exist if columnname not in self.getColumnNames(tablename): # Fit characters to the allowed format if necessary fmt = '' if (self.connector == 'mysql' and ('TEXT' in columntype or 'VARCHAR' in columntype) and not ('CHARACTER SET' in columntype or 'utf8' in columntype)): # We need to enforze utf8 for mysql fmt = ' CHARACTER SET utf8' sqlcmd = ('ALTER TABLE ' + tablename + ' ADD COLUMN ' + columnname + ' ' + columntype + fmt) self._c.execute(sqlcmd) # Commit changes self._conn.commit() else: print(("WARNING: Column {0} already exist in table {1}." ).format(columnname, tablename)) else: print('Error adding column to table. Please, select a valid ' + 'table name from the list') print(self.getTableNames()) def dropTableColumn(self, tablename, columnname): """ Remove column from the specified table Args: tablename :Table to which the column will be added columnname :Name of column to be removed """ # Check if the table exists if tablename in self.getTableNames(): # Check that the column does not already exist if columnname in self.getColumnNames(tablename): # ALTER TABLE DROP COLUMN IS ONLY SUPPORTED IN MYSQL if self.connector == 'mysql': sqlcmd = ('ALTER TABLE ' + tablename + ' DROP COLUMN ' + columnname) self._c.execute(sqlcmd) # Commit changes self._conn.commit() else: print('Column drop not yet supported for SQLITE') else: print('Error deleting column. The column does not exist') print(tablename, columnname) else: print('Error deleting column. Please, select a valid table name' + ' from the list') print(self.getTableNames()) return def readDBtable(self, tablename, limit=None, selectOptions=None, filterOptions=None, orderOptions=None): """ Read data from a table in the database can choose to read only some specific fields Args: tablename : Table to read from selectOptions: string with fields that will be retrieved (e.g. 'REFERENCIA, Resumen') filterOptions: string with filtering options for the SQL query (e.g., 'WHERE UNESCO_cd=23') orderOptions: string with field that will be used for sorting the results of the query (e.g, 'Cconv') limit: The maximum number of records to retrieve """ try: # Check that table name is valid if tablename in self.getTableNames(): sqlQuery = 'SELECT ' if selectOptions: sqlQuery = sqlQuery + selectOptions else: sqlQuery = sqlQuery + '*' sqlQuery = sqlQuery + ' FROM ' + tablename + ' ' if filterOptions: sqlQuery = sqlQuery + ' WHERE ' + filterOptions if orderOptions: sqlQuery = sqlQuery + ' ORDER BY ' + orderOptions if limit: sqlQuery = sqlQuery + ' LIMIT ' + str(limit) # This is to update the connection to changes by other # processes. self._conn.commit() # Return the pandas dataframe. Note that numbers in text format # are not converted to return pd.read_sql(sqlQuery, con=self._conn, coerce_float=False) else: print('Error in query. Please, select a valid table name ' + 'from the list') print(self.getTableNames()) except Exception as E: print(str(E)) def getTableNames(self): """ Returns a list with the names of all tables in the database """ # The specific command depends on whether we are using mysql or sqlite if self.connector == 'mysql': sqlcmd = ("SELECT table_name FROM INFORMATION_SCHEMA.TABLES " + "WHERE table_schema='" + self.dbname + "'") else: sqlcmd = "SELECT name FROM sqlite_master WHERE type='table'" self._c.execute(sqlcmd) tbnames = [el[0] for el in self._c.fetchall()] return tbnames def getColumnNames(self, tablename): """ Returns a list with the names of all columns in the indicated table Args: tablename: the name of the table to retrieve column names """ # Check if tablename exists in database if tablename in self.getTableNames(): # The specific command depends on whether we are using mysql or # sqlite if self.connector == 'mysql': sqlcmd = "SHOW COLUMNS FROM " + tablename self._c.execute(sqlcmd) columnnames = [el[0] for el in self._c.fetchall()] else: sqlcmd = "PRAGMA table_info(" + tablename + ")" self._c.execute(sqlcmd) columnnames = [el[1] for el in self._c.fetchall()] return columnnames else: print('Error retrieving column names: Table does not exist on ' + 'database') return [] def getTableInfo(self, tablename): # Get columns cols = self.getColumnNames(tablename) # Get number of rows sqlcmd = "SELECT COUNT(*) FROM " + tablename self._c.execute(sqlcmd) n_rows = self._c.fetchall()[0][0] return cols, n_rows def showTable(self, tablename, max_rows=500, max_width=200): """ A simple method to display the content of a single table. Args: max_rows: Maximum number of rows to display. It the size of the table is higher, only the first max_rows rows are shown max_width: Maximum with of the table to display. If the size of the table is higher, the tabulate environment is not used and only a table heading is shown """ title = "= Database {} ====================".format(self.dbname) print("="*len(title)) print(title) print("="*len(title)) print("") print("==== Table {} ".format(tablename)) cols, n_rows = self.getTableInfo(tablename) df = self.readDBtable(tablename, limit=max_rows, selectOptions=None, filterOptions=None, orderOptions=None) txt = tabulate(df, headers='keys', tablefmt='psql') txt_width = max(len(z) for z in txt.split('\n')) if txt_width > max_width: print('---- The table is too wide (up to {}'.format(txt_width) + ' characters per line). Showing a portion of the table ' + 'header only') print(df.head(25)) else: print(txt) return def insertInTable(self, tablename, columns, arguments): """ Insert new records into table Args: tablename: Name of table in which the data will be inserted columns: Name of columns for which data are provided arguments: A list of lists or tuples, each element associated to one new entry for the table """ # Make sure columns is a list, and not a single string if not isinstance(columns, (list,)): columns = [columns] ncol = len(columns) if len(arguments[0]) == ncol: # Make sure the tablename is valid if tablename in self.getTableNames(): # Make sure we have a list of tuples; necessary for mysql arguments = list(map(tuple, arguments)) # # Update DB entries one by one. # for arg in arguments: # # sd # sqlcmd = ('INSERT INTO ' + tablename + '(' + # ','.join(columns) + ') VALUES(' + # ','.join('{}'.format(a) for a in arg) + ')' # ) # try: # self._c.execute(sqlcmd) # except: # import ipdb # ipdb.set_trace() sqlcmd = ('INSERT INTO ' + tablename + '(' + ','.join(columns) + ') VALUES (') if self.connector == 'mysql': sqlcmd += '%s' + (ncol-1)*',%s' + ')' else: sqlcmd += '?' + (ncol-1)*',?' + ')' self._c.executemany(sqlcmd, arguments) # Commit changes self._conn.commit() else: print('Error inserting data in table: number of columns mismatch') return def setField(self, tablename, keyfld, valueflds, values): """ Update records of a DB table Args: tablename: Table that will be modified keyfld: string with the column name that will be used as key (e.g. 'REFERENCIA') valueflds: list with the names of the columns that will be updated (e.g., 'Lemas') values: A list of tuples in the format (keyfldvalue, valuefldvalue) (e.g., [('Ref1', 'gen celula'), ('Ref2', 'big_data, algorithm')]) """ # Make sure valueflds is a list, and not a single string if not isinstance(valueflds, (list,)): valueflds = [valueflds] ncol = len(valueflds) if len(values[0]) == (ncol+1): # Make sure the tablename is valid if tablename in self.getTableNames(): # Update DB entries one by one. # WARNING: THIS VERSION MAY NOT WORK PROPERLY IF v # HAS A STRING CONTAINING "". for v in values: sqlcmd = ('UPDATE ' + tablename + ' SET ' + ', '.join(['{0} ="{1}"'.format(f, v[i + 1]) for i, f in enumerate(valueflds)]) + ' WHERE {0}="{1}"'.format(keyfld, v[0])) self._c.execute(sqlcmd) # This is the old version: it might not have the problem of # the above version, but did not work properly with sqlite. # # Make sure we have a list of tuples; necessary for mysql # # Put key value last in the tuples # values = list(map(circ_left_shift, values)) # sqlcmd = 'UPDATE ' + tablename + ' SET ' # if self.connector == 'mysql': # sqlcmd += ', '.join([el+'=%s' for el in valueflds]) # sqlcmd += ' WHERE ' + keyfld + '=%s' # else: # sqlcmd += ', '.join([el+'=?' for el in valueflds]) # sqlcmd += ' WHERE ' + keyfld + '=?' # self._c.executemany(sqlcmd, values) # Commit changes self._conn.commit() else: print('Error updating table values: number of columns mismatch') return def upsert(self, tablename, keyfld, df): """ Update records of a DB table with the values in the df This function implements the following additional functionality: * If there are coumns in df that are not in the SQL table, columns will be added * New records will be created in the table if there are rows in the dataframe without an entry already in the table. For this, keyfld indicates which is the column that will be used as an index Args: tablename: Table that will be modified keyfld: string with the column name that will be used as key (e.g. 'REFERENCIA') df: Dataframe that we wish to save in table tablename """ # Check that table exists and keyfld exists both in the Table and the # Dataframe if tablename in self.getTableNames(): if not ((keyfld in df.columns) and (keyfld in self.getColumnNames(tablename))): print("Upsert function failed: Key field does not exist", "in the selected table and/or dataframe") return else: print('Upsert function failed: Table does not exist') return # Reorder dataframe to make sure that the key field goes first flds = [keyfld] + [x for x in df.columns if x != keyfld] df = df[flds] # Create new columns if necessary for clname in df.columns: if clname not in self.getColumnNames(tablename): if df[clname].dtypes == np.float64: self.addTableColumn(tablename, clname, 'DOUBLE') else: if df[clname].dtypes == np.int64: self.addTableColumn(tablename, clname, 'INTEGER') else: self.addTableColumn(tablename, clname, 'TEXT') # Check which values are already in the table, and split # the dataframe into records that need to be updated, and # records that need to be inserted keyintable = self.readDBtable(tablename, limit=None, selectOptions=keyfld) keyintable = keyintable[keyfld].tolist() values = [tuple(x) for x in df.values] values_insert = list(filter(lambda x: x[0] not in keyintable, values)) values_update = list(filter(lambda x: x[0] in keyintable, values)) if len(values_update): self.setField(tablename, keyfld, df.columns[1:].tolist(), values_update) if len(values_insert): self.insertInTable(tablename, df.columns.tolist(), values_insert) return def exportTable(self, tablename, fileformat, path, filename, cols=None): """ Export columns from a table to a file. Args: :tablename: Name of the table :fileformat: Type of output file. Available options are - 'xlsx' - 'pkl' :filepath: Route to the output folder :filename: Name of the output file :columnames: Columns to save. It can be a list or a string of comma-separated columns. If None, all columns saved. """ # Path to the output file fpath = os.path.join(path, filename) # Read data: if cols is list: options = ','.join(cols) else: options = cols df = self.readDBtable(tablename, selectOptions=options) # ###################### # Export results to file if fileformat == 'pkl': df.to_pickle(fpath) else: df.to_excel(fpath) return
python
"""Jurisdictions are a small complete list. Thus they can be operated from a dictionary. Still, persisted in DB for query consitency. Thus maintaining both synchronised and using at convenience.""" from db import Session from db import Jurisdiction from .lazyinit import _cached_jurisdictions from .api_wikipedia import import_countries from .statements import get_jurisdictions_statement # from . import debug debug = False def _jurisdiction_by_code(s, code): return s.query(Jurisdiction).filter(Jurisdiction.code == code).first() def get_jurisdiction_code(id): s = Session() result = s.query(Jurisdiction).get(id).code s.close() return result def jurisdiction_by_code(code): return _cached_jurisdictions[code] if code in _cached_jurisdictions else _cached_jurisdictions["XX"] def _query_db_cache(s): return s.query(Jurisdiction).all() def _load_db_cache(s): global _cached_jurisdictions committed = _query_db_cache(s) for next in committed: _cached_jurisdictions[next.code] = next.id return len(_cached_jurisdictions) def cached_jurisdictions(): if not _cached_jurisdictions: s = Session() if len(_query_db_cache(s)) == 0: all = [] for next in import_countries(): all.append(Jurisdiction(name=next["name"], code=next["code"])) all.append(Jurisdiction(name="Unknown", code="XX")) s.add_all(all) s.commit() _load_db_cache(s) if debug: print(_cached_jurisdictions) s.close() return _cached_jurisdictions
python
#!/usr/bin/env python3 # Utility functions. import sys import os import re # Prints passed objects to stderr. def warning(*objs): print("WARNING: ", *objs, file=sys.stderr) # Converts passed string by uppercasing first letter. firstLetterToUppercase = lambda s: s[:1].upper() + s[1:] if s else '' # Converts passed string by lowercasing first letter. firstLetterToLowercase = lambda s: s[:1].lower() + s[1:] if s else '' # Converts description in form of a sentence (words separated by # spaces, ends with period) into a camel case form. def descriptionToCamelCase(command): words = [] for word in command.split(): words.append(firstLetterToUppercase(word)) words[0] = firstLetterToLowercase(words[0]) out = "".join(words) out = re.sub(' ', '', out) out = re.sub('\.', '', out) return "__"+out # Converts text in form of camel case into a sentence (First # letter of first word in upper case, words separated by spaces, # ends with period). def camelCaseToDescription(command): command = command.strip('_') command = re.sub(r'([A-Z])',r' \1',command) command = command.lower() return firstLetterToUppercase(command)+"." # Retruns files lines as list of strings. def getFileContents(fileName): with open(fileName) as f: return f.readlines() def underscoreToCamelcase(command): out = "" command = command.strip('_') command = command.strip(' ') tokens = command.split('_') first = True for token in tokens: token = token.lower() if not first: token = firstLetterToUppercase(token) out += token first = False return out def camelcaseToUnderscore(command): command = command.strip(' ') s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', command) return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower() import collections class OrderedSet(collections.MutableSet): def __init__(self, iterable=None): self.end = end = [] end += [None, end, end] # sentinel node for doubly linked list self.map = {} # key --> [key, prev, next] if iterable is not None: self |= iterable def __len__(self): return len(self.map) def __contains__(self, key): return key in self.map def add(self, key): if key not in self.map: end = self.end curr = end[1] curr[2] = end[1] = self.map[key] = [key, curr, end] def discard(self, key): if key in self.map: key, prev, next = self.map.pop(key) prev[2] = next next[1] = prev def __iter__(self): end = self.end curr = end[2] while curr is not end: yield curr[0] curr = curr[2] def __reversed__(self): end = self.end curr = end[1] while curr is not end: yield curr[0] curr = curr[1] def pop(self, last=True): if not self: raise KeyError('set is empty') key = self.end[1][0] if last else self.end[2][0] self.discard(key) return key def __repr__(self): if not self: return '%s()' % (self.__class__.__name__,) return '%s(%r)' % (self.__class__.__name__, list(self)) def __eq__(self, other): if isinstance(other, OrderedSet): return len(self) == len(other) and list(self) == list(other) return set(self) == set(other)
python
# Generated by Django 2.1.1 on 2018-09-18 12:24 from django.db import migrations, models class Migration(migrations.Migration): initial = True dependencies = [ ] operations = [ migrations.CreateModel( name='Products', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=200)), ('price', models.IntegerField(default=0)), ('quantity', models.IntegerField(default=0)), ('created', models.DateTimeField(verbose_name='created date')), ('updated_date', models.DateTimeField(verbose_name='updated date')), ], ), ]
python
import math import numpy as np from mlpy.numberGenerator.bounds import Bounds from experiments.problems.functions.structure.function import Function class Elliptic(Function): def function(self, x): return np.sum(np.power(np.power(10., 6 ), np.divide(np.arange(len(x)), np.subtract(x, 1.)))) def getBounds(self): return Bounds(-100, 100) def test(self): assert(1 == self.function(np.array([5]))) assert(math.pow(10, 6) + 1 == self.function(np.array([5, 2])))
python
""" Created by Constantin Philippenko, 18th January 2022. """ import matplotlib matplotlib.rcParams.update({ "pgf.texsystem": "pdflatex", 'font.family': 'serif', 'text.usetex': True, 'pgf.rcfonts': False, 'text.latex.preamble': r'\usepackage{amsfonts}' }) import hashlib import os import sys import numpy as np from matplotlib import pyplot as plt from src.PickleHandler import pickle_saver, pickle_loader from src.Utilities import create_folder_if_not_existing from src.hyperparameters_exploration import Explorer from src.hyperparameters_exploration.Hyperparameters import Hyperparameters from src.hyperparameters_exploration.Metric import Metric class Exploration: def __init__(self, name, hyperparameters: Hyperparameters, explorer: Explorer, metrics: Metric): # super().__init__() self.name = name self.hyperparameters = hyperparameters self.explorer = explorer self.metrics = metrics self.nb_runs = 2 self.results = np.zeros((self.explorer.nb_outputs, self.nb_runs, self.hyperparameters.nb_hyperparams)) self.string_before_hash = str(self.hyperparameters.range_hyperparameters) self.hash_string = self.explorer.function.__name__ + "-" + hashlib.shake_256(self.string_before_hash.encode()).hexdigest(4) # returns a hash value of length 2*4 self.pickle_folder = "./pickle/exploration/" self.pictures_folder = "./pictures/exploration/" create_folder_if_not_existing(self.pickle_folder) create_folder_if_not_existing(self.pictures_folder) def run_exploration(self): print("====> Starting exploration : ", self.name) for idx_param in range(self.hyperparameters.nb_hyperparams): param = self.hyperparameters.range_hyperparameters[idx_param] print("Hyperparameter's value:", param) # self.blockPrint() for idx_run in range(self.nb_runs): output = self.explorer.explore(param) for i in range(len(output)): self.results[i, idx_run, idx_param] = self.metrics.compute(output[i]) pickle_saver(self, self.pickle_folder + self.hash_string) self.enablePrint() def load(self): self.results = pickle_loader(self.pickle_folder + self.hash_string).results[:,:,:-1] self.hyperparameters.range_hyperparameters = self.hyperparameters.range_hyperparameters[:-1] self.hyperparameters.nb_hyperparams -= 1 def plot_exploration(self): fig, ax = plt.subplots(figsize=(8, 7)) for i in range(len(self.explorer.outputs_label)): plt.errorbar(range(self.hyperparameters.nb_hyperparams), np.mean(self.results[i], axis=0), yerr=np.std(self.results[i], axis=0), label=self.explorer.outputs_label[i], lw=4) plt.xticks([i for i in range(0, len(self.hyperparameters.range_hyperparameters))], self.hyperparameters.range_hyperparameters, rotation=30, fontsize=15) plt.yticks(fontsize=15) ax.set_xlabel(self.hyperparameters.x_axis_label, fontsize=15) ax.set_ylabel(self.metrics.y_axis_label, fontsize=15) plt.title(self.hyperparameters.name, fontsize=15) plt.legend(loc='best', fontsize=15) ax.grid() plt.savefig('{0}.eps'.format(self.pictures_folder + self.hash_string), format='eps') plt.close() # Disable def blockPrint(self): sys.stdout = open(os.devnull, 'w') # Restore def enablePrint(self): sys.stdout = sys.__stdout__
python
x = 10.4 y = 3.5 x -= y print x
python
# Generated by Django 3.1.7 on 2021-03-15 22:30 from django.db import migrations, models import django.db.models.deletion import manager.storage import projects.models.sources class Migration(migrations.Migration): dependencies = [ ('socialaccount', '0003_extra_data_default_dict'), ('projects', '0029_auto_20210210_2340'), ] operations = [ migrations.AddField( model_name='googledocssource', name='social_app', field=models.ForeignKey(blank=True, help_text='The OAuth client that this Google Doc was linked using.', null=True, on_delete=django.db.models.deletion.SET_NULL, to='socialaccount.socialapp'), ), ]
python
import tensorflow as tf from tensorflow.keras import Sequential from tensorflow.keras.layers import Conv2D, MaxPool2D, Dense, Flatten, Dropout from tensorflow.keras.preprocessing.image import ImageDataGenerator from sklearn.utils import class_weight import numpy as np tf.keras.backend.set_floatx('float64') drop = 0.5 img_size = (128, 128) model = Sequential([ Conv2D(8, 5, activation = 'relu', input_shape = (img_size[0], img_size[1], 1)), MaxPool2D(3), Conv2D(16, 4, activation = 'relu'), MaxPool2D(2), Conv2D(32, 3, activation = 'relu'), Flatten(), Dense(32, activation = 'relu'), Dropout(drop), Dense(8, activation = 'relu'), Dense(3, activation = 'softmax') ]) model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy']) datagen = ImageDataGenerator( rescale = 1. / 255., shear_range = 0.2, zoom_range = 0.05, rotation_range = 10, width_shift_range = 0.1, height_shift_range = 0.05, brightness_range = [1, 1.5], horizontal_flip = True, dtype = tf.float64) train_generator = datagen.flow_from_directory( 'Dataset/Train', target_size = img_size, color_mode = 'grayscale', batch_size = 32, shuffle = True, class_mode='categorical') test_datagen = ImageDataGenerator( rescale = 1. / 255., dtype = tf.float64) test_generator = test_datagen.flow_from_directory( 'Dataset/Test', target_size = img_size, color_mode = 'grayscale', batch_size = 16, shuffle = True, class_mode='categorical') class_weights = class_weight.compute_class_weight( 'balanced', np.unique(train_generator.classes), train_generator.classes) model.fit(train_generator, epochs = 10, shuffle = True, validation_data = test_generator, class_weight = class_weights, workers = 8, max_queue_size = 512) model.save('saved/saved.h5')
python
import requests import re import json import os import copy class JsonProcessorFile(object): """Generate a dict of processing options that exist in a dictionary of dictionaries. Allow renaming of the fields. The results of this class is used to flatten out a JSON into CSV style. For example the following Dict below will generate another dictionary outlined below. Limitations: only works with 2 levels of dictionaries .. code-block:: python { "internal_loads_multiplier": { "lpd_multiplier": 0.7544625053841931, "epd_multiplier": 1.0, "people_per_floor_area_multiplier": 0.8572429796331562, "lpd_average": 7.30887013864965, "epd_average": 8.07293281253229, "ppl_average": 0.046136433190623, "applicable": true }, } .. code-block:: python { level_1: 'internal_loads_multiplier', level_2: 'lpd_multiplier', rename_to: '', order: 1 }, { level_1: 'internal_loads_multiplier', level_2: 'epd_multiplier', rename_to: '', order: 1 }, { level_1: 'internal_loads_multiplier', level_2: 'lpd_average', rename_to: '', order: 1 }, """ def __init__(self, json_files): """ :param json_files: list of files to process """ self.files = json_files self.data = [] self.process() def process(self): """Process the list of json files""" for file in self.files: data = { "file": os.path.basename(file), "data": [] } with open(file) as f: f = json.load(f) for k, v in f.items(): new_var = { "level_1": k, "level_2": None, "rename_to": "", # if there is no rename_to, then the name is set to the key "order": 1, # if there are duplicates, then the fields will be sorted alphabetically } if isinstance(v, dict): # The value is a dict, so process the dict values too for k2, v2 in v.items(): new_var_2 = copy.deepcopy(new_var) new_var_2["level_2"] = k2 data["data"].append(new_var_2) else: # single key -- just save the new variable data["data"].append(new_var) self.data.append(data) def save_as(self, filename): """Save the format to be used in the post_processor scripts""" if os.path.exists(filename): print(f"File already exists, will not overwrite, {filename}") return False else: with open(filename, 'w') as f: json.dump(self.data, f, indent=2) return True
python
# Copyright (c) 2020 Graphcore Ltd. All rights reserved. import numpy as np import popart import torch import pytest import torch.nn.functional as F from op_tester import op_tester # `import test_util` requires adding to sys.path import sys from pathlib import Path sys.path.append(Path(__file__).resolve().parent.parent) def test_scaledadd_constant(op_tester): d1 = np.random.rand(2).astype(np.float32) d2 = np.random.rand(2).astype(np.float32) def init_builder(builder): i1 = builder.addInputTensor(d1) i2 = builder.addInputTensor(d2) o = builder.aiGraphcore.scaledadd([i1, i2], scale0=0.5, scale1=0.8) builder.addOutputTensor(o) return [o] def reference(ref_data): t1 = torch.tensor(d1, requires_grad=False) t2 = torch.tensor(d2, requires_grad=False) out = 0.5 * t1 + 0.8 * t2 return [out] op_tester.setPatterns(['PreUniRepl', 'MulArgGradOp'], enableRuntimeAsserts=False) op_tester.run(init_builder, reference, step_type='infer') def test_scaledadd_tensor(op_tester): d1 = np.random.rand(2).astype(np.float32) d2 = np.random.rand(2).astype(np.float32) d3 = np.random.rand(1).astype(np.float32) d4 = np.random.rand(1).astype(np.float32) def init_builder(builder): i1 = builder.addInputTensor(d1) i2 = builder.addInputTensor(d2) i3 = builder.addInputTensor(d3) i4 = builder.addInputTensor(d4) o = builder.aiGraphcore.scaledadd([i1, i2, i3, i4]) builder.addOutputTensor(o) return [o] def reference(ref_data): t1 = torch.tensor(d1, requires_grad=False) t2 = torch.tensor(d2, requires_grad=False) t3 = torch.tensor(d3, requires_grad=False) t4 = torch.tensor(d4, requires_grad=False) out = t3 * t1 + t4 * t2 return [out] op_tester.setPatterns(['PreUniRepl', 'MulArgGradOp'], enableRuntimeAsserts=False) op_tester.run(init_builder, reference, step_type='infer')
python
import argparse import matplotlib.pyplot as plt import numpy as np import pandas as pd import seaborn as sns import torch from pytorch_lightning import seed_everything from sklearn.decomposition import PCA from sggm.data.uci_boston.datamodule import ( UCIBostonDataModule, UCIBostonDataModuleShifted, ) from sggm.data.uci_ccpp.datamodule import ( UCICCPPDataModule, UCICCPPDataModuleShifted, COLUMNS as uci_ccpp_columns, ) from sggm.data.uci_concrete.datamodule import ( UCIConcreteDataModule, UCIConcreteDataModuleShifted, COLUMNS as uci_concrete_columns, ) from sggm.data.uci_wine_red.datamodule import ( UCIWineRedDataModule, UCIWineRedDataModuleShifted, COLUMNS as uci_wine_red_columns, ) from sggm.data.uci_wine_white.datamodule import ( UCIWineWhiteDataModule, UCIWineWhiteDataModuleShifted, COLUMNS as uci_wine_white_columns, ) from sggm.data.uci_yacht.datamodule import ( UCIYachtDataModule, UCIYachtDataModuleShifted, UCIYachtDataModuleShiftedSplit, COLUMNS as uci_yacht_columns, ) from sggm.definitions import ( FASHION_MNIST, UCI_BOSTON, UCI_CONCRETE, UCI_CCPP, UCI_SUPERCONDUCT, UCI_WINE_RED, UCI_WINE_WHITE, UCI_YACHT, ) def main(experiment_name, with_pca=False): # Order of plotting TEST_FIRST = True # Investigate shift effect on pairplot SHIFTED = False sp_tot = 0.3 sp_k = 0.0002 TEST_FIRST = False if SHIFTED else TEST_FIRST seed_everything(123) # Get correct datamodule bs = 10000 if experiment_name == UCI_BOSTON: dm = ( UCIBostonDataModuleShifted( bs, 0, shifting_proportion_total=sp_tot, shifting_proportion_k=sp_k ) if SHIFTED else UCIBostonDataModule(bs, 0) ) columns = [i for i in range(1, 15)] elif experiment_name == UCI_CCPP: dm = ( UCICCPPDataModuleShifted( bs, 0, shifting_proportion_total=sp_tot, shifting_proportion_k=sp_k ) if SHIFTED else UCICCPPDataModule(bs, 0) ) columns = uci_ccpp_columns elif experiment_name == UCI_CONCRETE: dm = ( UCIConcreteDataModuleShifted( bs, 0, shifting_proportion_total=sp_tot, shifting_proportion_k=sp_k ) if SHIFTED else UCIConcreteDataModule(bs, 0) ) columns = uci_concrete_columns elif experiment_name == UCI_WINE_RED: dm = ( UCIWineRedDataModuleShifted( bs, 0, shifting_proportion_total=sp_tot, shifting_proportion_k=sp_k ) if SHIFTED else UCIWineRedDataModule(bs, 0) ) columns = uci_wine_red_columns elif experiment_name == UCI_WINE_WHITE: dm = ( UCIWineWhiteDataModuleShifted( bs, 0, shifting_proportion_total=sp_tot, shifting_proportion_k=sp_k ) if SHIFTED else UCIWineWhiteDataModule(bs, 0) ) columns = uci_wine_white_columns elif experiment_name == UCI_YACHT: dm = ( UCIYachtDataModuleShifted( bs, 0, shifting_proportion_total=sp_tot, shifting_proportion_k=sp_k ) if SHIFTED else UCIYachtDataModule(bs, 0) ) # dm = UCIYachtDataModuleShiftedSplit(bs, 0) columns = uci_yacht_columns dm.setup() # Extract data train = next(iter(dm.train_dataloader())) val = next(iter(dm.val_dataloader())) test = next(iter(dm.test_dataloader())) print( f"N_train={len(dm.train_dataset)}, N_val={len(dm.val_dataset)}, N_test={len(dm.test_dataset)}" ) # 1 = train, 2 = val, 3 = test df_columns = columns + ["dataset"] df = pd.DataFrame(columns=df_columns) if TEST_FIRST: dataset_order = [test, val, train] dataset_names = ["test", "val", "train"] else: dataset_order = [train, val, test] dataset_names = ["train", "val", "test"] for idx_ds, ds in enumerate(dataset_order): x, y = ds dump = np.concatenate( (x.numpy(), y.numpy(), idx_ds * torch.ones_like(y).numpy()), axis=1 ) update_df = pd.DataFrame(dump, columns=df_columns) df = df.append(update_df, ignore_index=True) # correct dataset name df["dataset"] = df["dataset"].map({i: v for i, v in enumerate(dataset_names)}) sns.pairplot( df, hue="dataset", palette=sns.color_palette("Set2", len(dataset_names)) ) if with_pca: pca = PCA(n_components=5) pca.fit(df.values[:-1]) print(pca.explained_variance_ratio_) print(pca.singular_values_) pca_x = pca.transform(df.values[:-1]) fig, ax = plt.subplots(1, 1) ax.scatter(pca_x[:, 0], pca_x[:, 1]) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( "--experiment_name", type=str, required=True, choices=[ UCI_BOSTON, UCI_CONCRETE, UCI_CCPP, UCI_SUPERCONDUCT, UCI_WINE_RED, UCI_WINE_WHITE, UCI_YACHT, ], ) args = parser.parse_args() main(args.experiment_name) plt.show()
python
import PySimpleGUI as sg from NetLogoDOE.src.gui.custom_components import title, question_mark_button from NetLogoDOE.src.gui.custom_windows import show_help_window from NetLogoDOE.src.gui.help_dictionary import help_text class StandardResultsScreen: def __init__(self): button_size = (30, 1) button_pad = ((5, 5), (20, 5)) self.layout = [[title("Reporter value analysis results")], [sg.Frame(title='Plots', border_width=1, relief='solid', layout= [[sg.Button('Timeseries', key='standard_results_timeseries_button', size=button_size, pad=button_pad), question_mark_button('standard_results_timeseries_help_button', padding=button_pad)], [sg.Button('Boxplot', key='standard_results_boxplot_button', size=button_size, pad=button_pad), question_mark_button('standard_results_boxplot_help_button', padding=button_pad)], [sg.Button('Violin plot', key='standard_results_violinplot_button', size=button_size, pad=button_pad), question_mark_button('standard_results_violinplot_help_button', padding=button_pad)], [sg.Button('Histogram', key='standard_results_histogram_button', size=button_size, pad=button_pad), question_mark_button('standard_results_histogram_help_button', padding=button_pad)], [sg.Button('Distribution plot', key='standard_results_distplot_button', size=button_size, pad=button_pad), question_mark_button('standard_results_distplot_help_button', padding=button_pad)]])], [sg.Button('Experiment Configuration Information', key='standard_results_configtable_button', size=button_size, pad=button_pad), question_mark_button('standard_results_configtable_help_button', padding=button_pad)], [sg.Input(key='standard_results_dummy_export', enable_events=True, visible=False, size=(0, 0)), sg.SaveAs('Save Results', file_types=[("Text Files", "*.txt")], target='standard_results_dummy_export', key="standard_results_save_button", size=button_size, pad=button_pad), question_mark_button('standard_results_save_help_button', padding=button_pad)], [sg.Button('Back to main menu', key='standard_results_back_button', pad=button_pad)]] self.results = None def check_events(self, event, values, window): if event == 'standard_write_results_event': self.results = values['standard_write_results_event'] if event == 'standard_results_configtable_button': window['standard_result_panel'].update(visible=False) window['standard_configtable_panel'].update(visible=True) if event == 'standard_results_timeseries_button': window['standard_result_panel'].update(visible=False) window['timeseries_panel'].update(visible=True) if event == 'standard_results_boxplot_button': window['standard_result_panel'].update(visible=False) window['boxplot_panel'].update(visible=True) if event == 'standard_results_violinplot_button': window['standard_result_panel'].update(visible=False) window['violinplot_panel'].update(visible=True) if event == 'standard_results_histogram_button': window['standard_result_panel'].update(visible=False) window['histogram_panel'].update(visible=True) if event == 'standard_results_distplot_button': window['standard_result_panel'].update(visible=False) window['distplot_panel'].update(visible=True) if event == 'standard_results_dummy_export' and not (values['standard_results_dummy_export'] == ''): self.export_standard_results(values, values['standard_results_dummy_export']) if event == 'standard_results_back_button': window['standard_result_panel'].update(visible=False) window['main_panel'].update(visible=True) # Help events if event == 'standard_results_configtable_help_button': show_help_window(help_text['config_information'], location=(window.CurrentLocation()[0] - ((434 - window.size[0]) / 2), window.CurrentLocation()[1] + 100)) if event == 'standard_results_timeseries_help_button': show_help_window(help_text['timeseries'], location=(window.CurrentLocation()[0] - ((434 - window.size[0]) / 2), window.CurrentLocation()[1] + 100)) if event == 'standard_results_boxplot_help_button': show_help_window(help_text['boxplot'], location=(window.CurrentLocation()[0] - ((434 - window.size[0]) / 2), window.CurrentLocation()[1] + 100)) if event == 'standard_results_violinplot_help_button': show_help_window(help_text['violinplot'], location=(window.CurrentLocation()[0] - ((434 - window.size[0]) / 2), window.CurrentLocation()[1] + 100)) if event == 'standard_results_histogram_help_button': show_help_window(help_text['histogram'], location=(window.CurrentLocation()[0] - ((434 - window.size[0]) / 2), window.CurrentLocation()[1] + 100)) if event == 'standard_results_distplot_help_button': show_help_window(help_text['distributionplot'], location=(window.CurrentLocation()[0] - ((434 - window.size[0]) / 2), window.CurrentLocation()[1] + 100)) if event == 'standard_results_save_help_button': show_help_window(help_text['save_results'], location=(window.CurrentLocation()[0] - ((434 - window.size[0]) / 2), window.CurrentLocation()[1] + 100)) def export_standard_results(self, values, file_path): results_dict = {} results_dict['Configuration'] = self.results[2] results_dict['Parameter settings'] = self.results[0] results_dict['Reporter values'] = self.results[1] f = open(file_path, "w") f.write(str(results_dict)) f.close()
python
from models.gru_net import GRUNet from models.res_gru_net import ResidualGRUNet from models.multi_res_gru_net import MultiResidualGRUNet from models.multi_seres_gru_net import MultiSEResidualGRUNet from models.multi_res2d3d_gru_net import MultiResidual2D3DGRUNet from models.multi_seres2d3d_gru_net import MultiSEResidual2D3DGRUNet MODELS = (GRUNet, ResidualGRUNet, MultiResidualGRUNet, MultiSEResidualGRUNet, MultiResidual2D3DGRUNet, MultiSEResidual2D3DGRUNet) def get_models(): '''Returns a tuple of sample models.''' return MODELS def load_model(name): '''Creates and returns an instance of the model given its class name. The created model has a single placeholder node for feeding images. ''' # Find the model class from its name all_models = get_models() mdict = {model.__name__: model for model in all_models} if name not in mdict: print('Invalid model index. Options are:') # Display a list of valid model names for model in all_models: print('\t* {}'.format(model.__name__)) return None NetClass = mdict[name] return NetClass
python
# programa que solicita uma temperatura em graus Farenheit e converte para graus Celsius # solicita a temperatura em graus Farenheit temperatura_farenheit = float(input("Informe a temperatura em graus Farenheit: ")) # Converte a temperatura de Farenheit para Celsius temperatura_celsius = (5 * (temperatura_farenheit - 32) / 9) # apresenta o resultado da conversão print(temperatura_farenheit, " graus Farenheit corresponde a ", temperatura_celsius, " graus Celsius.")
python
import math import discord import mail import os client = discord.Client() env = os.environ DISCORD_TOKEN = env.get("DISCORD_TOKEN") CHANS = env.get("DISCORD_CHANS") if CHANS: CHANS = list(map(lambda x: int(x), CHANS.split(","))) else: CHANS = [] @client.event async def on_ready(): print("Logged in") @client.event async def on_message(message): if message.channel.id not in CHANS: return text = message.content if len(text) > 0: remote_attachments = list(map(lambda x: x.url, message.attachments)) print(text) print(remote_attachments) mail.send_email(text, remote_attachments) client.run(DISCORD_TOKEN)
python
# # This file is part of Python Client Library for STAC. # Copyright (C) 2019 INPE. # # Python Client Library for STAC is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. # """Utility data structures and algorithms.""" import json import pkg_resources from jsonschema import validate, RefResolver import requests import os resource_package = __name__ try: schema_path = 'file:///{0}/'.format( os.path.dirname(pkg_resources.resource_filename('stac.utils', 'jsonschemas/0.8.0/catalog.json'))) catalog_schema = json.loads(pkg_resources.resource_string(resource_package, f'jsonschemas/0.8.0/catalog.json')) collection_schema = json.loads(pkg_resources.resource_string(resource_package, f'jsonschemas/0.8.0/collection.json')) item_schema = json.loads(pkg_resources.resource_string(resource_package, f'jsonschemas/0.8.0/item.json')) item_collection_schema = json.loads(pkg_resources.resource_string(resource_package, f'jsonschemas/0.8.0/itemcollection.json')) except Exception as e: raise Exception(f'Error while loading validation schemas: {e}') class Utils: """Utils STAC object.""" @staticmethod def _get(url, params=None): """Query the STAC service using HTTP GET verb and return the result as a JSON document. :param url: The URL to query must be a valid STAC endpoint. :type url: str :param params: (optional) Dictionary, list of tuples or bytes to send in the query string for the underlying `Requests`. :type params: dict :rtype: dict :raises ValueError: If the response body does not contain a valid json. """ response = requests.get(url, params=params) response.raise_for_status() content_type = response.headers.get('content-type') if content_type not in ('application/json', 'application/geo+json'): raise ValueError('HTTP response is not JSON: Content-Type: {}'.format(content_type)) return response.json() class Link(dict): """Link object.""" def __init__(self, data): """Initialize instance with dictionary data. :param data: Dict with Link metadata. """ super(Link, self).__init__(data or {}) @property def rel(self): """:return: the Link relation.""" return self['rel'] @property def href(self): """:return: the Link url.""" return self['href'] @property def type(self): """:return: the type of the Link object.""" return self['type'] @property def title(self): """:return: the title of the Link object.""" return self['title'] class Extent(dict): """The Extent object.""" def __init__(self, data): """Initialize instance with dictionary data. :param data: Dict with Extent metadata. """ super(Extent, self).__init__(data or {}) @property def spatial(self): """:return: the spatial extent.""" return self['spatial'] @property def temporal(self): """:return: the temporal extent.""" return self['temporal'] class Provider(dict): """The Provider Object.""" def __init__(self, data): """Initialize instance with dictionary data. :param data: Dict with Provider metadata. """ super(Provider, self).__init__(data or {}) @property def name(self): """:return: the Provider name.""" return self['name'] @property def description(self): """:return: the Provider description.""" return self['description'] @property def roles(self): """:return: the Provider roles.""" return self['description'] @property def url(self): """:return: the Provider url.""" return self['url'] class Catalog(dict): """The STAC Catalog.""" def __init__(self, data, validation=False): """Initialize instance with dictionary data. :param data: Dict with catalog metadata. :param validation: True if the Catalog must be validated. (Default is False) """ if validation: validate(data, schema=catalog_schema) super(Catalog, self).__init__(data or {}) @property def stac_version(self): """:return: the STAC version.""" return self['stac_version'] @property def stac_extensions(self): """:return: the STAC extensions.""" return self['stac_extensions'] @property def id(self): """:return: the catalog identifier.""" return self['id'] @property def title(self): """:return: the catalog title.""" return self['title'] if 'title' in self else None @property def description(self): """:return: the catalog description.""" return self['description'] @property def summaries(self): """:return: the catalog summaries.""" return self['summaries'] @property def links(self): """:return: a list of resources in the catalog.""" return self['links'] class Collection(Catalog): """The STAC Collection.""" def __init__(self, data, validation=False): """Initialize instance with dictionary data. :param data: Dict with collection metadata. :param validation: True if the Collection must be validated. (Default is False) """ if validation: validate(data, schema=collection_schema, resolver=RefResolver(schema_path, collection_schema)) super(Collection, self).__init__(data or {}) @property def keywords(self): """:return: the Collection list of keywords.""" return self['keywords'] @property def version(self): """:return: the Collection version.""" return self['version'] @property def license(self): """:return: the Collection license.""" return self['license'] @property def providers(self): """:return: the Collection list of providers.""" return self['providers'] @property def extent(self): """:return: the Collection extent.""" return self['extent'] @property def properties(self): """:return: the Collection properties.""" return self['properties'] def items(self, filter=None): """:return: the Collection list of items.""" for link in self['links']: if link['rel'] == 'items': data = Utils._get(link['href'], params=filter) return ItemCollection(data) return ItemCollection({}) class Geometry(dict): """The Geometry Object.""" def __init__(self, data): """Initialize instance with dictionary data. :param data: Dict with Geometry metadata. """ super(Geometry, self).__init__(data or {}) @property def type(self): """:return: the Geometry type.""" return self['type'] @property def coordinates(self): """:return: the Geometry coordinates.""" return self['coordinates'] class Item(dict): """The GeoJSON Feature of a STAC Item.""" def __init__(self, data, validation=False): """Initialize instance with dictionary data. :param data: Dict with Item metadata. :param validation: True if the Item must be validated. (Default is False) """ if validation: validate(data, schema=item_schema, ) super(Item, self).__init__(data or {}) @property def stac_version(self): """:return: the STAC version.""" return self['stac_version'] @property def stac_extensions(self): """:return: the STAC extensions.""" return self['stac_extensions'] @property def id(self): """:return: the Item identifier.""" return self['id'] @property def type(self): """:return: the Item type.""" return self['type'] @property def bbox(self): """:return: the Item Bounding Box.""" return self['bbox'] @property def collection(self): """:return: the Item Collection.""" return self['collection'] @property def geometry(self): """:return: the Item Geometry.""" return self['geometry'] @property def properties(self): """:return: the Item properties.""" return self['properties'] @property def links(self): """:return: the Item related links.""" return self['links'] @property def assets(self): """:return: the Item related assets.""" return self['assets'] class ItemCollection(dict): """The GeoJSON Feature Collection of STAC Items.""" def __init__(self, data, validation=False): """Initialize instance with dictionary data. :param data: Dict with Item Collection metadata. :param validation: True if the Item Collection must be validated. (Default is False) """ if validation: validate(data, schema=item_collection_schema, resolver=RefResolver(schema_path, item_collection_schema)) super(ItemCollection, self).__init__(data or {}) @property def type(self): """:return: the Item Collection type.""" return self['type'] @property def features(self): """:return: the Item Collection list of GeoJSON Features.""" return [Item(i) for i in self['features']]
python
import random import re from abc import ABC, abstractmethod from datetime import datetime from pathlib import Path from typing import Dict, List from itertools import groupby, chain import pandas as pd from models import ListNews, News, Cluster from sqlalchemy.orm import Session from utils import convert_str_to_date from config import LIMIT_NEWS from db_lib import crud from db_lib.database import SessionLocal from statistics import NgramsBuilder, StatisticsByResource, ByDayCounter, CategoriesStatistics class BaseNewsExtractor(ABC): """ Предполагается использовать этот класс как прародитель для всех остальных при обращении к разным источникам данных """ @abstractmethod def show_random_news(self, db: Session, num_random_news: int) -> ListNews: """ Метод для показа нескольких случаных новостей """ pass @abstractmethod def show_news_by_days(self, db: Session, start_date: str, end_date: str): """ Метод для показа новостей за конкретный день """ pass @abstractmethod def show_news_by_topic( self, db: Session, topic: str, start_date: str, end_date: str ): """ Метод для показа новостей по определённой теме """ pass @abstractmethod def show_news_by_filters(self, db: Session, topic: str, end_date: str, start_date: str, num_random_news: int): """ Метод для показа новостей по заданным фильтрам """ pass @abstractmethod def show_clusters_by_filters(self, db: Session, topic: str, end_date: str, start_date: str, num_news: int = 10): """ Метод для кластеров новостей по заданным фильтрам """ pass @abstractmethod def show_cluster_by_id(self, db: Session, cluster_id): """ Метод для показа новостей по кластеру """ pass @abstractmethod def show_news_by_regex(self, db: Session, word: str, mode: str, cnt: int): """ Метод для поиска новостей по регулярному выражению """ pass @abstractmethod def show_single_news(self, db: Session, news_id: int): """ Метод для показа новости по id """ pass @abstractmethod def show_posts_by_filters(self, db: Session, end_date: str, start_date: str, num_news: int): """ Метод для вывода последних постов с филтрацией """ pass @abstractmethod def show_last_posts(self, db: Session, num: int): """ Метод для вывода последних постов """ pass @abstractmethod def show_vk_tg_news(self, db, news_id): pass @abstractmethod def show_vk_tg_stat(self, db, post_id, social_network): pass class PandasNewsExtractor(BaseNewsExtractor): def __init__(self, path_to_df: Path): self.source_df = pd.read_csv(path_to_df, parse_dates=['date']) self.source_df['date'] = self.source_df['date'].map(lambda x: x.date()) def show_random_news(self, num_random_news: int = 10, **kwargs) -> ListNews: df_random = self.source_df.sample(n=num_random_news) news_list = self._convert_df_to_list_news(df_random) return news_list def show_news_by_days( self, start_date: str = '1991-05-12', end_date: str = '1991-05-12', **kwargs, ) -> ListNews: start_date = convert_str_to_date(start_date) end_date = convert_str_to_date(end_date) df_date = self.source_df[ (self.source_df['date'] >= start_date) & (self.source_df['date'] <= end_date) ] news_list = self._convert_df_to_list_news(df_date) return news_list def show_news_by_topic( self, topic: str = 'Футбол', start_date: str = '1991-05-12', end_date: str = '1991-05-12', **kwargs, ) -> ListNews: start_date = convert_str_to_date(start_date) end_date = convert_str_to_date(end_date) df_topic = self.source_df[ (self.source_df['topic'] == topic) & (self.source_df['date'] >= start_date) & (self.source_df['date'] <= end_date) ] news_list = self._convert_df_to_list_news(df_topic) return news_list def _convert_df_to_list_news(self, selected_df: pd.DataFrame) -> ListNews: news_list = [None] * len(selected_df) for i, (_, row) in enumerate(selected_df.iterrows()): news_list[i] = self._convert_row_to_news(row) news_list_dict = {'news_list': news_list} # TODO here one can add interesting statistics news_list_dict['statistics'] = None return ListNews(**news_list_dict) @staticmethod def _convert_row_to_news(row) -> News: news_dict = { 'source_url': row['url'], 'title': row['title'], 'content': row['text'], 'topic': row['topic'], 'tags': row['tags'], 'date': row['date'], 'time': datetime.combine(row['date'], datetime.min.time()), } return News(**news_dict) class DBNewsExtractor(BaseNewsExtractor): def get_db(self): db = SessionLocal() try: yield db finally: db.close() def show_random_news(self, db: Session, num_random_news: int = 10) -> ListNews: news_list = random.choices(crud.get_all_news(db), k=num_random_news) return ListNews( **{'news_list': news_list, 'statistics': [ NgramsBuilder().predict(news_list, ) ]} ) def show_news_by_days( self, db, start_date: str = '1991-05-12', end_date: str = '1991-05-12' ) -> ListNews: news_list = crud.get_news_by_date( db, convert_str_to_date(start_date), convert_str_to_date(end_date) ) return ListNews( **{'news_list': news_list, 'statistics': [ NgramsBuilder().predict(news_list) ]} ) def show_news_by_topic( self, db, topic: str = 'Футбол', start_date: str = '1991-05-12', end_date: str = '1991-05-12', ) -> ListNews: news_list = crud.get_news_by_topic_and_date( db, topic, convert_str_to_date(start_date), convert_str_to_date(end_date) ) return ListNews( **{'news_list': news_list, 'statistics': [ NgramsBuilder().predict(news_list, ) ]} ) def _clusters_from_news(self, news_list: List[News]) -> List[Cluster]: news_list.sort(key=lambda x: x.cluster_num, reverse=True) return [ Cluster.parse_obj( { 'cluster_id': key, 'news': list(group_news), 'topic': list(set([n.category for n in list(group_news) if n.category])), 'tags': list(set(chain(*[n.tags for n in list(group_news) if n.tags]))), 'statistics': [] } ) for key, group_news in groupby(news_list, lambda news: news.cluster_num) ] def show_clusters_by_filters(self, db: Session, topic: str, end_date: str, start_date: str = '1991-05-12', num_news: int = 10) -> Dict: news_list = crud.get_news_by_filters_with_cluster(db, topic=topic, start_date=convert_str_to_date(start_date), end_date=convert_str_to_date(end_date), limit=num_news * 20) cluster_nums = [n.cluster_num for n in news_list] news_list = crud.get_news_in_clusters(db, cluster_nums) news_list = _clean_img_urls(news_list) news_list = _json_tags_to_list(news_list) clusters = self._clusters_from_news(news_list)[:num_news] return { 'clusters': clusters, 'statistics': [] } def show_cluster_by_id(self, db: Session, cluster_id) -> Dict: news_list = crud.get_news_by_cluster_id(db, cluster_id) news_list = _clean_img_urls(news_list) news_list = _json_tags_to_list(news_list) cluster = { 'cluster_id': cluster_id, 'news': news_list, 'topic': list(set([n.category for n in news_list if n.category])), 'tags': list(set(chain(*[n.tags for n in news_list if n.tags]))), 'statistics': [NgramsBuilder().predict(news_list)] } return cluster def show_news_by_filters( self, db: Session, topic: str, end_date: str, start_date: str = '1991-05-12', num_news: int = 10, ) -> ListNews: news_list = crud.get_news_by_filters(db, topic=topic, start_date=convert_str_to_date(start_date), end_date=convert_str_to_date(end_date), limit=num_news) # news_list_len = len(news_list) # if num_news > news_list_len: # num_news = news_list_len # news_list = random.choices(news_list, k=num_random_news) news_list = _clean_img_urls(news_list) news_list = _json_tags_to_list(news_list) # Не менять порядок в statistics return ListNews.parse_obj( { 'news_list': news_list, 'statistics': [ NgramsBuilder().predict(news_list), # StatisticsByResource().predict(news_list), # ByDayCounter().predict(news_list), # CategoriesStatistics().predict(news_list), ] } ) def show_news_by_regex(self, db: Session, word: str, mode: str = 'full', cnt: int = 2) -> ListNews: if word: news_list = crud.get_n_last_news(db, limit=LIMIT_NEWS) else: news_list = crud.get_all_news(db, limit=0) word_re = rf'\b{word}\b' if mode == 'full': selected_news = [ one_news for one_news in news_list if re.search(word_re, _one_news_to_string(one_news), flags=re.IGNORECASE) ] else: news_list = clean_nones_from_content(news_list) selected_news = [ one_news for one_news in news_list if re.search(word_re, str(one_news.content), flags=re.IGNORECASE) ] selected_news = _clean_img_urls(selected_news) selected_news = _json_tags_to_list(selected_news) # Не менять порядок в statistics return ListNews.parse_obj( { 'news_list': selected_news, 'statistics': [ NgramsBuilder().predict(selected_news, cnt), StatisticsByResource().predict(selected_news), ByDayCounter().predict(selected_news), # CategoriesStatistics().predict(selected_news), ] } ) def show_single_news(self, db: Session, news_id: int) -> Dict: single_news = crud.get_single_news(db, news_id) single_news.image_url = _remove_extra_link(single_news.image_url) if single_news.tags: single_news.tags = single_news.tags.lstrip('{').rstrip('}').replace('"', '').split(',') return { 'single_news': single_news, } def show_posts_by_filters(self, db: Session, end_date: str, start_date: str = '1991-05-12', num: int = 100) -> Dict: vk_tg_news_list = crud.get_social_network_news_list_by_filters(db, convert_str_to_date(start_date), convert_str_to_date(end_date), num) return { 'news_list': vk_tg_news_list, } def show_last_posts(self, db: Session, num: int) -> Dict: vk_tg_news_list = crud.get_social_network_news_list(db, num) return { 'news_list': vk_tg_news_list, } def show_vk_tg_news(self, db: Session, news_id: int) -> Dict: vk_tg_news = crud.get_social_network_news(db, news_id) return { 'single_news': vk_tg_news, } def show_vk_tg_stat(self, db: Session, post_id: int, social_network: str): vk_tg_stat = crud.get_social_network_stats(db, post_id, social_network) # List[SocialNetworkStats] return vk_tg_stat def _to_str(text): return '' if text is None else str(text) def _one_news_to_string(one_news: News) -> str: return _to_str(one_news.title) + ' ' + _to_str(one_news.content) def clean_nones_from_content(news_list: List[News]) -> List[News]: for i, news in enumerate(news_list): if news.content is None: news_list[i].content = news.title return news_list def _clean_img_urls(news_list: List[News]) -> List[News]: for i, news in enumerate(news_list): news_list[i].image_url = _remove_extra_link(news_list[i].image_url) return news_list def _json_tags_to_list(news_list: List[News]) -> List[News]: for i, news in enumerate(news_list): if news_list[i].tags and not isinstance(news_list[i].tags, list): news_list[i].tags = news_list[i].tags \ .lstrip('{').rstrip('}') \ .replace('"', '').replace('&laquo', '').replace('&raquo', '') \ .replace('[', '').replace(']', '').replace("'", "") \ .split(',') return news_list def _remove_extra_link(links: str) -> str: if links: return links.lstrip('{').rstrip('}').split(',')[0]
python
import sys import io from arc import color, CLI def test_colorize(): colored = color.colorize("test", color.fg.RED) assert colored.startswith(str(color.fg.RED)) assert colored.endswith(str(color.effects.CLEAR)) colored = color.colorize("test", color.fg.RED, clear=False) assert colored.startswith(str(color.fg.RED)) assert not colored.endswith(str(color.effects.CLEAR)) # Because StringIO is not terminal-like, escape-sequnces will be removed def test_output(cli: CLI): try: stdout = sys.stdout fake = io.StringIO() sys.stdout = fake @cli.command() def test(): print(f"{color.fg.GREEN}green!{color.effects.CLEAR}") cli("test") fake.seek(0) assert fake.read() == "green!\n" finally: sys.stdout = stdout
python
# Copyright 2013 Google Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS-IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """MapReduce extensions for ETL.""" __author__ = [ '[email protected] (John Cox)', '[email protected] (Julia Oh)', ] import csv import os import sys from xml.etree import ElementTree from models import transforms import mrs from tools.etl import etl_lib class MapReduceJob(etl_lib.Job): """Parent classes for custom jobs that run a mapreduce. Usage: python etl.py run path.to.my.job / appid server.appspot.com \ --disable_remote \ --job_args='path_to_input_file path_to_output_directory' """ # Subclass of mrs.MapReduce; override in child. MAPREDUCE_CLASS = None def _configure_parser(self): """Shim that works with the arg parser expected by mrs.Mapreduce.""" self.parser.add_argument( 'file', help='Absolute path of the input file', type=str) self.parser.add_argument( 'output', help='Absolute path of the output directory', type=str) def main(self): if not os.path.exists(self.args.file): sys.exit('Input file %s not found' % self.args.file) if not os.path.exists(self.args.output): sys.exit('Output directory %s not found' % self.args.output) mrs.main(self.MAPREDUCE_CLASS, args=self._parsed_etl_args.job_args) class MapReduceBase(mrs.MapReduce): """Common functionalities of MR jobs combined into one class.""" def json_parse(self, value): """Parses JSON file into Python.""" if value.strip()[-1] == ',': value = value.strip()[:-1] try: return transforms.loads(value) # Skip unparseable rows like the first and last # pylint: disable=bare-except except: return None def make_reduce_data(self, job, interm_data): """Change the outout format to JSON.""" outdir = self.output_dir() output_data = job.reduce_data( interm_data, self.reduce, outdir=outdir, format=JsonWriter) return output_data class JsonWriter(mrs.fileformats.Writer): """Outputs one JSON literal per line. Example JSON output may look like: {'foo': 123, 'bar': 456, 'quz': 789} {'foo': 321, 'bar': 654, 'quz': 987} . . . {'foo': 456, 'bar': 534, 'quz': 154} """ ext = 'json' def __init__(self, fileobj, *args, **kwds): super(JsonWriter, self).__init__(fileobj, *args, **kwds) def _write_json(self, write_fn, python_object): """Writes serialized JSON representation of python_object to file. Args: write_fn: Python file object write() method. python_object: object. Contents to write. Must be JSON-serializable. Raises: TypeError: if python_object is not a dict or a list. """ if isinstance(python_object, dict): write_fn(unicode( transforms.dumps(python_object) + '\n').encode('utf-8')) elif isinstance(python_object, list): for item in python_object: self._write_json(write_fn, item) else: raise TypeError('Value must be a dict or a list of dicts.') def writepair(self, kvpair, **unused_kwds): unused_key, value = kvpair self._write_json(self.fileobj.write, value) class Histogram(object): """Histogram that bins values into _bucket_size sized intervals.""" # Int. Number of consecutive zeros in list of integer values to determine # the cutoff point. _NUM_ZEROS = 3 def __init__(self, bucket_size): # Map of 0-indexed bin #int -> count int self._values = {} self._bucket_size = bucket_size def add(self, value): """Adds value into self._values.""" bin_number = self._get_bin_number(value) self._increment_bin(bin_number) def _get_bin_number(self, value): """Returns appropriate bin number for given value.""" if value < 0: raise ValueError('Cannot calculate index for negative value') return max(0, (value - 1) // self._bucket_size) def _increment_bin(self, n): self._values[n] = self._values.get(n, 0) + 1 def to_list(self): """Returns self._values converted into a list, sorted by its keys.""" try: max_key = max(self._values.iterkeys()) return [self._values.get(n, 0) for n in xrange(0, max_key+1)] except ValueError: return [] def to_noise_filtered_list(self): """Converts self._values to a list with junk data removed. Returns: self.to_list(), with junk data removed "Junk data" refers to noise in EventEntity data caused by API misbehaviors and certain user behavior. Two known issues are: 1. Youtube video data from event source 'tag-youtube-video' and 'tag-youtube-milestone' represent user engagement at certain playhead positions. Youtube API continues to emit these values even when the video has stopped playing, causing a trail of meaningless values in the histogram. 2. Data from event source 'visit-page' logs duration of a page visit. If a user keeps the browser open and goes idle, the duration value recorded is skewed since the user wasn't engaged. These values tend to be significantly larger than more reliable duration values. This method filters the long trail of insignificant data by counting number of consecutive zeros set in self._NUM_ZEROS and disregarding any data after the zeros. Example: self.to_list() returns [1, 2, 3, 4, 5, 0, 0, 0, 0, 1] _NUM_ZEROS = 3 output = [1, 2, 3, 4, 5] """ zero_counts = 0 cutoff_index = 0 values = self.to_list() for index, value in enumerate(values): if value == 0: zero_counts += 1 if zero_counts == 1: cutoff_index = index if zero_counts == self._NUM_ZEROS: return values[:cutoff_index] else: cutoff_index = 0 zero_counts = 0 return values class XmlWriter(mrs.fileformats.Writer): """Writes file in XML format. The writer does not use the key from kvpair and expects the value to be a list of string representation of XML elements. Example: kvpair: some_key, ['<row><name>Jane</name></row>', '<row><name>John</name></row>'] Output: <rows> <row> <name>Jane</name> </row> <row> <name>John</name> </row> </rows> """ ext = 'xml' def __init__(self, fileobj, *args, **kwds): super(XmlWriter, self).__init__(fileobj, *args, **kwds) self.fileobj.write('<rows>') def writepair(self, kvpair, **unused_kwds): unused_key, values = kvpair write = self.fileobj.write for value in values: write(value) write('\n') def finish(self): self.fileobj.write('</rows>') self.fileobj.flush() class XmlGenerator(MapReduceBase): """Generates a XML file from a JSON formatted input file.""" def map(self, key, value): """Converts JSON object to xml. Args: key: int. line number of the value in Entity file. value: str. A line of JSON literal extracted from Entity file. Yields: A tuple with the string 'key' and a tuple containing line number and string representaiton of the XML element. """ json = self.json_parse(value) if json: root = ElementTree.Element('row') transforms.convert_dict_to_xml(root, json) yield 'key', (key, ElementTree.tostring(root, encoding='utf-8')) def reduce(self, unused_key, values): """Sorts the values by line number to keep the order of the document. Args: unused_key: str. The arbitrary string 'key' set to accumulate all values under one key. values: list of tuples. Each tuple contains line number and JSON literal converted to XML string. Yields: A list of XML strings sorted by the line number. """ sorted_values = sorted(values, key=lambda x: x[0]) yield [value[1] for value in sorted_values] def make_reduce_data(self, job, interm_data): """Change the outout format to XML.""" outdir = self.output_dir() output_data = job.reduce_data( interm_data, self.reduce, outdir=outdir, format=XmlWriter) return output_data class JsonToXml(MapReduceJob): """MapReduce Job that converts JSON formatted Entity files to XML. Usage: run the following command from the app root folder. python tools/etl/etl.py run tools.etl.mapreduce.JsonToXml \ /coursename appid server.appspot.com \ --job_args='path_to_any_Entity_file path_to_output_directory' """ MAPREDUCE_CLASS = XmlGenerator class CsvWriter(mrs.fileformats.Writer): """Writes file in CSV format. The default value to be written if the dictionary is missing a key is an empty string. Example: kvpair: (some_key, (['bar', 'foo', 'quz'], [{'foo': 1, 'bar': 2, 'quz': 3}, {'bar': 2, 'foo': 3}]) Output: 'bar', 'foo', 'quz' 2, 1, 3 2, 3, '' """ ext = 'csv' def __init__(self, fileobj, *args, **kwds): super(CsvWriter, self).__init__(fileobj, *args, **kwds) def writepair(self, kvpair, **unused_kwds): """Writes list of JSON objects to CSV format. Args: kvpair: tuple of unused_key, and a tuple of master_list and json_list. Master_list is a list that contains all the fieldnames across json_list sorted in alphabetical order, and json_list is a list of JSON objects. **unused_kwds: keyword args that won't be used. """ unused_key, (master_list, json_list) = kvpair writer = csv.DictWriter( self.fileobj, fieldnames=master_list, restval='') writer.writeheader() writer.writerows(json_list) class CsvGenerator(MapReduceBase): """Generates a CSV file from a JSON formatted input file.""" @classmethod def _flatten_json(cls, json, prefix=''): """Flattens given JSON object and encodes all the values in utf-8.""" for k, v in json.items(): try: if type(transforms.loads(v)) == dict: flattened = cls._flatten_json( transforms.loads(json.pop(k)), prefix=prefix + k + '_') json.update(flattened) # pylint: disable=bare-except except: json[prefix + k] = unicode(json.pop(k)).encode('utf-8') return json def map(self, unused_key, value): """Loads JSON object and flattens it. Example: json['data']['foo'] = 'bar' -> json['data_foo'] = 'bar', with json['data'] removed. Args: unused_key: int. line number of the value in Entity file. value: str. instance of Entity file extracted from file. Yields: A tuple of string key and flattened dictionary. map() outputs constant string 'key' as the key so that all the values can be accumulated under one key in reduce(). This accumulation is necessary because reduce() must go through the list of all JSON literals and determine all existing fieldnames. Then, reduce() supplies the master_list of fieldnames to CSVWriter's writepair() which uses the list as csv header. """ json = self.json_parse(value) if json: json = CsvGenerator._flatten_json(json) yield 'key', json def reduce(self, unused_key, values): """Creates a master_list of all the keys present in an Entity file. Args: unused_key: str. constant string 'key' emitted by map(). values: a generator over list of json objects. Yields: A tuple of master_list and list of json objects. master_list is a list of all keys present across every json object. This list is used to create header for CSV files. """ master_list = [] values = [value for value in values] for value in values: for key in value: if key not in master_list: master_list.append(key) try: # Convert integer keys from unicode to ints to be sorted correctly. # pylint: disable-msg=unnecessary-lambda master_list = sorted(master_list, key=lambda item: int(item)) except ValueError: # String keys cannot be converted into integers.. master_list = sorted(master_list) yield master_list, values def make_reduce_data(self, job, interm_data): """Set the output data format to CSV.""" outdir = self.output_dir() output_data = job.reduce_data( interm_data, self.reduce, outdir=outdir, format=CsvWriter) return output_data class JsonToCsv(MapReduceJob): """MapReduce Job that converts JSON formatted Entity files to CSV format. Usage: run the following command from the app root folder. python tools/etl/etl.py run tools.etl.mapreduce.JsonToCSV /coursename appid server.appspot.com \ --job_args='path_to_an_Entity_file path_to_output_directory' """ MAPREDUCE_CLASS = CsvGenerator mrs.fileformats.writer_map['csv'] = CsvWriter mrs.fileformats.writer_map['json'] = JsonWriter mrs.fileformats.writer_map['xml'] = XmlWriter
python
import numpy as np import lmdb import caffe N = 1000 # Test Data X = np.zeros((N, 3, 32, 32), dtype=np.uint8) y = np.zeros(N, dtype=np.int64) # We need to prepare the database for the size. We'll set it 10 times # greater than what we theoretically need. There is little drawback to # setting this too big. If you still run into problem after raising # this, you might want to try saving fewer entries in a single # transaction. map_size = X.nbytes * 10 env = lmdb.open('mylmdb', map_size=map_size) with env.begin(write=True) as txn: # txn is a Transaction object for i in range(N): datum = caffe.proto.caffe_pb2.Datum() datum.channels = X.shape[1] datum.height = X.shape[2] datum.width = X.shape[3] datum.data = X[i].tobytes() # or .tostring() if numpy < 1.9 datum.label = int(y[i]) str_id = '{:08}'.format(i) # The encode is only essential in Python 3 txn.put(str_id.encode('ascii'), datum.SerializeToString())
python
import sys from glob import glob from os.path import join, dirname from kivy.uix.scatter import Scatter from kivy.app import App from kivy.graphics.svg import Svg from kivy.core.window import Window from kivy.uix.floatlayout import FloatLayout from kivy.lang import Builder Builder.load_string(""" <SvgWidget>: do_rotation: False <FloatLayout>: canvas.before: Color: rgb: (1, 1, 1) Rectangle: pos: self.pos size: self.size """) class SvgWidget(Scatter): def __init__(self, filename, **kwargs): super(SvgWidget, self).__init__(**kwargs) with self.canvas: svg = Svg(filename) self.size = svg.width, svg.height class SvgApp(App): def build(self): self.root = FloatLayout() filenames = sys.argv[1:] if not filenames: filenames = glob(join(dirname(__file__), '*.svg')) for filename in filenames: svg = SvgWidget(filename, size_hint=(None, None)) self.root.add_widget(svg) svg.scale = 5. svg.center = Window.center if __name__ == '__main__': SvgApp().run()
python
from decimal import Decimal from django.contrib.auth.models import AbstractUser from django.db import models from django.db.models import Sum class CustomUser(AbstractUser): """ Clase que modela un usuario del sistema. Atributos: avatar: avatar que identifica al usuario. (String) cash: dinero liquido disponible para el usuario. (Decimal) rank: posicion que ocupa en el ranking de usuarios. (Int) """ avatar = models.CharField(blank=False, max_length=12) cash = models.DecimalField(max_digits=14, decimal_places=2, default=5000) rank = models.IntegerField(default=0) def __str__(self): return self.email def wallet_quote(self): """ Metodo que recorre las inversiones del usuario y cotiza su cartera en base a los precios actuales de los activos y al dinero liquido. """ quote = 0 investments = self.investments.all()\ .values('asset__buy')\ .order_by()\ .annotate(amountSum=Sum('amount'))\ .filter(amountSum__gte=0.01) for investment in investments: quote += investment['amountSum'] * investment['asset__buy'] return Decimal('%.2f' % (quote + self.cash))
python
# AUTOGENERATED BY NBDEV! DO NOT EDIT! __all__ = ["index", "modules", "custom_doc_links", "git_url"] index = {"USE_64": "01_Pairwise_Distance.ipynb", "gpu_dist_matrix": "01_Pairwise_Distance.ipynb", "component_mixture_dist_matrix": "01_Pairwise_Distance.ipynb", "makeCurvesFromDistanceMatrix": "02_Curve_Constructor.ipynb", "makeCurve": "02_Curve_Constructor.ipynb", "plotCurve": "02_Curve_Constructor.ipynb", "getEstimatorModel": "03_get_estimator_model.ipynb", "getTrainedEstimator": "03_get_estimator_model.ipynb", "prepFeatures": "04_Univariate_Transforms.ipynb", "trainOOBClassifier": "04_Univariate_Transforms.ipynb", "trainKFoldClassifier": "04_Univariate_Transforms.ipynb", "getOptimalTransform": "04_Univariate_Transforms.ipynb"} modules = ["pairwise_distance.py", "curve_constructor.py", "model.py", "transforms.py"] doc_url = "https://Dzeiberg.github.io/dist_curve/" git_url = "https://github.com/Dzeiberg/dist_curve/tree/master/" def custom_doc_links(name): return None
python
import pytest from app.models import Expense, User from app.models import expense pytestmark = pytest.mark.nologin def headers(tok): return {'Authorization': f'Bearer {tok}'} def test_get_expenses(db_with_expenses, token, client): resp = client.get('/api/expenses?page=1&page_size=10', headers=headers(token)) assert resp.status_code == 200 expenses = resp.get_json() assert len(expenses) == 10 for i, e in enumerate(expenses): assert e['description'] == f'Item {15-i}' def test_get_expense(db_with_expenses, token, client): exp = Expense.query.filter_by(description='Item 10').first() db_data = { 'id': exp.id, 'description': exp.description, 'amount': exp.amount_str, 'date': exp.date.isoformat(), 'payment_mode': exp.payment_mode.mode, 'estimate': exp.estimate.item if exp.estimate else None, 'tags': ','.join([tag.tagname for tag in exp.tags]), 'comments': exp.comments, 'created_on': exp.created_on.isoformat(), 'updated_on': exp.updated_on.isoformat() } resp = client.get(f'/api/expenses/{exp.id}', headers=headers(token)) assert resp.status_code == 200 e = resp.get_json() assert e == db_data def test_update_expense(db_with_expenses, token, client): # Following code is needed because we are accessing amount expense.current_user = User.query.get(1) exp = Expense.query.filter_by(description='Item 10').first() orig_amount = exp.amount orig_comments = exp.comments data = { 'amount': int(orig_amount + 10), 'comments': 'Amount increased by 10' } resp = client.patch(f'/api/expenses/{exp.id}', json=data, headers=headers(token)) assert resp.status_code == 200 e = resp.get_json() assert e['id'] == exp.id assert e['amount'] == str(orig_amount + 10) assert e['comments'] != orig_comments assert e['comments'] == 'Amount increased by 10' def test_delete_expense(db_with_expenses, token, client): exp = Expense.query.filter_by(description='Item 10').first() resp = client.delete(f'/api/expenses/{exp.id}', headers=headers(token)) assert resp.status_code == 204 def test_delete_forbidden(db_with_expenses, token, client): exp = Expense.query.filter_by(description='Item user2').first() resp = client.delete(f'/api/expenses/{exp.id}', headers=headers(token)) assert resp.status_code == 403 assert resp.get_json()['msg'].startswith('Forbidden') def test_delete_not_found(db_with_expenses, token, client): resp = client.delete('/api/expenses/50', headers=headers(token)) assert resp.status_code == 404 assert resp.get_json()['msg'] == 'Expense not found.'
python
import os from oraculo.gods import faveo, faveo_db, exceptions from . import entitys, base from .exceptions import ( DoesNotExist, NotSetHelpDeskUserInstance, NotIsInstance) class Prioritys(object): _url = 'api/v1/helpdesk/priority' _entity = entitys.Priority objects = base.BaseManageEntity(_url, _entity, key_name='priority') class Topics(object): _url = 'api/v1/helpdesk/help-topic' _entity = entitys.Topic objects = base.BaseManageEntity(_url, _entity, key_name='topic') class Deparments(object): _url = 'api/v1/helpdesk/department' _entity = entitys.Department objects = base.BaseManageEntity(_url, _entity) class Status(base.BaseManageEntity): _url = 'api/v1/helpdesk/dependency' _entity = entitys.State _client = faveo.APIClient() @staticmethod def get_entitys( entity=entitys.State, url='api/v1/status', client=faveo_db.APIClient): client = client() response = client.get(url) return [entity(**state) for state in response] @staticmethod def get_state_by_name( name, entity=entitys.State, url='api/v1/status', client=faveo_db.APIClient): client = client() response = client.get(url, params=dict(name=name)) return entity(**response[0]) class HelpDeskTicket( base.BaseEntityHelpDesk, base.BaseHelpDesk): _user = None _client = faveo.APIClient() _status_close_name = 'Closed' _department = os.environ.get('PARTENON_DEPARTMENT') _create_url = 'api/v1/helpdesk/create' _list_url = 'api/v1/helpdesk/my-tickets-user' _url_detail = 'api/v1/helpdesk/ticket' _url_to_change_status = 'api/v2/helpdesk/status/change' _url_to_add_note = 'api/v1/helpdesk/internal-note' ticket_name = None @property def state(self): if not self.ticket_name: response = self._client.get( self._url_detail, params=dict(id=self.ticket_id)) ticket = response.get('data').get('ticket') self.ticket_name = ticket.get('status_name') return Status.get_state_by_name(self.ticket_name) @property def user(self): return self._user @staticmethod def get_specific_ticket( ticket_id, url='api/v1/helpdesk/ticket', client=faveo.APIClient()): response = client.get(url, params=dict(id=ticket_id)) ticket_detail = response.get('data').get('ticket') ticket_detail['ticket_id'] = ticket_detail['id'] user = HelpDeskUser(**ticket_detail.get('from')) return HelpDeskTicket(_user=user, **ticket_detail) def add_note(self, note, user): body = dict(ticket_id=self.ticket_id, user_id=user.id, body=note) response = self._client.post(self._url_to_add_note, body=body) return response def create(self, subject, body, priority, topic, department): if not isinstance(priority, entitys.Priority): raise NotIsInstance('Priority not is instance of Priority') if not isinstance(topic, entitys.Topic): raise NotIsInstance('Topic not is instance of Topic') if not isinstance(department, entitys.Department): raise NotIsInstance('Department not is instance of Department') body = dict( subject=subject, body=body, first_name=self._user.first_name, email=self._user.email, priority=priority.priority_id, help_topic=topic.id, department=department.id) response = self._client.post(self._create_url, body).get('response') return self.get_specific_ticket(response.get('ticket_id')) def list(self): if not self._user: raise NotSetHelpDeskUserInstance('Need set _user instance') params = dict(user_id=self._user.id) response = self._client.get(self._list_url, params) if not isinstance(response, dict) and response.get('tickets', None): raise NotIsInstance(response.get('error')) return [HelpDeskTicket(**ticket) for ticket in response.get('tickets')] def change_state(self, state): body = dict(ticket_id=self.ticket_id, status_id=state.id) response = self._client.post(self._url_to_change_status, body) return response.get('success') def close(self): state_close = Status.get_state_by_name(self._status_close_name) body = dict(ticket_id=self.ticket_id, status_id=state_close.id) response = self._client.post(self._url_to_change_status, body) return response.get('success') class HelpDeskUser(base.BaseEntityHelpDesk, base.BaseHelpDesk): _url = 'api/v1/helpdesk/register' _search_url = 'api/v1/helpdesk/agents' def __init__(self, *args, **kwargs): self.ticket = HelpDeskTicket(**{'_user': self}) super().__init__(*args, **kwargs) @staticmethod def get(email, url='api/v1/users', client=faveo_db.APIClient): client = client() response = client.get(url, params={'email': email}) return HelpDeskUser(**response) if response else None @staticmethod def create_user( email, first_name, last_name, url='api/v1/helpdesk/register', client=faveo.APIClient): client = client() user = HelpDeskUser.get(email) if user: return user params = dict(email=email, first_name=first_name, last_name=last_name) result = client.post(url, params) return HelpDeskUser(**result[0].get('user')) class HelpDesk(object): user = HelpDeskUser topics = Topics prioritys = Prioritys departments = Deparments
python
import re import time import io import sys import argparse from collections import defaultdict # parse/validate arguments argParser = argparse.ArgumentParser() argParser.add_argument("-d", "--delimiter", type=str, default='\t', help="delimiter defaults to \t") argParser.add_argument("-1", "--firstFilename", type=str) argParser.add_argument("-2", "--secondFilename", type=str) argParser.add_argument("-o", "--outputFilename", type=str) argParser.add_argument("-ie", "--input_encoding", type=str, default='utf8') argParser.add_argument("-oe", "--output_encoding", type=str, default='utf8') args = argParser.parse_args() firstFile = io.open(args.firstFilename, encoding=args.input_encoding, mode='r') secondFile = io.open(args.secondFilename, encoding=args.input_encoding, mode='r') outputFile = io.open(args.outputFilename, encoding=args.output_encoding, mode='w') counter = 0 max_line = 100001 try: for firstLine in firstFile: secondLine = secondFile.readline() if len(secondLine) == 0: print 'error: second file is shorter than first file at line {0}'.format(counter) exit(1) if counter == 0: # outputFile.write(firstLine.strip() + ' ' + str(len(secondLine.strip().split())) + '\n') outputFile.write(u'{0}'.format(str(max_line - 1) + ' ' + str(len(secondLine.strip().split())) + '\n')) else: outputFile.write(u'{0}{1}{2}'.format(firstLine.strip(), args.delimiter, secondLine)) counter += 1 if counter == max_line: break except UnicodeDecodeError: print 'unicode error' firstFile.close() secondFile.close() outputFile.close()
python
""" File name: predict_full_brain.py Author: Jana Rieger Date created: 03/12/2018 This is the main script for predicting a segmentation of an input MRA image. Segmentations can be predicted for multiple models eather on rough grid (the parameters are then read out from the Unet/models/tuned_params.cvs file) or on fine grid. """ import os from Unet import config from Unet.utils.helper import read_tuned_params_from_csv from Unet.utils.predict_function import predict_and_save ################################################ # SET PARAMETERS FOR FINE GRID ################################################ dataset = 'test' # train / val / set patch_size_list = [96] # list of sizes of one patch n x n batch_size_list = [8, 16, 32, 64] # list of batch sizes num_epochs = 10 # number of epochs learning_rate_list = [1e-4, 1e-5] # list of learning rates of the optimizer Adam dropout_list = [0.0, 0.1, 0.2] # list of dropout rates: percentage of weights to be dropped fine_grid = True # True for tuning hyperparameters in fine grid tuning, False for random rough grid ################################################ num_patients_train = len(config.PATIENTS['train']['working']) + len( config.PATIENTS['train']['working_augmented']) # number of patients in training category num_patients_val = len(config.PATIENTS['val']['working']) + len( config.PATIENTS['val']['working_augmented']) # number of patients in validation category patients = config.PATIENTS[dataset]['working'] + config.PATIENTS[dataset]['working_augmented'] data_dirs = config.MODEL_DATA_DIRS if not os.path.exists(config.RESULTS_DIR + dataset + '/'): os.makedirs(config.RESULTS_DIR + dataset + '/') tuned_params_file = config.get_tuned_parameters() # PARAMETER LOOPS if fine_grid: # ----------------------------------------------------------- # FINE GRID FOR PARAMETER TUNING # ----------------------------------------------------------- for patch_size in patch_size_list: for batch_size in batch_size_list: for lr in learning_rate_list: for dropout in dropout_list: for patient in patients: predict_and_save(patch_size, num_epochs, batch_size, lr, dropout, patient, num_patients_train, num_patients_val, data_dirs, dataset) else: # ----------------------------------------------------------- # RANDOM ROUGH GRID FOR PARAMETER TUNING # ----------------------------------------------------------- patch_size_list, num_epochs_list, batch_size_list, learning_rate_list, dropout_list = read_tuned_params_from_csv( tuned_params_file) for i in range(len(patch_size_list)): patch_size = patch_size_list[i] num_epochs = num_epochs_list[i] batch_size = batch_size_list[i] lr = learning_rate_list[i] dropout = dropout_list[i] for patient in patients: predict_and_save(patch_size, num_epochs, batch_size, lr, dropout, patient, num_patients_train, num_patients_val, data_dirs, dataset) print('DONE')
python
import lumos.numpy.casadi_numpy as np import pytest def test_basic_logicals_numpy(): a = np.array([True, True, False, False]) b = np.array([True, False, True, False]) assert np.all(a & b == np.array([True, False, False, False])) if __name__ == "__main__": pytest.main()
python
import asyncio from string import capwords import DiscordUtils import discord from discord.ext.commands import Context from embeds import error_embed, success_embed, infoCheckEmbed from settings import BOT_TOKEN, prefix, description, verified_role_id from settings import verification_channel_id from database import emailTaken, addVerification, verifyUser, idTaken from database import isEDUEmail, addEDUEmail, authCodeTaken from database import getUserFromId from database import newInvite, wasInvited, removeInvite, useInvite from logs import logRegistered, logVerified, logRejoin # discord gateway intents intents = discord.Intents.all() allowed_mentions = discord.AllowedMentions(everyone=False, users=True, roles=False) # bot instance bot = discord.ext.commands.Bot(command_prefix=prefix, intents=intents, description=description, case_insensitive=True, allowed_mentions=allowed_mentions) # invite tracker tracker = DiscordUtils.InviteTracker(bot) @bot.event async def on_member_join(member): # check if user was previously registered if await idTaken(member.id): # get user user = await getUserFromId(member.id) # set role and nick nick = f"{user['first_name']} {user['last_name'][0]}" await member.add_roles(member.guild.get_role(verified_role_id)) await member.edit(nick=nick) # send a dm channel = await member.create_dm() await channel.send(f"Welcome back to the " f"OHSEA Discord {user['first_name']}! " f"I've automagically applied " f"your verification again." f"\n\n" f"If you think this was a mistake, " f"let an admin know :smile:") # log it down await logRejoin(member.id, nick, bot) # otherwise log down the inviter else: # get inviter inviter = await tracker.fetch_inviter(member) await newInvite(member.id, inviter.id) @bot.event async def on_member_remove(member): if await wasInvited(member.id): await removeInvite(member.id) @bot.command() async def register(ctx): if not isinstance(ctx.channel, discord.channel.DMChannel): await ctx.send(embed=await error_embed("Command can only be run " "in my DM's!")) return def messageCheck(message): return message.channel == ctx.channel and ctx.author == message.author user = {} # get first name await ctx.send('What is your first name?') msg = await bot.wait_for('message', check=messageCheck, timeout=1800) user['first_name'] = capwords(msg.content) # get last name await ctx.send('What is your last name?') msg = await bot.wait_for('message', check=messageCheck, timeout=1800) user['last_name'] = capwords(msg.content) while True: # get email await ctx.send('What is your **personal** school email?') msg = await bot.wait_for('message', check=messageCheck, timeout=1800) user['email'] = msg.content # break out if valid edu email if await isEDUEmail(msg.content): break # else tell them its not a valid edu email await ctx.send(embed=await error_embed('That is not a valid ' 'EDU email!' '\n\n' 'Contact modmail if you\'d ' 'like to add yours.')) # check if email is already used if await emailTaken(msg.content): await ctx.send(embed=await error_embed('Your email is already taken!' '\n\nPlease contact modmail' 'if you think this ' 'was a mistake.')) return # get confirmation msg = await ctx.send(embed=await infoCheckEmbed(user, ctx.author.id)) await msg.add_reaction('✅') await msg.add_reaction('❌') def confirmCheck(react, person): return person == ctx.author and \ (str(react.emoji) == '✅' or str(react.emoji == '❌')) try: reaction, member = await bot.wait_for(event='reaction_add', timeout=60.0, check=confirmCheck) # exit if wrong info if str(reaction.emoji) == '❌': await ctx.send("Try again with `!register`") return except asyncio.TimeoutError: await ctx.send('Timed out. Please try again.') return # otherwise add user to verification await addVerification(user) await ctx.send(embed=await success_embed('Check your email for further' ' instructions :smile:')) await logRegistered(ctx, user, bot) @bot.command() async def verify(ctx: discord.ext.commands.Context, auth_code=None): # check if auth code was provided if auth_code is None: await ctx.send(embed=await error_embed("No email verification code " "provided.")) # cancel the command return # command can only be run verification channel elif ctx.channel.id != verification_channel_id: await ctx.send(embed=await error_embed(f"Command can only be run in " f"<#{verification_channel_id}>")) return # check if user is already verified elif await idTaken(ctx.author.id): await ctx.send(embed=await error_embed("Your ID is already registered." "\n" "If you think this was a " "mistake " "please contact an admin.")) return # check if auth code is valid elif not await authCodeTaken(auth_code): await ctx.reply(embed=await error_embed("Not a valid verification " "code.")) return # verify user nick = await verifyUser(ctx.author.id, auth_code) await ctx.reply(embed=await success_embed("You're in! :smile:")) # give role await ctx.author.add_roles(ctx.guild.get_role(verified_role_id)) await ctx.author.edit(nick=nick) await logVerified(ctx, nick, bot) # log invite if was invited if await wasInvited(ctx.author.id): await useInvite(ctx.author.id) @bot.command() async def addemail(ctx, address): if await isEDUEmail(address, True): await ctx.send(embed=await error_embed('Already a valid ' 'email address.')) else: await addEDUEmail(address) await ctx.send(embed=await success_embed(f'Added @{address} as a ' f'valid email address.')) # run the bot bot.run(BOT_TOKEN)
python
class DataIntegrityException(Exception): pass class AuthenticationException(Exception): pass class UnauthorizedException(Exception): pass
python
"""Module for defining primitives and primitve categories """ from collections import defaultdict from enum import Enum import json import pprint import pkgutil class Primitive(object): """A primitive""" def __init__(self): self.name = '' self.task = '' self.learning_type = '' self.ml_algorithm = '' self.tags = ['NA', 'NA'] self.weight = 1 def __str__(self): return 'Primitive("{}")'.format(self.name) def __repr__(self): return 'Primitive("{}")'.format(self.name) def __eq__(self, other): """Define equals based on name""" if isinstance(other, self.__class__): return self.name == other.name return NotImplemented def __ne__(self, other): """Overide non-equality test""" if isinstance(other, self.__class__): return not self.__eq__(other) return NotImplemented def __hash__(self): """Overide hash by using name attribute""" return hash(self.name) class DSBoxPrimitive(Primitive): """A primitive""" def __init__(self, definition): super(DSBoxPrimitive, self).__init__() self.name = definition['Name'] self.task = definition['Task'] self.learning_type = definition['LearningType'] self.ml_algorithm = definition['MachineLearningAlgorithm'] self.tags = [self.ml_algorithm, self.ml_algorithm] self.weight = 1 def __str__(self): return 'DSBoxPrimitive("{}")'.format(self.name) def __repr__(self): return 'DSBoxPrimitive("{}")'.format(self.name) class D3mPrimitive(Primitive): """Primitive defined using D3M metadata""" def __init__(self, definition): super(D3mPrimitive, self).__init__() self.name = definition['id'].split('.')[-1] self.task = '' if 'task_type' in definition: if 'feature extraction' in definition['task_type']: self.task = "FeatureExtraction" if 'data preprocessing' in definition['task_type']: self.task = "DataPreprocessing" self.learning_type = 'NA' if 'handles_classification' in definition and definition['handles_classification']: self.learning_type = 'classification' self.task = 'Modeling' if 'handles_regression' in definition and definition['handles_regression']: self.learning_type = 'regression' self.task = 'Modeling' self.handles_multiclass = False if 'handles_multiclass' in definition: self.handles_multiclass = definition['handles_multiclass'] self.handles_multilabel = False if 'handles_multilabel' in definition: self.handles_multilabel = definition['handles_multilabel'] if 'algorithm_type' in definition: # !!!! For now get only the first type self.ml_algorithm = definition['algorithm_type'][0] if 'graph matching' in definition['algorithm_type']: self.learning_type = 'graphMatching' else: self.ml_algorithm = 'NA' self.tags = definition['tags'] # make sure tag hierarchy is at least 2 if len(self.tags) == 0: self.tags = ['NA', 'NA'] elif len(self.tags) == 1: self.tags = [self.tags[0], self.tags[0]] self.weight = 1 def __str__(self): return 'D3mPrimitive("{}")'.format(self.name) def __repr__(self): return 'D3mPrimitive("{}")'.format(self.name) class HierarchyNode(object): """Node in the Hierarchy""" def __init__(self, hierarchy, name, parent, content=None): self.hierarchy = hierarchy self.name = name self.parent = parent self.children = [] self._content = content def get_content(self): return self._content def add_child(self, name, content=None): """Add a child to the hierarchy""" child = HierarchyNode(self.hierarchy, name, self, content) self.children.append(child) return child def has_child(self, name): """Return true if node has child with given name""" for node in self.children: if node.name == name: return True return False def get_child(self, name): """Return child by name""" for node in self.children: if node.name == name: return node raise Exception('Child not found: {}'.format(name)) def get_siblings(self): if self.parent is None: result = [] else: result = [x for x in self.parent.children if not x==self] return result def add_primitive(self, primitive): self.hierarchy.add_primitive(self, primitive) def _add_primitive(self, primitive): if self._content is None: self._content = [primitive] else: self._content.append(primitive) def __str__(self): return 'Node({},num_child={})'.format(self.name, len(self.children)) class Hierarchy(object): """Generic tree of nodes""" def __init__(self, name): # name of this hierarchy self.name = name self.root = HierarchyNode(self, 'root', None) self._changed = False self._level_count = [] self.node_by_primitive = dict() def add_child(self, node, name, content=None): """Create and add child node""" assert node.hierarchy == self node.add_child(name, content) if content is not None: for primitive in content: self.node_by_primitive[primitive] = node self._changed = True def add_path(self, names): """Create and add all nodes in path""" curr_node = self.root for name in names: if curr_node.has_child(name): curr_node = curr_node.get_child(name) else: curr_node = curr_node.add_child(name) self._changed = True return curr_node def add_primitive(self, node, primitive): self.node_by_primitive[primitive] = node node._add_primitive(primitive) def get_level_counts(self): """Computes the number of nodes at each level""" if not self._changed: return self._level_count self._level_count = self._compute_level_counts(self.root, 0, list()) self._changed = False return self._level_count def _compute_level_counts(self, node, level, counts): """Computes the number of nodes at each level""" if len(counts) < level + 1: counts = counts + [0] counts[level] = counts[level] + 1 for child in node.children: counts = self._compute_level_counts(child, level+1, counts) return counts def get_primitive_count(self): """Returns the number of primitives""" return self._get_primitive_count(self.root) def _get_primitive_count(self, curr_node): """Returns the number of primitives""" count = 0 if curr_node._content is not None: count = count + len(curr_node._content) for child in curr_node.children: count = count + self._get_primitive_count(child) return count def get_primitives(self, curr_node=None): if curr_node is None: curr_node = self.root result = [] if curr_node._content is not None: result.append(curr_node._content) for child in curr_node.children: result = result + self.get_primitives(child) return result def get_primitives_as_list(self, curr_node=None): return [p for plist in self.get_primitives(curr_node) for p in plist] def get_nodes_by_level(self, level): """Returns node at a specified level of the tree""" return self._get_nodes_by_level(self.root, 0, level) def _get_nodes_by_level(self, curr_node, curr_level, target_level): """Returns node at a specified level of the tree""" if curr_level >= target_level: return [curr_node] elif curr_level +1 == target_level: return curr_node.children else: result = [] for node in curr_node.children: result = result + self._get_nodes_by_level(node, curr_level + 1, target_level) return result def get_node_by_primitive(self, primitive): return self.node_by_primitive[primitive] def pprint(self): """Print hierarchy""" print('Hierarchy({}, level_counts={})'.format(self.name, self.get_level_counts())) self._print(self.root, []) def _print(self, curr_node, path, max_depth=2): new_path = path + [curr_node] if len(new_path) > max_depth: print(' '*4 + ':'.join([node.name for node in new_path[1:]])) for line in pprint.pformat(curr_node._content).splitlines(): print(' '*8 + line) else: for child in curr_node.children: self._print(child, new_path, max_depth=max_depth) def __str__(self): return 'Hierarchy({}, num_primitives={}, level_node_counts={})'.format( self.name, self.get_primitive_count(), self.get_level_counts()) class Category(Enum): PREPROCESSING = 1 FEATURE = 2 CLASSIFICATION = 3 REGRESSION = 4 UNSUPERVISED = 5 EVALUATION = 6 METRICS = 7 GRAPH = 8 OTHER = 9 class Primitives(object): """Base Primitives class""" def __init__(self): self.primitives = [] self._index = dict() self.size = 0 self.hierarchy_types = [Category.PREPROCESSING, Category.FEATURE, Category.CLASSIFICATION, Category.REGRESSION, Category.UNSUPERVISED, Category.EVALUATION, Category.METRICS, Category.GRAPH, Category.OTHER] self.hierarchies = dict() for name in Category: self.hierarchies[name] = Hierarchy(name) def filter_equality(self, aspect, name): """Find primitive by aspect and name value""" result = [p for p in self.primitives if getattr(p, aspect) == name] return result def filter_by_task(self, name): """Find primitive by task aspect and name value""" return self.filter_equality('task', name) def filter_by_learning_type(self, name): """Find primitive by learning-type aspect and name value""" return self.filter_equality('learning_type', name) def filter_by_algo(self, name): """Find primitive by algorithm aspect and name value""" return self.filter_equality('ml_algorithm', name) def get_by_name(self, name): """Get primitve by unique name""" for primitive in self.primitives: if primitive.name == name: return primitive return None def get_index(self, name): """Returns the index of the primitive given its name""" return self._index[name] def get_hierarchies(self): """Returns all primitive hierarchies as dict""" return self.hierarchies def print_statistics_old(self): """Print statistics of the primitives""" classification = 0 regression = 0 classification_algo = defaultdict(int) regression_algo = defaultdict(int) tag_primitive = defaultdict(list) for primitive in self.primitives: if len(primitive.tags) > 0: tag_str = ':'.join(primitive.tags) if primitive.learning_type == Category.CLASSIFICATION: classification = classification + 1 # classification_algo[primitive.ml_algorithm] += 1 classification_algo[tag_str] = classification_algo[tag_str] + 1 tag_primitive['C:' + tag_str].append(primitive.name) elif primitive.learning_type == Category.REGRESSION: regression = regression + 1 regression_algo[tag_str] = regression_algo[tag_str] + 1 tag_primitive['R:' + tag_str].append(primitive.name) else: tag_primitive['O:' + tag_str].append(primitive.name) print('Primtive by Tag:') pprint.pprint(tag_primitive) print('Total number of primitives = {}'.format(self.size)) print('num classifiers = {}'.format(classification)) pprint.pprint(classification_algo) print('num regressors = {}'.format(regression)) pprint.pprint(regression_algo) def print_statistics(self): """Print statistics of the primitives""" print('Total number of primitives = {}'.format(self.size)) print('Number of primitives by hierarchy:') hierarchies = self.get_hierarchies() for name in Category: print(' '*4 + str(hierarchies[name])) def _compute_tag_hierarchy(self): """Compute hierarchy based on sklearn tags""" for primitive in self.primitives: # Put base/mixin and functions into other category if primitive.tags[0] in ['metrics']: node = self.hierarchies[Category.METRICS].add_path(primitive.tags[:2]) elif (primitive.tags[0] == 'base' or (primitive.tags[1] == 'base' and not 'LinearRegression' in primitive.name) or 'Base' in primitive.name or 'Mixin' in primitive.name or primitive.name[0].islower() or primitive.name == 'ForestRegressor' or primitive.name == 'ForestClassifier'): node = self.hierarchies[Category.OTHER].add_path(primitive.tags[:2]) elif (primitive.learning_type == 'classification' or primitive.tags[0] in ['lda', 'qda', 'naive_bayes'] or ('Classifier' in primitive.name and not primitive.tags[0] in ['multiclass', 'multioutput', 'calibration']) or 'SVC' in primitive.name or 'LogisticRegression' in primitive.name or 'Perceptron' in primitive.name ): # Same as SGDClassifier node = self.hierarchies[Category.CLASSIFICATION].add_path(primitive.tags[:2]) # Modify primitive learning type primitive.learning_type = 'classification' elif (primitive.learning_type == 'regression' or primitive.tags[0] in ['isotonic'] or ('Regressor' in primitive.name and not primitive.tags[0] in ['multioutput']) or 'SVR' in primitive.name or 'ElasticNet' in primitive.name or 'KernelRidge' in primitive.name or 'Lars' in primitive.name or 'Lasso' in primitive.name or 'LinearRegression' in primitive.name or 'Ridge' in primitive.name): node = self.hierarchies[Category.REGRESSION].add_path(primitive.tags[:2]) # Modify primitive learning type primitive.learning_type = 'regression' elif primitive.tags[0] in ['cluster', 'mixture']: node = self.hierarchies[Category.UNSUPERVISED].add_path(primitive.tags[:2]) elif (primitive.tags[0] in ['feature_extraction', 'feature_selection', 'decomposition', 'random_projection', 'manifold'] or 'OrthogonalMatchingPursuit' in primitive.name): node = self.hierarchies[Category.FEATURE].add_path(primitive.tags[:2]) elif primitive.tags[0] == 'preprocessing': node = self.hierarchies[Category.PREPROCESSING].add_path(primitive.tags[:2]) elif (primitive.tags[0] in ['cross_validation', 'model_selection']): node = self.hierarchies[Category.EVALUATION].add_path(primitive.tags[:2]) elif (primitive.tags[0] in ['cross_validation', 'graph_matching']): node = self.hierarchies[Category.GRAPH].add_path(primitive.tags[:2]) else: node = self.hierarchies[Category.OTHER].add_path(primitive.tags[:2]) node.add_primitive(primitive) class DSBoxPrimitives(Primitives): """Maintain available primitives""" PRIMITIVE_FILE = 'primitives.json' def __init__(self): super(DSBoxPrimitives, self).__init__() self._load() for index, primitive in enumerate(self.primitives): self._index[primitive] = index self.size = len(self.primitives) self._compute_tag_hierarchy() def _load(self): """Load primitive definition from JSON file""" text = pkgutil.get_data('dsbox.planner.levelone', self.PRIMITIVE_FILE) content = json.loads(text.decode()) self.primitives = [DSBoxPrimitive(primitive_dict) for primitive_dict in content['Primitives']] def _compute_tag_hierarchy(self): """Compute hierarchy based on sklearn tags""" for primitive in self.primitives: if primitive.learning_type == 'Classification': node = self.hierarchies[Category.CLASSIFICATION].add_path( [primitive.ml_algorithm, primitive.ml_algorithm]) primitive.learning_type = 'classification' elif primitive.learning_type == 'Regression': node = self.hierarchies[Category.REGRESSION].add_path( [primitive.ml_algorithm, primitive.ml_algorithm]) primitive.learning_type = 'regression' elif primitive.learning_type == 'UnsupervisedLearning': node = self.hierarchies[Category.UNSUPERVISED].add_path( [primitive.ml_algorithm, primitive.ml_algorithm]) elif primitive.task == 'FeatureExtraction': node = self.hierarchies[Category.FEATURE].add_path( [primitive.ml_algorithm, primitive.ml_algorithm]) elif primitive.task == 'DataPreprocessing': node = self.hierarchies[Category.PREPROCESSING].add_path( [primitive.ml_algorithm, primitive.ml_algorithm]) elif primitive.task == 'Evaluation': node = self.hierarchies[Category.EVALUATION].add_path( [primitive.ml_algorithm, primitive.ml_algorithm]) else: node = self.hierarchies[Category.OTHER].add_path( [primitive.ml_algorithm, primitive.ml_algorithm]) node.add_primitive(primitive) class D3mPrimitives(Primitives): """Primitives from D3M metadata""" PRIMITIVE_FILE = 'sklearn.json' def __init__(self, additional_primitives): # additional_primitives is list of Primitives super(D3mPrimitives, self).__init__() self._load() if additional_primitives: self.primitives = self.primitives + additional_primitives for index, primitive in enumerate(self.primitives): self._index[primitive] = index self.size = len(self.primitives) self._compute_tag_hierarchy() def _load(self): """Load primitve from json""" text = pkgutil.get_data('dsbox.planner.levelone', self.PRIMITIVE_FILE) content = json.loads(text.decode()) self.primitives = [D3mPrimitive(primitive_dict) for primitive_dict in content['search_primitives']]
python
from django.db.models import Q from rest_framework.filters import (OrderingFilter, SearchFilter) from rest_framework.generics import (ListAPIView, RetrieveAPIView) from posts.models import Comment from posts.api.permissions import IsOwnerOrReadOnly from posts.api.pagination import PostLimitOffsetPagination, PostPageNumberPagination from .serializers import CommentSerializer class CommentDetailAPIView(RetrieveAPIView): queryset = Comment.objects.all() serializer_class = CommentSerializer class CommentListAPIView(ListAPIView): serializer_class = CommentSerializer filter_backends = [SearchFilter, OrderingFilter] search_fields = ['content', 'user__first_name'] pagination_class = PostPageNumberPagination def get_queryset(self, *args, **kwargs): queryset_list = Comment.objects.all() query = self.request.GET.get("q") if query: queryset_list = queryset_list.filter( Q(content__icontains=query) | Q(user__first_name__icontains=query) | Q(user__last_name__icontains=query) ).distinct() return queryset_list
python
from tkinter import * from tkinter import ttk from tkinter import messagebox my_window = Tk() my_window.title("our final project/help") my_window.geometry("1366x768") my_window.resizable(1, 1) my_window.configure(bg="grey") L1 = Label(my_window, text="ENQUIRY MANAGEMENT SYSTEM", bg="lavender", fg="blue", font=("Algerian", 40), bd=4) L1.pack(fill=X) L2=Label(my_window, text="How can i help you? :", bg="grey", fg="white", font=("cambria",20),width=40) L2.place(x=200,y=200) E2=Entry(my_window,width=50,font="20",show="*",bd=3) E2.place(x=230,y=250) F1 = Frame(my_window, height=60, width=1366, bg="#ffff00") F1.place(x=0, y=620) L7 = Label(F1, text="Designed & Developed by : ", fg="red", bg="#ffff00", font=("cambria", 20), width="30") L7.place(x=600, y=20) L8 = Label(F1, text="Pushpa Kumari", bg="#ffff00", fg="black", font=("arial black", 13), width="20") L8.place(x=1000, y=30) my_window.mainloop()
python
import os import shutil import sys import time import unittest from sitetree import * class TestCopyFuncs(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) os.makedirs('testdata/test_sitetree/src/nested') with open('testdata/test_sitetree/src/nested/fileA.txt', "w") as f: f.write("fileA") with open('testdata/test_sitetree/src/nested/fileB.txt', "w") as f: f.write("fileB") with open('testdata/test_sitetree/src/fileC.txt', "w") as f: f.write("fileC") def tearDown(self): unittest.TestCase.tearDown(self) shutil.rmtree('testdata/test_sitetree') def test_is_newer(self): time.sleep(0.1) with open('testdata/test_sitetree/fileC.txt', "w") as f: f.write("fileC") self.assertTrue(is_newer('testdata/test_sitetree/fileC.txt', 'testdata/test_sitetree/src/fileC.txt')) self.assertFalse(is_newer('testdata/test_sitetree/src/fileC.txt', 'testdata/test_sitetree/fileC.txt')) os.rename('testdata/test_sitetree/fileC.txt', 'testdata/test_sitetree/fileD.txt') self.assertRaises(ValueError, is_newer, 'testdata/test_sitetree/fileD.txt', 'testdata/test_sitetree/src/fileC.txt') os.remove('testdata/test_sitetree/fileD.txt') self.assertTrue(is_newer('testdata/test_sitetree/src/fileC.txt', 'testdata/test_sitetree/fileC.txt')) def test_copy_on_condition(self): time.sleep(0.1) ALT_TEXT = "fileC alternative version" with open('testdata/test_sitetree/fileC.txt', "w") as f: f.write(ALT_TEXT) copy_on_condition('testdata/test_sitetree/fileC.txt', 'testdata/test_sitetree/src/fileC.txt', is_newer) with open('testdata/test_sitetree/src/fileC.txt', "r") as f: content = f.read() self.assertEqual(content, ALT_TEXT) time.sleep(1) with open('testdata/test_sitetree/src/fileC.txt', "w") as f: f.write("fileC") copy_on_condition('testdata/test_sitetree/fileC.txt', 'testdata/test_sitetree/src/fileC.txt', is_newer) with open('testdata/test_sitetree/src/fileC.txt', "r") as f: content = f.read() self.assertNotEqual(content, ALT_TEXT) os.remove('testdata/test_sitetree/fileC.txt') # if __name__ == "__main__": # sys.path.append( # os.path.split(os.path.dirname(os.path.abspath(sys.argv[0])))[0]) # from sitetree import * # unittest.main()
python
# copyright 1999 McMillan Enterprises, Inc. # license: use as you please. No warranty. # # A subclass of Archive that can be understood # by a C program. See uplaunch.cpp for unpacking # from C. import archive import struct import zlib import strop class CTOC: """A class encapsulating the table of contents of a CArchive. When written to disk, it is easily read from C.""" ENTRYSTRUCT = 'iiiibc' #(structlen, dpos, dlen, ulen, flag, typcd) followed by name def __init__(self): self.data = [] def frombinary(self, s): """Decode the binary string into an in memory list. S is a binary string.""" entrylen = struct.calcsize(self.ENTRYSTRUCT) p = 0 while p<len(s): (slen, dpos, dlen, ulen, flag, typcd) = struct.unpack(self.ENTRYSTRUCT, s[p:p+entrylen]) nmlen = slen - entrylen p = p + entrylen (nm,) = struct.unpack(repr(nmlen)+'s', s[p:p+nmlen]) p = p + nmlen self.data.append((dpos, dlen, ulen, flag, typcd, nm[:-1])) def tobinary(self): """Return self as a binary string.""" import string entrylen = struct.calcsize(self.ENTRYSTRUCT) rslt = [] for (dpos, dlen, ulen, flag, typcd, nm) in self.data: nmlen = len(nm) + 1 # add 1 for a '\0' rslt.append(struct.pack(self.ENTRYSTRUCT+repr(nmlen)+'s', nmlen+entrylen, dpos, dlen, ulen, flag, typcd, nm+'\0')) return string.join(rslt, '') def add(self, dpos, dlen, ulen, flag, typcd, nm): """Add an entry to the table of contents. DPOS is data position. DLEN is data length. ULEN is the uncompressed data len. FLAG says if the data is compressed. TYPCD is the "type" of the entry (used by the C code) NM is the entry's name.""" self.data.append((dpos, dlen, ulen, flag, typcd, nm)) def get(self, ndx): """return the toc entry (tuple) at index NDX""" return self.data[ndx] def __getitem__(self, ndx): return self.data[ndx] def find(self, name): """Return the index of the toc entry with name NAME. Return -1 for failure.""" for i in range(len(self.data)): if self.data[i][-1] == name: return i return -1 class CArchive(archive.Archive): """An Archive subclass that an hold arbitrary data. Easily handled from C or from Python.""" MAGIC = 'MEI\014\013\012\013\015' HDRLEN = 0 TOCTMPLT = CTOC TRLSTRUCT = '8siii' TRLLEN = 20 LEVEL = 9 def __init__(self, path=None, start=0, len=0): """Constructor. PATH is path name of file (create an empty CArchive if path is None). START is the seekposition within PATH. LEN is the length of the CArchive (if 0, then read till EOF). """ self.len = len archive.Archive.__init__(self, path, start) def checkmagic(self): """Verify that self is a valid CArchive. Magic signature is at end of the archive.""" #magic is at EOF; if we're embedded, we need to figure where that is if self.len: self.lib.seek(self.start+self.len, 0) else: self.lib.seek(0, 2) filelen = self.lib.tell() if self.len: self.lib.seek(self.start+self.len-self.TRLLEN, 0) else: self.lib.seek(-self.TRLLEN, 2) (magic, totallen, tocpos, toclen) = struct.unpack(self.TRLSTRUCT, self.lib.read(self.TRLLEN)) if magic != self.MAGIC: raise RuntimeError, "%s is not a valid %s archive file" \ % (self.path, self.__class__.__name__) self.pkgstart = filelen - totallen if self.len: if totallen != self.len or self.pkgstart != self.start: raise RuntimeError, "Problem with embedded archive in %s" % self.path self.tocpos, self.toclen = tocpos, toclen def loadtoc(self): """Load the table of contents into memory.""" self.toc = self.TOCTMPLT() self.lib.seek(self.pkgstart+self.tocpos) tocstr = self.lib.read(self.toclen) self.toc.frombinary(tocstr) def extract(self, name): """Get the contents of an entry. NAME is an entry name. Return the tuple (ispkg, contents). For non-Python resoures, ispkg is meaningless (and 0). Used by the import mechanism.""" if type(name) == type(''): ndx = self.toc.find(name) if ndx == -1: return None else: ndx = name (dpos, dlen, ulen, flag, typcd, nm) = self.toc.get(ndx) self.lib.seek(self.pkgstart+dpos) rslt = self.lib.read(dlen) if flag == 1: rslt = zlib.decompress(rslt) if typcd == 'M': return (1, rslt) return (0, rslt) def contents(self): """Return the names of the entries""" rslt = [] for (dpos, dlen, ulen, flag, typcd, nm) in self.toc: rslt.append(nm) return rslt def add(self, entry): """Add an ENTRY to the CArchive. ENTRY must have: entry[0] is name (under which it will be saved). entry[1] is fullpathname of the file. entry[2] is a flag for it's storage format (0==uncompressed, 1==compressed, 2==Python source format) entry[3] is the entry's type code.""" (nm, pathnm, flag, typcd) = entry[:4] if flag == 2: s = open(pathnm, 'r').read() s = s + '\n\0' else: s = open(pathnm, 'rb').read() ulen = len(s) if flag == 1: s = zlib.compress(s, self.LEVEL) dlen = len(s) where = self.lib.tell() if typcd == 'm': if strop.find(pathnm, '.__init__.py') > -1: typcd = 'M' self.toc.add(where, dlen, ulen, flag, typcd, nm) self.lib.write(s) def save_toc(self, tocpos): """Save the table of contents to disk.""" self.tocpos = tocpos tocstr = self.toc.tobinary() self.toclen = len(tocstr) self.lib.write(tocstr) def save_trailer(self, tocpos): """Save the trailer to disk. CArchives can be opened from the end - the trailer points back to the start. """ totallen = tocpos + self.toclen + self.TRLLEN trl = struct.pack(self.TRLSTRUCT, self.MAGIC, totallen, tocpos, self.toclen) self.lib.write(trl) def openEmbedded(self, name): """Open a CArchive of name NAME embedded within this CArchive.""" ndx = self.toc.find(name) if ndx == -1: raise KeyError, "Member '%s' not found in %s" % (name, self.path) (dpos, dlen, ulen, flag, typcd, nm) = self.toc.get(ndx) if flag: raise ValueError, "Cannot open compressed archive %s in place" return CArchive(self.path, self.pkgstart+dpos, dlen)
python
from django.contrib import admin from .models import Organization, Metadata admin.site.register(Organization) admin.site.register(Metadata)
python
# coding=utf-8 # Copyright 2022 The Google Research Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tests for sim.py in the exp_framework module.""" from absl.testing import absltest import numpy as np from sparse_data.data import sim NUM_DATASET = 5 NUM_REPLICATE = 5 SEED = 2462723 def setUpModule(): np.random.seed(SEED) class TestSim(absltest.TestCase): def setUp(self): super(TestSim, self).setUp() self.init_method = None # children should define this def test_reproducability(self): if self.init_method is None: return # pylint: disable=not-callable datasets = [self.init_method() for _ in range(NUM_DATASET)] # check reproducability of get() function for _ in range(NUM_REPLICATE): xs, ys = [], [] for d in datasets: d.reset() x, y = d.generate() xs.append(x) ys.append(y) np.random.randn() # make calls to global np.random RNG for i in range(NUM_DATASET - 1): self.assertTrue(np.array_equal(xs[i], xs[i + 1])) self.assertTrue(np.array_equal(ys[i], ys[i + 1])) # check reproducability of generate() function for _ in range(NUM_REPLICATE): x_trains, y_trains, x_tests, y_tests = [], [], [], [] for d in datasets: d.reset() x_train, y_train, x_test, y_test = d.get() x_trains.append(x_train) y_trains.append(y_train) x_tests.append(x_test) y_tests.append(y_test) np.random.randn() # make calls to global np.random RNG for i in range(NUM_DATASET - 1): self.assertTrue(np.array_equal(x_trains[i], x_trains[i + 1])) self.assertTrue(np.array_equal(y_trains[i], y_trains[i + 1])) self.assertTrue(np.array_equal(x_tests[i], x_tests[i + 1])) self.assertTrue(np.array_equal(y_tests[i], y_tests[i + 1])) class TestLinear(TestSim): def setUp(self): super(TestLinear, self).setUp() self.init_method = sim.LinearSimulation def test_shape(self): num_sample = np.random.randint(10, 20) num_feature = np.random.randint(10, 20) problem = 'classification' for _ in range(NUM_REPLICATE): d = self.init_method( num_sample=num_sample, num_feature=num_feature, problem=problem) d.reset() x, y = d.generate() self.assertEqual(x.shape, (num_sample, num_feature)) self.assertEqual(y.shape, (num_sample,)) def test_sparsity(self): num_sample = 1000 num_feature = 10 problem = 'classification' for _ in range(NUM_REPLICATE): prop_nonzero = np.random.uniform(0.2, 0.8) d = self.init_method( num_sample=num_sample, num_feature=num_feature, prop_nonzero=prop_nonzero, problem=problem) d.reset() x, _ = d.generate() observed_prop_nonzero = np.true_divide(np.sum(x > 0), np.size(x)) self.assertLess( np.abs(observed_prop_nonzero - prop_nonzero), 0.1 * prop_nonzero) class TestCardinality(TestLinear): def setUp(self): super(TestCardinality, self).setUp() self.init_method = sim.CardinalitySimulation def test_shape(self): num_sample = np.random.randint(10, 20) num_feature = np.random.randint(10, 20) * 2 # should be even problem = 'classification' for _ in range(NUM_REPLICATE): d = self.init_method( num_sample=num_sample, num_feature=num_feature, problem=problem) d.reset() x, y = d.generate() self.assertEqual(x.shape, (num_sample, num_feature)) self.assertEqual(y.shape, (num_sample,)) def test_sparsity(self): pass class TestSparsity(TestCardinality): def setUp(self): super(TestSparsity, self).setUp() self.init_method = sim.SparsitySimulation def test_sparsity(self): num_sample = 1000 num_feature = 50 problem = 'classification' for _ in range(NUM_REPLICATE): prop_nonzero = np.random.uniform(0.2, 0.8) d = self.init_method( num_sample=num_sample, num_feature=num_feature, prop_nonzero=prop_nonzero, problem=problem) d.reset() x, _ = d.generate() x_inf = x[:, :int(num_feature / 2)] observed_prop_nonzero = np.true_divide(np.sum(x_inf > 0), np.size(x_inf)) self.assertLess( np.abs(observed_prop_nonzero - prop_nonzero), 0.1 * prop_nonzero) class TestMultiplicative(TestLinear): def setUp(self): super(TestMultiplicative, self).setUp() self.init_method = sim.MultiplicativeSimulation def test_shape(self): orders = range(1, 10) problem = 'classification' for _ in range(NUM_REPLICATE): num_sample = np.random.randint(10, 20) num_group_per_order = np.random.randint(10, 20) num_feature = np.sum([o * num_group_per_order for o in orders], dtype=np.int) d = self.init_method( num_sample=num_sample, num_feature=num_feature, orders=orders, problem=problem) d.reset() x, y = d.generate() self.assertEqual(x.shape, (num_sample, num_feature)) self.assertEqual(y.shape, (num_sample,)) def test_sparsity(self): num_sample = 1000 num_group_per_order = 10 orders = range(1, 10) problem = 'classification' num_feature = np.sum([o * num_group_per_order for o in orders], dtype=np.int) for _ in range(NUM_REPLICATE): prop_nonzero = np.random.uniform(0.2, 0.8) d = self.init_method( num_sample=num_sample, num_feature=num_feature, orders=orders, prop_nonzero=prop_nonzero, problem=problem) d.reset() x, _ = d.generate() observed_prop_nonzero = np.true_divide(np.sum(x > 0), np.size(x)) self.assertLess( np.abs(observed_prop_nonzero - prop_nonzero), 0.1 * prop_nonzero) class TestXOR(TestLinear): def setUp(self): super(TestXOR, self).setUp() self.init_method = sim.XORSimulation def test_shape(self): problem = 'classification' for _ in range(NUM_REPLICATE): num_sample = np.random.randint(10, 20) num_features = 2 * np.random.randint(10, 20) d = self.init_method( num_sample=num_sample, num_feature=num_features, problem=problem) d.reset() x, y = d.generate() self.assertEqual(x.shape, (num_sample, num_features)) self.assertEqual(y.shape, (num_sample,)) def test_sparsity(self): num_sample = 1000 num_pair = 10 problem = 'classification' for _ in range(NUM_REPLICATE): prop_nonzero = np.random.uniform(0.2, 0.8) d = self.init_method( num_sample=num_sample, num_feature=num_pair / 2, prop_nonzero=prop_nonzero, problem=problem) d.reset() x, _ = d.generate() observed_prop_nonzero = np.true_divide(np.sum(x > 0), np.size(x)) self.assertLess( np.abs(observed_prop_nonzero - prop_nonzero), 0.1 * prop_nonzero) class TestFunctions(absltest.TestCase): def test_continuous_to_binary(self): # TODO(jisungkim) add more tests here y = [0, 1, 2, 3, 4, 5] exp_y_squashed = [0, 0, 0, 1, 1, 1] self.assertTrue( np.array_equal(exp_y_squashed, sim.continuous_to_binary(y, squashing='linear'))) if __name__ == '__main__': absltest.main()
python
# __init__.py # # Copyright(c) Exequiel Ceasar Navarrete <exequiel.navarrete09gmail.com> # Licensed under MIT # Version 1.0.0-alpha2 from compiler.tokenizer import Tokenizer from compiler.parser import Parser from compiler.transformer import Transformer from compiler.code_generator import CodeGenerator class Compiler(object): """ Class that compiles given code to another language """ def __init__(self, input_code): self.input_code = input_code def compile(self): tknizer = Tokenizer(self.input_code) parser = Parser(tknizer.run()) transformer = Transformer(parser.run()) code_generator = CodeGenerator(transformer.run()) return code_generator.run()
python
from testtube.helpers import Frosted, Nosetests, Pep257, Flake8 PATTERNS = ( # Run pep257 check against a file if it changes, excluding files that have # test_ or tube.py in the name. # If this test fails, don't make any noise (0 bells on failure) ( r'((?!test_)(?!tube\.py).)*\.py$', [Pep257(bells=0)] ), # Run flake8 and Frosted on all python files when they change. If these # checks fail, abort the entire test suite because it might be due to a # syntax error. There's no point running the subsequent tests if there # is such an error. ( r'.*\.py$', [Flake8(all_files=True), Frosted(all_files=True)], {'fail_fast': True} ), # Run the test suite whenever python or test config files change ( r'(.*setup\.cfg$)|(.*\.coveragerc)|(.*\.py$)', [Nosetests()] ) )
python
# Automatically generated by pb2py # fmt: off from .. import protobuf as p if __debug__: try: from typing import Dict, List # noqa: F401 from typing_extensions import Literal # noqa: F401 except ImportError: pass class MoneroSubAddressIndicesList(p.MessageType): def __init__( self, account: int = None, minor_indices: List[int] = None, ) -> None: self.account = account self.minor_indices = minor_indices if minor_indices is not None else [] @classmethod def get_fields(cls) -> Dict: return { 1: ('account', p.UVarintType, 0), 2: ('minor_indices', p.UVarintType, p.FLAG_REPEATED), }
python
def power_set(s): return power_set_util(list(s), 0) def power_set_util(s, index): if index == len(s): all_subsets = [set()] else: all_subsets = power_set_util(s, index + 1) new_subsets = [] for subset in all_subsets: concat = set(subset) concat.add(s[index]) new_subsets.append(concat) all_subsets.extend(new_subsets) return all_subsets def main(): print(power_set({8, 9, 3})) if __name__ == '__main__': main()
python
name = "mtgtools"
python
import os import re import setuptools with open("README.MD", "r") as fh: long_description = fh.read() def find_version(fnam, version="VERSION"): with open(fnam) as f: cont = f.read() regex = f'{version}\s*=\s*["]([^"]+)["]' match = re.search(regex, cont) if match is None: raise Exception( f"version with spec={version} not found, use double quotes for version string" ) return match.group(1) def find_projectname(): cwd = os.getcwd() name = os.path.basename(cwd) return name projectname = find_projectname() file = os.path.join(projectname, "__init__.py") version = find_version(file) setuptools.setup( name="pttydev", version=version, author="k.r. goger", author_email="k.r.goger+{projectname}@gmail.com", description="TTYDev - Pseudo TTY Device", long_description=long_description, long_description_content_type="text/markdown", url="https://github.com/kr-g/pttydev", packages=setuptools.find_packages(), license="MIT", keywords="python threading pyserial websocket websocket-client micropython webrepl esp8266 esp32", install_requires=[ "pyatomic", "pyserial", "websocket-client", ], classifiers=[ "Development Status :: 3 - Alpha", "Operating System :: POSIX :: Linux", "Intended Audience :: Developers", "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", ], python_requires=">=3.8", )
python
#Crie um programa que leia uma frase qualquer # e dia se ela é um palindromo; desconsiderando os espaços # Ex. apos a sopa ; a sacada da casa, a torre da derrota frase = str(input("Digite uma frase: ")).strip().upper() palavras = frase.split() junto = ''.join(palavras) inverso = '' for letra in range(len(junto) -1, -1, -1): inverso += junto[letra] print(junto, inverso) if inverso == junto: print("Temos um palíndromo") else: print("A frase digitada nõ é um palíndromo")
python
class Agent: def __init__(self, screen_resolution): self.resolution = screen_resolution self.fitness = 0 self.dead = False self.screen = None self.y = screen_resolution[1]//2 self.rect = ((10, self.y), (self.resolution[0]//20, self.resolution[0]//25)) self.vvelocity = 0 def copyview(self, surface_array): self.screen = surface_array def jump(self): self.vvelocity = 3.0 def move(self): self.vvelocity -= .08 self.y -= 1*self.vvelocity self.rect = ((10, self.y), (self.resolution[0]//20, self.resolution[1]//20))
python
from lstm_model import LstmModel import numpy as np from trajectory import Trajectory, generate_trajectory, generate_trajectories, stochastic_policy_adapter from solver import value_iteration, stochastic_policy_from_value_expectation from snake_ladder import SnakeLadderWorld from supervised_utils import trajectory_list_to_xy, shuffle, x_to_ragged, train_test_split, compute_class_accuracy, sigmoid, get_fixed_policies, get_expert_policy, generate_trajectories_from_policy_list import tensorflow as tf from lstm_model import LstmModel import plotly.express as px import datetime from importance import calc_instance_score, instance_importance_plot def main(): # define some consants world_size = 20 shortcut_density = 0.1 # create our world world = SnakeLadderWorld(size=world_size, shortcut_density=shortcut_density) # game board print("board: ") print(world.game_board) # create "fixed" policies which each execeute one of the three actions w/ prob p (success_prob) # randomly sample from all actions w/ prob 1 - p # so excute one action with prob p + 1/3(1 - p) and others with 1/3(1 - p) fixed_policies = get_fixed_policies # get policy using value iteration expert_policy = get_expert_policy(world) # create list of policies policies = [] #policies = policies_fixed policies.append(expert_policy) # add expert policy to list policies.append(world._smartish_policy) num_policies = len(policies) # generate trajectories for all policies # each index of list contains array of corresponding policy trajectories n_trajectories_per_policy = 1000 trajectories_list = generate_trajectories_from_policy_list(world, policies,n_trajectories_per_policy=n_trajectories_per_policy) # print an example trajectory # a trajectory from policy 0 print(trajectories_list[0][0]) # seperate trajectories into x,y data x_data, y_data = trajectory_list_to_xy(trajectories_list) x_data, y_data = shuffle(x_data, y_data) # convert data to ragged tensor # max_seq contains length of longest trajectory x_data, max_seq = x_to_ragged(x_data) y_data = np.array(y_data) # do a simple train/test split x_train, y_train, x_test, y_test = train_test_split(x_data,y_data, test_prop =.20) # create lstm model lstm_model = LstmModel(max_trajectory_len=max_seq, num_features=3, num_outputs=num_policies) print(lstm_model.model.summary()) # train model lstm_model.train(x_train, y_train, x_test, y_test, log_dir="./logs/fit/", epochs = 500, batch_size=int(n_trajectories_per_policy / 10), early_stopping=True,patience=10) # compute accuracy by class y_predicted = lstm_model.predict_classes(x_test) print(compute_class_accuracy(y_test, y_predicted)) # create instance importance plot for i in range(5): trajectory_index = i fig = instance_importance_plot(x_test, y_test, trajectory_index, lstm_model, scale_constant=10) fig.show() if __name__ == "__main__": main()
python
from .legion_tools import * from .hamiltonian_gen import * from collections import OrderedDict def mf_calc(base_params): fd = base_params.fd fd_array = np.linspace(10.44, 10.5, 2001) fd_array = np.hstack([fd_array, fd]) fd_array = np.unique(np.sort(fd_array)) mf_amplitude_frame = mf_characterise(base_params, fd_array) return mf_amplitude_frame.loc[fd] def slowdown_sim(job_index, output_directory='./results', bistable_initial=True, transmon=True, transformation=False, mf_init=False, g=np.sqrt(2)): bistable_initial = bistable_initial print('In slowdown_sim.py we have bistable_initial = ' + str(bistable_initial) + ' for job_index = ' + str(job_index)) with open('stack.csv', 'r') as f: header = f.readline() stack_name = header.split('\n')[0] stack_frame = pd.read_csv(f) stack_directory = output_directory kappa_phi = 0.0 sys_params = stack_frame.iloc[job_index] frame_params = sys_params if transmon is True: packaged_params = Parameters(frame_params.fc, frame_params.Ej, frame_params.g, frame_params.Ec, frame_params.eps, frame_params.fd, frame_params.kappa, frame_params.gamma, frame_params.t_levels, frame_params.c_levels, frame_params.gamma_phi, kappa_phi, frame_params.n_t, frame_params.n_c) H = hamiltonian(packaged_params, transmon=transmon) c_ops = collapse_operators(packaged_params) else: packaged_params = Parameters(frame_params.fc, None, frame_params.g, None, frame_params.eps, frame_params.fd, frame_params.kappa, frame_params.gamma, frame_params.t_levels, frame_params.c_levels, frame_params.gamma_phi, kappa_phi, frame_params.n_t, frame_params.n_c, frame_params.f01) H = hamiltonian(packaged_params, transmon=transmon) c_ops = collapse_operators(packaged_params) a = tensor(destroy(sys_params.c_levels), qeye(sys_params.t_levels)) sm = tensor(qeye(sys_params.c_levels), destroy(sys_params.t_levels)) directory = stack_directory + '/' + sys_params.group_folder + '/' + str(sys_params.job_index) if not os.path.exists(directory): os.makedirs(directory) cwd = os.getcwd() os.chdir(directory) print('The working directory for the current job index is ' + str(directory)) sys_params.to_csv('settings.csv') options = Options(nsteps=1e6) if os.path.exists('./state_checkpoint.qu'): print('Loading state checkpoint for job_index = '+str(sys_params.job_index)) initial_state = qload('./state_checkpoint') H = qload('./hamiltonian') c_ops = qload('./c_ops') previous_results = pd.read_csv('./results.csv') delta_t = 1.0 * sys_params.end_time / (sys_params.snapshots - 1) start_time = float(previous_results['times'].iloc[-1]) new_snapshots = sys_params.snapshots - start_time / delta_t snapshot_times = np.linspace(start_time, sys_params.end_time, new_snapshots) save = False #don't save the first row of results, it's already there bistability = True else: start_time = 0 snapshot_times = np.linspace(start_time, sys_params.end_time, sys_params.snapshots) save = True #save the first row of results if bistable_initial is True: bistability_characteristics = dict() if os.path.exists('./steady_state.qu'): rho_ss = qload('steady_state') print('MF init = ' + str(mf_init)) if mf_init: print('mf_init is true') mf_amplitudes = mf_calc(packaged_params) if mf_amplitudes.dropna().shape[0] == 4: bistability = True bright_alpha = mf_amplitudes.a_bright bright_projector = tensor(coherent_dm(packaged_params.c_levels, g*bright_alpha), qeye(packaged_params.t_levels)) rho_bright = bright_projector * rho_ss rho_bright /= rho_bright.norm() dim_alpha = mf_amplitudes.a_dim dim_projector = tensor(coherent_dm(packaged_params.c_levels, g*dim_alpha), qeye(packaged_params.t_levels)) rho_dim = dim_projector * rho_ss rho_dim /= rho_dim.norm() characteristics = None else: bistability = False rho_dim = None rho_bright = None characteristics = None else: #raise AssertionError bistability, rho_dim, rho_bright, characteristics = bistable_states_calc(rho_ss) if sys_params.qubit_state == 0: print('Dim initial state.') initial_state = rho_dim else: print('Bright initial state.') initial_state = rho_bright else: print('Finding steady state for job_index = '+str(sys_params.job_index)) rho_ss = steadystate(H, c_ops) qsave(rho_ss, './steady_state') bistability, rho_dim, rho_bright, characteristics = bistable_states_calc(rho_ss) if sys_params.qubit_state == 0: print('Dim initial state.') initial_state = rho_dim else: print('Bright initial state.') initial_state = rho_bright if transformation and bistability: alpha_bright = expect(a,rho_bright) alpha_dim = expect(a,rho_dim) bistability_characteristics['alpha_bright'] = alpha_bright bistability_characteristics['alpha_dim'] = alpha_dim alpha = 0.5*(alpha_bright+alpha_dim) beta = 0.0 else: alpha = 0.0 beta = 0.0 bistability_characteristics['bistability'] = bistability bistability_characteristics['rho_dim'] = rho_dim bistability_characteristics['rho_bright'] = rho_bright bistability_characteristics['characteristics'] = characteristics bistability_characteristics['alpha'] = alpha bistability_characteristics['beta'] = beta qsave(bistability_characteristics, './characteristics') else: print('Choosing initial state in the transmon basis.') initial_state = tensor(fock_dm(sys_params.c_levels,0), fock_dm(sys_params.t_levels, sys_params.qubit_state)) bistability = None alpha = 0.0 beta = 0.0 if transmon is True: packaged_params = Parameters(frame_params.fc, frame_params.Ej, frame_params.g, frame_params.Ec, frame_params.eps, frame_params.fd, frame_params.kappa, frame_params.gamma, frame_params.t_levels, frame_params.c_levels, frame_params.gamma_phi, kappa_phi, frame_params.n_t, frame_params.n_c) H = hamiltonian(packaged_params, transmon=transmon, alpha=alpha, beta=beta) c_ops = collapse_operators(packaged_params, alpha=alpha, beta=beta) else: packaged_params = Parameters(frame_params.fc, None, frame_params.g, None, frame_params.eps, frame_params.fd, frame_params.kappa, frame_params.gamma, frame_params.t_levels, frame_params.c_levels, frame_params.gamma_phi, kappa_phi, frame_params.n_t, frame_params.n_c, frame_params.f01) H = hamiltonian(packaged_params, transmon=transmon, alpha=alpha, beta=beta) c_ops = collapse_operators(packaged_params, alpha=alpha, beta=beta) qsave(H, 'hamiltonian') qsave(c_ops, 'c_ops') options.store_final_state = True e_ops = OrderedDict() e_ops['a_op_re'] = (a + a.dag()) / 2 e_ops['a_op_im'] = -1j * (a - a.dag()) / 2 e_ops['photons'] = a.dag() * a for level in range(sys_params.c_levels): e_ops['c_level_' + str(level)] = tensor(fock_dm(sys_params.c_levels, level), qeye(sys_params.t_levels)) e_ops['sm_op_re'] = (sm.dag() + sm) / 2 e_ops['sm_op_im'] = -1j * (sm - sm.dag()) / 2 e_ops['excitations'] = sm.dag() * sm for level in range(sys_params.t_levels): e_ops['t_level_' + str(level)] = tensor(qeye(sys_params.c_levels), fock_dm(sys_params.t_levels, level)) qsave(H,'slowdown_hamiltonian') if bistability: print('Going into the mesolve function we have a_op_re = ' + str(expect(e_ops['a_op_re'],initial_state))) output = mesolve_checkpoint(H, initial_state, snapshot_times, c_ops, e_ops, save, directory, options=options) os.chdir(cwd)
python
#!/bin/python def sortSelection(A, k): """ Selects the @k-th smallest number from @A in O(nlogn) time by sorting and returning A[k] Note that indexing begins at 0, so call sortSelection(A, 0) to get the smallest number in the list, call sortselection(A, len(A) / 2) to get the median number of the list, call sortselection(A, len(A) - 1) to get the largest number of the list param A : an unsorted list param k : the k-th smallest number of @A to find return : the k-th smallest number of @A """ if k < 0 or k >= len(A): raise IndexError\ ('Requested k-th smallest value is out of index range for the provided list') B = A[:] B.sort() return B[k] def pickPivot(A): """ Picks a pivot by arbitrarily partitioning @A into groups of 5, finding the median of each group, and selecting the median of those medians as a pivot param A : an unsorted list return : the pivot """ i = 0 j = 5 B = [] for _ in range((len(A) / j) + 1): if A[i:j]: B.append(selection(A[i:j], len(A[i:j]) / 2)) i += 5 j += 5 return selection(B, len(B) / 2) def selection(A, k): """ Selects the @k-th smallest number from @A in O(n) time Note that indexing begins at 0, so call selection(A, 0) to get the smallest number in the list, call selection(A, len(A) / 2) to get the median number of the list, call selection(A, len(A) - 1) to get the largest number of the list param A : an unsorted list param k : the k-th smallest number of @A to find return : the k-th smallest number of @A """ if k < 0 or k >= len(A): raise IndexError\ ('Requested k-th smallest value is out of index range for the provided list') if len(A) <= 100: return sortSelection(A, k) pivot = pickPivot(A) A_1 = [] A_2 = [] A_3 = [] for item in A: if item < pivot: A_1.append(item) # A_1 = items of @A less than pivot elif item > pivot: A_2.append(item) # A_2 = items of @A greater than pivot else: A_3.append(item) # A_3 = items of @A equal to pivot i = len(A_1) j = len(A_3) if i <= k <= (i + j): return pivot if k < i: return selection(A_1, k) if k > (i + j): return selection(A_2, k - i - j)
python
#!/usr/bin/python3 import sys import os import re import shutil optimize = 3 link_time_optimize = 3 sources = [ 'audio/audio.c', 'audio/source.c', 'audio/staticsource.c', 'audio/wav_decoder.c', 'audio/vorbis_decoder.c', 'filesystem/filesystem.c', 'graphics/batch.c', 'graphics/font.c', 'graphics/graphics.c', 'graphics/geometry.c', 'graphics/image.c', 'graphics/matrixstack.c', 'graphics/quad.c', 'graphics/shader.c', 'graphics/gltools.c', 'image/imagedata.c', 'luaapi/audio.c', 'luaapi/boot.c', 'luaapi/math.c', 'luaapi/filesystem.c', 'luaapi/keyboard.c', 'luaapi/event.c', 'luaapi/bonding.c', 'luaapi/mouse.c', 'luaapi/graphics.c', 'luaapi/graphics_geometry.c', 'luaapi/graphics_batch.c', 'luaapi/graphics_font.c', 'luaapi/graphics_image.c', 'luaapi/graphics_quad.c', 'luaapi/graphics_shader.c', 'luaapi/graphics_window.c', 'luaapi/image.c', 'luaapi/timer.c', 'luaapi/tools.c', 'math/vector.c', 'math/minmax.c', 'math/random.c', 'math/randomgenerator.c', 'main.c', 'bonding.c', 'mouse.c', 'keyboard.c', 'tools/utf8.c', 'tools/log.c', 'timer/timer.c', # SLRE '3rdparty/slre/slre.c', # Lua '3rdparty/lua/src/lapi.c', '3rdparty/lua/src/lauxlib.c', '3rdparty/lua/src/lbaselib.c', '3rdparty/lua/src/lcode.c', '3rdparty/lua/src/ldblib.c', '3rdparty/lua/src/ldebug.c', '3rdparty/lua/src/ldo.c', '3rdparty/lua/src/ldump.c', '3rdparty/lua/src/lfunc.c', '3rdparty/lua/src/lgc.c', '3rdparty/lua/src/linit.c', '3rdparty/lua/src/liolib.c', '3rdparty/lua/src/llex.c', '3rdparty/lua/src/lmathlib.c', '3rdparty/lua/src/lmem.c', '3rdparty/lua/src/loadlib.c', '3rdparty/lua/src/lobject.c', '3rdparty/lua/src/lopcodes.c', '3rdparty/lua/src/loslib.c', '3rdparty/lua/src/lparser.c', '3rdparty/lua/src/lstate.c', '3rdparty/lua/src/lstring.c', '3rdparty/lua/src/lstrlib.c', '3rdparty/lua/src/ltable.c', '3rdparty/lua/src/ltablib.c', '3rdparty/lua/src/ltm.c', '3rdparty/lua/src/lundump.c', '3rdparty/lua/src/lvm.c', '3rdparty/lua/src/lzio.c', # FreeType '3rdparty/freetype/src/base/ftbitmap.c', '3rdparty/freetype/src/base/ftcalc.c', '3rdparty/freetype/src/base/ftgloadr.c', '3rdparty/freetype/src/base/ftglyph.c', '3rdparty/freetype/src/base/ftinit.c', '3rdparty/freetype/src/base/ftobjs.c', '3rdparty/freetype/src/base/ftoutln.c', '3rdparty/freetype/src/base/ftrfork.c', '3rdparty/freetype/src/base/ftstream.c', '3rdparty/freetype/src/base/ftsystem.c', '3rdparty/freetype/src/base/fttrigon.c', '3rdparty/freetype/src/base/ftutil.c', '3rdparty/freetype/src/gzip/ftgzip.c', '3rdparty/freetype/src/sfnt/sfnt.c', '3rdparty/freetype/src/smooth/smooth.c', '3rdparty/freetype/src/truetype/truetype.c', ] SRCDIR = os.path.dirname(sys.argv[0]) + "/src" ftinc = " ".join(map(lambda x: "-I" + os.path.relpath(SRCDIR) + "/3rdparty/freetype/src/" + x, ["truetype", "sfnt", "autofit", "smooth", "raster", "psaux", "psnames"])) + " -I" + os.path.relpath(SRCDIR) + "/3rdparty/freetype/include" output = '' CFLAGS = '' LDFLAGS = '' CC = '' LD = '' if SRCDIR == '.' or SRCDIR == '': print("Please build out-of-source") sys.exit(1) includeregex = re.compile('^\s*#\s*include\s*"([^"]+)"\s*$') os.system('sed -e "s/FT_CONFIG_OPTIONS_H/<ftoption.h>/" -e "s/FT_CONFIG_STANDARD_LIBRARY_H/<ftstdlib.h>/" -e "s?/undef ?#undef ?" <{srcdir}/3rdparty/freetype/builds/unix/ftconfig.in >ftconfig.h'.format(srcdir=os.path.relpath(SRCDIR))) os.system('mkdir -p config; sed -e \'/tt_driver\\|sfnt_module\\|ft_smooth_renderer/ !d\' < {srcdir}/3rdparty/freetype/include/config/ftmodule.h >config/ftmodule.h'.format(srcdir=os.path.relpath(SRCDIR))) def newestDependency(filename, trace=[]): newest = os.path.getmtime(sys.argv[0]) with open(filename) as f: for line in f: res = includeregex.match(line) if res: dep = os.path.dirname(filename) + "/" + res.group(1) if os.path.exists(dep) and dep not in trace: newest = max(newest, os.path.getmtime(dep), newestDependency(dep, trace + [dep])) return newest def makeFontFile(): sourcefile = os.path.join(os.path.dirname(sys.argv[0]), "data", "vera.ttf") compiled = os.path.exists("vera_ttf.c") and os.path.getmtime("vera_ttf.c") or 0 source = os.path.getmtime(sourcefile) if compiled > source: return False with open(sourcefile, "rb") as datafile, open("vera_ttf.c", "w") as outputfile: content = bytearray(datafile.read()) outputfile.write("static unsigned char const defaultFontData[] = {\n") for i in range(len(content)): outputfile.write("0x{:02x}, ".format(content[i])) if i % 16 == 15: outputfile.write("\n") outputfile.write("}};\nstatic size_t defaultFontSize = {};".format(len(content))) return True def needsRebuild(filename): return not os.path.exists(getObjFilename(filename)) or \ os.path.getmtime(SRCDIR + "/" + filename) > os.path.getmtime(getObjFilename(filename)) or \ newestDependency(SRCDIR + "/" + filename) > os.path.getmtime(getObjFilename(filename)) def getObjFilename(filename): return os.path.splitext(filename)[0] + ".o" def compile(filename): objfile = getObjFilename(filename) outdir = os.path.dirname(filename) if outdir == "": outdir = "." if not os.path.exists(outdir): print("Making directory " + outdir) os.makedirs(outdir) print("Compile {filename} -> {objfile}".format(filename=filename, objfile=objfile)) cmd = "{CC} {CFLAGS} -c -o {objfile} {filename}".format(CC=CC, CFLAGS=CFLAGS, filename=SRCDIR + "/" + filename, objfile=objfile) return os.system(cmd) == 0 def build(): global output, CFLAGS, LDFLAGS, CC, LD if '--native' in sys.argv: output = 'love' CFLAGS = '-g -O{optimize} -I/usr/include/SDL2 -DFT2_BUILD_LIBRARY -Wall -g -std=c11 -I{ftconfig} -I{srcdir}/3rdparty/lua/src'.format(optimize=optimize, link_time_optimize=link_time_optimize, srcdir = os.path.relpath(SRCDIR), ftconfig=".") + " " + ftinc LDFLAGS = '-lm -lSDL2 -lGL -lGLEW -lopenal -g'.format(optimize=optimize, link_time_optimize=link_time_optimize) CC = 'clang' LD = 'clang' else: output = 'love.js' CFLAGS = '-s USE_SDL=1 -s -FULL_ES2=1 -O{optimize} --memory-init-file 0 --llvm-lto {link_time_optimize} -DFT2_BUILD_LIBRARY -Wall -std=c11 -I{ftconfig} -I{srcdir}/3rdparty/lua/src'.format(optimize=optimize, link_time_optimize=link_time_optimize, srcdir = os.path.relpath(SRCDIR), ftconfig=".") + " " + ftinc LDFLAGS = '-s USE_SDL=1 -s ALLOW_MEMORY_GROWTH=1 --no-heap-copy -O{optimize} --llvm-lto {link_time_optimize} --memory-init-file 0 -o love.html --preload-file lib/ --preload-file main.lua --preload-file logic/ --preload-file res/ --preload-file res/bgs/ --preload-file res/sounds/'.format(optimize=optimize, link_time_optimize=link_time_optimize) CC = 'emcc' LD = 'emcc' needsLinking = False fontChanged = makeFontFile() needsLinking = needsLinking or fontChanged for i in sources: filename = i if needsRebuild(filename): if not compile(filename): print("Failed") sys.exit(1) needsLinking = True if needsLinking: print("Linking {output}".format(output=output)) cmd = "{LD} {LDFLAGS} -o {outfile} {infiles}".format(LD=LD, LDFLAGS=LDFLAGS, outfile=output, infiles=" ".join(map(getObjFilename, sources))) if os.system(cmd) != 0: print("Failed") def remove(f): if os.path.exists(f): os.remove(f) def clean(): for i in sources: remove(getObjFilename(i)) remove('motor2d.js') remove('motor2d') try: for i in extra_output: remove(i) except NameError: pass def usage(): print(sys.argv[0] + " (build|buildloader|clean) [--native]") print(" Verbs:") print(" build build motor2d executable") print(" clean delete intermediate files and final executable (doesn't clean loader)") print(" Flags:") print(" --native build native executable (not supported for buildloader)") if len(sys.argv) == 1: usage() elif sys.argv[1] == 'build': build() elif sys.argv[1] == 'clean': clean()
python
import unittest from board import Board class BoardTests(unittest.TestCase): def setUp(self): self.b = Board() return super().setUp() def test_initial_board(self): expected = [['.', '.', '.'], ['.', '.', '.'], ['.', '.', '.']] self.assertEqual(self.b.board, expected) def test_player_move(self): self.b.player_move(0, 0) expected = [['X', '.', '.'], ['.', '.', '.'], ['.', '.', '.']] self.assertEqual(self.b.board, expected) def test_bot_move(self): self.b.bot_move(1, 1) expected = [['.', '.', '.'], ['.', 'O', '.'], ['.', '.', '.']] self.assertEqual(self.b.board, expected) def test_make_move(self): self.b.make_move('X', 0, 0) expected = [['X', '.', '.'], ['.', '.', '.'], ['.', '.', '.']] self.assertEqual(self.b.board, expected) def test_bot_make_move(self): self.b.bot_make_move() expected = [['O', '.', '.'], ['.', '.', '.'], ['.', '.', '.']] self.assertEqual(self.b.board, expected)
python
import os from bot import app_vars def clean_file_name(file_name: str) -> str: for char in ["\\", "/", "%", "*", "?", ":", '"', "|"] + [ chr(i) for i in range(1, 32) ]: file_name = file_name.replace(char, "_") file_name = file_name.strip() return file_name def get_abs_path(file_name: str) -> str: return os.path.join(app_vars.directory, file_name)
python
import copy import six from .error import SchemaError, Error, Invalid class Schema(object): """ A schema that validates data given to it using the specified rules. The ``schema`` must be a dictionary of key-value mappings. Values must be callable validators. See ``XX``. The ``entire`` argument allows specifying a callable validator that runs on the entire input after every field is validated. If provided, the validator will always run, even if validation errors are raised beforehand. Failed keys will not be included in the given data. The ``extra_keys`` argument must be one of :attr:`.ACCEPT`, :attr:`.IGNORE` or :attr:`.REJECT`. The ``required_error`` argument specifies the error message used when a key is missing. :attr:`.REQUIRED_ERROR` is the default. """ ACCEPT = 'ACCEPT' IGNORE = 'IGNORE' REJECT = 'REJECT' REQUIRED_ERROR = "This field is required." """ The default error message for a missing required key. """ REJECT_ERROR = "This field is unknown." """ The default error message for an unknown rejected key. """ def __init__(self, schema, entire=None, extra_keys=IGNORE, required_error=None): self.extra_keys = extra_keys self.entire = entire self.required_error = required_error or self.REQUIRED_ERROR if not isinstance(schema, dict): raise SchemaError("The provided schema must be a dictionary.") self.schema = schema self.validator = self._build(schema) def __call__(self, data): """ Validates the given ``data`` dictionary and returns transformed values. Will raise :class:`decent.error.Invalid` if any validation errors are encountered. """ return self.validator(copy.deepcopy(data)) def _build(self, schema): extra_keys = self.extra_keys entire = self.entire # Enumerate all the keys in the schema. all_keys = set(schema.keys()) _required_keys = set([key for key in all_keys if not isinstance(key, Optional)]) # Enumerate default key values. defaults = {} for key in all_keys: if isinstance(key, Marker) and key.default != None: defaults[key] = key.default # Make sure all validators are callable. for key, value in six.iteritems(schema): if not hasattr(value, '__call__'): raise SchemaError("Validator {!r} for key '{!s}' is not callable.".format(value, key)) def validator(data): # Sanity check. if not isinstance(data, dict): raise Invalid([Error("Data must be a dictionary.")]) # Track which required keys are not present. required_keys = _required_keys.copy() # Fill available defaults before validating. missing = all_keys.copy() - set(data.keys()) for key in missing: if key in defaults: data[key] = defaults[key] errors = [] result = {} for key, value in six.iteritems(data): # If this key is not in the schema, decide what to do with it. if key not in all_keys: if extra_keys == self.ACCEPT: # Pass through as is. result[key] = value elif extra_keys == self.REJECT: # Reject with error. errors.append(Error(self.REJECT_ERROR, [key])) continue # pragma: no cover # Validate. validator = schema[key] result_value = self._run_validator(validator, value, errors, key) if result_value: result[key] = result_value # Track required keys. if key in required_keys: required_keys.remove(key) # Add an error for every missing key. for key in required_keys: errors.append(Error(self.required_error, [key])) # Run the validator for the entire schema. if entire: result = self._run_validator(entire, result, errors) if errors: raise Invalid(errors) return result return validator def _run_validator(self, validator, data, errors, key=None): try: return validator(data) except Invalid as all: for e in all: self._add_error(e, errors, key) except Error as e: self._add_error(e, errors, key) def _add_error(self, error, errors, key=None): if key: error.path.insert(0, key) errors.append(error) class Marker(object): """ A base class for key markers that wrap a key. """ def __init__(self, key, default=None): self.key = key self.default = default def __str__(self): return str(self.key) def __eq__(self, other): return self.key == other def __hash__(self): return hash(self.key) __repr__ = __str__ class Default(Marker): """ A marker for specifying a default value for a key. """ pass class Optional(Marker): """ A marker for specifying a key as optional. The schema will validate data without the key present. """ pass __all__ = ('Schema', 'Marker', 'Default', 'Optional',)
python
#!/usr/bin/env python # # Copyright 2013 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. import optparse import os import shutil import re import sys import textwrap from util import build_utils import jar sys.path.append(build_utils.COLORAMA_ROOT) import colorama def ColorJavacOutput(output): fileline_prefix = r'(?P<fileline>(?P<file>[-.\w/\\]+.java):(?P<line>[0-9]+):)' warning_re = re.compile( fileline_prefix + r'(?P<full_message> warning: (?P<message>.*))$') error_re = re.compile( fileline_prefix + r'(?P<full_message> (?P<message>.*))$') marker_re = re.compile(r'\s*(?P<marker>\^)\s*$') warning_color = ['full_message', colorama.Fore.YELLOW + colorama.Style.DIM] error_color = ['full_message', colorama.Fore.MAGENTA + colorama.Style.BRIGHT] marker_color = ['marker', colorama.Fore.BLUE + colorama.Style.BRIGHT] def Colorize(line, regex, color): match = regex.match(line) start = match.start(color[0]) end = match.end(color[0]) return (line[:start] + color[1] + line[start:end] + colorama.Fore.RESET + colorama.Style.RESET_ALL + line[end:]) def ApplyColor(line): if warning_re.match(line): line = Colorize(line, warning_re, warning_color) elif error_re.match(line): line = Colorize(line, error_re, error_color) elif marker_re.match(line): line = Colorize(line, marker_re, marker_color) return line return '\n'.join(map(ApplyColor, output.split('\n'))) ERRORPRONE_OPTIONS = [ # These crash on lots of targets. '-Xep:ParameterPackage:OFF', '-Xep:OverridesGuiceInjectableMethod:OFF', '-Xep:OverridesJavaxInjectableMethod:OFF', ] def _FilterJavaFiles(paths, filters): return [f for f in paths if not filters or build_utils.MatchesGlob(f, filters)] _MAX_MANIFEST_LINE_LEN = 72 def _CreateManifest(manifest_path, classpath, main_class=None, manifest_entries=None): """Creates a manifest file with the given parameters. This generates a manifest file that compiles with the spec found at http://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html#JAR_Manifest Args: manifest_path: The path to the manifest file that should be created. classpath: The JAR files that should be listed on the manifest file's classpath. main_class: If present, the class containing the main() function. manifest_entries: If present, a list of (key, value) pairs to add to the manifest. """ output = ['Manifest-Version: 1.0'] if main_class: output.append('Main-Class: %s' % main_class) if manifest_entries: for k, v in manifest_entries: output.append('%s: %s' % (k, v)) if classpath: sanitized_paths = [] for path in classpath: sanitized_paths.append(os.path.basename(path.strip('"'))) output.append('Class-Path: %s' % ' '.join(sanitized_paths)) output.append('Created-By: ') output.append('') wrapper = textwrap.TextWrapper(break_long_words=True, drop_whitespace=False, subsequent_indent=' ', width=_MAX_MANIFEST_LINE_LEN - 2) output = '\r\n'.join(w for l in output for w in wrapper.wrap(l)) with open(manifest_path, 'w') as f: f.write(output) def _ExtractClassFiles(jar_path, dest_dir, java_files): """Extracts all .class files not corresponding to |java_files|.""" # Two challenges exist here: # 1. |java_files| have prefixes that are not represented in the the jar paths. # 2. A single .java file results in multiple .class files when it contains # nested classes. # Here's an example: # source path: ../../base/android/java/src/org/chromium/Foo.java # jar paths: org/chromium/Foo.class, org/chromium/Foo$Inner.class # To extract only .class files not related to the given .java files, we strip # off ".class" and "$*.class" and use a substring match against java_files. def extract_predicate(path): if not path.endswith('.class'): return False path_without_suffix = re.sub(r'(?:\$[^/]+)?\.class$', '', path) return not any(path_without_suffix in p for p in java_files) build_utils.ExtractAll(jar_path, path=dest_dir, predicate=extract_predicate) def _OnStaleMd5(changes, options, javac_cmd, java_files, classpath_inputs, runtime_classpath): with build_utils.TempDir() as temp_dir: srcjars = options.java_srcjars # The .excluded.jar contains .class files excluded from the main jar. # It is used for incremental compiles. excluded_jar_path = options.jar_path.replace('.jar', '.excluded.jar') classes_dir = os.path.join(temp_dir, 'classes') os.makedirs(classes_dir) changed_paths = None if options.incremental and changes.AddedOrModifiedOnly(): changed_paths = set(changes.IterChangedPaths()) # Do a full compile if classpath has changed. if any(p in changed_paths for p in classpath_inputs): changed_paths = None else: java_files = [p for p in java_files if p in changed_paths] srcjars = [p for p in srcjars if p in changed_paths] if srcjars: java_dir = os.path.join(temp_dir, 'java') os.makedirs(java_dir) for srcjar in options.java_srcjars: extract_predicate = None if changed_paths: changed_subpaths = set(changes.IterChangedSubpaths(srcjar)) extract_predicate = lambda p: p in changed_subpaths build_utils.ExtractAll(srcjar, path=java_dir, pattern='*.java', predicate=extract_predicate) jar_srcs = build_utils.FindInDirectory(java_dir, '*.java') java_files.extend(_FilterJavaFiles(jar_srcs, options.javac_includes)) if java_files: if changed_paths: # When no files have been removed and the output jar already # exists, reuse .class files from the existing jar. _ExtractClassFiles(options.jar_path, classes_dir, java_files) _ExtractClassFiles(excluded_jar_path, classes_dir, java_files) # Add the extracted files to the classpath. classpath_idx = javac_cmd.index('-classpath') javac_cmd[classpath_idx + 1] += ':' + classes_dir # Don't include the output directory in the initial set of args since it # being in a temp dir makes it unstable (breaks md5 stamping). cmd = javac_cmd + ['-d', classes_dir] + java_files build_utils.CheckOutput( cmd, print_stdout=options.chromium_code, stderr_filter=ColorJavacOutput) if options.main_class or options.manifest_entry: entries = [] if options.manifest_entry: entries = [e.split(':') for e in options.manifest_entry] manifest_file = os.path.join(temp_dir, 'manifest') _CreateManifest(manifest_file, runtime_classpath, options.main_class, entries) else: manifest_file = None glob = options.jar_excluded_classes inclusion_predicate = lambda f: not build_utils.MatchesGlob(f, glob) exclusion_predicate = lambda f: not inclusion_predicate(f) jar.JarDirectory(classes_dir, options.jar_path, manifest_file=manifest_file, predicate=inclusion_predicate) jar.JarDirectory(classes_dir, excluded_jar_path, predicate=exclusion_predicate) def _ParseOptions(argv): parser = optparse.OptionParser() build_utils.AddDepfileOption(parser) parser.add_option( '--src-gendirs', help='Directories containing generated java files.') parser.add_option( '--java-srcjars', action='append', default=[], help='List of srcjars to include in compilation.') parser.add_option( '--bootclasspath', action='append', default=[], help='Boot classpath for javac. If this is specified multiple times, ' 'they will all be appended to construct the classpath.') parser.add_option( '--classpath', action='append', help='Classpath for javac. If this is specified multiple times, they ' 'will all be appended to construct the classpath.') parser.add_option( '--use-ijars', action='store_true', help='Whether to use interface jars (.interface.jar) when compiling') parser.add_option( '--incremental', action='store_true', help='Whether to re-use .class files rather than recompiling them ' '(when possible).') parser.add_option( '--javac-includes', default='', help='A list of file patterns. If provided, only java files that match' 'one of the patterns will be compiled.') parser.add_option( '--jar-excluded-classes', default='', help='List of .class file patterns to exclude from the jar.') parser.add_option( '--chromium-code', type='int', help='Whether code being compiled should be built with stricter ' 'warnings for chromium code.') parser.add_option( '--use-errorprone-path', help='Use the Errorprone compiler at this path.') parser.add_option('--jar-path', help='Jar output path.') parser.add_option( '--main-class', help='The class containing the main method.') parser.add_option( '--manifest-entry', action='append', help='Key:value pairs to add to the .jar manifest.') parser.add_option('--stamp', help='Path to touch on success.') options, args = parser.parse_args(argv) build_utils.CheckOptions(options, parser, required=('jar_path',)) bootclasspath = [] for arg in options.bootclasspath: bootclasspath += build_utils.ParseGypList(arg) options.bootclasspath = bootclasspath classpath = [] for arg in options.classpath: classpath += build_utils.ParseGypList(arg) options.classpath = classpath java_srcjars = [] for arg in options.java_srcjars: java_srcjars += build_utils.ParseGypList(arg) options.java_srcjars = java_srcjars if options.src_gendirs: options.src_gendirs = build_utils.ParseGypList(options.src_gendirs) options.javac_includes = build_utils.ParseGypList(options.javac_includes) options.jar_excluded_classes = ( build_utils.ParseGypList(options.jar_excluded_classes)) return options, args def main(argv): colorama.init() argv = build_utils.ExpandFileArgs(argv) options, java_files = _ParseOptions(argv) if options.src_gendirs: java_files += build_utils.FindInDirectories(options.src_gendirs, '*.java') java_files = _FilterJavaFiles(java_files, options.javac_includes) runtime_classpath = options.classpath compile_classpath = runtime_classpath if options.use_ijars: ijar_re = re.compile(r'\.jar$') compile_classpath = ( [ijar_re.sub('.interface.jar', p) for p in runtime_classpath]) javac_cmd = ['javac'] if options.use_errorprone_path: javac_cmd = [options.use_errorprone_path] + ERRORPRONE_OPTIONS javac_cmd.extend(( '-g', # Chromium only allows UTF8 source files. Being explicit avoids # javac pulling a default encoding from the user's environment. '-encoding', 'UTF-8', '-classpath', ':'.join(compile_classpath), # Prevent compiler from compiling .java files not listed as inputs. # See: http://blog.ltgt.net/most-build-tools-misuse-javac/ '-sourcepath', '' )) if options.bootclasspath: javac_cmd.extend([ '-bootclasspath', ':'.join(options.bootclasspath), '-source', '1.7', '-target', '1.7', ]) if options.chromium_code: javac_cmd.extend(['-Xlint:unchecked', '-Xlint:deprecation']) else: # XDignore.symbol.file makes javac compile against rt.jar instead of # ct.sym. This means that using a java internal package/class will not # trigger a compile warning or error. javac_cmd.extend(['-XDignore.symbol.file']) classpath_inputs = options.bootclasspath # TODO(agrieve): Remove this .TOC heuristic once GYP is no more. if options.use_ijars: classpath_inputs.extend(compile_classpath) else: for path in compile_classpath: if os.path.exists(path + '.TOC'): classpath_inputs.append(path + '.TOC') else: classpath_inputs.append(path) # Compute the list of paths that when changed, we need to rebuild. input_paths = classpath_inputs + options.java_srcjars + java_files output_paths = [ options.jar_path, options.jar_path.replace('.jar', '.excluded.jar'), ] # An escape hatch to be able to check if incremental compiles are causing # problems. force = int(os.environ.get('DISABLE_INCREMENTAL_JAVAC', 0)) # List python deps in input_strings rather than input_paths since the contents # of them does not change what gets written to the depsfile. build_utils.CallAndWriteDepfileIfStale( lambda changes: _OnStaleMd5(changes, options, javac_cmd, java_files, classpath_inputs, runtime_classpath), options, input_paths=input_paths, input_strings=javac_cmd, output_paths=output_paths, force=force, pass_changes=True) if __name__ == '__main__': sys.exit(main(sys.argv[1:]))
python
import gym import torch import numpy as np import seaborn as sns from hips.plotting.colormaps import gradient_cmap import matplotlib.pyplot as plt import os from tikzplotlib import save from sds_numpy import rARHMM from sds_torch.rarhmm import rARHMM from lax.a2c_lax import learn device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") to_torch = lambda arr: torch.from_numpy(arr).float().to(device) to_npy = lambda arr: arr.detach().double().cpu().numpy() # env = gym.make('Cartpole-ID-v1') # <--- eval on cartpole env = gym.make('HybridCartpole-ID-v1') # <--- train on hybrid cartpole env.unwrapped._dt = 0.01 env.unwrapped._sigma = 1e-4 env._max_episode_steps = 5000 """ learn(env, seed=42, obfilter=True, tsteps_per_batch=5000, cv_opt_epochs=5, lax=False, animate=False, gamma=0.99, vf_opt_epochs=50, total_steps=int(50e6), save_loc='/Users/kek/Documents/informatik/master/semester_3/thesis/code/' 'sds/evaluation/l4dc2020/cartpole/evals') """ model = torch.load('/Users/kek/Documents/informatik/master/semester_3/thesis/code/sds/evaluation/l4dc2020/cartpole/thesis_eval/checkpoint_HybridCartpole-ID-v1_model_887_epochs_.pkl', map_location='cpu') model.step_policy_model.policy.training = False seed = 100 env.seed(seed) torch.manual_seed(seed) np.random.seed(seed) obs = env.reset() obs = to_torch(model.obfilter(obs)) prev_obs = torch.zeros_like(obs) reward = 0 all_rewards = [] env_obs = [] env_acts = [] horizon = 100000 for i in range(horizon): identified_states = torch.cat([obs, prev_obs], -1) prev_obs = torch.clone(obs) sampled_u, _, mean, _ = model.step_policy_model.act(identified_states) scaled_u = env.action_space.low + (to_npy(sampled_u) + 1.) * 0.5 * ( env.action_space.high - env.action_space.low) scaled_u = np.clip(scaled_u, a_min=env.action_space.low, a_max=env.action_space.high) _obs, r, done, _ = env.step(scaled_u) obs = to_torch(model.obfilter(_obs)) reward += r env_acts.append(sampled_u.detach()) # acts.append(scaled_u) # print(i, _obs) env_obs.append(to_torch(_obs)) if done: obs = env.reset() obs = to_torch(model.obfilter(obs)) prev_obs = torch.zeros_like(obs) print(reward) all_rewards.append(reward) reward = 0 print("Expected reward: {} ± {}".format(np.mean(all_rewards), np.std(all_rewards))) """ rarhmm = torch.load(open(os.path.abspath(os.path.join(os.path.dirname(__file__), '..','..','..' )) + '/sds_numpy/envs/hybrid/models/neural_rarhmm_cartpole_cart.pkl', 'rb'), map_location='cpu') _, identified_states = rarhmm.viterbi([np.stack([to_npy(o) for o in env_obs])], [np.stack([to_npy(a) for a in env_acts])]) # rarhmm.viterbi([to_npy(env_obs[i][None]) for i in range(500)], [to_npy(env_acts[i][None]) for i in range(500)]) color_names = ["windows blue", "red", "amber", "faded green", "dusty purple", "orange", "pale red", "medium green", "denim blue", "muted purple"] colors = sns.xkcd_palette(color_names) cmap = gradient_cmap(colors) identified_states = [np.stack(identified_states).squeeze()] n_plots = env.observation_space.shape[0] + env.action_space.shape[0] fig, axs = plt.subplots(n_plots) x = np.arange(len(env_obs)) y_labels = ['x', '$\cos(\\theta)$', '$\sin(\\theta)$', '$\\dot{x}$', '$\\dot{\\theta}$', 'control'] y_lims = [{'low': -5.2, 'high': 5.2}, {'low': -1.5, 'high': 1.5}, {'low': -1.2, 'high': 1.2}, {'low': -5.2, 'high': 5.2}, {'low': -11.8, 'high': 11.8}, {'low': -5.4, 'high': 5.4}] env_obs = np.stack(env_obs) for n in range(n_plots - 1): axs[n].plot(x, env_obs[:, n], color='black') axs[n].imshow(identified_states[0][None, :], aspect='auto', cmap=cmap, vmin=0, vmax=len(colors) - 1, extent=[0, horizon, y_lims[n]['low'], y_lims[n]['high']]) axs[n].set_ylabel(y_labels[n], fontsize=12) axs[n].set_ylim(bottom=y_lims[n]['low'], top=y_lims[n]['high']) axs[-1].plot(x, env_acts, color='black') axs[-1].set_ylabel(y_labels[-1], fontsize=12) axs[-1].imshow(identified_states[0][None, :], aspect='auto', cmap=cmap, vmin=0, vmax=len(colors) - 1, extent=[0, horizon, y_lims[-1]['low'], y_lims[-1]['high']]) axs[-1].set_ylim(bottom=y_lims[-1]['low'], top=y_lims[-1]['high']) axs[-1].set_xlim(left=0, right=horizon) axs[-1].set_xlabel('steps') plt.tight_layout() save('cartpole-policy-rarhmm-dynamics.tex', externalize_tables=True) plt.show() """
python
import requests from bs4 import BeautifulSoup as bs import time from time import sleep import os import sys import xlsxwriter from random import randint import pyautogui import pickle from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import re import json #HERE IT FINDS THE PATH if getattr(sys, 'frozen', False): application_path = os.path.dirname(sys.executable) else: try: app_full_path = os.path.realpath(__file__) application_path = os.path.dirname(app_full_path) except NameError: application_path = os.getcwd() #Here we create the variable that is going to be used to all the functions for the path path = os.path.join(application_path) url = "https://ais.usvisa-info.com/en-il/niv/users/sign_in" #Prefered months to check prefered_months = ['July', 'August', 'September'] #Selenium options = webdriver.ChromeOptions() options.headless = False options.add_argument("start-maximized") options.add_argument("Cashe-Control=no-cashe") options.add_argument('--no-sandbox') options.add_argument('--no-cookies') options.add_argument('--dns-prefetch-disable') options.add_argument('--disable-dev-shm-usage') options.add_argument('--disablle-web-security') options.add_argument('--ignore-certificate-errors') options.page_load_strategy = 'none' options.add_argument('--ingore-certificate-errors-spki-list') options.add_argument('--ignore-ssl-errors') options.add_experimental_option("excludeSwitches", ["enable-logging"]) browser = webdriver.Chrome(options=options, executable_path= path + '\\chromedriver.exe') browser.get(url) browser.implicitly_wait(10) action = ActionChains(browser) sleep(5) headers = { "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36" } #cookie #/html/body/div[6]/div[3]/div/button #try: # browser.find_element_by_class_name("ui-button.ui-corner-all.ui-widget").click() # sleep(5) #except Exception: # try: # browser.find_element_by_xpath("""/html/body/div[6]/div[3]/div/button""").click() # sleep(5) # except Exception as Err: # print(Err) # print() def main(): login() cont_loop() def login(): #login login_data = [] with open(path + "\\login.txt", 'r') as l_f: log_d = l_f.readlines() if log_d != []: for lg in log_d: lg = lg.rstrip('\n') login_data.append(lg) l_f.close() email = login_data[0] password = login_data[1] browser.get(url) browser_cookies = browser.get_cookies() browser.find_element_by_id("user_email").send_keys(email) sleep(1) browser.find_element_by_id("user_password").send_keys(password) sleep(1) browser.find_elements_by_class_name("icheckbox")[0].click() sleep(1) browser.find_elements_by_name("commit")[0].click() sleep(1) #browser_cookies = browser.get_cookies() browser.implicitly_wait(10) sleep(5) #Pay with Visa window browser.get("https://ais.usvisa-info.com/en-il/niv/schedule/34027977/payment") browser.implicitly_wait(10) sleep(5) def cont_loop(): cont = False count = 1 while cont == False: cont = data_extraction() print(f"\nRefreshes: {count}\n") count += 1 def data_extraction(): #Get the element dates try: page_code = browser.find_element_by_class_name("medium-3.column").get_attribute('outerHTML') except Exception: try: page_code = browser.find_element_by_xpath("""//*[@id="paymentOptions"]/div[2]""").get_attribute('outerHTML') except Exception: try: page_code = browser.find_element_by_xpath("""/html/body/div[4]/main/div[4]/div[2]""").get_attribute('outerHTML') except Exception as Err: print(Err) print() soup = bs(page_code, 'html.parser') try: date1 = soup.find_all('table',{'class':'for-layout'})[0].text print(date1) print() except Exception as Err: print(Err) print() cont = extract_dates(date1) sleep(randint(60, 180)) browser.refresh() return cont def extract_dates(date1): count = 0 cont = False if "2021" in date1: for p in prefered_months: if p in date1: count += 1 cont = True if count == 1: print('\nFound 1 match, for the dates that you wanted\n') elif count == 2: print('\nFound 2 matches, for the dates that you wanted\n') return cont main()
python
import unittest from rover import control class ControlTest(unittest.TestCase): def test_example(self): input = """5 5 1 2 N LMLMLMLMM 3 3 E MMRMMRMRRM""" expected = """1 3 N 5 1 E""" actual = control.launch_mission(input) self.assertEqual(actual, expected) def test_simple_move(self): commands_from_0_0_to_0_1 = """0 1\n0 0 N\nM""" expected = "0 1 N" actual = control.launch_mission(commands_from_0_0_to_0_1) self.assertEqual(actual, expected) def test_move(self): self.assertEqual((2, 3), control.execute_move('N', 2, 2)) self.assertEqual((3, 2), control.execute_move('E', 2, 2)) self.assertEqual((2, 1), control.execute_move('S', 2, 2)) self.assertEqual((1, 2), control.execute_move('W', 2, 2)) def test_turn_right(self): self.assertEqual('E', control.execute_turn('N', 'R')) self.assertEqual('S', control.execute_turn('E', 'R')) self.assertEqual('W', control.execute_turn('S', 'R')) self.assertEqual('N', control.execute_turn('W', 'R')) def test_turn_left(self): self.assertEqual('W', control.execute_turn('N', 'L')) self.assertEqual('S', control.execute_turn('W', 'L')) self.assertEqual('E', control.execute_turn('S', 'L')) self.assertEqual('N', control.execute_turn('E', 'L'))
python
class Property: def __init__(self, name='', value=''): self.name = name self.value = value
python
from pypy.module.cpyext.test.test_api import BaseApiTest class TestIterator(BaseApiTest): def test_check_iter(self, space, api): assert api.PyIter_Check(space.iter(space.wrap("a"))) assert api.PyIter_Check(space.iter(space.newlist([]))) assert not api.PyIter_Check(space.w_type) assert not api.PyIter_Check(space.wrap(2)) def test_getIter(self, space, api): w_iter = api.PyObject_GetIter(space.wrap([1, 2, 3])) assert space.unwrap(api.PyIter_Next(w_iter)) == 1 assert space.unwrap(api.PyIter_Next(w_iter)) == 2 assert space.unwrap(api.PyIter_Next(w_iter)) == 3 assert api.PyIter_Next(w_iter) is None assert not api.PyErr_Occurred() def test_iternext_error(self,space, api): assert api.PyIter_Next(space.w_None) is None assert api.PyErr_Occurred() is space.w_TypeError api.PyErr_Clear()
python
import os from jarjar import jarjar def writetofile(f, **kwargs): """write kwargs to a file""" s = '' for k, v in kwargs.items(): s += '%s=\'%s\'\n' % (k, v) with open(f, 'w') as fh: fh.write(s) jj = jarjar() print('-- vanilla') print('channel', jj.default_channel) print('message', jj.default_message) print('webhook', jj.default_webhook) print() writetofile('.jarjar', webhook='1', channel='2', message='3') jj = jarjar() print('-- inferred .jarjar') print('channel', jj.default_channel) print('message', jj.default_message) print('webhook', jj.default_webhook) print() os.remove('.jarjar') writetofile('.jjconfig', webhook='4', channel='5', message='6') jj = jarjar(config='.jjconfig') print('-- specified .jjconfig') print('channel', jj.default_channel) print('message', jj.default_message) print('webhook', jj.default_webhook) print() os.remove('.jjconfig')
python