def range(self, start, stop=None, months=0, days=0, granularity=None): """ Define a date range for the report. * start -- The start date of the report. If stop is not present it is assumed to be the to and from dates. * stop (optional) -- the end date of the report (inclusive). * months (optional, named) -- months to run used for relative dates * days (optional, named)-- days to run used for relative dates) * granulartiy (optional, named) -- set the granularity for the report """ start = utils.date(start) stop = utils.date(stop) if days or months: stop = start + relativedelta(days=days - 1, months=months) else: stop = stop or start if start == stop: self.raw['date'] = start.isoformat() else: self.raw.update({ 'dateFrom': start.isoformat(), 'dateTo': stop.isoformat(), }) if granularity: self.raw = self.granularity(granularity).raw return self
def range(self, start, stop=None, months=0, days=0, granularity=None): start = utils.date(start) stop = utils.date(stop) if days or months: stop = start + relativedelta(days=days-1, months=months) else: stop = stop or start if start == stop: self.raw['date'] = start.isoformat() else: self.raw.update({ 'dateFrom': start.isoformat(), 'dateTo': stop.isoformat(), }) if granularity: if granularity not in self.GRANULARITY_LEVELS: levels = ", ".join(self.GRANULARITY_LEVELS) raise ValueError("Granularity should be one of: " + levels) self.raw['dateGranularity'] = granularity return self
def create_tra(service=SERVICE, ttl=2400): "Crear un Ticket de Requerimiento de Acceso (TRA)" tra = SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?>' '<loginTicketRequest version="1.0">' '</loginTicketRequest>') tra.add_child('header') # El source es opcional. Si falta, toma la firma (recomendado). #tra.header.addChild('source','subject=...') #tra.header.addChild('destination','cn=wsaahomo,o=afip,c=ar,serialNumber=CUIT 33693450239') tra.header.add_child('uniqueId', str(date('U'))) tra.header.add_child('generationTime', str(date('c', date('U') - ttl))) tra.header.add_child('expirationTime', str(date('c', date('U') + ttl))) tra.add_child('service', service) return tra.as_xml()
def create_tra(service=SERVICE,ttl=2400): "Crear un Ticket de Requerimiento de Acceso (TRA)" tra = SimpleXMLElement( '<?xml version="1.0" encoding="UTF-8"?>' '<loginTicketRequest version="1.0">' '</loginTicketRequest>') tra.add_child('header') # El source es opcional. Si falta, toma la firma (recomendado). #tra.header.addChild('source','subject=...') #tra.header.addChild('destination','cn=wsaahomo,o=afip,c=ar,serialNumber=CUIT 33693450239') tra.header.add_child('uniqueId',str(date('U'))) tra.header.add_child('generationTime',str(date('c',date('U')-ttl))) tra.header.add_child('expirationTime',str(date('c',date('U')+ttl))) tra.add_child('service',service) return tra.as_xml()
def multi_clust(tested, score_cutoffs=None, length_cutoffs=None, fracs=[.012,.014], frac_retain=.1, ds=[.1,.25,.3,.35], ms=[.1,.15,.2], penalties=[.1,1], overlaps=[.55], haircuts=[0,.2], max_pval=1, savef=None, runid=None, show_stats=True, pres=None, gold_nspecies=1, gold_splits=None, gold_minlen=3, mdprod_min=.01, **kwargs): runid = runid or random.randrange(1,1000) fracs = (fracs if fracs is not None else [cl.n_thresh(tested, s)/len(tested) for s in score_cutoffs] if score_cutoffs is not None else [le/len(tested) for le in length_cutoffs]) print "random id:", runid clusts = [] params = [fracs, ds, ms, penalties, overlaps, haircuts] products = it.product(*params) for (f,d,m,p,o,h) in products: if d*m >= mdprod_min: cxstruct = cl.filter_clust(ut.list_frac(tested, f), ut.list_frac(tested, frac_retain), merge_cutoff=o, negmult=m, min_density=d, runid=runid, penalty=p, max_pval=max_pval, max_overlap=o, haircut=h, **kwargs) cxstruct.params = ('density=%s,frac=%s,f_retain=%s,negmult=%s,penalty=%s,max_overlap=%s,haircut=%s' % (d,f,frac_retain,m,p,o,h)) clusts.append(cxstruct) if show_stats and len(cxstruct.cxs)>0: if pres is not None and gold_splits is not None: out = cp.select_best(cp.result_stats(pres.species, gold_splits, clusts[-1:], gold_nspecies, min_gold_size=gold_minlen)) else: print "Can't show stats: pres and gold_splits required." if savef and (len(clusts) % 10 == 1): ut.savepy(clusts, ut.pre_ext(savef, "clusts_temp_%s_%s" % (ut.date(), runid))) return clusts, runid
def test_date_functionality(): """Tests different date operations that can be performed.""" orders = Table(("OrderId", int), ("ProductName", str), ("OrderDate", date)) orders.insert(1, "Geitost", date(2008, 11, 11)) orders.insert(2, "Camembert Pierrot", date(2008, 11, 9)) orders.insert(3, "Mozzarella di Giovanni", date(2008, 11, 11)) orders.insert(4, "Mascarpone Fabioloi", date(2008, 10, 29)) assert len(orders.where(ROW.OrderDate == date(2008, 11, 11))) == 2 orders.alter_column("OrderDate", datetime) orders.where(ROW.OrderId == 1).update( OrderDate=datetime(2008, 11, 11, 13, 23, 44)) orders.where(ROW.OrderId == 2).update( OrderDate=datetime(2008, 11, 9, 15, 45, 21)) orders.where(ROW.OrderId == 3).update( OrderDate=datetime(2008, 11, 11, 11, 12, 1)) orders.where(ROW.OrderId == 4).update( OrderDate=datetime(2008, 10, 29, 14, 56, 59)) assert len(orders.where(ROW.OrderDate == datetime(2008, 11, 11))) == 0 assert len( orders.where(ROW.OrderDate == datetime(2008, 11, 11, 13, 23, 44))) == 1
def set_output_directory(parameters, problem, case): "Set and create output directory" # Set output directory if parameters["output_directory"] == "unspecified": if case is None: parameters["output_directory"] = "results-%s-%s" % (problem, date()) else: parameters["output_directory"] = "results-%s-%s" % (problem, str(case)) # Create output directory dir = parameters["output_directory"] if not os.path.exists(dir): os.makedirs(dir)
def read_parameters(): """Read parametrs from file specified on command-line or return default parameters if no file is specified""" # Read parameters from the command-line import sys p = default_parameters() try: file = File(sys.argv[1]) file >> p info("Parameters read from %s." % sys.argv[1]) except: info("No parameters specified, using default parameters.") # Set output directory if p["output_directory"] == "unspecified": p["output_directory"] = "results-%s"%date() return p
def store_parameters(p, problem, case): "Store parameters to file and return filename" # Set output directory if p["output_directory"] == "unspecified": if case is None: p["output_directory"] = "results-%s-%s" % (problem, date()) else: p["output_directory"] = "results-%s-%s" % (problem, str(case)) # Save to file application_parameters.xml file = File("application_parameters.xml") file << p # Save to file <output_directory>/application_parameters.xml filename = "%s/application_parameters.xml" % p["output_directory"] file = File(filename) file << p return filename
def run_local(problem, parameters, case=None): "Run problem on local machine with given parameters." # Set output directory set_output_directory(parameters, problem, case) # Store parameters to file filename = store_parameters(parameters) # Set name of logfile if case is None: logfile = "output-%s-%s.log" % (problem, date()) else: logfile = "output-%s-%s.log" % (problem, str(case)) logfile = os.path.join(parameters["output_directory"], logfile) # Submit job status, output = getstatusoutput("python %s.py %s | tee %s" % (problem, filename, logfile)) return status, output
def main(path): user=utils.user() date=utils.date() duration=utils.audio_duration(path)
def main(path,user): date=utils.date() duration=utils.audio_duration(path) crud.Insert(user,path,duration,date)
def test_column_functions(): """Tests various functions that operate on specified column.""" orders = Table(("O_Id", int), ("OrderDate", date), ("OrderPrice", int), ("Customer", str)) orders.insert(1, date(2008, 11, 12), 1000, "Hansen") orders.insert(2, date(2008, 10, 23), 1600, "Nilsen") orders.insert(3, date(2008, 9, 2), 700, "Hansen") orders.insert(4, date(2008, 9, 3), 300, "Hansen") orders.insert(5, date(2008, 9, 30), 2000, "Jensen") orders.insert(6, date(2008, 10, 4), 100, "Nilsen") order_average = orders.avg("OrderPrice") assert order_average == 950 assert len(orders.where(ROW.OrderPrice > order_average)) == 3 # Test the "count" function. assert orders.where(ROW.Customer == "Nilsen").count("Customer") == 2 assert orders.count() == 6 assert orders.select("Customer").distinct().count("Customer") == 3 # Test the "first" function. print("FirstOrderPrice =", orders.first("OrderPrice")) # Test the "last" function. print("LastOrderPrice =", orders.last("OrderPrice")) # Test the "max_" function. print("LargestOrderPrice =", orders.max_("OrderPrice")) # Test the "min_" function. print("SmallestOrderPrice =", orders.min_("OrderPrice")) # Test the "sum_" function. print("OrderTotal =", orders.sum_("OrderPrice"), "\n") # Test the "group_by" statement. result = Table(("Customer", str), ("OrderPrice", int)) for table in orders.group_by("Customer"): result.insert(table.first("Customer"), table.sum_("OrderPrice")) result.print() orders.insert(7, date(2008, 11, 12), 950, "Hansen") orders.insert(8, date(2008, 10, 23), 1900, "Nilsen") orders.insert(9, date(2008, 9, 2), 2850, "Hansen") orders.insert(10, date(2008, 9, 3), 3800, "Hansen") orders.insert(11, date(2008, 9, 30), 4750, "Jensen") orders.insert(12, date(2008, 10, 4), 5700, "Nilsen") result.truncate().alter_add("OrderDate", date) for table in orders.group_by("Customer", "OrderDate"): result.insert(table.first("Customer"), table.sum_("OrderPrice"), table.first("OrderDate")) result.print()
def main(options): if options.initial_point.lower( ) == 'true' and options.qbits_limit.lower() != 'true': raise Exception( "Can't use initial_point if the number of required qubits varies from one optimization step to the following. Set also qbits_limit to true." ) # fixed values stock_tickers = ['FXD', 'FXR', 'FXL', 'FTXR', 'QTEC'] start_date = date(2017, 1, 1) computation_date = date(2020, 1, 1) end_date = date(2021, 1, 1) #hyperparameters optim_dict = { "quantum_instance": 'qasm_simulator', "shots": 1024, "print": True, "logfile": True, "solver": 'vqe', "optimizer": SLSQP, "maxiter": 1000, "depth": 1, "alpha": 0.35 } # etf collector etfs = {} # building the quantum etf quantum_etf = {} date_ = copy(computation_date) while date_ < end_date: # generate main values for the model prices_, mu_, sigma_ = generate_values(stock_tickers=stock_tickers + ['IFV'], start_date=start_date, end_date=date_) # values for quantum etf prices = prices_[:-1] mu = mu_[:-1] sigma = sigma_[:-1, :-1] # find budget, best allocation, results if len(quantum_etf) == 0: B = copy(options.budget) else: previous_month_etf = copy( quantum_etf[previous_month(date_).strftime('%Y-%m-%d')]) B = previous_month_etf['liquidity'] + np.sum( np.array(previous_month_etf['allocation']) * prices) if options.initial_point.lower() == 'true': print( "Using best parameters of previous optimization as an initial point" ) optim_dict['initial_point'] = last_optimal_params if options.qbits_limit.lower() == 'true': qbits_dict = { k0: max(l0) for k0, l0 in dict_inverse({ j: model_qbits(prices, j, B) for j in range(1, options.max_k) }).items() } if options.max_qbits in qbits_dict: k_ = qbits_dict[options.max_qbits] elif options.max_qbits > max(qbits_dict): k_ = copy(options.max_k) warn( "Number of qbits provided exceeds qbits dictionary. Initialising k as the maximum given." ) else: warn( 'Number of qbits given not found among possible models.Choosing k equal to given value' ) k_ = copy(options.k) else: k_ = copy(options.k) mdl, grouping = qcmodel(prices, k_, B, mu, sigma, options.q) optim_dict["docplex_mod"] = mdl for i in range(options.n_trials): results = aggregator('optimizer', optim_dict) if results['is_qp_feasible']: break # Integer results (amount of groups of stocks): x x_val = [ results['result'].variables_dict[f'x{i}'] for i in range(len(prices)) ] # Amount of individual stock best_allocation = grouping * np.array(x_val) budget_spent = np.sum(best_allocation * prices) # Saves optimal parameters as initial point for next iteration last_optimal_params = results['solver_info']['optimal_params'] #printing tmp_results tmp_results = { 'computational_time': float(results['computational_time']), 'optimal_function_value': float(results['result'].fval), 'status': str(results['result'].status).split('.')[-1], 'is_qp_feasible': results['is_qp_feasible'] } # generate etf datapoint if budget_spent > B: # if budget spent is bigger than current liquidity tmp_results['wrong_results'] = { 'best_allocation_found': [int(i) for i in best_allocation], 'budget_spent': float(budget_spent) } if len(quantum_etf ) == 0: # at step 0, etf does not spend any budget quantum_etf[date_.strftime('%Y-%m-%d')] = { 'allocation': [0] * len(prices), 'prices': [float(p) for p in prices], 'liquidity': B, 'portfolio_value': 0., 'results': tmp_results, 'k': k_ } else: # at step k (any k), etf keeps previous allocation and updates its values with current prices quantum_etf[date_.strftime('%Y-%m-%d')] = { 'allocation': previous_month_etf['allocation'], 'prices': [float(p) for p in prices], 'liquidity': previous_month_etf['liquidity'], 'portfolio_value': float( np.sum( np.array(previous_month_etf['allocation']) * prices)), 'results': tmp_results, 'k': k_ } else: # if budget spent < B tmp_results['wrong_results'] = None quantum_etf[date_.strftime('%Y-%m-%d')] = { 'allocation': [int(i) for i in best_allocation], 'prices': [float(p) for p in prices], 'liquidity': float(B - budget_spent), 'portfolio_value': float(budget_spent), 'results': copy(tmp_results), 'k': k_ } if not os.path.exists(options.savepath): os.makedirs(options.savepath) fs = os.path.join(options.savepath, f'quantum_etf_results.json') with open(fs, 'wt') as qf: json.dump(quantum_etf, qf) # next datapoint date_ = next_month(date_) # building the optimum and real etf # steps separated for clarity only opt_etf = {} real_etf = {} date_ = copy(computation_date) while date_ < end_date: # generate main values for the model prices_, mu_, sigma_ = generate_values(stock_tickers=stock_tickers + ['IFV'], start_date=start_date, end_date=date_) # values for quantum etf prices = prices_[:-1] mu = mu_[:-1] sigma = sigma_[:-1, :-1] # price of real etf ifv_price = prices_[-1] # find budget, best allocation, results if len(opt_etf) == 0: B = copy(options.budget) # real values no_real_bought_stocks = floor(B / ifv_price) real_liquidity = B - no_real_bought_stocks * ifv_price else: previous_month_etf = copy( opt_etf[previous_month(date_).strftime('%Y-%m-%d')]) B = previous_month_etf['liquidity'] + np.sum( np.array(previous_month_etf['allocation']) * prices) if options.qbits_limit == 'true': qbits_dict = { k0: max(l0) for k0, l0 in dict_inverse({ j: model_qbits(prices, j, B) for j in range(1, options.max_k) }).items() } if options.max_qbits in qbits_dict: k_ = qbits_dict[options.max_qbits] elif options.max_qbits > max(qbits_dict): k_ = copy(options.max_k) warn( "Number of qbits provided exceeds qbits dictionary. Initialising k as the maximum given." ) else: warn( 'Number of qbits given not found among possible models.Choosing k equal to given value' ) k_ = copy(options.k) else: k_ = copy(options.k) mdl, grouping = qcmodel(prices, k_, B, mu, sigma, options.q) # solver mdl.solve() sols_dict = dict( zip([f'x{i}' for i in range(len(prices))], [0] * len(prices))) for j, v in mdl.solution.as_name_dict().items(): sols_dict[j] = int(v) x_val = list(sols_dict.values()) # Amount of individual stock best_allocation = grouping * np.array(x_val) budget_spent = np.sum(best_allocation * prices) # generate etf datapoint if budget_spent > B: # if budget spent is bigger than current liquidity if len(opt_etf) == 0: # at step 0, etf does not spend any budget opt_etf[date_.strftime('%Y-%m-%d')] = { 'allocation': [0] * len(prices), 'prices': [float(p) for p in prices], 'liquidity': B, 'portfolio_value': 0., 'objective_value': mdl.solution.get_objective_value() } else: # at step k (any k), etf keeps previous allocation and updates its values with current prices opt_etf[date_.strftime('%Y-%m-%d')] = { 'allocation': previous_month_etf['allocation'], 'prices': [float(p) for p in prices], 'liquidity': previous_month_etf['liquidity'], 'portfolio_value': float( np.sum( np.array(previous_month_etf['allocation']) * prices)), 'objective_value': mdl.solution.get_objective_value() } else: # if budget spent < B opt_etf[date_.strftime('%Y-%m-%d')] = { 'allocation': [int(i) for i in best_allocation], 'prices': [float(p) for p in prices], 'liquidity': float(B - budget_spent), 'portfolio_value': float(budget_spent), 'objective_value': mdl.solution.get_objective_value() } # real etf real_etf[date_.strftime('%Y-%m-%d')] = { 'liquidity': real_liquidity, 'prices': float(ifv_price), 'portfolio_value': no_real_bought_stocks * ifv_price } # etf is saved at every step fs_real = os.path.join(options.savepath, f'real_etf_results.json') with open(fs_real, 'wt') as rf: json.dump(real_etf, rf) fs_opt = os.path.join(options.savepath, f'opt_etf_results.json') with open(fs_opt, 'wt') as rf: json.dump(opt_etf, rf) # next datapoint date_ = next_month(date_) # clearing notebook output clear_output() etfs['quantum'] = copy(quantum_etf) etfs['optimum'] = copy(opt_etf) etfs['IFV'] = copy(real_etf) print_etfs(etfs, savepath=options.savepath) return None
from utils import date from parameters import * def run_local(problem, parameters, case=None): "Run problem on local machine with given parameters." # Set output directory set_output_directory(parameters, problem, case) # Store parameters to file filename = store_parameters(parameters) # Set name of logfile if case is None: logfile = "output-%s-%s.log" % (problem, date()) else: logfile = "output-%s-%s.log" % (problem, str(case)) logfile = os.path.join(parameters["output_directory"], logfile) # Submit job status, output = getstatusoutput("python %s.py %s | tee %s" % (problem, filename, logfile)) return status, output def run_bb(problem, parameters, case=None): "Run problem on bigblue with given parameters." # Store parameters to file