def test_module(self): model = modules.model_search('Powertec 250 PL')[0] p = modules.module(model) self.assertAlmostEquals(p.Vmax(-10),42.3129) self.assertAlmostEquals(p.Vdc(),31.28) self.assertAlmostEquals(p.Idc(),8.01) self.assertAlmostEquals(p.Vmin(40),24.931724) self.assertAlmostEquals(p.output(900),225.49752)
def test_tools_fill(self): m = "Mage Solar : USA Powertec Plus 250-6 MNCS" ms = modules.module(m) zc = '27713' system = inverters.inverter("SMA America: SB7000US-11 277V",modules.pvArray(ms,[14]*2)) sols = design.tools_fill(system,zc,mount="Roof") ans = ['8266.5W : 11S x 3P : ratio 1.18 : 265.0 - 467.0 V', '6012.0W : 12S x 2P : ratio 0.86 : 290.0 - 510.0 V', '9018.0W : 12S x 3P : ratio 1.29 : 290.0 - 510.0 V', '6513.0W : 13S x 2P : ratio 0.93 : 314.0 - 552.0 V', '9769.5W : 13S x 3P : ratio 1.4 : 314.0 - 552.0 V', '7014.0W : 14S x 2P : ratio 1.0 : 338.0 - 595.0 V', '10521.0W : 14S x 3P : ratio 1.5 : 338.0 - 595.0 V'] self.assertAlmostEquals(ans,sols)
def sqlexe(req, res): ''' sql注入点,可以直接传递sql语句 ''' # cookie = logined(cookiec, req) # if not cookie: # yield '没有登陆'.encode('utf-8') # return try: reqdata = req['params'] sql = reqdata['sql'].replace('+', ' ') table = modules.module(None) result = table.exe(sql) table.commit() # 允许ajax跨域请求 simpleresponse(res, [ ('Access-Control-Allow-Origin', '*'), ('Access-Control-Allow-Methods', 'POST'), ('Access-Control-Allow-Headers', 'x-requested-with,content-type') ]) yield bytes(json.dumps(result), 'utf-8') except Exception as e: res500(res) yield bytes(str(e), 'utf-8')
# fill has moved to design if __name__ == "__main__": from design import tools_fill as fill import inverters import modules zc='44701' zc='27713' #zc='44050' #zc='23173' #m = "Suntech Power : STP245-20-Wd" #m = "Mage Solar : Powertec Plus 285-6 PL" #m = "Mage Solar : Powertec Plus 245-6 PL *" m = "Mage Solar : USA Powertec Plus 250-6 MNCS" ms = modules.module(m) system = inverters.inverter("Refusol: 20 kW 480V",modules.pvArray(ms,[11]*6)) print fill(system,zc) #system = inverters.inverter("Refusol: 24 kW 480V",modules.pvArray(ms,11,6)) print fill(system,zc) system = inverters.inverter("SMA America: SB7000US-11 277V",modules.pvArray(ms,[14]*2)) print fill(system,zc,mount="Roof") system = inverters.inverter("SMA America: SB8000US-11 277V",modules.pvArray(ms,[14]*2)) print fill(system,zc) #iname = "Shanghai Chint Power Systems: CPS SCE7KTL-O US (240V) 240V" iname = "Refusol: 24 kW 480V" #system = inverters.inverter(iname,modules.pvArray(m,1,1)) #system = inverters.inverter(iname,modules.pvArray(modules.module(m),1,1)) #fill(system,zc,1000)
def jsonToSystem(jsonDescription): """Load a system from a json description""" #todo: this is getting unweildy should probably be refactored jsonShape = [] orientations = [] for i in jsonDescription["array"]: o = {} scale = 1 if "scale" in i: scale = i["scale"] if "quantity" in i: scale = i["quantity"] if "shape" in i: shape = [] for string in i["shape"]: if "parallel" in string: shape +=[string["series"]]*string["parallel"] else: shape.append(string["series"]) else: if "series" in i: series = i["series"] else: series = 1 if "parallel" in i: parallel = i["parallel"] else: parallel = 1 shape = [series]*parallel if "tilt" in i: o["tilt"] = i["tilt"] else: o["tilt"] = jsonDescription["tilt"] if "azimuth" in i: o["azimuth"] = i["azimuth"] else: o["azimuth"] = jsonDescription["azimuth"] orientations.append(o) block = inverters.inverter(i["inverter"], \ modules.pvArray(modules.module(i["panel"]),\ shape),(o["azimuth"],o["tilt"])) #i["series"],i["parallel"])) if "derate" in i: block.derate = i["derate"] jsonShape += [ block ] * scale plant = system(jsonShape) plant.setZipcode(jsonDescription["zipcode"]) if "address" in jsonDescription: plant.address = jsonDescription["address"] try: g = geocoders.GoogleV3() place, (lat, lng) = g.geocode(plant.address) plant.place = lat,lng #print "%s, %s Geolocated" % plant.place except: pass #print "%s, %s location from zipcode" % plant.place #print orientations #print set(["%s_%s" % (i['azimuth'],i['tilt']) for i in orientations]) if len(set(["%s_%s" % (i['azimuth'],i['tilt']) for i in orientations])) > 1: print "WARNING: multiple tilts not implimented" plant.tilt = o[0]["tilt"] plant.azimuth = o[0]["azimuth"] elif ("tilt" in jsonDescription and "azimuth" in jsonDescription): plant.tilt = jsonDescription["tilt"] plant.azimuth = jsonDescription["azimuth"] else: "maybe incomplete" plant.tilt = orientations[0]["tilt"] plant.azimuth = orientations[0]["azimuth"] if 'shade' in jsonDescription: plant.hourlyShade = pathfinder.hourly(jsonDescription['shade']) plant.phase = jsonDescription["phase"] plant.voltage = jsonDescription["voltage"] plant.systemName = jsonDescription["system_name"] return plant
print "%s USAF: %s" % (name, usaf) print "Minimum Temperature: %s C" % minimum(usaf) print "2%% Max: %s C" % twopercent(usaf) print "Heating Degree days: %s" % hdd(usaf) print "Cooling Degree days: %s" % cdd(usaf) if args['mname']: print "" import modules models = modules.model_search(args['mname'].split(' ')) m = None if len(models) > 1: for i in models: print i sys.exit(1) elif len(models) == 1: print models[0] m = modules.module(models[0]) else: print "Model not found" sys.exit(1) print "Maximum: %sV" % m.Vmax(minimum(usaf)) print "Max in series", int(maxVoltage/m.Vmax(minimum(usaf))) print "Minimum: %sV" % m.Vmin(twopercent(usaf)) except (KeyboardInterrupt, SystemExit): sys.exit(1) except: raise
import random import time import messagesend import modules from tools.cache import * from tools.cookie import * from tools.render import * from tools.response import * message = Cache('message') cookiec = Cache('cookie') # 设置缓存 # 实例化数据表对象 usertable = modules.module('user') movietable = modules.module('Movie') urltable = modules.module('movieUrl') actortable = modules.module('actor') actmovietable = modules.module('actor-movie') clatable = modules.module('class') clamovietable = modules.module('class-movie') regtable = modules.module('region') regmovietable = modules.module('region-movie') dynamictable = modules.module('dynamic') collectiontable = modules.module('collection') datatuple = ('id', '片名', '海报', '简介', '评分') def finduser(req, res):
def train(): ## Get imageNet dataset file queue for task1 and task2 tr_data1, tr_label1 = imagenet_data.create_file_queue( FLAGS.imagenet_data_dir1) tr_data2, tr_label2 = imagenet_data.create_file_queue( FLAGS.imagenet_data_dir2) ## TASK 1 sess = tf.InteractiveSession() # Input placeholders with tf.name_scope('input'): x = tf.placeholder(tf.float32, [None, 224 * 224 * 3], name='x-input') y_ = tf.placeholder(tf.float32, [None, 10], name='y-input') with tf.name_scope('input_reshape'): image_shaped_input = tf.reshape(x, [-1, 224, 224, 3]) tf.summary.image('input', image_shaped_input, 2) # geopath_examples geopath = modules.geopath_initializer(FLAGS.L, FLAGS.M) # fixed weights list fixed_list = np.ones((FLAGS.L, FLAGS.M), dtype=str) for i in range(FLAGS.L): for j in range(FLAGS.M): fixed_list[i, j] = '0' # Hidden Layers weights_list = np.zeros((FLAGS.L, FLAGS.M), dtype=object) biases_list = np.zeros((FLAGS.L, FLAGS.M), dtype=object) # model define layer_modules_list = np.zeros(FLAGS.M, dtype=object) # conv layer i = 0 for j in range(FLAGS.M): layer_modules_list[j], weights_list[i, j], biases_list[ i, j] = modules.conv_module(image_shaped_input, FLAGS.filt, [11, 11], geopath[i, j], 1, 'layer' + str(i + 1) + "_" + str(j + 1)) net = np.sum(layer_modules_list) / FLAGS.M # dimensionality_reduction layer i = 1 for j in range(FLAGS.M): layer_modules_list[j], weights_list[i, j], biases_list[ i, j] = modules.Dimensionality_reduction_module( net, FLAGS.filt / 2, geopath[i, j], 'layer' + str(i + 1) + "_" + str(j + 1)) net = np.sum(layer_modules_list) / FLAGS.M # res_fire layer i = 2 for j in range(FLAGS.M): layer_modules_list[j], weights_list[i, j], biases_list[ i, j] = modules.res_fire_layer( net, FLAGS.filt / 2, geopath[i, j], 'layer' + str(i + 1) + "_" + str(j + 1)) net = np.sum(layer_modules_list) / FLAGS.M # dimensionality_reduction layer i = 3 for j in range(FLAGS.M): layer_modules_list[j], weights_list[i, j], biases_list[ i, j] = modules.Dimensionality_reduction_module( net, FLAGS.filt / 2, geopath[i, j], 'layer' + str(i + 1) + "_" + str(j + 1)) net = np.sum(layer_modules_list) / FLAGS.M # reshape before full connection layer _shape = net.shape[1:] _length = 1 for _i in _shape: _length *= int(_i) net = tf.reshape(net, [-1, _length]) # model1 layer i = 4 for j in range(FLAGS.M): layer_modules_list[j], weights_list[i, j], biases_list[ i, j] = modules.module(net, FLAGS.full_connection_filt, geopath[i, j], 'layer' + str(i + 1) + "_" + str(j + 1)) net = np.sum(layer_modules_list) / FLAGS.M # output layer y, output_weights, output_biases = modules.nn_layer( net, 10, 'output_layer') # Cross Entropy with tf.name_scope('cross_entropy'): diff = tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y) with tf.name_scope('total'): cross_entropy = tf.reduce_mean(diff) tf.summary.scalar('cross_entropy', cross_entropy) # Need to learn variables var_list_to_learn = [] + output_weights + output_biases for i in range(FLAGS.L): for j in range(FLAGS.M): if (fixed_list[i, j] == '0'): var_list_to_learn += weights_list[i, j] + biases_list[i, j] # GradientDescent with tf.name_scope('train'): train_step = tf.train.GradientDescentOptimizer( FLAGS.learning_rate).minimize(cross_entropy, var_list=var_list_to_learn) # Accuracy with tf.name_scope('accuracy'): with tf.name_scope('correct_prediction'): correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) with tf.name_scope('accuracy'): accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) tf.summary.scalar('accuracy', accuracy) # Merge all the summaries and write them out to /tmp/tensorflow/mnist/logs/mnist_with_summaries (by default) merged = tf.summary.merge_all() train_writer = tf.summary.FileWriter(FLAGS.log_dir + '/train1', sess.graph) test_writer = tf.summary.FileWriter(FLAGS.log_dir + '/test1') # init tf.global_variables_initializer().run() tf.local_variables_initializer().run() # start data reading queue coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) # Generating randomly geopath geopath_set = np.zeros(FLAGS.candi, dtype=object) for i in range(FLAGS.candi): geopath_set[i] = modules.get_geopath(FLAGS.L, FLAGS.M, FLAGS.N) # parameters placeholders and ops var_update_ops = np.zeros(len(var_list_to_learn), dtype=object) var_update_placeholders = np.zeros(len(var_list_to_learn), dtype=object) for i in range(len(var_list_to_learn)): var_update_placeholders[i] = tf.placeholder( var_list_to_learn[i].dtype, shape=var_list_to_learn[i].get_shape()) var_update_ops[i] = var_list_to_learn[i].assign( var_update_placeholders[i]) # geopathes placeholders and ops geopath_update_ops = np.zeros((len(geopath), len(geopath[0])), dtype=object) geopath_update_placeholders = np.zeros((len(geopath), len(geopath[0])), dtype=object) for i in range(len(geopath)): for j in range(len(geopath[0])): geopath_update_placeholders[i, j] = tf.placeholder( geopath[i, j].dtype, shape=geopath[i, j].get_shape()) geopath_update_ops[i, j] = geopath[i, j].assign( geopath_update_placeholders[i, j]) acc_geo = np.zeros(FLAGS.B, dtype=float) summary_geo = np.zeros(FLAGS.B, dtype=object) for i in range(FLAGS.max_steps): # Select Candidates to Tournament compet_idx = range(FLAGS.candi) np.random.shuffle(compet_idx) compet_idx = compet_idx[:FLAGS.B] # Learning & Evaluating for j in range(len(compet_idx)): # Insert Candidate modules.geopath_insert(sess, geopath_update_placeholders, geopath_update_ops, geopath_set[compet_idx[j]], FLAGS.L, FLAGS.M) acc_geo_tr = 0 for k in range(FLAGS.T): ''' print(x.shape) print(tr_data1[k*FLAGS.batch_num:(k+1)*FLAGS.batch_num,:].shape) print(y.shape) print(tr_label1[k*FLAGS.batch_num:(k+1)*FLAGS.batch_num,:].shape) ''' tr_data1_val, tr_label1_val = imagenet_data.read_batch( sess, tr_data1, tr_label1, FLAGS.batch_num, FLAGS.imagenet_data_dir1) summary_geo_tr, _, acc_geo_tmp = sess.run( [merged, train_step, accuracy], feed_dict={ x: tr_data1_val, y_: tr_label1_val }) acc_geo_tr += acc_geo_tmp acc_geo[j] = acc_geo_tr / FLAGS.T summary_geo[j] = summary_geo_tr # Tournament winner_idx = np.argmax(acc_geo) acc = acc_geo[winner_idx] summary = summary_geo[winner_idx] # Copy and Mutation for j in range(len(compet_idx)): if (j != winner_idx): geopath_set[compet_idx[j]] = np.copy( geopath_set[compet_idx[winner_idx]]) geopath_set[compet_idx[j]] = modules.mutation( geopath_set[compet_idx[j]], FLAGS.L, FLAGS.M, FLAGS.N) train_writer.add_summary(summary, i) print('Training Accuracy at step %s: %s' % (i, acc)) if acc >= 0.5: step_task1 = i task1_optimal_path = geopath_set[compet_idx[winner_idx]] print('Task1 Optimal Path is as followed.') print(task1_optimal_path) break # Fix task1 Optimal Path for i in range(FLAGS.L): for j in range(FLAGS.M): if (task1_optimal_path[i, j] == 1.0): fixed_list[i, j] = '1' # Get variables of fixed list var_list_to_fix = [] #var_list_to_fix=[]+output_weights+output_biases; for i in range(FLAGS.L): for j in range(FLAGS.M): if (fixed_list[i, j] == '1'): var_list_to_fix += weights_list[i, j] + biases_list[i, j] var_list_fix = modules.parameters_backup(var_list_to_fix) # parameters placeholders and ops var_fix_ops = np.zeros(len(var_list_to_fix), dtype=object) var_fix_placeholders = np.zeros(len(var_list_to_fix), dtype=object) for i in range(len(var_list_to_fix)): var_fix_placeholders[i] = tf.placeholder( var_list_to_fix[i].dtype, shape=var_list_to_fix[i].get_shape()) var_fix_ops[i] = var_list_to_fix[i].assign(var_fix_placeholders[i]) ## TASK 2 # Need to learn variables var_list_to_learn = [] + output_weights + output_biases for i in range(FLAGS.L): for j in range(FLAGS.M): if (fixed_list[i, j] == '0'): var_list_to_learn += weights_list[i, j] + biases_list[i, j] for i in range(FLAGS.L): for j in range(FLAGS.M): if (fixed_list[i, j] == '1'): tmp = biases_list[i, j][0] break break # Initialization merged = tf.summary.merge_all() train_writer = tf.summary.FileWriter(FLAGS.log_dir + '/train2', sess.graph) test_writer = tf.summary.FileWriter(FLAGS.log_dir + '/test2') tf.global_variables_initializer().run() tf.local_variables_initializer().run() # Update fixed values modules.parameters_update(sess, var_fix_placeholders, var_fix_ops, var_list_fix) # GradientDescent with tf.name_scope('train'): train_step = tf.train.GradientDescentOptimizer( FLAGS.learning_rate).minimize(cross_entropy, var_list=var_list_to_learn) # Generating randomly geopath geopath_set = np.zeros(FLAGS.candi, dtype=object) for i in range(FLAGS.candi): geopath_set[i] = modules.get_geopath(FLAGS.L, FLAGS.M, FLAGS.N) # parameters placeholders and ops var_update_ops = np.zeros(len(var_list_to_learn), dtype=object) var_update_placeholders = np.zeros(len(var_list_to_learn), dtype=object) for i in range(len(var_list_to_learn)): var_update_placeholders[i] = tf.placeholder( var_list_to_learn[i].dtype, shape=var_list_to_learn[i].get_shape()) var_update_ops[i] = var_list_to_learn[i].assign( var_update_placeholders[i]) acc_geo = np.zeros(FLAGS.B, dtype=float) summary_geo = np.zeros(FLAGS.B, dtype=object) for i in range(FLAGS.max_steps): # Select Candidates to Tournament compet_idx = range(FLAGS.candi) np.random.shuffle(compet_idx) compet_idx = compet_idx[:FLAGS.B] # Learning & Evaluating for j in range(len(compet_idx)): geopath_insert = np.copy(geopath_set[compet_idx[j]]) for l in range(FLAGS.L): for m in range(FLAGS.M): if (fixed_list[l, m] == '1'): geopath_insert[l, m] = 1.0 # Insert Candidate modules.geopath_insert(sess, geopath_update_placeholders, geopath_update_ops, geopath_insert, FLAGS.L, FLAGS.M) acc_geo_tr = 0 for k in range(FLAGS.T): tr_data2_val, tr_label2_val = imagenet_data.read_batch( sess, tr_data2, tr_label2, FLAGS.batch_num, FLAGS.imagenet_data_dir2) summary_geo_tr, _, acc_geo_tmp = sess.run( [merged, train_step, accuracy], feed_dict={ x: tr_data2_val, y_: tr_label2_val }) acc_geo_tr += acc_geo_tmp acc_geo[j] = acc_geo_tr / FLAGS.T summary_geo[j] = summary_geo_tr # Tournament winner_idx = np.argmax(acc_geo) acc = acc_geo[winner_idx] summary = summary_geo[winner_idx] # Copy and Mutation for j in range(len(compet_idx)): if (j != winner_idx): geopath_set[compet_idx[j]] = np.copy( geopath_set[compet_idx[winner_idx]]) geopath_set[compet_idx[j]] = modules.mutation( geopath_set[compet_idx[j]], FLAGS.L, FLAGS.M, FLAGS.N) train_writer.add_summary(summary, i) print('Training Accuracy at step %s: %s' % (i, acc)) if acc >= 0.5: step_task2 = i task2_optimal_path = geopath_set[compet_idx[winner_idx]] print('Task2 Optimal Path is as followed.') print(task2_optimal_path) break # close data reading queue coord.request_stop() coord.join(threads) overlap = 0 for i in range(len(task1_optimal_path)): for j in range(len(task1_optimal_path[0])): if (task1_optimal_path[i, j] == task2_optimal_path[i, j]) & ( task1_optimal_path[i, j] == 1.0): overlap += 1 print("ImageNet,TASK1:" + str(step_task1) + ",TASK2:" + str(step_task2) + ", Overlap:" + str(overlap)) train_writer.close() test_writer.close()
import modules tb=modules.module('movies') print(tb.exe('select id,片名 from movies')) print(tb.exe('desc movies'))
import requests from lxml import html import modules # 目录页电影个数 pagelimit = 20 zt = 0 # 目录页 homeurl = 'https://movie.douban.com/j/search_subjects' # 实例化会话对象 session = requests.Session() # 实例化数据表对象 movietable = modules.module('Movie') urltable = modules.module('movieUrl') actortable = modules.module('actor') actmovietable = modules.module('actor-movie') clatable = modules.module('class') clamovietable = modules.module('class-movie') regtable = modules.module('region') regmovietable = modules.module('region-movie') def getid(table): id = table.exe('select max(id) from {}'.format(table.table))[0][0] return id if id else 0 movieid = getid(movietable)