def __init__(self, config: Configuration, tracing_enabled: bool = False): """Set up required properties to run training or prediction. :param config: configuration provided via yaml or environment variables """ self.config = config # self.feedback_strategy = feedback_strategy self.pipeline = DetectorPipeline() self.tracing_enabled = tracing_enabled
def __init__(self, config: Configuration, feedback_strategy: FeedbackStrategy = None, tracing_enabled: bool = False): """Set up required properties to run training or prediction. :param config: configuration provided via yaml or environment variables :param feedback_strategy: a function that runs to improve the feedback of system """ self.config = config self.feedback_strategy = feedback_strategy self.pipeline = DetectorPipeline() self.tracing_enabled = tracing_enabled
def test_valid_command(self): """Test case for validating that when we train a model and add it to task queue that it will run.""" mgr = DetectorPipeline() class mock_func(AbstractCommand): def execute(self): print("Test") mock = mock_func() mgr.add_steps(mock) self.assertEqual(len(mgr), TASKS_IN_QUEUE) self.assertNotEqual(mgr.count, TASKS_IN_QUEUE) mgr.execute_steps() self.assertEqual(mgr.count, TASKS_IN_QUEUE) mgr.clear()
def test_train_command(self): """Test case for validating that when we train a model and add it to task queue that it will run.""" mgr = DetectorPipeline() config = Configuration() config.STORAGE_DATASOURCE = "local" config.STORAGE_DATASINK = "stdout" config.LS_INPUT_PATH = "validation_data/Hadoop_2k.json" storage_adapter = SomStorageAdapter(config=config, feedback_strategy=None) model_adapter = SomModelAdapter(storage_adapter) tc = SomTrainJob(node_map=2, model_adapter=model_adapter) mgr.add_steps(tc) self.assertEqual(len(mgr), TASKS_IN_QUEUE) self.assertNotEqual(mgr.count, TASKS_IN_QUEUE) mgr.execute_steps() self.assertEqual(mgr.count, TASKS_IN_QUEUE) mgr.clear()
def test_invalid_command(self): """Test case for validating that when we train a model and add it to task queue that it will run.""" mgr = DetectorPipeline() class mock_func: def execute(self): print("Test") mock = mock_func() with self.assertRaises(TypeError) as context: mgr.add_steps(mock) mgr.clear()
def pipeline(): """Providing pipeline that clears up history of task runs.""" pipeline = DetectorPipeline() yield pipeline pipeline.clear()
class Facade: """For external interface for integration different adapters for custom models and training logic.""" def __init__(self, config: Configuration, tracing_enabled: bool = False): """Set up required properties to run training or prediction. :param config: configuration provided via yaml or environment variables """ self.config = config # self.feedback_strategy = feedback_strategy self.pipeline = DetectorPipeline() self.tracing_enabled = tracing_enabled @staticmethod def create_tracer(service): """Initialize tracer for open tracing. :param service: service name to use for tracing this application. :return: Tracer(opentracing.Tracer) """ logging.getLogger('').handlers = [] logging.basicConfig(format='%(message)s', level=logging.DEBUG) config = Config( config={ 'sampler': { 'type': 'const', 'param': 1, }, 'logging': True, }, service_name=service, ) return config.initialize_tracer() def run(self, single_run: bool = False): """Run train and inference and main event loop. :param single_run: if this is set to TRUE then we exit loop after first iteration. :return: None """ exit = False while exit is False: try: jobs = DetectorPipelineCatalog(config=self.config, job="sompy.train.inference") self.pipeline = jobs.get_pipeline() self.start_job() logging.info("Job ran succesfully") except EmptyDataSetException as e: logging.debug(e) finally: time.sleep(5) exit = single_run def start_job(self): """Start job to run all steps in workflow.""" job_id = uuid.uuid4() logging.info("Executing job: {}".format(job_id)) tracer = self.create_tracer('log-anomaly-detection') if self.tracing_enabled: logging.info("Tracing enabled") with tracer.start_span('anomaly_facade_run') as span: span.set_tag('job_id', job_id) with span_in_context(span): self.pipeline.execute_steps(tracer=tracer) else: self.pipeline.execute_steps() def train(self): """Run training of model and provides size of map. :return: None """ jobs = DetectorPipelineCatalog(config=self.config, job="sompy.train") self.pipeline = jobs.get_pipeline() self.start_job() def infer(self): """Run inference of model. :return: None """ jobs = DetectorPipelineCatalog(config=self.config, job="sompy.inference") self.pipeline = jobs.get_pipeline() self.start_job()