示例#1
0
def show_confirmation():
    """ confirm information about habit to user """

    hour = int(request.args.get("hour"))
    user_id = session['user_id']
    user = User.query.filter(User.user_id == user_id).one()
    tz = user.tz

    # create an arrow object, replace hour and timezone, convert to UTC and format for db
    t = arrow.now()
    time = t.replace(hour=int('{}'.format(hour)), tzinfo='{}'.format(tz))
    utc_time = time.to('UTC')
    utc_time = utc_time.format('YYYY-MM-DD HH:mm:ss ZZ')

    create_habit_id = session['habit_id']

    if 'break_habit_id' not in session:
        break_habit_id = None
    else:
        break_habit_id = session['break_habit_id']
    if 'partner_id' not in session:
        partner_id = None
    else:
        partner_id = session['partner_id']

    habit_id = add_new_habit_return_id(user_id, create_habit_id,
                                       break_habit_id, utc_time, partner_id)

    habit = UserHabit.query.filter(UserHabit.habit_id == habit_id).first()
    send_habit_intro_msg(user_id)

    return render_template('confirm.html', habit=habit)
    def test(self, loader, epoch, name):

        self.model.eval()
        user_id_list, pred_y = [], []
        with torch.no_grad():
            for index, datum_tuple in enumerate(loader):
                creative_id, ad_id, product_id, advertiser_id, industry, product_category, time, user_id, _ = datum_tuple

                advertiser_id, product_id, product_category, industry, time = advertiser_id.to(
                    device, non_blocking=True), product_id.to(
                        device, non_blocking=True), product_category.to(
                            device, non_blocking=True), industry.to(
                                device,
                                non_blocking=True), time.to(device,
                                                            non_blocking=True)

                #获取embedding抽取的向量
                inputlist_tensor = [
                    creative_id, ad_id, advertiser_id, product_id,
                    product_category, industry, time
                ]
                emb_layer_mat = []
                for index, input_col in enumerate(inputlist_tensor):
                    emb_layer_col_mat = {}
                    for j in range(len(self.emb_layer[index])):
                        if index in [2, 3, 4, 5, 6]:
                            self.emb_layer[index][j] = self.emb_layer[index][
                                j].to(device, non_blocking=True)
                        emb_layer_col_mat[j] = self.emb_layer[index][j](
                            input_col)
                        emb_layer_col_mat[j] = emb_layer_col_mat[j].to(
                            device, non_blocking=True)
                    emb_layer_mat.append(emb_layer_col_mat)

                output = self.model(emb_layer_mat)

                pred_y.extend(list(output.cpu().detach().numpy()))
                user_id_list.extend(list(user_id.numpy()))

        output_data = DataFrame({'user_id': user_id_list, 'pred': pred_y})

        if not os.path.isdir(
                '../../oof/bk_oof/Multi_Head_ResNext_seed_{}_aug'.format(
                    self.seed)):
            os.mkdir('../../oof/bk_oof/Multi_Head_ResNext_seed_{}_aug'.format(
                self.seed))

        pickle.dump(
            output_data,
            open(
                '../../oof/bk_oof/Multi_Head_ResNext_seed_{}_aug/{}_{}_folds_{}.pkl'
                .format(self.seed, name, epoch, self.folds), 'wb'))
示例#3
0
    def _process_data_batch(self, data, phase, retain_graph=None):
        if len(data) == 3:
            data, time, event = data
        elif len(data) == 4:  # With patient id (not needed here)
            data, time, event, pid = data
        data = self._data_to_device(data)
        time = time.to(self.device)
        event = event.to(self.device)

        with torch.set_grad_enabled(phase == 'train'):
            feature_representations, risk = self.model(data)
            modality_features = feature_representations['modalities']
            loss = self._compute_loss(risk, time, event, modality_features)

            if phase == 'train':
                # Zero out parameter gradients
                self.optimizer.zero_grad()
                if retain_graph is not None:
                    loss.backward(retain_graph=retain_graph)
                else:
                    loss.backward()
                self.optimizer.step()

        return loss, risk, time, event
