|
| 1 | +package actors |
| 2 | + |
| 3 | +import actors.BatchTrainer.BatchTrainerModel |
| 4 | +import actors.ModelPerformanceSupervisor.TrainerType.TrainerType |
| 5 | +import akka.actor.{Actor, ActorLogging, ActorRef, Props} |
| 6 | +import akka.event.LoggingReceive |
| 7 | +import models.preprocessing.DataFrame |
| 8 | +import play.api.libs.json.{Json, Reads, Writes} |
| 9 | + |
| 10 | +import utils.EnumeratorUtils |
| 11 | + |
| 12 | +class ModelPerformanceSupervisor extends Actor with ActorLogging { |
| 13 | + |
| 14 | + import ModelPerformanceSupervisor._ |
| 15 | + |
| 16 | + var clients = Set.empty[ActorRef] |
| 17 | + |
| 18 | + var df: Option[DataFrame] = None |
| 19 | + |
| 20 | + var batchTrainerModel: Option[BatchTrainerModel] = None |
| 21 | + |
| 22 | + override def receive = LoggingReceive { |
| 23 | + |
| 24 | + case batchModel: BatchTrainerModel => |
| 25 | + batchTrainerModel = Some(batchModel) |
| 26 | + validateBatchModel(batchModel) foreach sendMessage |
| 27 | + |
| 28 | + case TrainingSet(c: DataFrame) => |
| 29 | + df = Some(c) |
| 30 | + |
| 31 | + case Subscribe => |
| 32 | + context.watch(sender) |
| 33 | + clients += sender |
| 34 | + for { |
| 35 | + model <- batchTrainerModel |
| 36 | + performance <- validateBatchModel(model) |
| 37 | + } yield sender ! performance |
| 38 | + |
| 39 | + case Unsubscribe => |
| 40 | + context.unwatch(sender) |
| 41 | + clients -= sender |
| 42 | + |
| 43 | + } |
| 44 | + |
| 45 | + //TODO: Write actual performance implementation. |
| 46 | + def validateBatchModel(batchTrainerModel: BatchTrainerModel): Option[ModelPerformance] = { |
| 47 | + Option(ModelPerformance("Regression", "1.0", .99, .99)) |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + def sendMessage(msg: ModelPerformance) = clients.foreach(_ ! msg) |
| 52 | + |
| 53 | + def logStatistics(performance: ModelPerformance): Unit = { |
| 54 | + log.info(s"Trainer type: ${performance.trainer}") |
| 55 | + log.info(s"Current model: ${performance.model}") |
| 56 | + log.info(s"Area under the ROC curve: ${performance.areaUnderRoc}") |
| 57 | + log.info(s"Accuracy: ${performance.accuracy}") |
| 58 | + } |
| 59 | + |
| 60 | +} |
| 61 | + |
| 62 | +object ModelPerformanceSupervisor { |
| 63 | + |
| 64 | + def props() = Props(new ModelPerformanceSupervisor) |
| 65 | + |
| 66 | + object TrainerType extends Enumeration { |
| 67 | + |
| 68 | + type TrainerType = TrainerType.Value |
| 69 | + |
| 70 | + val Batch = Value |
| 71 | + |
| 72 | + implicit val reads: Reads[TrainerType] = EnumeratorUtils.enumReads(TrainerType) |
| 73 | + |
| 74 | + implicit val writes: Writes[TrainerType] = EnumeratorUtils.enumWrites |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + case class TrainingSet(data: DataFrame) |
| 79 | + |
| 80 | + case class ModelPerformance(trainer: String, model: String, areaUnderRoc: Double, accuracy: Double) |
| 81 | + |
| 82 | + |
| 83 | + object ModelPerformance { |
| 84 | + |
| 85 | + implicit val formatter = Json.format[ModelPerformance] |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments