def notifications(self, from_date=None, to_date=None): if not to_date: to_date = date2str(datetime.utcnow()) from_date = date2str(datetime.utcnow() - timedelta(days=7)) poll_url = self.baseURL + "/SentNotifications/" + self.token + \ "?from=" + from_date + "&to=" + to_date print(poll_url) response = requests.get(poll_url) data = response.json().get("Result") print(len(data), json.dumps(data, indent=4)) return data
def sensor_data(self, sensor_id, from_date=None, to_date=None): if not to_date: to_date = date2str(datetime.utcnow()) from_date = date2str(datetime.utcnow() - timedelta(days=7)) poll_url = self.baseURL + "/SensorDataMessages/" + self.token + "?" +\ "sensorID=" + str(sensor_id) + "&fromDate=" + from_date + \ "&toDate=" + to_date response = requests.get(poll_url) data = None print(response.status_code, response.reason) if response.status_code == 200: data = response.json().get("Result") print(json.dumps(data, indent=4)) return data
def save_segments(original_image, segments, image_saved_path): original_image = original_image.split(".")[0] output_names = [] time = date2str() for k, segment in enumerate(segments): pil_object = Image.fromarray(segment.astype('uint8'), 'RGB') output_name = original_image + "segment{0}_{1}.jpg".format(k, time) output_names.append(output_name) pil_object.save(image_saved_path + output_name) return output_names
def _async_select(self, sql, vals, inst=None, **kwargs): ret = [] max_id = 0 key_set = set() auto_convert = False account_max_id = False mk_timestamp = False set_key = None if kwargs.has_key("auto_convert"): auto_convert = kwargs["auto_convert"] if kwargs.has_key("account_max_id"): account_max_id = kwargs["account_max_id"] if kwargs.has_key("mk_timestamp"): mk_timestamp = kwargs["mk_timestamp"] if kwargs.has_key("set_key"): set_key = kwargs["set_key"] if not vals: vals = None if not inst: inst = self.get_mysql_inst(**kwargs) cursor = yield inst.execute(sql, vals) for row in cursor: tmp = {} for i in range(0, len(row)): colval = row[i] if auto_convert: if isinstance(colval, datetime.date) or isinstance( colval, datetime.datetime): if mk_timestamp: colval = utils.date2int(colval) else: colval = utils.date2str(colval) elif isinstance(colval, float): colval = str(colval) tmp[cursor.description[i][0]] = colval if account_max_id and cursor.description[i][ 0] == "id" and colval > max_id: max_id = colval if set_key and cursor.description[i][0] == set_key: key_set.add(colval) ret.append(tmp) tmp_ret = [ret] if account_max_id: tmp_ret.append(max_id) if set_key: tmp_ret.append(key_set) if len(tmp_ret) > 1: raise gen.Return(tuple(tmp_ret)) else: raise gen.Return(tmp_ret[0])
def save_masks(original_image, masks, image_saved_path): original_image = original_image.split(".")[0] output_names = [] time = date2str() for k, mask in enumerate(masks): def img_frombytes(data): size = data.shape[::-1] databytes = np.packbits(data, axis=1) return Image.frombytes(mode='1', size=size, data=databytes) pil_object = img_frombytes(mask) output_name = original_image + "mask{0}_{1}.jpg".format(k, time) output_names.append(output_name) pil_object.save(image_saved_path + output_name) return output_names
def train(model, epochs, train_loader, val_loader=None, frequency=5): config = get_light_mask_rcnn_config() learning_rate = config['learning_rate'] decay_param = config['decay_param'] num_warmup_steps = config['num_warmup_steps'] checkpoint_path = config['checkpoints'] optimizer = AdamW(model.parameters(), lr=learning_rate, eps=1e-8) num_training_steps = len(train_loader) * epochs scheduler = get_linear_schedule_with_warmup( optimizer, num_warmup_steps=num_warmup_steps, num_training_steps=num_training_steps) device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") #device = torch.device("cpu") model.to(device) record = [] for epoch in range(epochs): timestamp = date2str() metric_logger = train_one_epoch(model, optimizer, scheduler, train_loader, device, epoch, frequency, num_warmup_steps, True) record.append(metric_logger) if val_loader is not None: coco_evaluator = evaluate(model, val_loader, device) file_name = checkpoint_path + 'coco_evaluator_{0}.pkl'.format( timestamp) with open(file_name.format(timestamp), 'wb') as f: pickle.dump(coco_evaluator, f) if epoch % frequency == 0: save_checkpoint( { 'epoch': epoch, 'state_dict': model.state_dict(), 'optimizer': optimizer.state_dict(), 'scheduler': scheduler.state_dict(), }, checkpoint_path, timestamp=timestamp) file_name = checkpoint_path + 'loss_{0}.pkl'.format(timestamp) with open(file_name.format(timestamp), 'wb') as f: pickle.dump(record, f) file_name = checkpoint_path + "resnet_101.pth" torch.save(model.state_dict(), file_name)
table = HtmlTable(border=1, cellpadding=3) # table header header = ['#', '<a href= "main2.py?order_by=title">Title</a>', '<a href= "main2.py?order_by=author">Author</a>', '<a href= "main2.py?order_by=notes">Notes</a>', '<a href= "main2.py?order_by=when_read">Date</a>'] table.addHeader(header) # table body i = 0 for (book_id, title, author, notes, when_read) in results: i += 1 href = '<a href="detail.py?book_id=%d">%s' % (book_id, title) date = '<nobr>%s</nobr>' % date2str(when_read) table.addRow([i, href, author, notes, date]) # Output HTML print 'Content-Type: text/html\n' print "<html>" print """ <head> <title> Read A Book! </title> <link href="css/main.css" rel="stylesheet" type="text/css"> </head>""" print "<body>" print "<h3>Books, Authors, and Notes</h3>"
if value == None: value= 'Unknown' #Special Handeling for binary field published if key =='published': key = 'Published' if key == 'Published' and value == 1: value = 'Yes' if key == 'Published' and value == 0: value = 'No' #Special handeling for date output if key == 'when_read': key= 'Date Read' value =date2str(value) # form +='''<br> # %s: <input type = "textfield" name = "entry" value = "%s"><br> # '''% (key, value) if key == 'title': key = 'Title' form += '''<br> %s: <input type = "textfield" name = "entry" value = "%s"><br> '''% (key, value) if key == 'last_name': key = 'Author Last Name' form += '''<br> %s: <input type = "textfield" name = "entry" value = "%s">
def __doc_acu(request, id, mode): ''' Anon/Create/Update :param id:int - uuid (anon/create) or doc id (update) :param mode:int (0: anon (print), 1: create, 2: update) ''' __log_request(request) if (mode == 2): item = models.Doc.objects.get(pk=id) # Update only uuid = item.type if (uuid not in moduledict): return 'Template not found' else: uuid = id if (request.method == 'POST') and (mode > 0) and (request.POST.get('_action', None) in set(('print', 'view'))): mode = 0 tpl = moduledict[uuid] # 1. check <pkg>.ANON/CREATE/UPDATE self_func = [K_T_F_ANON, K_T_F_ADD, K_T_F_EDIT][mode] if (self_func in tpl[K_V_MODULE].__dict__): return tpl[K_V_MODULE].__dict__[self_func](request, id) # else: # 2. get FORM and FORMSETS formclass = tpl[K_T_FORM] formsetsclass = tpl[K_T_FORMSETS] # SortedDict of dicts if request.method == 'POST': #pprint.pprint(request.POST['_action']) form = formclass(request.POST) if (mode == 0): # ANON, Create/Update -> view/print del form.fields[K_T_F_NAME] formlist = SortedDict() isvalid = form.is_valid() for k, formset in formsetsclass.iteritems(): formlist[k] = formset(request.POST, prefix=k) isvalid = isvalid and formlist[k].is_valid() if isvalid: data = form.cleaned_data # inject formsets into data for k, v in formlist.iteritems(): dataset = list() for i in v.cleaned_data: # list of dicts if i: # reject empty dicts dataset.append(i) if dataset: # reject empty lists data[k] = dataset # inject datasets into data __try_to_call(tpl, K_T_F_POST_FORM, data) # split if (mode == 0): # ANON > PRINT, C/U -> V/P if ((K_T_T in tpl[K_V_MODULE].DATA) and (K_T_T_PRINT in tpl[K_V_MODULE].DATA[K_T_T])): context_dict = {'data': data} template = tpl[K_V_MODULE].DATA[K_T_T][K_T_T_PRINT] if (request.POST.get('_action', None) == u'view'): __try_to_call(tpl, K_T_F_PRE_VIEW, data) # Create/Update -> View return converter.html2html(request, context_dict, template) else: # Anon/Create/Update -> PRINT __try_to_call(tpl, K_T_F_PRE_PRINT, data) return __doc_print(request, context_dict, template) else: # tmp dummy return redirect('dox.views.index') else: # CREATE/UPDATE -> SAVE if (mode == 1): # CREATE name = data[K_T_F_NAME] else: item.name = data[K_T_F_NAME] del data[K_T_F_NAME] # convert dates if (K_V_DATES in tpl): for k in tpl[K_V_DATES]: utils.date2str(data, k) __try_to_call(tpl, K_T_F_PRE_SAVE, data) if (mode == 1): # CREATE # user, type, name, data item = models.Doc(user=request.user, type=uuid, name=name, data=json.dumps(data, indent=1, ensure_ascii=False)) else: item.data = json.dumps(data, indent=1, ensure_ascii=False) item.save() return redirect(doc_r, id=item.pk) else: # GET if (mode < 2): # ANON, CREATE form = formclass() if (mode == 0): # ANON del form.fields[K_T_F_NAME] formlist = SortedDict() for k, formset in formsetsclass.iteritems(): formlist[k] = formset(prefix=k) else: # UPDATE data = json.loads(item.data) data[K_T_F_NAME] = item.name # inject name # restore dates after loading if (K_V_DATES in tpl): for k in tpl[K_V_DATES]: utils.str2date(data, k) #pprint.pprint(data) __try_to_call(tpl, K_T_F_POST_LOAD, data) #pprint.pprint(data) __try_to_call(tpl, K_T_F_PRE_FORM, data) # split form and formsets # 1. eject formsets formlist = SortedDict([]) for pfx, formset in formsetsclass.iteritems(): # formsetsclass == SortedDict {name: FormSetClass} formset_data = dict() for i, l in enumerate(data.get(pfx, list())): # l:str - formset name; l: for k, v in l.iteritems(): formset_data[pfx+'-'+str(i)+'-'+k] = v formset_data.update({ pfx+'-TOTAL_FORMS': len(data[pfx]) if pfx in data else 1, pfx+'-INITIAL_FORMS': u'0', pfx+'-MAX_NUM_FORMS': u'', }) formlist[pfx] = formset(formset_data, prefix=pfx) if (pfx in data): del data[pfx] # 2. else form = formclass(data) return render_to_response( tpl[K_V_MODULE].DATA[K_T_T][K_T_T_FORM] if ((K_T_T in tpl[K_V_MODULE].DATA) and (K_T_T_FORM in tpl[K_V_MODULE].DATA[K_T_T])) else 'auto_form.html', context_instance=RequestContext(request, { 'name': tpl[K_V_MODULE].DATA[K_T_NAME], 'comments': tpl[K_V_MODULE].DATA[K_T_COMMENTS], 'legend': tpl[K_V_MODULE].DATA.get(K_T_LEGEND, ''), 'uuid': tpl[K_V_MODULE].DATA[K_T_UUID], 'form': form, 'formlist': formlist, 'example': tpl[K_V_MODULE].DATA.get('example', None), }))