Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.9K Views
|
8 Replies
|
12 Total Likes
View groups...
Share
Share this post:

[Solved] Questions about time interval processing?

I have a set of time data (such as data) and want to cut it into time intervals to display in TimelinePlot. I hope the answer is like out. How can I do it?

data =(DateObject@# & /@ #) & /@ {{{2019, 8, 29, 8, 30, 0.`}, {2019, 8, 29, 9, 0, 0.`}}, {{2019, 8, 29, 15, 0, 0.`}, {2019, 8, 29, 15, 30, 0.`}}, {{2019, 8, 29, 15, 30, 0.`}, {2019, 8, 29, 16, 0, 0.`}}, {{2019, 8, 29, 16, 0, 0.`}, {2019, 8, 29, 16, 30, 0.`}}, {{2019, 8, 29, 16, 30, 0.`}, {2019, 8, 29, 17, 0, 0.`}}, {{2019, 8, 30, 0, 0, 0.`}, {2019, 8, 30, 0, 30, 0.`}}, {{2019, 8, 30, 0, 30, 0.`}, {2019, 8, 30, 1, 0, 0.`}}, {{2019, 8, 30, 1, 0, 0.`}, {2019, 8, 30, 1, 30, 0.`}}, {{2019, 8, 30, 1, 30, 0.`}, {2019, 8, 30, 2, 0, 0.`}}, {{2019, 8, 30, 2, 0, 0.`}, {2019, 8, 30, 2, 30, 0.`}}, {{2019, 8, 30, 2, 30, 0.`}, {2019, 8, 30, 3, 0, 0.`}}, {{2019, 8, 30, 3, 0, 0.`}, {2019, 8, 30, 3, 30, 0.`}}, {{2019, 8, 30, 3, 30, 0.`}, {2019, 8, 30, 4, 0, 0.`}}, {{2019, 8, 30, 4, 0, 0.`}, {2019, 8, 30, 4, 30, 0.`}}, {{2019, 8, 30, 4, 30, 0.`}, {2019, 8, 30, 5, 0, 0.`}}, {{2019, 8, 30, 5, 0, 0.`}, {2019, 8, 30, 5, 30, 0.`}}, {{2019, 8, 30, 5, 30, 0.`}, {2019, 8, 30, 6, 0, 0.`}}, {{2019, 8, 30, 6, 0, 0.`}, {2019, 8, 30, 6, 30, 0.`}}, {{2019, 8, 30, 6, 30, 0.`}, {2019, 8, 30, 7, 0, 0.`}}, {{2019, 8, 30, 7, 0, 0.`}, {2019, 8, 30, 7, 30, 0.`}}, {{2019, 8, 30, 7, 30, 0.`}, {2019, 8, 30, 8, 0, 0.`}}, {{2019, 8, 30, 8, 0, 0.`}, {2019, 8, 30, 8, 30, 0.`}}}// Flatten // Union;

out =(DateObject[#] & /@ #) & /@ {{{2019, 8, 29, 8, 30, 0.`}, {2019, 8, 29, 9, 0, 0.`}}, {{2019, 8, 29, 15, 0, 0.`}, {2019, 8, 29, 17, 0, 0.`}}, {{2019, 8, 30, 0, 0, 0.`}, {2019, 8, 30, 8, 30, 0.`}}};
POSTED BY: Tsai Ming-Chou
8 Replies
Posted 5 years ago

Hi Tsai,

Looks like you want the min/max values split by day and AM/PM. If so, here is one way to do that

data // GatherBy[#, (DateValue[#, {"Day", "AMPM"}] &)] & // Map[MinMax]
POSTED BY: Rohit Namjoshi

Thank you~~ I just tried it, and it was indeed close to the answer I wanted.

But I haven’t understood the logic yet~~I will continue to study hard~~

I wonder if the time interval can span to the next day?

Thank you again for your help!

POSTED BY: Tsai Ming-Chou

Hello Tsai Ming-Chou,

you probably want FindClusters:

out1 = Map[FromUnixTime, MinMax /@ FindClusters[UnixTime /@ data], {2}]
POSTED BY: Henrik Schachner
Posted 5 years ago

It was not clear to me what the criteria for grouping/splitting the data is. Based on the example output I guessed that it was by day + AM/PM. That will not work if the interval spans days. In that case, clustering, as suggested by Henrik would be a better option.

POSTED BY: Rohit Namjoshi

Yes, this is the characteristic of many recent questions: They are so poorly stated that one can just guess about what is wanted. Trying to help then is no fun!

POSTED BY: Henrik Schachner

I am sorry for asking the question of unclear definition~~ I don't understand the features or parameters that can use to divide time intervals; this is one of the things that bothers me. The problem often comes from something I don't understand.

I try to explain the problem again~~ I cut the continuous 24 hours into 48 equal parts (one segment every 30 minutes), and each segment records an event. Then I hope to categorize the events and integrate the time segments as much as possible and draw them in TimelinePlot. After the event is classified, some segments may be merged into a continuous interval, and some cannot be combined (the characteristic is that the time gap between the segments exceeds 30 minutes).

I tried to use GatherBy & SplitBy. But I all failed~~

I know the function of UnixTime from the examples provided by Henrik. Let me learn more about this feature; I can use FindClusters for segmentation. It is the critical for me~~~ In short, I appreciate the help from Henrik and Rohit~~

POSTED BY: Tsai Ming-Chou
Posted 5 years ago

Hi Tsai,

Seems like the criteria for grouping / splitting is

the characteristic is that the time gap between the segments exceeds 30 minutes

Here is one way to do that

merged = MinMax /@ Split[data, QuantityMagnitude@DateDifference[##, "Minute"] <= 30 &]

You can visualize the merging to see if it matches what you expect

TimelinePlot[{data, DateInterval /@ merged}, PlotLegends -> {"Data", "Merged"}]

enter image description here

POSTED BY: Rohit Namjoshi

Great~~ This is what I want~~~

Thank you!

I really learned a lot this time!!

POSTED BY: Tsai Ming-Chou
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard