-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_interactive_mortality_params.exs
70 lines (61 loc) · 1.68 KB
/
import_interactive_mortality_params.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Import parameters for mortality table and interactively computing mortality
#
# Usage: mix run <mortality-params-csv-file>
#
# where <mortality-params-csv-file> is a csv file witht the following columns
# - covariate
# - coef
# - ci95_lower
# - ci95_upper
# - p_value
# - endpoint
# - mean
# - sex
alias Risteys.{Repo, FGEndpoint.Definition, MortalityParams}
require Logger
Logger.configure(level: :info)
[filepath | _] = System.argv()
filepath
|> File.stream!()
|> CSV.decode!(headers: :true)
|> Enum.each(fn row ->
%{
"endpoint" => name,
"covariate" => covariate,
"coef" => coef,
"ci95_lower" => ci95_lower,
"ci95_upper" => ci95_upper,
"p_value" => p_value,
"mean" => mean,
"sex" => sex
} = row
Logger.info("Handling data of #{name}")
endpoint = Repo.get_by(Definition, name: name)
case endpoint do
nil ->
Logger.warning("Enpoint #{name} not in DB, skipping.")
endpoint ->
params =
case Repo.get_by(MortalityParams, fg_endpoint_id: endpoint.id, covariate: covariate, sex: sex) do
nil -> %MortalityParams{}
existing -> existing
end
|> MortalityParams.changeset(%{
fg_endpoint_id: endpoint.id,
covariate: covariate,
coef: String.to_float(coef),
ci95_lower: String.to_float(ci95_lower),
ci95_upper: String.to_float(ci95_upper),
p_value: String.to_float(p_value),
mean: String.to_float(mean),
sex: sex
})
|> Repo.insert_or_update()
case params do
{:ok, _} ->
Logger.info("Insert ok")
{:error, changeset} ->
Logger.warning(inspect(changeset))
end
end
end)