Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 1.1.2 and major changes #4

Merged
merged 174 commits into from
Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
174 commits
Select commit Hold shift + click to select a range
fe0840c
Add error message in get temporal and sub type in Temporal factory
Diviloper Sep 26, 2022
35fd37c
Remove InstantSet classes from init
Diviloper Sep 26, 2022
f2d6a02
Rename temporal hexwkb factory method
Diviloper Sep 26, 2022
6997fab
Replace random maxcount for current count
Diviloper Sep 26, 2022
b2e1424
Update examples
Diviloper Sep 26, 2022
90622ac
Update package version and set pymeos-cffi dependency version
Diviloper Sep 26, 2022
88564ed
Rename process_ais example to assemble_ais
Diviloper Sep 26, 2022
9916955
Update trips with new binaries
Diviloper Sep 26, 2022
dfa6eb3
Add geojson and shapely conversion functions to TPoint
Diviloper Sep 30, 2022
1b9ee9f
Add TInerpolation enum to pymeos exports
Diviloper Oct 2, 2022
0bb123a
Add nullable parameter in gserialized as geojson function
Diviloper Oct 2, 2022
1ff7948
Add mfjson in/output and hexwkb input
Diviloper Oct 2, 2022
095d183
Bump version number
Diviloper Oct 2, 2022
df58d7f
Bump pymeos_cffi version number
Diviloper Oct 2, 2022
8a852a6
Update pymeos_cffi dependency version in pymeos
Diviloper Oct 2, 2022
ba96e6a
Update meos and meos_initialize
Diviloper Oct 2, 2022
f3045c8
Make meos_initialize parameter nullable
Diviloper Oct 2, 2022
96b317d
Change initialization/finalization functions (breaking change).
Diviloper Oct 2, 2022
62b0576
Add as wkt and ewkt (for points) functions
Diviloper Oct 2, 2022
ba5d419
Fix simplification function (parameters swapped)
Diviloper Oct 2, 2022
36d1804
Fix from_arrays bug due to interpolation update
Diviloper Oct 2, 2022
c098c37
Update examples to reflect last changes
Diviloper Oct 2, 2022
a510e76
Update hello_world example
Diviloper Oct 2, 2022
dbd75d9
Update readme
Diviloper Oct 3, 2022
bceebdf
Add pymeos CHANGELOG
Diviloper Oct 4, 2022
13508de
Remove psycopg2 interface of objects
Diviloper Oct 7, 2022
e4562f0
Add list of MEOS functions to keep track of progress (it's almost all…
Diviloper Oct 7, 2022
f4dddf9
Add hexWKB in/out for PeriodSet
Diviloper Oct 7, 2022
03debc1
Add hexWKB in/out for Period
Diviloper Oct 7, 2022
c5480a4
Add hexWKB in/out for Period
Diviloper Oct 7, 2022
181ef30
Check constructor functions for span and time types
Diviloper Oct 7, 2022
89c7cf2
Check cast functions for span and time types
Diviloper Oct 7, 2022
e0053e1
Check accessor functions for span and time types
Diviloper Oct 7, 2022
e566fe7
Add comments to deliberately unused functions
Diviloper Oct 7, 2022
f06f493
Check transformation functions for span and time types and modify exp…
Diviloper Oct 7, 2022
3979209
Check topological functions for span and time types
Diviloper Oct 7, 2022
c2fee01
Check position functions for span and time types
Diviloper Oct 7, 2022
d83776d
Check set functions for span and time types
Diviloper Oct 7, 2022
cef2d80
Check distance functions for span and time types
Diviloper Oct 7, 2022
06a3ed6
Check comparison functions for span and time types
Diviloper Oct 7, 2022
a1cbeeb
Fix comment in build_wheels.sh script
Diviloper Oct 7, 2022
71a3362
Set size parameters of TBox/STBox hexWKB functions as output parameters
Diviloper Oct 7, 2022
afc2222
Check inout functions for box types and add HexWKB inout
Diviloper Oct 7, 2022
68033ed
Check constructor functions for box types
Diviloper Oct 7, 2022
211bcdc
Add TNumber to init
Diviloper Oct 7, 2022
9204c23
Add TPoint to init
Diviloper Oct 7, 2022
57859eb
Add cast functions for box types
Diviloper Oct 7, 2022
28791dc
Check accessor functions for box types
Diviloper Oct 7, 2022
78de197
Check transformation functions for box types and modify expand box fu…
Diviloper Oct 7, 2022
832b758
Check topological functions for box types
Diviloper Oct 7, 2022
15da93a
Check position functions for box types
Diviloper Oct 7, 2022
01fd69f
Check set functions for box types and fix null intersection corner ca…
Diviloper Oct 7, 2022
5dd0d8f
Check comparison functions for box types and add non-equality functions
Diviloper Oct 7, 2022
adc1944
Check input/output functions for temporal types
Diviloper Oct 7, 2022
544e5c5
Remove interpolation overrides
Diviloper Oct 7, 2022
c9f5129
Update tfloat constructor functions and bump version number
Diviloper Oct 7, 2022
ce066fb
Add from_base and from_base_time constructors for temporal types and …
Diviloper Oct 7, 2022
3338283
Add cast functions for temporal types
Diviloper Oct 7, 2022
80adfae
Add gserialized to shapely function
Diviloper Oct 7, 2022
7a71d2a
Check and add missing accessor functions for temporal types
Diviloper Oct 7, 2022
f167049
Check and add transformation functions for temporal types
Diviloper Oct 8, 2022
aaf0aa3
Add span/range utilities
Diviloper Oct 8, 2022
a359f49
Fix at_values functions and add generic modifier to remove array leng…
Diviloper Oct 8, 2022
2560a84
Add restriction functions for temporal types
Diviloper Oct 8, 2022
48eeea0
Check boolean functions for temporal types
Diviloper Oct 8, 2022
2065c43
Fix import error and allow at function for tfloat and int
Diviloper Oct 8, 2022
726c400
Fix casting in some at_values functions
Diviloper Oct 8, 2022
f3ccbb3
Check mathematical functions for temporal types and add left-normal-r…
Diviloper Oct 8, 2022
371cdeb
Check text functions for temporal types and add left-normal-right-tem…
Diviloper Oct 8, 2022
7880705
Add topological functions for temporal types
Diviloper Oct 10, 2022
3e72f6a
Add distance functions for temporal types
Diviloper Oct 10, 2022
66f27d4
Check time functions for temporal types
Diviloper Oct 10, 2022
6919b49
Add local aggregate functions for temporal types
Diviloper Oct 10, 2022
0bf7f3f
Check tile functions for temporal types
Diviloper Oct 10, 2022
bf12178
Check similarity functions for temporal types
Diviloper Oct 10, 2022
e950028
Add ever/always functions for temporal types (and never)
Diviloper Oct 10, 2022
46642d0
Add comparison functions for temporal types
Diviloper Oct 11, 2022
3b91fed
Add spatial accessor functions for temporal point types
Diviloper Oct 16, 2022
3af942d
Add position functions for temporal types
Diviloper Oct 17, 2022
d41ff03
Make register method in MobilityDB class public, so it can be used in…
Diviloper Oct 18, 2022
1a583d9
Add aggregator functions
Diviloper Oct 18, 2022
4595bba
Add whenTrue function
Diviloper Oct 18, 2022
ed4090c
Add when_true/false function to tbool
Diviloper Oct 18, 2022
7939d44
Add geographic/geometric conversion
Diviloper Oct 18, 2022
9242125
Add spatial transformation functions for temporal point types
Diviloper Oct 18, 2022
7c21c69
Add spatial relationship functions for temporal point types
Diviloper Oct 19, 2022
0068c41
Make state parameter in aggregator functions optional
Diviloper Oct 19, 2022
552938d
Add temporal aggregate functions for temporal types
Diviloper Oct 19, 2022
4140ffe
Add extent aggregators for temporal types
Diviloper Oct 19, 2022
e151cfd
Add optional dependencies
Diviloper Oct 19, 2022
6fdba5a
Update MEOS backend
Diviloper Oct 20, 2022
dc7c5e1
Add point extent aggregator
Diviloper Oct 20, 2022
c56e509
Update temporal to period conversion
Diviloper Oct 20, 2022
392bd63
Add granularity in count aggregate
Diviloper Oct 20, 2022
f4bd8d9
Remove outdated examples folder (real examples are in the pymeos_exam…
Diviloper Oct 20, 2022
e4d9fbc
Make interval parameter in count aggregation optional
Diviloper Oct 20, 2022
57beae5
Add granularity in aggregators
Diviloper Oct 20, 2022
660bbc4
Update init file with new classes
Diviloper Oct 20, 2022
bf11e8b
Add dataframe conversion
Diviloper Oct 21, 2022
73d5647
Update split function header
Diviloper Oct 21, 2022
22e08ab
Add casting functions to instant, sequence and sequenceset
Diviloper Oct 21, 2022
211725b
Change values function to value_set and add actual values function.
Diviloper Oct 24, 2022
2d80325
Add Box and Time classes to group box and time classes respectively
Diviloper Oct 25, 2022
b371a34
Update split (time and value) functions
Diviloper Oct 25, 2022
aa5c4cd
Add new aggregators and refactor aggregator base class
Diviloper Oct 25, 2022
0594e4a
Update MEOS version (new split and aggregation functions)
Diviloper Oct 25, 2022
9442027
Add basic plotting capabilities to TSequence and TSequenceSet
Diviloper Oct 25, 2022
a46c629
Add TimestampTz as interoperable so it gets unpacked as int
Diviloper Oct 25, 2022
ac6b293
Add missing period extent aggregator
Diviloper Oct 26, 2022
d5bed3b
Rename period extent aggregator
Diviloper Oct 26, 2022
a070336
Add more plotting capabilities
Diviloper Oct 26, 2022
45b4c5d
Fix splitting function inner call
Diviloper Oct 26, 2022
63f7cd3
Allow TGeogPointInst to be created with a lat,lon tuple
Diviloper Oct 26, 2022
a0c2a5f
Add from instants/sequences constructors for ease
Diviloper Oct 26, 2022
3cd1c9c
Fix functions missing an import
Diviloper Oct 26, 2022
657c9e4
Make empty markers be drawn over filled markers
Diviloper Oct 26, 2022
69984b3
Replace AIS examples with new single AIS notebook
Diviloper Oct 26, 2022
a67874a
Fix plot title in example
Diviloper Oct 26, 2022
00b812e
Regenerate outputs
Diviloper Oct 26, 2022
7ba984f
Update intro message
Diviloper Oct 27, 2022
a7a9fda
Allow precision parameter in values and value function for TPoint
Diviloper Oct 27, 2022
71424c8
Allow precision parameter in to_dataframe method for TPoint
Diviloper Oct 27, 2022
f4adcf4
Fix call to modified method
Diviloper Oct 27, 2022
999e4d1
First steps toward better typing
Diviloper Oct 27, 2022
157338b
Simplify signatures using Time type
Diviloper Nov 2, 2022
112bfb4
Simplify signatures using Time type
Diviloper Nov 2, 2022
406a14b
Fix precision param in gserialized_to_shapely_geometry function
Diviloper Oct 28, 2022
19e1e0e
Add method to plot multiple unrelated point sequences and fix color s…
Diviloper Oct 28, 2022
bd13036
Add shapely geometry support for some operations (others will follow)
Diviloper Oct 28, 2022
c51ef7b
Fix xticks in point sequence plotter
Diviloper Oct 28, 2022
448dfd0
Update Disassemble and Clip BerlinMOD examples
Diviloper Oct 28, 2022
f06b033
Update BerlinMOD notebook dataframe outputs
Diviloper Oct 28, 2022
e656c65
Fix typo
Diviloper Oct 28, 2022
c79978a
Add Shapely/Postgis geometry conversion
Diviloper Oct 28, 2022
f097f12
Fix xticks in box plotter
Diviloper Oct 29, 2022
b7e349e
Update last MEOS to add box tiling
Diviloper Oct 30, 2022
4334097
Make origin tile parameters optional.
Diviloper Oct 30, 2022
a69825e
Fix optionality of parameters that don't require any conversion
Diviloper Oct 30, 2022
b964b40
Add tiling and more stbox plots
Diviloper Oct 30, 2022
3359b85
Add simplification example
Diviloper Oct 30, 2022
a9abf49
Make box parameter in tpoint extent aggregator optional
Diviloper Nov 1, 2022
3f11537
Increase to string precision
Diviloper Nov 1, 2022
6cc3c09
Add typing for Aggregators
Diviloper Nov 1, 2022
a802d1c
Only add label to first line in multiple-element plots
Diviloper Nov 1, 2022
b188182
Fix marker z-order
Diviloper Nov 1, 2022
09be3ef
Add temporal time split in n sections
Diviloper Nov 1, 2022
03bf45f
Rename finish function in aggregation class to avoid confusion
Diviloper Nov 1, 2022
b05be11
Fix granular start_aggregation
Diviloper Nov 1, 2022
483441e
Add Aggregation example
Diviloper Nov 1, 2022
533b43d
Make duration parameter in STBox tiling optional
Diviloper Nov 1, 2022
c27ec79
Add tiling and flat tiling functions
Diviloper Nov 2, 2022
73d0a04
First steps toward better typing
Diviloper Oct 27, 2022
29f196b
Simplify signatures using Time type
Diviloper Nov 2, 2022
7e5c2b8
Remove type check in aggregator function since now type hints show pr…
Diviloper Nov 2, 2022
b9e84fb
Add type parameters for temporal abstract classes
Diviloper Nov 3, 2022
70036a4
Expose only meos functions in star import
Diviloper Nov 3, 2022
58acda8
Remove unused lw functions and add new gserialized to point
Diviloper Nov 3, 2022
0fe4a05
Finish typing
Diviloper Nov 3, 2022
e521c66
Replace pymeos_cffi imports with star imports
Diviloper Nov 3, 2022
14d851f
Update Readme
Diviloper Nov 3, 2022
647919a
Update to last MEOS version and make some parameter of STBox tiling n…
Diviloper Nov 4, 2022
94312eb
Update some typings, fix box tiling functions and option to not draw …
Diviloper Nov 4, 2022
184dc55
Finish PyMEOS examples
Diviloper Nov 4, 2022
5e9611c
Update to last version of MEOS
Diviloper Nov 7, 2022
dc85661
Add shift and scale only functions
Diviloper Nov 7, 2022
300cfc2
Update to last version of MEOS and remove some functions from the add…
Diviloper Nov 9, 2022
8787eb9
Update build tools
Diviloper Nov 9, 2022
d03d209
Update project metadata
Diviloper Nov 9, 2022
11942b2
Add streaming operations
Diviloper Nov 9, 2022
d4defb8
Update READMEs
Diviloper Nov 19, 2022
16d3ff2
Add support for asyncpg and bump to version 1.1.2
Diviloper Nov 18, 2022
c8100e6
Add __version__ string
Diviloper Nov 19, 2022
5cbd974
Update imports and function names
anitagraser Nov 19, 2022
b05b718
Create mpd-to-pymeos.ipynb
anitagraser Nov 19, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
Pipfile.lock
Pipfile.lock
*-checkpoint.ipynb
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ pip install pymeos

## Sample code

> **IMPORTANT** Before using any PyMEOS function, always call `meos_initialize`. Otherwise, the library will
> crash with a `Segmentation Fault` error. You should also always call `meos_finish` at the end of your code.
> **IMPORTANT** Before using any PyMEOS function, always call `pymeos_initialize`. Otherwise, the library will
> crash with a `Segmentation Fault` error. You should also always call `pymeos_finalize` at the end of your code.

````python
from pymeos import meos_initialize, meos_finish, TGeogPointInst, TGeogPointSeq
from pymeos import pymeos_initialize, pymeos_finalize, TGeogPointInst, TGeogPointSeq

# Important: Always initialize MEOS library
meos_initialize()
pymeos_initialize()

sequence_from_string = TGeogPointSeq(string='[Point(10.0 10.0)@2019-09-01 00:00:00+01, Point(20.0 20.0)@2019-09-02 00:00:00+01, Point(10.0 10.0)@2019-09-03 00:00:00+01]')
print(f'Output: {sequence_from_string}')

sequence_from_points = TGeogPointSeq(instant_list=[TGeogPointInst(string='Point(10.0 10.0)@2019-09-01 00:00:00+01'), TGeogPointInst(string='Point(20.0 20.0)@2019-09-02 00:00:00+01'), TGeogPointInst(string='Point(10.0 10.0)@2019-09-03 00:00:00+01')], lower_inc=True, upper_inc=True)
speed = sequence_from_points.speed
speed = sequence_from_points.speed()
print(f'Speeds: {speed}')

# Call finish at the end of your code
meos_finish()
pymeos_finalize()
````
24 changes: 24 additions & 0 deletions pymeos/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## 1.1.2

- Add support for `asyncpg`.

### Breaking changes

- `MobilityDB` using `psycopg2` has been moved from `pymeos.db` to `pymeos.db.psycopg` due to the addition of `asyncpg`
support.

## 1.1.1

- All MEOS functions added to PyMEOS.

## 1.1.0

### Breaking changes

- Function `meos_initialize` is now called `pymeos_initialize` and can receive a `str` parameter stating the desired
timezone (e.b. `pymeos_initialize('UTC')`)
- Function `meos_finish` is now called `pymeos_finalize`.

## 1.0.0

Use MEOS as backend.
12 changes: 6 additions & 6 deletions pymeos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ pip install pymeos

## Sample code

> **IMPORTANT** Before using any PyMEOS function, always call `meos_initialize`. Otherwise, the library will
> crash with a `Segmentation Fault` error. You should also always call `meos_finish` at the end of your code.
> **IMPORTANT** Before using any PyMEOS function, always call `pymeos_initialize`. Otherwise, the library will
> crash with a `Segmentation Fault` error. You should also always call `pymeos_finalize` at the end of your code.

````python
from pymeos import meos_initialize, meos_finish, TGeogPointInst, TGeogPointSeq
from pymeos import pymeos_initialize, pymeos_finalize, TGeogPointInst, TGeogPointSeq

# Important: Always initialize MEOS library
meos_initialize()
pymeos_initialize()

sequence_from_string = TGeogPointSeq(string='[Point(10.0 10.0)@2019-09-01 00:00:00+01, Point(20.0 20.0)@2019-09-02 00:00:00+01, Point(10.0 10.0)@2019-09-03 00:00:00+01]')
print(f'Output: {sequence_from_string}')

sequence_from_points = TGeogPointSeq(instant_list=[TGeogPointInst(string='Point(10.0 10.0)@2019-09-01 00:00:00+01'), TGeogPointInst(string='Point(20.0 20.0)@2019-09-02 00:00:00+01'), TGeogPointInst(string='Point(10.0 10.0)@2019-09-03 00:00:00+01')], lower_inc=True, upper_inc=True)
speed = sequence_from_points.speed
speed = sequence_from_points.speed()
print(f'Speeds: {speed}')

# Call finish at the end of your code
meos_finish()
pymeos_finalize()
````
40 changes: 27 additions & 13 deletions pymeos/pymeos/__init__.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
from .aggregators import *
from .boxes import *
from .main import *
from .meos_init import *
from .temporal import *
from .time import *
from pymeos_cffi.functions import meos_initialize, meos_finish

__version__ = '1.1.2'
__all__ = [
#initialization
'meos_initialize', 'meos_finish',
# initialization
'pymeos_initialize', 'pymeos_finalize',
# boxes
'TBox', 'STBox',
'Box', 'TBox', 'STBox',
# main
'TBool', 'TBoolInst', 'TBoolInstSet', 'TBoolSeq', 'TBoolSeqSet',
'TInt', 'TIntInst', 'TIntInstSet', 'TIntSeq', 'TIntSeqSet',
'TFloat', 'TFloatInst', 'TFloatInstSet', 'TFloatSeq', 'TFloatSeqSet',
'TText', 'TTextInst', 'TTextInstSet', 'TTextSeq', 'TTextSeqSet',
'TPointInst', 'TPointInstSet', 'TPointSeq', 'TPointSeqSet',
'TGeomPoint', 'TGeomPointInst', 'TGeomPointInstSet', 'TGeomPointSeq', 'TGeomPointSeqSet',
'TGeogPoint', 'TGeogPointInst', 'TGeogPointInstSet', 'TGeogPointSeq', 'TGeogPointSeqSet',
'TBool', 'TBoolInst', 'TBoolSeq', 'TBoolSeqSet',
'TInt', 'TIntInst', 'TIntSeq', 'TIntSeqSet',
'TFloat', 'TFloatInst', 'TFloatSeq', 'TFloatSeqSet',
'TText', 'TTextInst', 'TTextSeq', 'TTextSeqSet',
'TPointInst', 'TPointSeq', 'TPointSeqSet',
'TGeomPoint', 'TGeomPointInst', 'TGeomPointSeq', 'TGeomPointSeqSet',
'TGeogPoint', 'TGeogPointInst', 'TGeogPointSeq', 'TGeogPointSeqSet',
# temporal
'Temporal', 'TInstant', 'TSequence', 'TSequenceSet',
# time
'Period', 'TimestampSet', 'PeriodSet'
]
'Time', 'Period', 'TimestampSet', 'PeriodSet',
# extras
'TInterpolation',
# aggregators
'TemporalInstantCountAggregator', 'TemporalPeriodCountAggregator', 'TemporalExtentAggregator',
'TemporalAndAggregator', 'TemporalOrAggregator',
'TemporalAverageAggregator',
'TemporalNumberExtentAggregator',
'TemporalIntMaxAggregator', 'TemporalIntMinAggregator', 'TemporalIntSumAggregator',
'TemporalFloatMaxAggregator', 'TemporalFloatMinAggregator', 'TemporalFloatSumAggregator',
'TemporalTextMaxAggregator', 'TemporalTextMinAggregator',
'TemporalPointExtentAggregator',
'TemporalTimestampUnionAggregator', 'TemporalPeriodUnionAggregator',
]
24 changes: 24 additions & 0 deletions pymeos/pymeos/aggregators/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from .bool_aggregators import *
from .general_aggregators import *
from .number_aggregators import *
from .text_aggregators import *
from .point_aggregators import *
from .time_aggregators import *

__all__ = [
# General
'TemporalInstantCountAggregator', 'TemporalPeriodCountAggregator', 'TemporalExtentAggregator',
# Bool
'TemporalAndAggregator', 'TemporalOrAggregator',
# Number
'TemporalAverageAggregator',
'TemporalNumberExtentAggregator',
'TemporalIntMaxAggregator', 'TemporalIntMinAggregator', 'TemporalIntSumAggregator',
'TemporalFloatMaxAggregator', 'TemporalFloatMinAggregator', 'TemporalFloatSumAggregator',
# Text
'TemporalTextMaxAggregator', 'TemporalTextMinAggregator',
# Point
'TemporalPointExtentAggregator',
# Time
'TemporalTimestampUnionAggregator', 'TemporalPeriodUnionAggregator',
]
97 changes: 97 additions & 0 deletions pymeos/pymeos/aggregators/aggregator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
from __future__ import annotations

import abc
from datetime import datetime, timedelta
from typing import Optional, Union, List, Generic, TypeVar

from pymeos_cffi import *

from ..boxes import Box
from ..factory import _TemporalFactory
from ..temporal import Temporal
from ..time import Time

ResultType = TypeVar('ResultType', bound=Union[Temporal, Time, Box])
SourceType = TypeVar('SourceType', bound=Union[Temporal, Time, Box])
SelfAgg = TypeVar('SelfAgg', bound='Aggregation')


class BaseAggregator(Generic[SourceType, ResultType], abc.ABC):
_add_function = None
_final_function = temporal_tagg_finalfn

@classmethod
def aggregate(cls, temporals: List[SourceType]) -> ResultType:
state = None
for t in temporals:
state = cls._add(state, t)
return cls._finish(state)

@classmethod
def _add(cls, state, temporal: SourceType):
return cls._add_function(state, temporal._inner)

@classmethod
def _finish(cls, state) -> SourceType:
result = cls._final_function(state)
return _TemporalFactory.create_temporal(result)

@classmethod
def start_aggregation(cls) -> Aggregation:
return Aggregation(cls._add, cls._finish)

@classmethod
def _error(cls, element):
raise TypeError(f'Cannot perform aggregation ({cls.__name__}) with the following element: '
f'{element} (Class: {element.__class__})')


class Aggregation(Generic[SourceType, ResultType]):

def __init__(self, add_function, finish_function) -> None:
super().__init__()
self._add_function = add_function
self._finish_function = finish_function
self._state = None

def add(self: SelfAgg, new_temporal: SourceType) -> SelfAgg:
self._state = self._add_function(self._state, new_temporal)
return self

def aggregation(self) -> ResultType:
return self._finish_function(self._state)


class BaseGranularityAggregator(BaseAggregator[SourceType, ResultType]):

@classmethod
def aggregate(cls, temporals: List[SourceType], interval: Optional[Union[str, timedelta]] = None,
origin: Union['str', datetime] = '1970-01-01') -> ResultType:
state = None
for t in temporals:
state = cls._add(state, t, interval, origin)
return cls._finish(state)

@classmethod
def _add(cls, state, temporal: SourceType, interval=None, origin='1970-01-01'):
interval_converted = timedelta_to_interval(interval) if isinstance(interval, timedelta) else \
pg_interval_in(interval, -1) if isinstance(interval, str) else None
origin_converted = datetime_to_timestamptz(origin) if isinstance(origin, datetime) else \
pg_timestamptz_in(origin, -1)
return cls._add_function(state, temporal._inner, interval_converted, origin_converted)

@classmethod
def start_aggregation(cls, interval: Optional[Union[str, timedelta]] = None,
origin: Union['str', datetime] = '1970-01-01') -> GranularAggregation[SourceType, ResultType]:
return GranularAggregation(cls._add, cls._finish, interval, origin)


class GranularAggregation(Aggregation[SourceType, ResultType]):
def __init__(self, add_function, finish_function, interval, origin) -> None:
super().__init__(add_function, finish_function)
self._interval = interval
self._origin = origin

def add(self: SelfAgg, new_temporal: SourceType) -> SelfAgg:
self._state = self._add_function(self._state, new_temporal, self._interval, self._origin)
return self
12 changes: 12 additions & 0 deletions pymeos/pymeos/aggregators/bool_aggregators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from pymeos_cffi import *

from .aggregator import BaseAggregator
from ..main import TBool


class TemporalAndAggregator(BaseAggregator[TBool, TBool]):
_add_function = tbool_tand_transfn


class TemporalOrAggregator(BaseAggregator[TBool, TBool]):
_add_function = tbool_tor_transfn
71 changes: 71 additions & 0 deletions pymeos/pymeos/aggregators/general_aggregators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from datetime import datetime, timedelta
from typing import Union

from pymeos_cffi import *

from .aggregator import BaseAggregator, BaseGranularityAggregator
from ..boxes import Box
from ..main import TIntSeq, TIntSeqSet
from ..temporal import Temporal, TInterpolation
from ..time import Time, TimestampSet, Period, PeriodSet


class TemporalInstantCountAggregator(BaseGranularityAggregator[Union[Time, Temporal], TIntSeq]):
@classmethod
def _add(cls, state, temporal, interval=None, origin='1970-01-01'):
interval_converted = timedelta_to_interval(interval) if isinstance(interval, timedelta) else \
pg_interval_in(interval, -1) if isinstance(interval, str) else None
origin_converted = datetime_to_timestamptz(origin) if isinstance(origin, datetime) else \
pg_timestamptz_in(origin, -1)
if isinstance(temporal, datetime):
state = timestamp_tcount_transfn(state, datetime_to_timestamptz(temporal), interval_converted,
origin_converted)
elif isinstance(temporal, TimestampSet):
state = timestampset_tcount_transfn(state, temporal._inner, interval_converted, origin_converted)
elif isinstance(temporal, Temporal) and temporal.interpolation == TInterpolation.DISCRETE:
state = temporal_tcount_transfn(state, temporal._inner, interval_converted, origin_converted)
else:
cls._error(temporal)
return state


class TemporalPeriodCountAggregator(BaseGranularityAggregator[Union[Time, Temporal], TIntSeqSet]):
@classmethod
def _add(cls, state, temporal, interval=None, origin='1970-01-01'):
interval_converted = timedelta_to_interval(interval) if isinstance(interval, timedelta) else \
pg_interval_in(interval, -1) if isinstance(interval, str) else None
origin_converted = datetime_to_timestamptz(origin) if isinstance(origin, datetime) else \
pg_timestamptz_in(origin, -1)
if isinstance(temporal, Period):
state = period_tcount_transfn(state, temporal._inner, interval_converted, origin_converted)
elif isinstance(temporal, PeriodSet):
state = periodset_tcount_transfn(state, temporal._inner, interval_converted, origin_converted)
elif isinstance(temporal, Temporal) and temporal.interpolation != TInterpolation.DISCRETE:
state = temporal_tcount_transfn(state, temporal._inner, interval_converted, origin_converted)
else:
cls._error(temporal)
return state


class TemporalExtentAggregator(BaseAggregator[Union[Time, Temporal], Period]):

@classmethod
def _add(cls, state, temporal):
if isinstance(temporal, Temporal):
state = temporal_extent_transfn(state, temporal._inner)
elif isinstance(temporal, datetime):
state = timestamp_extent_transfn(state, datetime_to_timestamptz(temporal))
elif isinstance(temporal, TimestampSet):
state = timestampset_extent_transfn(state, temporal._inner)
elif isinstance(temporal, Period):
state = span_extent_transfn(state, temporal._inner)
pass
elif isinstance(temporal, PeriodSet):
state = periodset_extent_transfn(state, temporal._inner)
else:
cls._error(temporal)
return state

@classmethod
def _finish(cls, state) -> Union[Temporal, Time, Box]:
return Period(_inner=state)
42 changes: 42 additions & 0 deletions pymeos/pymeos/aggregators/number_aggregators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from pymeos_cffi import *

from .aggregator import BaseAggregator
from ..boxes import TBox
from ..main import TInt, TFloat, TNumber


class TemporalAverageAggregator(BaseAggregator[TNumber, TNumber]):
_add_function = tnumber_tavg_transfn
_final_function = tnumber_tavg_finalfn


class TemporalNumberExtentAggregator(BaseAggregator[TNumber, TBox]):
_add_function = tnumber_extent_transfn

@classmethod
def _finish(cls, state) -> TBox:
return TBox(_inner=state)


class TemporalIntMaxAggregator(BaseAggregator[TNumber, TBox]):
_add_function = tint_tmax_transfn


class TemporalIntMinAggregator(BaseAggregator[TInt, TInt]):
_add_function = tint_tmin_transfn


class TemporalIntSumAggregator(BaseAggregator[TInt, TInt]):
_add_function = tint_tsum_transfn


class TemporalFloatMaxAggregator(BaseAggregator[TFloat, TFloat]):
_add_function = tfloat_tmax_transfn


class TemporalFloatMinAggregator(BaseAggregator[TFloat, TFloat]):
_add_function = tfloat_tmin_transfn


class TemporalFloatSumAggregator(BaseAggregator[TFloat, TFloat]):
_add_function = tfloat_tsum_transfn
Loading