示例#4
0
    def forward(self,
                time,
                click_time,
                creative_id,
                ad_id,
                product_id,
                product_category,
                advertiser_id,
                industry,

                advertiser_id_industry,
                product_category_advertiser_id,
                product_category_industry,
                product_id_advertiser_id,
                product_id_industry,
                product_id_product_category,

                target_encode_sequence,
                x_len,
                x_flatten,
                gender = None,\
                age = None):

        time = time.to(args.device)
        click_time = click_time.to(args.device)

        creative_id = creative_id.to(args.device)
        ad_id = ad_id.to(args.device)
        product_id = product_id.to(args.device)
        advertiser_id = advertiser_id.to(args.device)

        advertiser_id_industry = advertiser_id_industry.to(args.device)
        product_category_advertiser_id = product_category_advertiser_id.to(
            args.device)
        product_id_advertiser_id = product_id_advertiser_id.to(args.device)
        product_id_product_category = product_id_product_category.to(
            args.device)

        target_encode_sequence = target_encode_sequence.to(args.device)
        x_len = x_len.to(args.device)
        x_flatten = x_flatten.to(args.device)

        if gender is not None:
            gender = gender.to(args.device)
            age = age.to(args.device)

        a_wv = torch.cat([
            self.ln_50s[0](ad_id),
            self.ln_50s[1](creative_id),
            self.ln_50s[2](product_id_product_category),
            self.ln_50s[3](product_id),
            self.ln_one_hot_target(target_encode_sequence),
            self.position_embeddings(time),
            self.click_time_embeddings(click_time),
        ],
                         dim=-1)

        b_wv = torch.cat([
            self.ln_50s[4](advertiser_id),
            self.ln_50s[5](advertiser_id_industry),
            self.ln_50s[6](product_category_advertiser_id),
            self.ln_50s[7](product_id_advertiser_id),
            self.ln_one_hot_target(target_encode_sequence),
            self.position_embeddings(time),
            self.click_time_embeddings(click_time),
        ],
                         dim=-1)

        hidden = self.re2_block(a_wv, b_wv, x_len)
        cat = torch.cat(
            [self.ln_hidden(hidden),
             self.ln_tfidf_stack(x_flatten)], dim=-1)
        output_age = self.decoder_age(cat)
        output_gender = self.decoder_gender(cat)

        if (gender is None):
            return output_gender, output_age, hidden

        loss_age = nn.CrossEntropyLoss()
        loss_gender = nn.CrossEntropyLoss()
        l_age = loss_age(output_age, age.long())
        l_gender = loss_gender(output_gender, gender.long())

        l = 0.5 * l_gender + 0.5 * l_age

        return l, l_gender, l_age, output_gender, output_age, hidden
