def background_data(): while True: table.update_all() table.view() data = table.gen_data() socketio.emit('reload-table', data) socketio.sleep(45)
def get_genotypes(json): ids = extract_ids(json) for id_subset in subset(ids, 500): genotypes = [] for id in id_subset: genotype = next(Genotype.query.filter(Genotype.id == id).values( Genotype.id, Genotype.project_id, Genotype.sample_annotations_id, Genotype.locus_id, Genotype.annotated_peaks, Genotype.reference_run_id, Genotype.flags, Genotype.comments, Genotype.last_updated, Genotype.alleles )) genotypes.append(genotype) # genotypes = Genotype.query.filter(Genotype.id.in_(id_subset)).values( # Genotype.id, Genotype.project_id, Genotype.sample_annotations_id, Genotype.locus_id, # Genotype.annotated_peaks, Genotype.reference_run_id, Genotype.flags, Genotype.comments, # Genotype.last_updated, Genotype.alleles # ) res = [] for s in genotypes: g = { 'id': s[0], 'project': s[1], 'sample_annotations': s[2], 'locus': s[3], 'annotated_peaks': s[4], 'reference_run': s[5], 'flags': s[6], 'comments': s[7], 'last_updated': s[8], 'alleles': s[9] } res.append(g) genotypes_dump = schema.dumps(res, many=True) socketio.emit('get_updated', {JSON_NAMESPACE: genotypes_dump.data}, namespace=make_namespace(JSON_NAMESPACE)) socketio.sleep()
def recalculate_channel(self, channel_annotation, rescan_peaks): if not isinstance(channel_annotation, ProjectChannelAnnotations): channel_annotation = ProjectChannelAnnotations.query.get(channel_annotation) channel = channel_annotation.channel if channel.well.sizing_quality > channel.well.ladder.unusable_sq_limit: channel_annotation.set_flag('poor_sizing_quality', True) else: channel_annotation.set_flag('poor_sizing_quality', False) if channel.well.base_sizes: filter_params = self.get_filter_parameters(channel.locus_id) if rescan_peaks: scanning_params = self.get_scanning_parameters(channel.locus_id) channel.identify_peak_indices(scanning_params) channel_annotation.peak_indices = channel.peak_indices else: channel.set_peak_indices(channel_annotation.peak_indices) channel.pre_annotate_and_filter(filter_params) total_peaks = -1 while len(channel.peaks) != total_peaks: socketio.sleep() channel.post_annotate_peaks() channel.post_filter_peaks(filter_params) total_peaks = len(channel.peaks) channel_annotation.annotated_peaks = channel.peaks[:] return channel_annotation
def test_untheard_connect(message): #value = message.get('param') count = 0 while True: socketio.sleep(1) count += 1 emit('my_request', {'data': 'Connected', 'untheardCount': count})
def read_and_forward_pty_output(app): max_read_bytes = 1024 * 20 running = True while running: socketio.sleep(0.01) with app.app_context(): if app.config['fd']: timeout_sec = 0 (data_ready, _, _) = select.select([app.config['fd']], [], [], timeout_sec) if data_ready: try: output = os.read(app.config['fd'], max_read_bytes).decode() socketio.emit('pty-output', {'output': output}, namespace='/pty') except: msg = 'Close the connection to childpid={}'.format( current_app.config['child_pid']) current_app.logger.info(msg) os.waitpid(current_app.config['child_pid'], os.WNOHANG) current_app.config['child_pid'] = None current_app.config['fd'] = None else: # break the loop msg = 'Stop the backgroud pty forward loop!' current_app.logger.info(msg) running = False
def send_stats(): """Runs forever in a socket.io background task and sends updates on all monitored apps. Updates are checked and sent on the configured interval `scaling.monitor_interval`. """ while True: global track_stats if track_stats: try: apps = sockets.rooms_with_members('apps/') print ('getting stats for apps', apps) for room in apps: app_id = room.split('/')[-1] try: app = App.find_by_id(app_id) stats = app.get_current_stats( include_instance_stats=True) update_app_watchers(app_id, 'stats', stats.to_dict()) except Exception as e: import traceback print(traceback.format_exc()) print(type(e), e.message) except Exception as e: print (e) # else: # print ('No clients active, sleeping.') socketio.sleep(config.scaling_monitor_interval)
def select_best_run(channel_annotations, offscale_threshold): channel_annotations = [x for x in channel_annotations if not x.get_flag('poor_sizing_quality')] best_annotation = None for annotation in channel_annotations: socketio.sleep() if not annotation.annotated_peaks: annotation.annotated_peaks = [] assert isinstance(annotation, ProjectChannelAnnotations) if not best_annotation: best_annotation = annotation else: best_peaks = filter(lambda y: y['peak_height'] < offscale_threshold and y.get('in_bin', True), best_annotation.annotated_peaks) best_peaks = list(best_peaks) if best_peaks: max_best_peak = max(best_peaks, key=lambda x: x['peak_height']) else: max_best_peak = {'peak_height': 0} curr_peaks = filter(lambda y: y['peak_height'] < offscale_threshold and y.get('in_bin', True), annotation.annotated_peaks) curr_peaks = list(curr_peaks) if curr_peaks: max_curr_peak = max(curr_peaks, key=lambda x: x['peak_height']) else: max_curr_peak = {'peak_height': 0} if max_curr_peak['peak_height'] > max_best_peak['peak_height']: best_annotation = annotation return best_annotation
def analyze_samples(self, locus_id): self.clear_sample_annotations(locus_id) self.initialize_alleles(locus_id) locus_params = self.get_locus_parameters(locus_id) assert isinstance(locus_params, GenotypingLocusParams) locus_annotations = self.get_locus_sample_annotations(locus_id) all_runs = self.get_runs(locus_id) for locus_annotation in locus_annotations: socketio.sleep() try: locus_annotation.alleles.pop('None') locus_annotation.alleles.changed() except KeyError: pass locus_annotation.set_flag('manual_curation', False) assert isinstance(locus_annotation, SampleLocusAnnotation) runs = all_runs.get(locus_annotation.sample_annotation.sample_id, []) if runs: channel_annotation = self.select_best_run(all_runs[locus_annotation.sample_annotation.sample_id], locus_params.offscale_threshold) else: channel_annotation = None if channel_annotation: locus_annotation.reference_run = channel_annotation peaks = channel_annotation.annotated_peaks[:] peaks = [self.flag_peak(_, locus_params) for _ in peaks] locus_annotation.annotated_peaks = peaks locus_annotation.set_flag('failure', True) locus_annotation.set_flag('offscale', False) for peak in locus_annotation.annotated_peaks: if peak['peak_height'] > locus_params.failure_threshold: locus_annotation.set_flag('failure', False) if ( peak['peak_height'] > locus_params.offscale_threshold or peak['peak_height'] * peak['bleedthrough_ratio'] > locus_params.offscale_threshold or peak['peak_height'] * peak['crosstalk_ratio'] > locus_params.offscale_threshold ): locus_annotation.set_flag('offscale', True) if not locus_annotation.get_flag('failure'): for peak in locus_annotation.annotated_peaks: if peak['bin_id'] == 'None': peak['bin_id'] = None peak['bin'] = None peak['in_bin'] = False if not any(peak['flags'].values()) and peak['bin_id'] and peak['bin_id'] != 'None': locus_annotation.alleles[str(peak['bin_id'])] = True else: locus_annotation.reference_run = None locus_annotation.annotated_peaks = [] return self
def action_timer_thread(): while True: socketio.sleep(0.1) try: with app.app_context(): process_action_queue() except Exception as e: log.error("action timer thread error: " + str(e))
def recalculate_channels(self, channel_annotations, rescan_peaks): recalculated_channel_annotations = [] for channel_annotation in channel_annotations: socketio.sleep() recalculated_channel_annotations.append( self.recalculate_channel(channel_annotation, rescan_peaks)) return recalculated_channel_annotations
def emit_task_start(self, data=None, callback=None): socketio.emit(self.task, { 'task_args': self.task_args, 'status': 'start', 'id': self.task_id, 'payload': data }, namespace=self.namespace, callback=callback) socketio.sleep()
def timer(): global timeout while game_in_progress: if(timeout == 0): socketio.emit('timeout', namespace='/test') socketio.emit('tick', timeout , namespace='/test') socketio.sleep(1) timeout = timeout - 1
def emit_task_progress(self, progress, callback=None): socketio.emit(self.task, { 'task_args': self.task_args, 'status': 'in_progress', 'id': self.task_id, 'payload': progress }, namespace=self.namespace, callback=callback) socketio.sleep()
def emit_task_success(self, message=None, callback=None): socketio.emit(self.task, { 'task_args': self.task_args, 'status': 'success', 'id': self.task_id, 'payload': message }, namespace=self.namespace, callback=callback) socketio.sleep()
def estimate_allele_frequencies(self): peak_filters = {} for lp in self.locus_parameters: peak_filters[lp.locus.label] = compose_filters(bin_filter(in_bin=True), flags_filter(), probability_filter(lp.bootstrap_probability_threshold)) all_locus_annotations = SampleLocusAnnotation.query.join(ProjectSampleAnnotations).join(Sample).filter( Sample.designation == 'sample').filter(SampleLocusAnnotation.project_id == self.id).all() all_locus_annotations = [_ for _ in all_locus_annotations if not _.get_flag('failure')] locus_annotation_dict = defaultdict(list) for annotation in all_locus_annotations: locus_annotation_dict[annotation.sample_annotations_id].append(annotation) self.initialize_probability_annotations(all_locus_annotations) sample_annotations = self.sample_annotations.join(Sample).filter(Sample.designation == 'sample').all() alleles_changed = True cycles = 0 allele_frequencies = {} while alleles_changed: cycles += 1 alleles_changed = False allele_frequency_locus_annotations = format_locus_annotations(all_locus_annotations, peak_filters) allele_frequencies = calculate_allele_frequencies(allele_frequency_locus_annotations) for sample_annotation in sample_annotations: assert isinstance(sample_annotation, ProjectSampleAnnotations) locus_annotations = locus_annotation_dict[sample_annotation.id] formatted_locus_annotations = format_locus_annotations(locus_annotations, peak_filters) moi = calculate_moi(formatted_locus_annotations, offset=1) for locus_annotation in locus_annotations: socketio.sleep() lp = self.get_locus_parameters(locus_annotation.locus_id) assert isinstance(lp, GenotypingLocusParams) if len(locus_annotation.annotated_peaks) > 0 and not locus_annotation.get_flag('failure'): all_peaks = locus_annotation.annotated_peaks[:] possible_peaks = peak_filters[lp.locus.label](all_peaks) possible_peaks = calculate_prob_negative(possible_peaks, moi, allele_frequencies[locus_annotation.locus.label]) prob_annotated_possible_peaks = calculate_prob_pos_if_observed(possible_peaks) recalculated_peak_probabilities = {p['peak_index']: p['probability'] for p in prob_annotated_possible_peaks} for peak in locus_annotation.annotated_peaks: socketio.sleep() if peak['peak_index'] in recalculated_peak_probabilities: peak['probability'] = recalculated_peak_probabilities[peak['peak_index']] if peak['probability'] < lp.bootstrap_probability_threshold: alleles_changed = True self.initialize_probability_annotations(all_locus_annotations) return allele_frequencies
def background_virtual_thread(sid): while thread_pool['sid']['status']: socketio.sleep(1) try: data = VirtualCoin().get_ws_content(thread_pool[sid]['coinName']) socketio.emit('Virtual', {'data': data}, namespace='/VirtualCoin', room=thread_pool[sid]['sid']) except: pass del thread_pool[sid]
def on_connect(self): from app import socketio global thread emit("connected", "connected") socketio.sleep(5) if thread is None: with current_app.app_context(): thread = socketio.start_background_task( background_thread, socketio, request.sid)
def recycle_timer_thread(): while True: socketio.sleep(30 * 60) try: with app.app_context(): clean_recycle_bin() except Exception as e: log.error("recycle timer thread error: " + str(e))
def sendValues(): global channel print("Taking values") while not thread_stop_event.isSet(): k, x = take_json(channel) if x: print(k) socketio.emit('atualiza', k) else: socketio.sleep(1)
def clear_sample_annotations(self, locus_id): sample_locus_annotations = SampleLocusAnnotation.query.join(ProjectSampleAnnotations).filter( SampleLocusAnnotation.locus_id == locus_id).filter(ProjectSampleAnnotations.project_id == self.id).all() for sample_annotation in sample_locus_annotations: socketio.sleep() assert isinstance(sample_annotation, SampleLocusAnnotation) sample_annotation.annotated_peaks = [] sample_annotation.reference_run_id = None sample_annotation.clear_flags() sample_annotation.clear_alleles() return self
def runCmd(self): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=host, port=int(22), username=user, password=_psw) stdin, stdout, stderr = ssh.exec_command( "cd /work/code/remastering-centos-7/kaeluster-master; packer build slave.json" ) for line in iter(stdout.readline, ''): socketio.emit('stdout', conv.convert(line).replace('\n', '') + '<br>') socketio.sleep(0)
def background_thread(): count = 0 while True: count += 1 t = time.strftime('%M:%S', time.localtime()) socketio.emit('server_msg', { 'data': t, 'count': count }, namespace='/tool_io') socketio.sleep(1)
def clear_plate_map(plate_id): channel_annotations = ProjectChannelAnnotations.query.join(Channel). \ join(Well).join(Plate).filter(Plate.id == plate_id).all() for ca in channel_annotations: socketio.sleep() ca.reinitialize() channels = Channel.query.join(Well).join(Plate).filter(Plate.id == plate_id).all() for ch in channels: socketio.sleep() ch.reinitialize()
def test_connect_unthread(message): """ 不使用线程,后台开异步执行推送 :param message: :return: """ count = 0 while True: socketio.sleep(1) count += 1 emit('my_request', {'data': 'Connected', 'unthreadCount': count})
def background_thread(): """Example of how to send server generated events to clients.""" count = 0 while True: socketio.sleep(10) count += 1 socketio.emit('my_response', { 'data': 'Server generated event', 'count': count }, namespace='/test')
def get_res(res, user_sid): ''' Hit the REST endpoint for word suggestion and fetch and emit update_suggestions event ''' response = requests.get('http://' + app.config['PATH'] + '/api/auto_complete?partial_query=' + res).text socketio.emit('update_suggestions', json.loads(response), broadcast=False, room=user_sid) socketio.sleep(0) return 0
def background_thread(): """ example of how to send server generated events to clients. 增加一个线程后台程序,每次返回消息会增加一个线程, 需要设置条件关闭,或者一直作为生产者 :return: """ count = 0 while True: socketio.sleep() count += 1 socketio.emit('my_request', {'data': 'server generated event', 'count': count}, namespace='/test')
def win(data): print('finish', data['winer']) game = roomsGame[data['room']] game.rasp += 1 if game.users == 2: if game.rasp == game.users: emit('finish', {'score': 5}, room=data['winer']) socketio.sleep(3) emit('redirect', room=game.room) elif game.rasp == game.users: emit('finish', {"score": 10}, room=data['winer']) game.rasp = 0
def background_task2(): start_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") # 开始时间 conn.rpush('count', json.dumps({'count': 0, 'time': start_time})) while True: future_time = (datetime.datetime.now() + datetime.timedelta(seconds=+5)).strftime( "%Y-%m-%d %H:%M:%S") # 当前5秒之后的时间 conn.rpush('count', json.dumps({'count': 0, 'time': future_time})) print(conn.llen('count')) while conn.llen('count') >= 3: conn.lpop('count') socketio.sleep(5)
def run_game(cls, game): # Start a new turn cls.start_turn(game) # Give the players some time to answer socketio.sleep(game.max_response_time) # End the turn cls.end_turn(game) # Send the new leaderboard to players cls.update_leaderboard(game)
def analyze_loci(json): task = 'analyze_loci' locus_parameter_ids = json.get('locus_parameter_ids') parameter_settings = json.get('parameter_settings') task_notifier = TaskNotifier(task=task, namespace=SOCK_NAMESPACE, locus_parameter_ids=locus_parameter_ids, parameter_settings=parameter_settings) task_notifier.emit_task_start() if not locus_parameter_ids: task_notifier.emit_task_failure(message="No Locus Parameter Selected.") return lps = [] for id in locus_parameter_ids: lp = ArtifactEstimatorLocusParams.query.get(id) lps.append(lp) # lps = ArtifactEstimatorLocusParams.query.filter( # ArtifactEstimatorLocusParams.id.in_(locus_parameter_ids) # ).all() if not lps: task_notifier.emit_task_failure(message="Locus Parameters No Longer Exist. Reload Application.") return if parameter_settings: for lp in lps: for k, v in parameter_settings.items(): if hasattr(lp, k): if getattr(lp, k) != v: setattr(lp, k, v) # db.session.flush() socketio.sleep() project = lps[0].project total_lps = len(lps) for idx, lp in enumerate(lps): task_notifier.emit_task_progress(progress={ 'style': 'determinate', 'total': total_lps, 'current_state': idx + 1, 'message': f'Analyzing {lp.locus.label}...' }) project.analyze_locus(lp.locus_id) socketio.sleep() project.last_updated = datetime.utcnow() task_notifier.emit_task_success("Successfully Analyzed All Loci")
def background_thread(): """Example of how to send server generated events to clients.""" from app.hzlbs.elecrail import hz_lbs_elect_rail count = 0 # hz_tk = {} while True: socketio.sleep(1) count += 1 # socketio.emit('hz_response', # {'data': 'Server generated event', 'count': count}, # namespace='/HeZhong') pos_to_client = hz_get_changed_pos() if len(pos_to_client) > 0: socketio.emit('hz_position', pos_to_client, namespace=HZ_NAMESPACE) # 每隔2分钟。强制刷新位置 和 导航路径。 if count % 120 == 0: socketio.emit('hz_position', hz_get_pos(), namespace=HZ_NAMESPACE) for client in hz_client_id: if hz_client_id[client]['navigating'] == 1: path = hz_get_path(hz_client_id[client]['location'], hz_client_id[client]['userId']) socketio.emit('hz_path', path, namespace=HZ_NAMESPACE, room=client) hz_client_id[client]['path_cmp'] = path data = hz_get_pos() with app.app_context(): rail_info = hz_lbs_elect_rail(data) # 判断是否进入/退出 电子围栏 if len(rail_info) != 0: socketio.emit('hz_electronic_tail', rail_info, namespace=HZ_NAMESPACE) for client in hz_client_id: if hz_client_id[client]['navigating'] == 1: path = hz_get_path(hz_client_id[client]['location'], hz_client_id[client]['userId']) if path == hz_client_id[client]['path_cmp']: continue socketio.emit('hz_path', path, namespace=HZ_NAMESPACE, room=client) hz_client_id[client]['path_cmp'] = path
def emit_data(): while runTracker.isRecording(): print("HELL YEAH YAM RECORDING") print("Emit runID " + str(runTracker.getID())) #info = data.Info().to_json() randData.gen_random() info = randData.to_json() data_json = msgpack.unpackb(info, raw=False) storeData(data_json) socketio.emit('dataEvent', data_json) socketio.sleep(1)
def annotate_bins(self, channel_annotations): socketio.sleep() if self.bin_estimator_id: self.clear_bin_annotations(channel_annotations) for annotation in channel_annotations: socketio.sleep() assert isinstance(annotation, ProjectChannelAnnotations) if annotation.annotated_peaks: self.bin_estimator.annotate_bins( annotation.channel.locus_id, annotation.annotated_peaks ) annotation.annotated_peaks.changed()
def play(self, schedule, current_rerun, total_reruns): duration = schedule['duration'] events = schedule['events'] stage_name = schedule['name'] if self.state == Chrono.CREATED: self.activate() start_second = self.minutes * 60 + self.seconds for t in range(start_second, duration + 1): if self.state == Chrono.FINISHED: break if t % 60 == 0 and self.seconds == 59: self.minutes += 1 self.seconds = 0 else: self.seconds = t % 60 tic_tac = self._create_tic_tac_dict(t, current_rerun, total_reruns, schedule) socketio.emit('tic_tac', tic_tac, namespace=self.namespace) self.dump() while self.is_paused(): socketio.emit('tic_tac', tic_tac, namespace=self.namespace) socketio.sleep(0.5) # send events for e in events: if e['t'] == t: # TODO: Change print values to UTF8 or use logger library #print (time.strftime("%H:%M:%S") + " [%s] Rueda %d enviando evento en t = %d para %s" % (stage_name, self.id, t, ','.join(map(str, e['stations'])))) socketio.emit('evento', { 'data': e['message'], 'sound': e['sound'], 'stage': schedule, 'target': ','.join(map(str, e['stations'])) }, namespace=self.namespace) if t == duration: self.stop() break socketio.sleep(1)
def background_thread(): """Example of how to send server generated events to clients. 增加一个线程处理后台程序,这里需要自己设置条件关闭,或者一直作为生产者 每次返回消息会增加一个线程,这个还需要看下websocket底层实现,会全局广播 """ count = 0 while True: socketio.sleep(1) count += 1 socketio.emit( 'my_request', {'data': 'Server generated event', 'count': count}, namespace='/test')
def _remove_samples(self, sample_ids): psas = [] slas = [] pcas = [] for id in sample_ids: psa = ProjectSampleAnnotations.query.filter( ProjectSampleAnnotations.project_id == self.id, ProjectSampleAnnotations.sample_id == id ).all() psas += psa sla = SampleLocusAnnotation.query.filter( SampleLocusAnnotation.project_id == self.id, ).join(ProjectSampleAnnotations).filter( ProjectSampleAnnotations.sample_id == id ).all() slas += sla pca = ProjectChannelAnnotations.query.filter( ProjectChannelAnnotations.project_id == self.id ).join(Channel).filter( Channel.sample_id == id ).all() pcas += pca socketio.sleep() # psas = ProjectSampleAnnotations.query.filter( # ProjectSampleAnnotations.project_id == self.id, # ProjectSampleAnnotations.sample_id.in_(sample_ids) # ).all() # socketio.sleep() # # slas = SampleLocusAnnotation.query.filter( # SampleLocusAnnotation.project_id == self.id # ).join(ProjectSampleAnnotations).filter( # ProjectSampleAnnotations.sample_id.in_(sample_ids) # ).all() # socketio.sleep() # # pcas = ProjectChannelAnnotations.query.filter( # ProjectChannelAnnotations.project_id == self.id # ).join(Channel).filter( # Channel.sample_id.in_(sample_ids) # ).all() # socketio.sleep() for _ in psas + slas + pcas: socketio.sleep() db.session.delete(_)
def remove_sample(self, sample_id): psas = ProjectSampleAnnotations.query.filter( ProjectSampleAnnotations.project_id == self.id, ProjectSampleAnnotations.sample_id == sample_id ).all() socketio.sleep() slas = SampleLocusAnnotation.query.filter( SampleLocusAnnotation.project_id == self.id ).join(ProjectSampleAnnotations).filter( ProjectSampleAnnotations.sample_id == sample_id ).all() socketio.sleep() pcas = ProjectChannelAnnotations.query.filter( ProjectChannelAnnotations.project_id == self.id ).join(Channel).filter( Channel.sample_id == sample_id ).all() socketio.sleep() for _ in psas + slas + pcas: db.session.delete(_) socketio.sleep() for locus in self.locus_set.loci: self.samples_changed(locus.id) return self
def add_sample(self, sample_id): sample_annotation = ProjectSampleAnnotations(sample_id=sample_id) self.sample_annotations.append(sample_annotation) channel_ids = self.valid_channel_ids(sample_id) self.add_channels(channel_ids) for locus in self.locus_set.loci: socketio.sleep() locus_sample_annotation = SampleLocusAnnotation(locus_id=locus.id, project_id=self.id) sample_annotation.locus_annotations.append(locus_sample_annotation) self.samples_changed(locus.id) return sample_annotation
def calculate_base_sizes(self, ladder, color, base_size_precision=2, sq_limit=1, filter_parameters=None, scanning_parameters=None): if filter_parameters is None: filter_parameters = {} if scanning_parameters is None: scanning_parameters = {} for well in self.wells_dict.values(): socketio.sleep() well.calculate_base_sizes(ladder=ladder, color=color, base_size_precision=base_size_precision, sq_limit=sq_limit, filter_parameters=filter_parameters, scanning_parameters=scanning_parameters) return self
def on_join(data): user = current_user.username id = request.sid room = data['room'] join_room(room) if room in roomsGame: print(f' room{room}') else: print(f'Else om{room}') roomsGame[room] = Game(room) some = {'id': id, 'user': user, 'room': room} socketio.sleep(1) emit('count', some, room=id)
def background_thread(): ''' Background thread for regular update of the web application for live count and live graph ''' while True: newcount = int(db_connector.getLastLog()[2]) socketio.emit('currentCount', newcount, namespace='/pilab') df = db_connector.getLiveLog(hour=24) data = db_connector.getLiveChartData(df) socketio.emit('liveGraph', data, namespace='/pilab') # check every minute since the database is updated every minute # TODO: this seems like a bad idea..maybe can change this socketio.sleep(1)
def background_thread(): while True: socketio.sleep(1) time_dict() """ On every seconds, the server send message to the client with updated time. First parameter of emit function tells which function to call on client side. """ socketio.emit('my_response', {'data': 'Message from server', 'time': time}, namespace='/test')
def run(self): while True: if self.new_positions: # Send the new positions to clients socketio.emit( 'positions_update', { 'positions': [position.__dict__ for position in self.new_positions] }) # Reset the list of new positions del self.new_positions[:] # Wait for a while before sending positions updates socketio.sleep(self.interval)
def background_task(): count = 0 while True: str = conn.lindex('count', -1) # 取倒数第二个 if str == None: continue else: dict = eval(str) print(dict) socketio.emit('count', { 'data': dict['time'], 'count': dict['count'] }, namespace='/websocket') socketio.sleep(5)
def clear_bin_annotations(self, channel_annotations): socketio.sleep() for annotation in channel_annotations: assert isinstance(annotation, ProjectChannelAnnotations) if annotation.annotated_peaks: for peak in annotation.annotated_peaks: socketio.sleep() if self.bin_estimator_id: peak['in_bin'] = False peak['bin'] = "" peak['bin_id'] = None elif 'bin' in peak: peak.pop('in_bin') peak.pop('bin') peak.pop('bin_id')
def annotate_peak_probability(self, allele_frequencies=None, fixed_moi=None): if not allele_frequencies: allele_frequencies = self.estimate_allele_frequencies() if not fixed_moi: moi_dict = self.estimate_moi(allele_frequencies) all_locus_annotations = SampleLocusAnnotation.query.join(ProjectSampleAnnotations).join(Sample).filter( Sample.designation == 'sample').filter(SampleLocusAnnotation.project_id == self.id).all() all_locus_annotations = [_ for _ in all_locus_annotations if not _.get_flag('failure')] locus_annotation_dict = defaultdict(list) for annotation in all_locus_annotations: socketio.sleep() locus_annotation_dict[annotation.sample_annotations_id].append(annotation) sample_annotations = self.sample_annotations.join(Sample).filter(Sample.designation == 'sample').all() for sample_annotation in sample_annotations: socketio.sleep() assert isinstance(sample_annotation, ProjectSampleAnnotations) if not fixed_moi: sample_annotation.moi = moi_dict[sample_annotation.id] else: sample_annotation.moi = fixed_moi locus_annotations = locus_annotation_dict[sample_annotation.id] for locus_annotation in locus_annotations: for peak in locus_annotation.annotated_peaks: peak['probability'] = 1.0 if len(locus_annotation.annotated_peaks) > 0 and not locus_annotation.get_flag('failure'): locus_annotation.annotated_peaks = calculate_prob_negative(locus_annotation.annotated_peaks, sample_annotation.moi, allele_frequencies[ locus_annotation.locus.label ]) locus_annotation.annotated_peaks = calculate_prob_pos_if_observed(locus_annotation.annotated_peaks) for peak in locus_annotation.annotated_peaks: if peak['flags']['bleedthrough'] or peak['flags']['crosstalk']: peak['probability'] = 0 locus_annotation.annotated_peaks.changed() self.recalculate_alleles(locus_annotation) return self
def run_game(cls, game): while True: # Start a new turn cls.start_turn(game) # Give the players some time to answer socketio.sleep(game.max_response_time) # End the turn cls.end_turn(game) # Send the new leaderboard to players cls.update_leaderboard(game) # Give the user some time between two turns socketio.sleep(game.between_turns_duration)
def from_peaks(cls, locus_id, peaks, min_peak_frequency, bin_buffer): locus = Locus.query.get(locus_id) locus_bin_set = cls() locus_bin_set.locus = locus db.session.add(locus_bin_set) socketio.sleep() bin_set = BinFinder.BinFinder.calculate_bins(peaks=peaks, nucleotide_repeat_length=locus.nucleotide_repeat_length, min_peak_frequency=min_peak_frequency, bin_buffer=bin_buffer) socketio.sleep() for b in bin_set.bins: assert isinstance(b, BinFinder.Bin) b = Bin(label=b.label, base_size=b.base_size, bin_buffer=b.bin_buffer, peak_count=b.peak_count) locus_bin_set.bins.append(b) return locus_bin_set
def run_game(self): while True: # Start a new turn self.start_turn() # Give the players some time to answer socketio.sleep(self.max_response_time) # End the turn self.end_turn() # Send the new leaderboard to players self.update_leaderboard() # Give the user some time between two turns socketio.sleep(self.time_between_turns)
def recalculate_alleles(self, locus_annotation): locus_param = self.get_locus_parameters(locus_annotation.locus_id) assert isinstance(locus_param, GenotypingLocusParams) true_peaks = [] locus_annotation.alleles = dict.fromkeys(locus_annotation.alleles, False) locus_annotation.set_flag('manual_curation', False) if locus_annotation.annotated_peaks and not locus_annotation.get_flag('failure'): for peak in locus_annotation.annotated_peaks: socketio.sleep() if not any(peak['flags'].values()) and peak['bin_id']: if peak['probability'] >= locus_param.probability_threshold or ( (peak['peak_height'] - peak['artifact_contribution']) / max(peak['artifact_error'], 1)) > locus_param.soft_artifact_sd_limit: locus_annotation.alleles[str(peak['bin_id'])] = True true_peaks.append(peak) if self.quantification_bias_estimator: self.annotate_quantification_bias([locus_annotation]) locus_annotation.alleles.changed()
def add_samples(self, sample_ids): present_sample_ids = set([_[0] for _ in ProjectSampleAnnotations.query .filter(ProjectSampleAnnotations.project_id == self.id) .values(ProjectSampleAnnotations.sample_id)]) full_sample_ids = list(set(sample_ids) - present_sample_ids) # Cache all channel IDs available sample_ids_map = defaultdict(list) channel_and_sample_ids = Channel.query.join(Sample).join(Locus).join(locus_set_association_table).join( LocusSet).join( Project).filter(Project.id == self.id).values(Channel.id, Channel.sample_id) for channel_id, sample_id in channel_and_sample_ids: sample_ids_map[sample_id].append(channel_id) # Cache all bin IDs bins_map = defaultdict(list) bin_and_locus_ids = Bin.query.join(LocusBinSet).join(BinEstimatorProject).filter( BinEstimatorProject.id == self.bin_estimator_id).values(Bin.id, LocusBinSet.locus_id) for bin_id, locus_id in bin_and_locus_ids: bins_map[locus_id].append(bin_id) n = 0 for sample_ids in subset(full_sample_ids, 100): channel_ids = [] for sample_id in sample_ids: socketio.sleep() channel_ids += sample_ids_map[sample_id] sample_annotation = ProjectSampleAnnotations(sample_id=sample_id, project_id=self.id) db.session.add(sample_annotation) self.sample_annotations.append(sample_annotation) for locus in self.locus_set.loci: locus_annotation = Genotype(locus_id=locus.id, project_id=self.id) bin_ids = bins_map[locus.id] locus_annotation.alleles = dict([(str(bin_id), False) for bin_id in bin_ids]) sample_annotation.locus_annotations.append(locus_annotation) self.bulk_create_channel_annotations(channel_ids) db.session.flush() n += 1 for locus in self.locus_set.loci: self.set_locus_parameters_stale(locus.id) return self
def recalculate_ladder(json): i = 1 well_id = json['well_id'] peak_indices = json['ladder_peak_indices'] task = 'recalculate_ladder' task_notifier = TaskNotifier(task=task, namespace=SOCK_NAMESPACE, well_id=well_id) task_notifier.emit_task_start() socketio.sleep() if not well_id: task_notifier.emit_task_failure(message="Well Not Selected.") return if not isinstance(peak_indices, list): task_notifier.emit_task_failure(message="Peak Indices malformed.") well = Well.query.get(well_id) if not well: task_notifier.emit_task_failure(message="Well No Longer Exists. Reload Page.") task_notifier.emit_task_progress(progress={ 'style': 'determinate', 'total': 5, 'current_state': i, 'message': 'Recalculating Well Ladder...' }) i += 1 well.calculate_base_sizes(peak_indices) for channel in well.channels: if channel.locus_id: task_notifier.emit_task_progress(progress={ 'style': 'determinate', 'total': 5, 'current_state': i, 'message': 'Recalculating Well Ladder...' }) channel.find_max_data_point() i += 1 task_notifier.emit_task_success(message="Well Ladder Recalculated Successfully.")
def get_quantification_bias_estimator_project(json): ids = extract_ids(json) projects = [] channels = [] locus_parameters = [] control_sample_associations = [] project_sample_annotations = [] for project_id in ids: p = QuantificationBiasEstimatorProject.query.get(project_id) if p: projects.append(p) channels += Channel.get_serialized_list(project_id) locus_parameters += p.locus_parameters.all() control_sample_associations += ControlSampleAssociation.get_serialized_list(project_id) project_sample_annotations += ProjectSampleAnnotations.get_serialized_list(project_id) else: socketio.emit('get_failed', {PROJECT_NAMESPACE: [project_id]}, namespace=make_namespace(PROJECT_NAMESPACE)) channel_dump = channel_schema.dumps(channels, many=True) socketio.emit('list', {CHANNEL_NAMESPACE: channel_dump.data}, namespace=make_namespace(CHANNEL_NAMESPACE)) socketio.sleep() control_sample_association_dump = control_sample_association_schema.dumps(control_sample_associations, many=True) socketio.emit('get', {CONTROL_SAMPLE_ASSOCIATION_NAMESPACE: control_sample_association_dump.data}, namespace=make_namespace(CONTROL_SAMPLE_ASSOCIATION_NAMESPACE)) socketio.sleep() project_sample_annotations_dump = project_sample_annotations_schema.dumps(project_sample_annotations, many=True) socketio.emit('get', {PROJECT_SAMPLE_ANNOTATIONS_NAMESPACE: project_sample_annotations_dump.data}, namespace=make_namespace(PROJECT_SAMPLE_ANNOTATIONS_NAMESPACE)) socketio.sleep() locus_params_dump = locus_params_schema.dumps(locus_parameters, many=True) socketio.emit('get', {LOCUS_PARAMS_NAMESPACE: locus_params_dump.data}, namespace=make_namespace(LOCUS_PARAMS_NAMESPACE)) socketio.sleep() project_dump = project_schema.dumps(projects, many=True) socketio.emit('get', {PROJECT_NAMESPACE: project_dump.data}, namespace=make_namespace(PROJECT_NAMESPACE)) socketio.sleep()
def recalculate_ladder(json): task = 'recalculate_ladder' ladder_id = json.get('ladder_id', None) plate_id = json.get('plate_id', None) task_notifier = TaskNotifier(task=task, namespace=SOCK_NAMESPACE, ladder_id=ladder_id, plate_id=plate_id) task_notifier.emit_task_start() if not ladder_id: task_notifier.emit_task_failure(message="Ladder Not Selected.") return if not plate_id: task_notifier.emit_task_failure(message="Plate Not Selected.") return ladder = Ladder.query.get(ladder_id) socketio.sleep() plate = Plate.query.get(plate_id) socketio.sleep() if not ladder: task_notifier.emit_task_failure(message="Ladder No Longer Exists. Reload Page.") return if not plate: task_notifier.emit_task_failure(message="Plate No Longer Exists. Reload Page.") return if ladder and plate: total_wells = len(plate.wells) for idx, w in enumerate(plate.wells): w.ladder = ladder w.calculate_base_sizes() task_notifier.emit_task_progress( progress={ 'style': 'determinate', 'total': total_wells, 'current_state': idx + 1, 'message': 'Recalculating Ladders...' } ) task_notifier.emit_task_success(message="Successfully Recalculated Ladder.")
def delete_plate(json): plate_id = json.get('plate_id', None) task = 'delete' task_notifier = TaskNotifier(task=task, namespace=SOCK_NAMESPACE, plate_id=plate_id) task_notifier.emit_task_start() plate = Plate.query.get(plate_id) if not plate: task_notifier.emit_task_failure(message='Plate Not Found.') return else: task_notifier.emit_task_progress(progress={ 'style': 'indeterminate', 'total': 1, 'current_state': 1, 'message': f'Deleting {plate.label}' }) db.session.delete(plate) socketio.sleep() task_notifier.emit_task_success(message='Deletion Complete.') return
def from_extracted_plate(cls, extracted_plate, ladder): p = cls(label=extracted_plate.label, comments=extracted_plate.comments, creator=extracted_plate.creator, date_run=extracted_plate.date_run, well_arrangement=extracted_plate.well_arrangement, ce_machine=extracted_plate.ce_machine, plate_hash=extracted_plate.plate_hash, current=extracted_plate.current, voltage=extracted_plate.voltage, temperature=extracted_plate.temperature, power=extracted_plate.power) db.session.add(p) socketio.sleep() for well in extracted_plate.wells: w = Well(well_label=well.well_label, comments=well.comments, base_sizes=well.base_sizes, ladder_peak_indices=well.ladder_peak_indices, sizing_quality=well.sizing_quality, offscale_indices=well.offscale_indices, fsa_hash=well.fsa_hash) w.plate = p w.ladder = ladder db.session.add(w) socketio.sleep() for channel in well.channels: c = Channel(wavelength=channel.wavelength, data=channel.data, color=channel.color) c.well = w db.session.add(c) socketio.sleep() return p
def add_samples(self, sample_ids): present_sample_ids = set([_[0] for _ in ProjectSampleAnnotations.query .filter(ProjectSampleAnnotations.project_id == self.id) .values(ProjectSampleAnnotations.sample_id)]) full_sample_ids = list(set(sample_ids) - present_sample_ids) sample_ids_map = defaultdict(list) channel_and_sample_ids = Channel.query.join(Sample).join(Locus).join(locus_set_association_table).join( LocusSet).join( Project).filter(Project.id == self.id).values(Channel.id, Channel.sample_id) socketio.sleep() for channel_id, sample_id in channel_and_sample_ids: sample_ids_map[sample_id].append(channel_id) for sample_ids in subset(full_sample_ids, 100): channel_ids = [] for sample_id in sample_ids: socketio.sleep() channel_ids += sample_ids_map[sample_id] sample_annotation = ProjectSampleAnnotations(sample_id=sample_id, project_id=self.id) db.session.add(sample_annotation) self.sample_annotations.append(sample_annotation) for locus in self.locus_set.loci: locus_annotation = Genotype(locus_id=locus.id, project_id=self.id) sample_annotation.locus_annotations.append(locus_annotation) self.bulk_create_channel_annotations(channel_ids) db.session.flush() for locus in self.locus_set.loci: self.samples_changed(locus.id) socketio.sleep() return self
def get_sample(json): ids = list(set(extract_ids(json))) channels = [] wells = [] samples = [] for sample_id in ids: sample = Sample.query.get(sample_id) if sample: samples.append(sample) channels += Channel.query.filter(Channel.sample_id == sample_id).all() wells += Well.query.join(Channel).filter(Channel.sample_id == sample_id).all() else: socketio.emit('get_failed', {SAMPLE_NAMESPACE: [sample_id]}, namespace=make_namespace(SAMPLE_NAMESPACE)) channel_dump = channel_schema.dumps(channels, many=True) socketio.emit('get', {CHANNEL_NAMESPACE: channel_dump.data}, namespace=make_namespace(CHANNEL_NAMESPACE)) socketio.sleep() well_dump = well_schema.dumps(wells, many=True) socketio.emit('get', {WELL_NAMESPACE: well_dump.data}, namespace=make_namespace(WELL_NAMESPACE)) socketio.sleep() sample_dump = sample_schema.dumps(samples, many=True) socketio.emit('get', {SAMPLE_NAMESPACE: sample_dump.data}, namespace=make_namespace(SAMPLE_NAMESPACE)) socketio.sleep()