Skip to content

Commit

Permalink
Merge pull request #15 from Deduction42/DEV
Browse files Browse the repository at this point in the history
Expanded support for setindex!(ts::TimeSeries,...)
  • Loading branch information
Deduction42 authored Nov 19, 2024
2 parents fbb2515 + 286c7a7 commit f2c8e6a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TimeRecords"
uuid = "b543fe20-4c68-4b5f-af79-4641a0d39826"
authors = ["Ruben Gonzalez <[email protected]> and contributors"]
version = "1.3.2"
version = "1.3.3"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ ts[TimeInterval(0=>3.1)]
TimeRecord{Int64}(t=1970-01-01T00:00:02, v=2)
TimeRecord{Int64}(t=1970-01-01T00:00:03, v=3)
```
Some additional notes on TimeSeries
- `records(ts::AbstractTimeSeries)` will return normal vector, but care must be taken with mutation in order to prevent violating the inherent chronological assumptions of the TimeSeries
Some additional notes on TimeSeries and its chronological API
- `ts[dt::TimeInterval]` will return any time series data points on or inside the time interval
- `push!(ts::AbstractTimeSeries, r::TimeRecord)` will insert `r` into `ts` while maintaining chronological order
- `setindex(AbstractTimeSeries, x, ind)` will only set the value, not the timestamp (in order to guarantee sorting).
- If timestamps need to be altered, use `deleteat!(ts, ind)` then `push!(ts, r, indhint=ind)`
- `ts[dt::TimeInterval]` will return any time series data points on or inside the time interval
- `setindex(ts::AbstractTimeSeries, x::Any, ind)` will only overwrite the value, keeping the timestamp the same
- `setindex(ts::AbstractTimeSeries, r::TimeRecord, ind)` replaces the value if the timestamps are equal, otherwise it uses `deleteat!(ts, ind)` then `push!(ts, r, indhint=ind)` (in order to guarantees sorting)
- `setindex(ts::AbstractTimeSeries, vr::AbstractVector{TimeRecord}, ind)` overwrites values in `records(ts)` and then sorts
- `records(ts::AbstractTimeSeries)` will return the internal timeseries vector, but care must be taken with mutation in order to prevent violating the inherent chronological assumptions of the TimeSeries

## Interpolation
The first major functionality supported is interpolation. Supported interpolation methods are zero-order-hold (order=0) or linear (order=1).
Expand Down
32 changes: 31 additions & 1 deletion src/_TimeSeries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,41 @@ function Base.getindex(ts::T, ind) where T <: AbstractTimeSeries
return T(getindex(records(ts), ind), issorted=issorted(ind))
end

#Set index basesd on value only (maintains the same timestamp to guarantee sorting)
function Base.setindex!(ts::AbstractTimeSeries{T}, x, ind::Integer) where T
setindex!(records(ts), TimeRecord(ts[ind].t, T(x)), ind)
setindex!(records(ts), TimeRecord(timestamp(ts[ind]), T(x)), ind)
return ts
end

#Sets multiple indices based on value only
function Base.setindex!(ts::AbstractTimeSeries{T}, X::AbstractArray, inds::AbstractVector) where T
Base.setindex_shape_check(X, length(inds))
ix0 = firstindex(X)
for (count, ind) in enumerate(inds)
x = T(X[ix0+count-1])
setindex!(records(ts), TimeRecord(timestamp(ts[ind]), x), ind)
end
return ts
end

#Set index checks for timestamp equality, but other wise deletes element [i] and uses that as the inertion hint
function Base.setindex!(ts::AbstractTimeSeries{T}, r::TimeRecord, ind::Integer) where T
if timestamp(r) == timestamp(ts[ind])
setindex!(records(ts), r, ind)
else
deleteat!(ts, ind)
push!(ts, r, indhint=ind)
end
return ts
end

#Set multiple indices and then sort the timeseries
function Base.setindex!(ts::AbstractTimeSeries{T}, vr::AbstractVector{<:TimeRecord}, ind::AbstractVector) where T
setindex!(records(ts), vr, ind)
sort!(ts)
return ts
end

function Base.fill!(ts::AbstractTimeSeries, x)
for ii in eachindex(ts)
ts[ii] = x
Expand Down
20 changes: 20 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,31 @@ using Dates
@test keeplatest!(TimeSeries{Float64}(), 4) == TimeSeries{Float64}()

ts = TimeSeries{Float64}(1:5, 1:5)

ts[1:3] .= 0
@test ts == TimeSeries{Float64}(1:5, [0,0,0,4,5])

ts[1:3] = 1:3
@test ts == TimeSeries{Float64}(1:5, 1:5)

ts[1:3] = TimeSeries(1:3,1:3)
@test ts == TimeSeries{Float64}(1:5, 1:5)

ts[2] = TimeRecord(2.5,2)
@test ts == TimeSeries{Float64}([1,2.5,3,4,5], 1:5)

ts[2] = TimeRecord(2,2)
@test ts == TimeSeries{Float64}(1:5, 1:5)

ts[2] = TimeRecord(3.5,2)
@test ts == TimeSeries{Float64}([1,3,3.5,4,5], [1,3,2,4,5])

ts[3] = TimeRecord(2,2)
@test ts == TimeSeries{Float64}(1:5,1:5)

ts[1:3] = TimeSeries(2:4, 1:3)
@test ts == TimeSeries{Float64}([2:4;4:5],1:5)

interval = TimeInterval(DateTime("2024-01-01T00:00:48.928"),DateTime("2024-01-01T00:00:49.115"))
dates = [DateTime("2024-01-01T00:00:48.393"), DateTime("2024-01-01T00:00:49.275"), DateTime("2024-01-01T00:00:50.470")]
ts = TimeSeries(dates, 1:3)
Expand Down

2 comments on commit f2c8e6a

@Deduction42
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register

Release notes:

Expanded support for setindex!(ts::TimeSeries,...)

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/119779

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.3.3 -m "<description of version>" f2c8e6a868a3b6125a721251feee5d633fafdf40
git push origin v1.3.3

Please sign in to comment.