示例#5
0
 def view_node_html(self, nid):
     # TODO: Move view code out of model
     ENV = Environment(loader=PackageLoader('controllers', 'templates')) 
     template = ENV.get_template('data.html') 
     # Setup some base variables
     ENV = Environment(loader=PackageLoader('controllers', 'templates')) 
     fields = ['datatype', 'apikey', 'title', 'description', 'lat', 'lon', 
               'createdhuman', 'updated', 'latest', 'nid', 'createdby']
     timeadj = int(self.kwargs['timeadj']) if 'timeadj' in self.kwargs else 0 
     timeadjcalc = (timeadj*60)*60 # Timestamp adjustment for local time
     count = int(self.kwargs['count']) if 'count' in self.kwargs else 3000
     if count > 12000: count = 12000
     countfrom = int(self.kwargs['from']) if 'from' in self.kwargs else 0
     if countfrom < 0: countfrom = 0
     # Now make the query
     #try:
     jsonstr = self.db.readasjson('nodes', fields, [int(nid)])  
     if jsonstr: 
         data = json.loads(jsonstr)
         node = data[0]
         graph = []
         # Speck data display
         if node['datatype'] == 'speck':
             keyarr = ['timestamp', 'raw', 'concentration', 'humidity']
             graph = {'humidity':'humidity','concentration':'particles'}          
         # Frackbox display
         elif node['datatype'] == 'frackbox':
             graph = {' NOppb':'NOppb', ' O3ppb':'O3ppb',' NO2ppb':'NO2ppb',' PIDppm':'PIDppm'}
             keyarr = node['latest']['csvheader'].split(',')
         # Observation
         else:
             node['title'] = 'Observation:<br /> {}'.format(node['title'])
             graph = {}
             keyarr = []
         if 'name' in node['latest']:
             node['title'] = '{} [{}]'.format(node['title'], node['latest']['name'])
         header = '<h2>{}: Created {}</h2><p>{}</p><hr />'.format(node['title'], node['createdhuman'], node['description'])
         # Now bring back some actual data!
         searchfor = {'nid':nid}
         intable = 'csvs'
         returnfields = ['timestamp', 'csv']
         sql = 'ORDER BY timestamp DESC LIMIT {}, {}'.format(countfrom, count) 
         rows = self.db.searchfor(intable, returnfields, searchfor, sql, 'many')
         # And grab a list of annotations: TODO: Think about limits i.e. sql = 'ORDER BY timestamp DESC LIMIT {}, {}'.format(countfrom, count) 
         sql = 'ORDER BY timestamp DESC '
         annotations = self.db.searchfor('annotations', ['aid','timestamp','text', 'nid'], {'nid':nid}, sql, 'many')
         i=0
         try:
             for ano in annotations:
                 timestamp = int(ano[1]+timeadjcalc)
                 mydate = datetime.datetime.fromtimestamp(timestamp).strftime('%d %b %Y %H:%M:%S ({}GMT)'.format(timeadj))
                 annotations[i] = (ano[0], ano[1], ano[2], mydate, ano[3])
                 i+=1
             annotationsjson = json.dumps(annotations)
         except:
             annotationsjson =  "[]"
         # And prep vars used to format the output
         table = '<table class="whitetable"><tr><th>'
         table += '</th><th>'.join(keyarr)+'</th></tr>\n\n\n'
         starttime = ''
         rowdatetime = ''
         # Make a record of the position of the keys
         graphpos = {}
         for item in graph:
             key = item
             mapname = graph[item].replace(' ', '')
             for position, findkey in enumerate(keyarr):
                 if findkey == key:
                     graphpos[key] = {'position':position,'color':"'#000'", 'data':[], 'name':"'{}'".format(mapname)}
         i = 0
         # Now loop through the data and generate data and json. The array is reversed to enable to graph to display
         for row in reversed(rows):
             vals = row[1].split(',')
             # Create a timestamp
             timestamp = int(vals[0]) #+timeadjcalc
             time = arrow.get(timestamp)
             local = time.to('US/Central')
             rowdatetime = local.format('YYYY-MM-DD HH:mm:ss')
             vals[0] = rowdatetime
             if i == 0: starttime = rowdatetime
             # Prep the js
             for key in graph:
                 n = graphpos[key]['position']
                 val = vals[n]
                 if val.replace(' ', '') is not '':
                     graphpos[key]['data'].append( {'x':timestamp, 'y':val} )
             # Prep the HTML
             line = '<tr><td>'
             line += '</td><td>'.join(vals)
             line += '</td></tr>'
             table += line
             i += 1
         # Now prep the final output
         data = []
         for item in graphpos: data.append(graphpos[item])
         jsondata = json.dumps(data)
         jsdata = jsondata.replace('"', '') # Javscript formated data
         jsdata = jsdata.replace(' ', '')
         table += '</table>'
         prevcount = countfrom-count
         nextlink = '<a class="prevnext" href="/api/viewhtml/{}/?count={}&from={}&timeadj={}">Next&raquo;</a>'.format(nid, count, countfrom+count, timeadj)
         if prevcount <= 0: 
             prevcount=0
             prevlink = ''
         if countfrom > 0:
             prevlink = '<a class="prevnext" href="/api/viewhtml/{}/?count={}&from={}&timeadj={}">&#171;Previous</a>'.format(nid, count, prevcount, timeadj)
         header += '<strong>{}</strong> | <strong>View</strong> {} Points <strong> From:</strong> {} <strong>To:</strong> {} | <strong>{}</strong>'.format(prevlink, count, rowdatetime, starttime,  nextlink)
         templatevars = {'nid':nid,'table':table, 'header':header, 'jsdata':jsdata, 'timeadj':timeadj, 'annotationsjson':annotationsjson, 'annotations':annotations}
         return template.render(var=templatevars)  
     else: 
         return 'No data'
    def forward(self,
                time,
                click_time,
                
                creative_id,
                ad_id,
                advertiser_id,
                
                advertiser_id_industry,
                product_category_advertiser_id,
                product_id_advertiser_id,
                product_id_product_category,
                
                product_id,
                target_encode_sequence,
                x_len,
                labels = None):
        
        
        time = time.to(args.device)
        click_time = click_time.to(args.device)
        
        creative_id = creative_id.to(args.device) 
        ad_id = ad_id.to(args.device) 
        advertiser_id = advertiser_id.to(args.device) 
        
        advertiser_id_industry = advertiser_id_industry.to(args.device)
        product_category_advertiser_id = product_category_advertiser_id.to(args.device) 
        product_id_advertiser_id = product_id_advertiser_id.to(args.device)
        product_id_product_category = product_id_product_category.to(args.device) 
        product_id = product_id.to(args.device)
        
        target_encode_sequence = target_encode_sequence.to(args.device) 
        x_len = x_len.to(args.device) 
        
        if labels is not None:
            labels = labels.to(args.device)     
        
        a_wv = torch.cat([
            self.ln_50s[0](ad_id),
            self.ln_50s[1](creative_id),
            self.ln_50s[2](product_id_product_category),
            self.ln_50s[3](product_id),
            self.ln_one_hot_target(target_encode_sequence),
            self.position_embeddings(time),
            self.click_time_embeddings(click_time),
        ],dim=-1)


        b_wv = torch.cat([
            self.ln_50s[4](advertiser_id),
            self.ln_50s[5](advertiser_id_industry),
            self.ln_50s[6](product_category_advertiser_id),
            self.ln_50s[7](product_id_advertiser_id),
            self.ln_one_hot_target(target_encode_sequence),
            self.position_embeddings(time),
            self.click_time_embeddings(click_time),
        ],dim=-1)
        
        
        hidden_wv = self.re2_block_wv(a_wv, b_wv, x_len)

