背景:
我有一系列连续的,带时间戳的数据。数据序列中存在间隙,其中数据不连续。我想创建一个方法将序列分成序列序列,以便每个子序列包含连续数据(在间隙处拆分输入序列)。
约束:
方法签名
let groupContiguousDataPoints (timeBetweenContiguousDataPoints : TimeSpan) (dataPointsWithHoles : seq<DateTime * float>) : (seq<seq< DateTime * float >>)= ...
从表面上看,这个问题看起来微不足道,但即使采用Seq.pairwise,IEnumerator&lt; _&gt ;,序列理解和屈服声明,解决方案也让我望而却步。我确信这是因为我仍然缺乏组合F#-idioms的经验,或者可能因为我还没有接触过一些语言结构。
// Test data
let numbers = {1.0..1000.0}
let baseTime = DateTime.Now
let contiguousTimeStamps = seq { for n in numbers ->baseTime.AddMinutes(n)}
let dataWithOccationalHoles = Seq.zip contiguousTimeStamps numbers |> Seq.filter (fun (dateTime, num) -> num % 77.0 <> 0.0) // Has a gap in the data every 77 items
let timeBetweenContiguousValues = (new TimeSpan(0,1,0))
dataWithOccationalHoles |> groupContiguousDataPoints timeBetweenContiguousValues |> Seq.iteri (fun i sequence -> printfn "Group %d has %d data-points: Head: %f" i (Seq.length sequence) (snd(Seq.hd sequence)))
答案 0 :(得分:3)
我认为这可以做你想要的事情
dataWithOccationalHoles
|> Seq.pairwise
|> Seq.map(fun ((time1,elem1),(time2,elem2)) -> if time2-time1 = timeBetweenContiguousValues then 0, ((time1,elem1),(time2,elem2)) else 1, ((time1,elem1),(time2,elem2)) )
|> Seq.scan(fun (indexres,(t1,e1),(t2,e2)) (index,((time1,elem1),(time2,elem2))) -> (index+indexres,(time1,elem1),(time2,elem2)) ) (0,(baseTime,-1.0),(baseTime,-1.0))
|> Seq.map( fun (index,(time1,elem1),(time2,elem2)) -> index,(time2,elem2) )
|> Seq.filter( fun (_,(_,elem)) -> elem <> -1.0)
|> PSeq.groupBy(fst)
|> Seq.map(snd>>Seq.map(snd))
感谢您提出这个很酷的问题
答案 1 :(得分:2)
我将Alexey的Haskell翻译成F#,但它在F#中并不漂亮,而且还有一个元素太急切了。
我希望有更好的方法,但我必须稍后再试。
let N = 20
let data = // produce some arbitrary data with holes
seq {
for x in 1..N do
if x % 4 <> 0 && x % 7 <> 0 then
printfn "producing %d" x
yield x
}
let rec GroupBy comp (input:LazyList<'a>) : LazyList<LazyList<'a>> =
LazyList.delayed (fun () ->
match input with
| LazyList.Nil -> LazyList.cons (LazyList.empty()) (LazyList.empty())
| LazyList.Cons(x,LazyList.Nil) ->
LazyList.cons (LazyList.cons x (LazyList.empty())) (LazyList.empty())
| LazyList.Cons(x,(LazyList.Cons(y,_) as xs)) ->
let groups = GroupBy comp xs
if comp x y then
LazyList.consf
(LazyList.consf x (fun () ->
let (LazyList.Cons(firstGroup,_)) = groups
firstGroup))
(fun () ->
let (LazyList.Cons(_,otherGroups)) = groups
otherGroups)
else
LazyList.cons (LazyList.cons x (LazyList.empty())) groups)
let result = data |> LazyList.of_seq |> GroupBy (fun x y -> y = x + 1)
printfn "Consuming..."
for group in result do
printfn "about to do a group"
for x in group do
printfn " %d" x
答案 2 :(得分:1)
您似乎想要一个具有签名
的功能(`a -> bool) -> seq<'a> -> seq<seq<'a>>
即。一个函数和一个序列,然后根据函数的结果将输入序列分解为一系列序列。
将值缓存到实现IEnumerable的集合中可能是最简单的(尽管不是完全纯粹的,但是避免多次迭代输入。它将失去输入的大部分懒惰):
let groupBy (fun: 'a -> bool) (input: seq) = seq { let cache = ref (new System.Collections.Generic.List()) for e in input do (!cache).Add(e) if not (fun e) then yield !cache cache := new System.Collections.Generic.List() if cache.Length > 0 then yield !cache }
另一种实现可以将缓存集合(如seq<'a>
)传递给函数,以便它可以看到多个元素来选择断点。
答案 3 :(得分:1)
一个Haskell解决方案,因为我不太了解F#语法,但它应该很容易翻译:
type TimeStamp = Integer -- ticks
type TimeSpan = Integer -- difference between TimeStamps
groupContiguousDataPoints :: TimeSpan -> [(TimeStamp, a)] -> [[(TimeStamp, a)]]
Prelude中有一个函数groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
:
group函数获取一个列表并返回一个列表列表,使得结果的串联等于参数。此外,结果中的每个子列表仅包含相同的元素。例如,
group "Mississippi" = ["M","i","ss","i","ss","i","pp","i"]
这是groupBy的一个特例,它允许程序员提供自己的相等测试。
这不是我们想要的,因为它将列表中的每个元素与当前组的 first 元素进行比较,我们需要比较连续的元素。如果我们有这样的函数groupBy1
,我们可以轻松地编写groupContiguousDataPoints
:
groupContiguousDataPoints maxTimeDiff list = groupBy1 (\(t1, _) (t2, _) -> t2 - t1 <= maxTimeDiff) list
所以让我们来写吧!
groupBy1 :: (a -> a -> Bool) -> [a] -> [[a]]
groupBy1 _ [] = [[]]
groupBy1 _ [x] = [[x]]
groupBy1 comp (x : xs@(y : _))
| comp x y = (x : firstGroup) : otherGroups
| otherwise = [x] : groups
where groups@(firstGroup : otherGroups) = groupBy1 comp xs
更新:看起来F#不允许你在seq
上进行模式匹配,所以毕竟翻译起来并不容易。但是,this thread on HubFS显示了一种模式匹配序列的方法,可以在需要时将其转换为LazyList
。
UPDATE2:Haskell列表是延迟并根据需要生成,因此它们对应于F#的LazyList
(而不是seq
,因为生成的数据被缓存(并且收集了垃圾)当然,如果你不再提及它))。
答案 4 :(得分:1)
(编辑:这与Brian的解决方案有类似的问题,因为迭代外部序列而不迭代每个内部序列会严重搞乱!)
这是一个嵌套序列表达式的解决方案。 .NET的IEnumerable<T>
的不透明性在这里非常明显,这使得为这个问题编写惯用的F#代码变得有点困难,但希望它仍然清楚发生了什么。
let groupBy cmp (sq:seq<_>) =
let en = sq.GetEnumerator()
let rec partitions (first:option<_>) =
seq {
match first with
| Some first' -> //'
(* The following value is always overwritten;
it represents the first element of the next subsequence to output, if any *)
let next = ref None
(* This function generates a subsequence to output,
setting next appropriately as it goes *)
let rec iter item =
seq {
yield item
if (en.MoveNext()) then
let curr = en.Current
if (cmp item curr) then
yield! iter curr
else // consumed one too many - pass it on as the start of the next sequence
next := Some curr
else
next := None
}
yield iter first' (* ' generate the first sequence *)
yield! partitions !next (* recursively generate all remaining sequences *)
| None -> () // return an empty sequence if there are no more values
}
let first = if en.MoveNext() then Some en.Current else None
partitions first
let groupContiguousDataPoints (time:TimeSpan) : (seq<DateTime*_> -> _) =
groupBy (fun (t,_) (t',_) -> t' - t <= time)
答案 5 :(得分:1)
好的,再试一次。在F#中实现最佳懒惰程度变得有点困难......从好的方面来说,这比我上一次尝试更有用,因为它不使用任何ref单元格。
let groupBy cmp (sq:seq<_>) =
let en = sq.GetEnumerator()
let next() = if en.MoveNext() then Some en.Current else None
(* this function returns a pair containing the first sequence and a lazy option indicating the first element in the next sequence (if any) *)
let rec seqStartingWith start =
match next() with
| Some y when cmp start y ->
let rest_next = lazy seqStartingWith y // delay evaluation until forced - stores the rest of this sequence and the start of the next one as a pair
seq { yield start; yield! fst (Lazy.force rest_next) },
lazy Lazy.force (snd (Lazy.force rest_next))
| next -> seq { yield start }, lazy next
let rec iter start =
seq {
match (Lazy.force start) with
| None -> ()
| Some start ->
let (first,next) = seqStartingWith start
yield first
yield! iter next
}
Seq.cache (iter (lazy next()))
答案 6 :(得分:0)
下面是一些符合我认为你想要的代码。这不是惯用的F#。
(它可能类似于Brian的答案,但我无法分辨,因为我不熟悉LazyList语义。)
但它与您的测试规范不完全匹配:Seq.length枚举其整个输入。您的“测试代码”会调用Seq.length
,然后调用Seq.hd
。这将生成一个枚举器两次,因为没有缓存,所以事情搞砸了。我不确定是否有任何干净的方法允许多个枚举器没有缓存。坦率地说,seq<seq<'a>>
可能不是此问题的最佳数据结构。
无论如何,这是代码:
type State<'a> = Unstarted | InnerOkay of 'a | NeedNewInner of 'a | Finished
// f() = true means the neighbors should be kept together
// f() = false means they should be split
let split_up (f : 'a -> 'a -> bool) (input : seq<'a>) =
// simple unfold that assumes f captured a mutable variable
let iter f = Seq.unfold (fun _ ->
match f() with
| Some(x) -> Some(x,())
| None -> None) ()
seq {
let state = ref (Unstarted)
use ie = input.GetEnumerator()
let innerMoveNext() =
match !state with
| Unstarted ->
if ie.MoveNext()
then let cur = ie.Current
state := InnerOkay(cur); Some(cur)
else state := Finished; None
| InnerOkay(last) ->
if ie.MoveNext()
then let cur = ie.Current
if f last cur
then state := InnerOkay(cur); Some(cur)
else state := NeedNewInner(cur); None
else state := Finished; None
| NeedNewInner(last) -> state := InnerOkay(last); Some(last)
| Finished -> None
let outerMoveNext() =
match !state with
| Unstarted | NeedNewInner(_) -> Some(iter innerMoveNext)
| InnerOkay(_) -> failwith "Move to next inner seq when current is active: undefined behavior."
| Finished -> None
yield! iter outerMoveNext }
open System
let groupContigs (contigTime : TimeSpan) (holey : seq<DateTime * int>) =
split_up (fun (t1,_) (t2,_) -> (t2 - t1) <= contigTime) holey
// Test data
let numbers = {1 .. 15}
let contiguousTimeStamps =
let baseTime = DateTime.Now
seq { for n in numbers -> baseTime.AddMinutes(float n)}
let holeyData =
Seq.zip contiguousTimeStamps numbers
|> Seq.filter (fun (dateTime, num) -> num % 7 <> 0)
let grouped_data = groupContigs (new TimeSpan(0,1,0)) holeyData
printfn "Consuming..."
for group in grouped_data do
printfn "about to do a group"
for x in group do
printfn " %A" x
答案 7 :(得分:0)
好的,这是我不满意的答案。
(编辑:我不开心 - 这是错的!虽然现在没时间尝试修复。)
它使用了一些命令状态,但它并不太难以遵循(前提是你记得'!'是F#取消引用运算符,而不是'不')。它尽可能地懒惰,并将seq作为输入并返回seqs seqs作为输出。
let N = 20
let data = // produce some arbitrary data with holes
seq {
for x in 1..N do
if x % 4 <> 0 && x % 7 <> 0 then
printfn "producing %d" x
yield x
}
let rec GroupBy comp (input:seq<_>) = seq {
let doneWithThisGroup = ref false
let areMore = ref true
use e = input.GetEnumerator()
let Next() = areMore := e.MoveNext(); !areMore
// deal with length 0 or 1, seed 'prev'
if not(e.MoveNext()) then () else
let prev = ref e.Current
while !areMore do
yield seq {
while not(!doneWithThisGroup) do
if Next() then
let next = e.Current
doneWithThisGroup := not(comp !prev next)
yield !prev
prev := next
else
// end of list, yield final value
yield !prev
doneWithThisGroup := true }
doneWithThisGroup := false }
let result = data |> GroupBy (fun x y -> y = x + 1)
printfn "Consuming..."
for group in result do
printfn "about to do a group"
for x in group do
printfn " %d" x