Skip to content

Commit 03723e1

Browse files
authored
Merge pull request #1279 from Flowminder/blocking-init
Remove blocking call from DistanceCounterparts init
2 parents 54f4011 + a965178 commit 03723e1

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

flowmachine/flowmachine/features/subscriber/distance_counterparts.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def __init__(
103103
subscriber_identifier="msisdn",
104104
hours=hours,
105105
subscriber_subset=subscriber_subset,
106-
).get_query()
106+
)
107107

108108
self.unioned_to_query = EventsTablesUnion(
109109
self.start,
@@ -113,7 +113,7 @@ def __init__(
113113
subscriber_identifier="msisdn_counterpart",
114114
hours=hours,
115115
subscriber_subset=subscriber_subset,
116-
).get_query()
116+
)
117117

118118
self.distance_matrix = DistanceMatrix()
119119

@@ -141,8 +141,8 @@ def _make_query(self):
141141
FROM
142142
(
143143
SELECT A.subscriber, A.location_id AS location_id_from, B.location_id AS location_id_to FROM
144-
({self.unioned_from_query}) AS A
145-
JOIN ({self.unioned_to_query}) AS B
144+
({self.unioned_from_query.get_query()}) AS A
145+
JOIN ({self.unioned_to_query.get_query()}) AS B
146146
ON A.id = B.id AND A.outgoing != B.outgoing {on_filters}
147147
) U
148148
JOIN

flowmachine/flowmachine/features/subscriber/interevent_period.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def _make_query(self):
110110
sql = f"""
111111
SELECT
112112
subscriber,
113-
EXTRACT(epoch FROM value/{self.time_divisor}) AS value
113+
FLOOR(EXTRACT(epoch FROM value)/{self.time_divisor}) AS value
114114
FROM ({self.event_interval.get_query()}) AS U
115115
"""
116116

flowmachine/tests/test_subscriber_interevent_gap.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def _intervent_period(*, start, stop, direction, subset, stat):
3535
agg = events.groupby("subscriber").agg(
3636
lambda x: getattr(x, postgres_stat_to_pandas_stat[stat])()
3737
)
38-
agg = (pd.to_numeric(agg["duration"]) / 1000000000.0).to_dict()
38+
agg = (agg["duration"].astype("timedelta64[s]")).to_dict()
3939

4040
return agg
4141

@@ -59,15 +59,19 @@ def test_interevent_period(
5959
"""
6060

6161
query = IntereventPeriod(
62-
start=start, stop=stop, direction=direction, statistic=stat
62+
start=start,
63+
stop=stop,
64+
direction=direction,
65+
statistic=stat,
66+
time_resolution="second",
6367
)
6468
df = get_dataframe(query).set_index("subscriber")
6569
sample = df.sample(n=5)
6670
want = intervent_period(
6771
start=start, stop=stop, direction=direction, stat=stat, subset=sample
6872
)
6973
assert query.column_names == ["subscriber", "value"]
70-
assert (sample["value"] * query.time_divisor).to_dict() == pytest.approx(want)
74+
assert (sample["value"]).to_dict() == pytest.approx(want)
7175

7276

7377
@pytest.mark.parametrize(
@@ -95,9 +99,7 @@ def test_interevent_interval(
9599
start=start, stop=stop, direction=direction, stat=stat, subset=sample
96100
)
97101
assert query.column_names == ["subscriber", "value"]
98-
assert (pd.to_numeric(sample["value"]) / 1000000000.0).to_dict() == pytest.approx(
99-
want
100-
)
102+
assert (sample["value"].astype("timedelta64[s]")).to_dict() == pytest.approx(want)
101103

102104

103105
@pytest.mark.parametrize("kwarg", ["direction", "statistic"])

0 commit comments

Comments
 (0)