#         cat = torch.cat([hidden_wv, x_flatten], dim = -1)
    
    
        out_label = self.decoder(hidden_wv)

        if(labels is None):
            return out_label
        
        loss_func = nn.CrossEntropyLoss()
            
        l = loss_func(out_label, labels.long())
        
        
        return l, out_label
    def forward(self,
                time,
                click_time,

                ad_id_wv,
                ad_id_glove,
                advertiser_id_wv,
                advertiser_id_glove,

                target_encode_sequence,
                x_len,
                gender = None,\
                age = None):

        time = time.to(args.device)
        click_time = click_time.to(args.device)

        ad_id_wv = ad_id_wv.to(args.device)
        ad_id_glove = ad_id_glove.to(args.device)
        advertiser_id_wv = advertiser_id_wv.to(args.device)
        advertiser_id_glove = advertiser_id_glove.to(args.device)

        target_encode_sequence = target_encode_sequence.to(args.device)
        x_len = x_len.to(args.device)
        #         x_flatten = x_flatten.to(args.device)

        if gender is not None:
            gender = gender.to(args.device)
            age = age.to(args.device)

        a_wv = torch.cat([
            self.ln_100s[0](ad_id_wv),
            self.ln_100s[1](ad_id_glove),
            self.ln_100s[2](advertiser_id_wv),
            self.ln_100s[3](advertiser_id_glove),
            self.ln_one_hot_target(target_encode_sequence),
            self.position_embeddings(time),
            self.click_time_embeddings(click_time),
        ],
                         dim=-1)

        hidden = self.re2_one(a_wv, x_len)
        #         cat = torch.cat([self.ln_hidden(hidden), self.ln_tfidf_stack(x_flatten)], dim = -1)
        cat = hidden
        output_age = self.decoder_age(cat)
        output_gender = self.decoder_gender(cat)

        if (gender is None):
            return output_gender, output_age, hidden


#         loss_age = nn.CrossEntropyLoss()
#         loss_gender = nn.CrossEntropyLoss()
#         l_age = loss_age(output_age,age.long())
#         l_gender = loss_gender(output_gender,gender.long())

        l_gender = self.label_smooth_gender(output_gender, gender.long())
        l_age = self.label_smooth_age(output_age, age.long())
        l = 0.5 * l_gender + 0.5 * l_age

        l = 0.5 * l_gender + 0.5 * l_age

        return l, l_gender, l_age, output_gender, output_age, hidden
    def evaluate(self, loader, epoch):

        self.model.eval()
        user_id_list, true_y, pred_y = [], [], []
        loss_all, num_batch = 0., 0.
        with torch.no_grad():
            for index, datum_tuple in enumerate(loader):
                creative_id, ad_id, product_id, advertiser_id, industry, product_category, time, user_id, y_label = datum_tuple


                advertiser_id, product_id, product_category, industry, time = advertiser_id.to(device,non_blocking=True), \
                                                                              product_id.to(device,non_blocking=True), \
                                                                              product_category.to(device,non_blocking=True), \
                                                                              industry.to(device,non_blocking=True), \
                                                                              time.to(device,non_blocking=True)

                #获取embedding抽取的向量
                inputlist_tensor = [
                    creative_id, ad_id, advertiser_id, product_id,
                    product_category, industry, time
                ]
                emb_layer_mat = []
                for index, input_col in enumerate(inputlist_tensor):
                    emb_layer_col_mat = {}
                    for j in range(len(self.emb_layer[index])):
                        if index in [2, 3, 4, 5, 6]:
                            self.emb_layer[index][j] = self.emb_layer[index][
                                j].to(device, non_blocking=True)
                        emb_layer_col_mat[j] = self.emb_layer[index][j](
                            input_col)
                        emb_layer_col_mat[j] = emb_layer_col_mat[j].to(
                            device, non_blocking=True)
                    emb_layer_mat.append(emb_layer_col_mat)

                output = self.model(emb_layer_mat)
                y_label = y_label.to(device, non_blocking=True)

                y_label = y_label.long()

                loss = self.loss_func(output, y_label)
                loss_all += loss.item()
                num_batch += 1

                pred_y.extend(list(output.cpu().detach().numpy()))
                true_y.extend(list(y_label.cpu().detach().numpy()))
                user_id_list.extend(list(user_id.numpy()))

                del creative_id, ad_id, product_id, advertiser_id, industry, product_category, time, y_label
                _ = gc.collect()

        pred = np.argmax(np.array(pred_y), 1)
        true = np.array(true_y).reshape((-1, ))
        acc_score = accuracy_score(true, pred)

        loss_valid = loss_all / num_batch

        output_data = DataFrame({'user_id': user_id_list, 'pred': pred_y})

        if acc_score > 0.48:

            if not os.path.isdir('../../oof/bk_oof/Multi_Head_ResNext'):
                os.mkdir('../../oof/bk_oof/Multi_Head_ResNext')

            pickle.dump(
                output_data,
                open(
                    '../../oof/bk_oof/Multi_Head_ResNext/val_{}_folds_{}.pkl'.
                    format(epoch, self.folds), 'wb'))

        del pred, true, pred_y, true_y
        _ = gc.collect()

        return acc_score, loss_valid
    def train(self):

        iter_wrapper = lambda x: tqdm(x, total=len(self.train_data))
        start_epoch = -1
        best_valid = 0.
        min_lr = 1e-7

        if self.is_resume:
            print('Let Continue!')
            checkpoint = torch.load(PATH_CHECKPOINT)  # 加载断点

            self.model.load_state_dict(checkpoint['model_state_dict'])

            self.optim.load_state_dict(checkpoint['optimizer_state_dict'])
            start_epoch = checkpoint['epoch']
            best_valid = checkpoint['best_valid']

        for epoch in range(start_epoch + 1, EPOCHS):

            print('=========================')
            print('Processing Epoch {}'.format(epoch))
            print('=========================')

            loss_per_epoch, train_n_batch = 0., 0.

            for index, data in iter_wrapper(enumerate(self.train_data)):

                creative_id, ad_id, product_id, advertiser_id, industry, product_category, time, user_id, y_label = data


                advertiser_id, product_id, product_category, industry, time = advertiser_id.to(device,non_blocking=True),\
                                                                              product_id.to(device,non_blocking=True), \
                                                                              product_category.to(device,non_blocking=True), \
                                                                              industry.to(device,non_blocking=True), \
                                                                              time.to(device,non_blocking=True)

                self.model.train()
                self.optim.zero_grad()

                #获取embedding抽取的向量
                inputlist_tensor = [
                    creative_id, ad_id, advertiser_id, product_id,
                    product_category, industry, time
                ]
                emb_layer_mat = []
                for index, input_col in enumerate(inputlist_tensor):
                    emb_layer_col_mat = {}
                    for j in range(len(self.emb_layer[index])):
                        if index in [2, 3, 4, 5, 6]:
                            self.emb_layer[index][j] = self.emb_layer[index][
                                j].to(device, non_blocking=True)
                        emb_layer_col_mat[j] = self.emb_layer[index][j](
                            input_col)
                        emb_layer_col_mat[j] = emb_layer_col_mat[j].to(
                            device, non_blocking=True)
                    emb_layer_mat.append(emb_layer_col_mat)

                output = self.model(emb_layer_mat)
                y_label = y_label.to(device, non_blocking=True)

                y_label = y_label.long()

                loss = self.loss_func(output, y_label)

                loss_per_epoch += loss.item()
                train_n_batch += 1

                loss.backward()

                nn.utils.clip_grad_norm_(self.model.parameters(), 10.)  # 梯度裁剪

                self.optim.step()

                del creative_id, ad_id, product_id, advertiser_id, industry, product_category, time, y_label
                _ = gc.collect()

            if self.val_data is not None:  # Do Validation

                valid_score, valid_loss = self.evaluate(self.val_data, epoch)
                print('evaluate done!')
                if valid_score > 0.48:
                    self.test(self.test_data, epoch)

                if valid_score > best_valid:
                    best_valid = valid_score

            self.scheduler_ReduceLROnPlateauLR.step(valid_score)

            if self.optim.param_groups[0]['lr'] < min_lr:
                print("stopping")
                break

            torch.cuda.empty_cache()
