Beispiel #1
0
 def answer_dynamic_query(self, query, bucket=EMMAA_BUCKET_NAME):
     """Answer user query by simulating a PySB model."""
     pysb_model, use_kappa, time_limit, num_times, num_sim = \
         self._get_dynamic_components()
     tra = TRA(use_kappa=use_kappa)
     tp = query.get_temporal_pattern(time_limit)
     try:
         sat_rate, num_sim, kpat, pat_obj, fig_path = tra.check_property(
             pysb_model, tp, num_times=num_times)
         if self.mode == 's3':
             fig_name, ext = os.path.splitext(os.path.basename(fig_path))
             date_str = make_date_str()
             s3_key = (f'query_images/{self.model.name}/{fig_name}_'
                       f'{date_str}{ext}')
             s3_path = f'https://{bucket}.s3.amazonaws.com/{s3_key}'
             client = get_s3_client(unsigned=False)
             logger.info(f'Uploading image to {s3_path}')
             client.upload_file(fig_path, Bucket=bucket, Key=s3_key)
             fig_path = s3_path
         resp_json = {
             'sat_rate': sat_rate,
             'num_sim': num_sim,
             'kpat': kpat,
             'fig_path': fig_path
         }
     except (MissingMonomerError, MissingMonomerSiteError):
         resp_json = RESULT_CODES['QUERY_NOT_APPLICABLE']
     return [('pysb', self.hash_response_list(resp_json), resp_json)]
Beispiel #2
0
 def answer_intervention_query(self, query, bucket=EMMAA_BUCKET_NAME):
     """Answer user intervention query by simulating a PySB model."""
     pysb_model, use_kappa, time_limit, num_times, num_sim, _ = \
         self._get_dynamic_components('intervention')
     tra = TRA(use_kappa=use_kappa)
     try:
         res, fig_path = tra.compare_conditions(pysb_model,
                                                query.condition_entity,
                                                query.target_entity,
                                                query.direction, time_limit,
                                                num_times)
         if self.mode == 's3':
             fig_name, ext = os.path.splitext(os.path.basename(fig_path))
             date_str = make_date_str()
             s3_key = (f'query_images/{self.model.name}/{fig_name}_'
                       f'{date_str}{ext}')
             s3_path = f'https://{bucket}.s3.amazonaws.com/{s3_key}'
             client = get_s3_client(unsigned=False)
             logger.info(f'Uploading image to {s3_path}')
             client.upload_file(fig_path, Bucket=bucket, Key=s3_key)
             fig_path = s3_path
         resp_json = {'result': res, 'fig_path': fig_path}
         return [('pysb', self.hash_response_list(resp_json), resp_json)]
     except (MissingMonomerError, MissingMonomerSiteError):
         resp_json = RESULT_CODES['QUERY_NOT_APPLICABLE']
         return [('pysb', self.hash_response_list(resp_json), {
             'fail_reason': RESULT_CODES['QUERY_NOT_APPLICABLE']
         })]