Skip to content

Commit

Permalink
Interpolations between timeseries (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfabianus committed Nov 13, 2023
1 parent 51f8c48 commit 53e3b4f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ version = "1.0.0-DEV"
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
Measurements = "eff96d63-e80a-5855-80a2-b1b0885c5ab7"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
Expand Down
1 change: 1 addition & 0 deletions src/Experiments.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using TimeSeries
using Dates
using DataFrames
using CSV
using Interpolations

include("types.jl")
include("datetime_handling.jl")
Expand Down
4 changes: 4 additions & 0 deletions src/datetime_handling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,8 @@ julia> duration(experiment)
"""
function duration(experiment::Experiment)
return Dates.canonicalize(endtime(experiment) - starttime(experiment))
end

function timestamp2hours(timeseries::TimeArray)
return (timestamp(timeseries) .- starttime(timeseries)) ./ Dates.Hour(1)
end
5 changes: 5 additions & 0 deletions src/importers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ function timeseries(experiment::Experiments.Experiment, names::Symbol...)
return ts_vec[1]
end
return merge(ts_vec..., method = :outer)
end

function interpolate(from_ts::TimeArray, to_ts::TimeArray)
lin = linear_interpolation(Experiments.timestamp2hours(from_ts), vec(values(from_ts)), extrapolation_bc=Line())
return timeseries(Symbol(meta(from_ts), :_interp), timestamp(to_ts), lin.(Experiments.timestamp2hours(to_ts)))
end
11 changes: 10 additions & 1 deletion test/usecases.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ using Plots
using Dates
using CSV
using DataFrames
using Interpolations

# First, creating some measurements from fermentations
df_offline = CSV.read("data/offline.csv", DataFrame)
Expand Down Expand Up @@ -52,4 +53,12 @@ Experiments.calc!(experiments, Experiments.diff, :OD, :DCW, :p_O2)
experiments

# interpolation
ts = Experiments.timeseries(exp, :OD)
ts = Experiments.timeseries(exp, :OD)
lin = linear_interpolation(Experiments.timestamp2hours(ts), vec(values(ts)), extrapolation_bc=Line())
plot([0:0.1:37], lin.([0:0.1:37]), label="interpolated")
scatter!(Experiments.timestamp2hours(ts), vec(values(ts)), label="original")
OD_2 = Experiments.interpolate(ts, pO2)
meta(OD_2)
plot(values(pO2),values(OD_2))
plot!(pO2)

0 comments on commit 53e3b4f

Please sign in to comment.