示例#10
0
def time_converter_1(time: str) -> str:
    ampm = time.replace(".", "")
    return to("%H:%M", from_(ampm, "%I:%M %p"))
示例#11
0
def PlotFunc_nina(dspec,time,freq,SS,fd,tau,
            edges,eta_fit,eta_sig,etas,measure,etas_fit,fit_res,
            tau_lim=None,fd_lim=None,method='eigenvalue'):
    '''
    Plotting script to look at invidivual chunks
    Arguments
    dspec -- 2D numpy array containing the dynamic spectrum
    time -- 1D numpy array of the dynamic spectrum time bins (with units)
    freq -- 1D numpy array of the dynamic spectrum frequency channels (with units)
    SS -- 2D numpy array of the conjugate spectrum
    fd -- 1D numpy array of the SS fd bins (with units)
    tau -- 1D numpy array of the SS tau bins (with units)
    edges -- 1D numpy array with the bin edges for theta-theta
    eta_fit -- Best fit curvature
    eta_sig -- Error on best fir curvature
    etas -- 1D numpy array of curvatures searched over
    measure -- 1D numpy array with largest eigenvalue (method = 'eigenvalue') or chisq value (method = 'chisq') for etas
    etas_fit -- Subarray of etas used for fitting
    fit_res -- Fit parameters for parabola at extremum
    tau_lim -- Largest tau value for SS plots
    method -- Either 'eigenvalue' or 'chisq' depending on how curvature was found
    '''
    if fd_lim is None:
        fd_lim=min(2*edges.max(),fd.max().value)
    if np.isnan(eta_fit):
        eta=etas.mean()
        thth_red, thth2_red, recov, model, edges_red,w,V = THTH.modeler(SS, tau, fd, etas.mean(), edges)
    else:
        eta=eta_fit
        thth_red, thth2_red, recov, model, edges_red,w,V = THTH.modeler(SS, tau, fd, eta_fit, edges)
    ththE_red=thth_red*0
    ththE_red[ththE_red.shape[0]//2,:]=np.conjugate(V)*np.sqrt(w)
    ##Map back to time/frequency space
    recov_E=THTH.rev_map(ththE_red,tau,fd,eta,edges_red,isdspec = False)
    model_E=np.fft.ifft2(np.fft.ifftshift(recov_E))[:dspec.shape[0],:dspec.shape[1]]
    model_E*=(dspec.shape[0]*dspec.shape[1]/4)
    model_E[dspec>0]=np.sqrt(dspec[dspec>0])*np.exp(1j*np.angle(model_E[dspec>0]))
    model_E=np.pad(model_E,
                    (   (0,SS.shape[0]-model_E.shape[0]),
                        (0,SS.shape[1]-model_E.shape[1])),
                    mode='constant',
                    constant_values=0)
    recov_E=np.abs(np.fft.fftshift(np.fft.fft2(model_E)))**2
    model_E=model_E[:dspec.shape[0],:dspec.shape[1]]
    N_E=recov_E[:recov_E.shape[0]//4,:].mean()
    
    SS_ext=ext_find(fd,tau)
    SS_min=np.median(np.abs(SS)**2)/10
    SS_max=np.max(np.abs(2*recov)**2)*np.exp(-2.7)
    
    
    thth_min=np.median(np.abs(thth_red))/10
    thth_max=np.max(np.abs(thth_red))

    grid=plt.GridSpec(5,2)
    plt.figure(figsize=(8,20))
    plt.subplot(grid[0,0])
    plt.imshow(dspec,
            aspect='auto',
            extent=ext_find(time.to(u.min),freq),
            origin='lower')#,vmin=0,vmax=dspec.max())
    plt.xlabel('Time (min)')
    plt.ylabel('Freq (MHz)')
    plt.title('Data Dynamic Spectrum')
    plt.subplot(grid[0,1])
    plt.imshow(model[:dspec.shape[0],:dspec.shape[1]],
            aspect='auto',
            extent=ext_find(time.to(u.min),freq),
            origin='lower')#,vmin=0,vmax=dspec.max())
    plt.xlabel('Time (min)')
    plt.ylabel('Freq (MHz)')
    plt.title('Model Dynamic Spectrum')
    plt.subplot(grid[1,0])
    plt.imshow(np.abs(SS)**2,
            norm=LogNorm(),
            origin='lower',
            aspect='auto',
            extent=SS_ext,
            vmin=SS_min,vmax=SS_max)
    plt.xlim((-fd_lim,fd_lim))
    plt.ylim((0,tau_lim))
    plt.xlabel(r'$f_D$ (mHz)')
    plt.ylabel(r'$\tau$ (us)')
    plt.title('Data Secondary Spectrum')
    plt.subplot(grid[1,1])
    plt.imshow(np.abs(recov)**2,
            norm=LogNorm(),
            origin='lower',
            aspect='auto',
            extent=SS_ext,
            vmin=SS_min,vmax=SS_max)
    plt.xlim((-fd_lim,fd_lim))
    plt.ylim((0,tau_lim))
    plt.xlabel(r'$f_D$ (mHz)')
    plt.ylabel(r'$\tau$ (us)')
    plt.title('Model Secondary Spectrum')
    
    plt.subplot(grid[2,0])
    plt.imshow(np.abs(thth_red)**2,
            norm=LogNorm(),
            origin='lower',
            aspect='auto',
            extent=[edges_red[0],edges_red[-1],edges_red[0],edges_red[-1]],
            vmin=thth_min,vmax=thth_max**2.)
    plt.xlabel(r'$\theta_1$')
    plt.ylabel(r'$\theta_2$')
    plt.title(r'Data $\theta-\theta$')
    plt.subplot(grid[2,1])
    
    plt.imshow(np.abs(thth2_red)**2,
            norm=LogNorm(),
            origin='lower',
            aspect='auto',
            extent=[edges_red[0],edges_red[-1],edges_red[0],edges_red[-1]],
            vmin=thth_min,vmax=thth_max**2.)
    plt.xlabel(r'$\theta_1$')
    plt.ylabel(r'$\theta_2$')
    plt.title(r'Data $\theta-\theta$')
    plt.subplot(grid[3,:])
    plt.plot(etas,measure)
    if not np.isnan(eta_fit):
        exp_fit = int(('%.0e' % eta_fit.value)[2:])
        exp_err = int(('%.0e' % eta_sig.value)[2:])
        fmt = "{:.%se}" % (exp_fit - exp_err)
        fit_string = fmt.format(eta_fit.value)[:2 + exp_fit - exp_err]
        err_string = '0%s' % fmt.format(10**(exp_fit) + eta_sig.value)[1:]
        
        plt.plot(etas_fit,
            THTH.chi_par(etas_fit.value, *fit_res),
            label=r'$\eta$ = %s $\pm$ %s $s^3$' % (fit_string, err_string))
        plt.legend()
    if method == 'eigenvalue':
        plt.title('Eigenvalue Search')
        plt.ylabel(r'Largest Eigenvalue')
    else:
        plt.title('Chisquare Search')
        plt.ylabel(r'$\chi^2$')
    plt.xlabel(r'$\eta$ ($s^3$)')
    plt.subplot(grid[4,0])
    plt.imshow(np.angle(model_E),
            cmap='twilight',
            aspect='auto',
            extent=ext_find(time.to(u.min),freq),
            origin='lower',
            vmin=-np.pi,vmax=np.pi)
    plt.xlabel('Time (min)')
    plt.ylabel('Freq (MHz)')
    plt.title('Recovered Phases')
    plt.subplot(grid[4,1])
    plt.imshow(recov_E,
            norm=LogNorm(),
            origin='lower',
            aspect='auto',
            extent=ext_find(fd,tau),
            vmin=N_E)
    plt.xlim((-fd_lim,fd_lim))
    plt.ylim((0,tau_lim))
    plt.xlabel(r'$f_D$ (mHz)')
    plt.ylabel(r'$\tau$ (us)')
    plt.title('Recovered Wavefield')
    plt.colorbar()
    plt.tight_layout()
示例#12
0
def test_timing():
    time = arrow.utcnow()
    write_to_file(time.format() + " " +  time.to("Asia/Singapore").format() + '\n')