|
| 1 | +package fr.polytechnique.cmap.cnam.filtering |
| 2 | + |
| 3 | +import java.sql.Timestamp |
| 4 | +import scala.collection.JavaConverters._ |
| 5 | +import org.apache.spark.SparkContext |
| 6 | +import org.apache.spark.sql.SQLContext |
| 7 | +import com.typesafe.config.{Config, ConfigFactory} |
| 8 | +import fr.polytechnique.cmap.cnam.utilities.functions._ |
| 9 | + |
| 10 | +object FilteringConfig { |
| 11 | + |
| 12 | +/* Alternative option using vars instead of SQLContext: |
| 13 | +
|
| 14 | + private var _conf: Config = _ |
| 15 | + private var _path: String = "" |
| 16 | + private var _env: String = "test" |
| 17 | + final private val defaultConfig = ConfigFactory.parseResources("filtering-default.conf") |
| 18 | +
|
| 19 | + def path = _path |
| 20 | + def env = _env |
| 21 | +
|
| 22 | + def setPath(path: String): Unit = { _path = path } |
| 23 | + def setEnv(env: String): Unit = { _env = env } |
| 24 | + def init(path: String, env: String ): Unit = { |
| 25 | + _path = path |
| 26 | + _env = env |
| 27 | + init() |
| 28 | + } |
| 29 | + def init(): Unit = { |
| 30 | + _conf = { |
| 31 | + val defaultConfig = ConfigFactory.parseResources("filtering-default.conf") |
| 32 | + val config = ConfigFactory.parseFile(new java.io.File(path)).withFallback(defaultConfig).resolve() |
| 33 | + config.getConfig(env) |
| 34 | + } |
| 35 | + } |
| 36 | +
|
| 37 | + def conf = _conf |
| 38 | +*/ |
| 39 | + |
| 40 | + private lazy val conf: Config = { |
| 41 | + // This is a little hacky. In the future, it may be nice to find a better way. |
| 42 | + val sqlContext = SQLContext.getOrCreate(SparkContext.getOrCreate()) |
| 43 | + val configPath: String = sqlContext.getConf("conf", "") |
| 44 | + val environment: String = sqlContext.getConf("env", "test") |
| 45 | + |
| 46 | + val defaultConfig = ConfigFactory.parseResources("filtering-default.conf").resolve().getConfig(environment) |
| 47 | + val newConfig = ConfigFactory.parseFile(new java.io.File(configPath)) |
| 48 | + |
| 49 | + newConfig.withFallback(defaultConfig).resolve() |
| 50 | + } |
| 51 | + |
| 52 | + case class InputPaths( |
| 53 | + dcir: String, |
| 54 | + pmsiMco: String, |
| 55 | + pmsiHad: String, |
| 56 | + pmsiSsr: String, |
| 57 | + irBen: String, |
| 58 | + irImb: String, |
| 59 | + irPha: String, |
| 60 | + dosages: String |
| 61 | + ) |
| 62 | + |
| 63 | + case class OutputPaths(root: String, patients: String, flatEvents: String) |
| 64 | + |
| 65 | + case class Limits( |
| 66 | + minYear: Int, |
| 67 | + maxYear: Int, |
| 68 | + minMonth: Int, |
| 69 | + maxMonth: Int, |
| 70 | + minGender: Int, |
| 71 | + maxGender: Int, |
| 72 | + minAge: Int, |
| 73 | + maxAge: Int |
| 74 | + ) |
| 75 | + |
| 76 | + case class Dates(ageReference: Timestamp) |
| 77 | + |
| 78 | + lazy val drugCategories: List[String] = conf.getStringList("drug_categories").asScala.toList |
| 79 | + lazy val cancerDefinition: String = conf.getString("cancer_definition") |
| 80 | + lazy val diseaseCode: String = conf.getString("disease_code") |
| 81 | + lazy val mcoDeathCode: Int = conf.getInt("mco_death_code") |
| 82 | + lazy val inputPaths = InputPaths( |
| 83 | + dcir = conf.getString("paths.input.dcir"), |
| 84 | + pmsiMco = conf.getString("paths.input.pmsi_mco"), |
| 85 | + pmsiHad = conf.getString("paths.input.pmsi_had"), |
| 86 | + pmsiSsr = conf.getString("paths.input.pmsi_ssr"), |
| 87 | + irBen = conf.getString("paths.input.ir_ben"), |
| 88 | + irImb = conf.getString("paths.input.ir_imb"), |
| 89 | + irPha = conf.getString("paths.input.ir_pha"), |
| 90 | + dosages = conf.getString("paths.input.dosages") |
| 91 | + ) |
| 92 | + lazy val outputPaths = OutputPaths( |
| 93 | + root = conf.getString("paths.output.root"), |
| 94 | + patients = conf.getString("paths.output.patients"), |
| 95 | + flatEvents = conf.getString("paths.output.flat_events") |
| 96 | + ) |
| 97 | + lazy val limits = Limits( |
| 98 | + minYear = conf.getInt("limits.min_year"), |
| 99 | + maxYear = conf.getInt("limits.max_year"), |
| 100 | + minMonth = conf.getInt("limits.min_month"), |
| 101 | + maxMonth = conf.getInt("limits.max_month"), |
| 102 | + minGender = conf.getInt("limits.min_gender"), |
| 103 | + maxGender = conf.getInt("limits.max_gender"), |
| 104 | + minAge = conf.getInt("limits.min_age"), |
| 105 | + maxAge = conf.getInt("limits.max_age") |
| 106 | + ) |
| 107 | + lazy val dates = Dates( |
| 108 | + ageReference = makeTS(conf.getIntList("dates.age_reference").asScala.toList) |
| 109 | + ) |
| 110 | +} |
0 commit comments