Skip to content

Commit

Permalink
Added viewership features support - Features API (#62)
Browse files Browse the repository at this point in the history
* Added viewership features support
  • Loading branch information
midithaf committed Sep 21, 2021
1 parent 0ff1f3e commit cc69032
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 5 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,31 @@ for feature in phq.features.obtain_features(
print(feature.date, feature.phq_attendance_sports.stats.count, feature.phq_rank_public_holidays.rank_levels)
```

The following example obtains features of broadcasts which are active between 2017-12-31 and 2018-01-02, with place_id 4671654

Requested features:
* count and median of broadcasts which start between 9am - 11am and have a `phq_rank` greater than 50

```Python
from predicthq import Client

phq = Client(access_token="abc123")


for feature in phq.features.obtain_features(
active__gte="2017-12-31",
active__lte="2018-01-02",
hour_of_day_start__gt=9,
hour_of_day_start__lte=11,
location__place_id=[4671654],
phq_viewership_sports_american_football__stats=["count", "median"],
phq_viewership_sports_american_football__phq_rank={
"gt": 50
}
):
print(feature.date, feature.phq_viewership_sports_american_football.stats.count, feature.phq_viewership_sports_american_football.stats.median)
```


Please refer to our [Features endpoint documentation](https://docs.predicthq.com/start/features-api/) for the lists of supported features and response fields available.

Expand Down
40 changes: 40 additions & 0 deletions predicthq/endpoints/v1/features/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,25 @@ class Options:
place_id = ListType(StringType)


class HourOfDayRange(Model):

class Options:
serialize_when_none = False

gt = IntType(min_value=0, max_value=23)
gte = IntType(min_value=0, max_value=23)
lt = IntType(min_value=0, max_value=23)
lte = IntType(min_value=0, max_value=23)


class FeatureRequest(Model):
class Options:
serialize_when_none = False

active = ModelType(DateRange, required=True)
hour_of_day_active = ModelType(HourOfDayRange)
hour_of_day_start = ModelType(HourOfDayRange)
hour_of_day_end = ModelType(HourOfDayRange)
location = ModelType(FeatureLocation, required=True)
# Attendance based feature criteria
phq_attendance_academic_graduation = ModelType(FeatureCriteria)
Expand All @@ -54,6 +68,19 @@ class Options:
phq_rank_academic_session = BooleanType()
phq_rank_academic_exam = BooleanType()
phq_rank_academic_holiday = BooleanType()
# Viewership based feature criteria
phq_viewership_sports_american_football = ModelType(FeatureCriteria)
phq_viewership_sports_american_football_ncaa_men = ModelType(FeatureCriteria)
phq_viewership_sports_american_football_nfl = ModelType(FeatureCriteria)
phq_viewership_sports_baseball = ModelType(FeatureCriteria)
phq_viewership_sports_baseball_mlb = ModelType(FeatureCriteria)
phq_viewership_sports_basketball = ModelType(FeatureCriteria)
phq_viewership_sports_basketball_ncaa_men = ModelType(FeatureCriteria)
phq_viewership_sports_basketball_nba = ModelType(FeatureCriteria)
phq_viewership_sports_ice_hockey = ModelType(FeatureCriteria)
phq_viewership_sports_ice_hockey_nhl = ModelType(FeatureCriteria)
phq_viewership_sports_soccer = ModelType(FeatureCriteria)
phq_viewership_sports_soccer_mls = ModelType(FeatureCriteria)


class FeatureRankLevel(Model):
Expand Down Expand Up @@ -108,6 +135,19 @@ class Options:
phq_rank_academic_session = ModelType(FeatureRankLevel)
phq_rank_academic_exam = ModelType(FeatureRankLevel)
phq_rank_academic_holiday = ModelType(FeatureRankLevel)
# Viewership based features
phq_viewership_sports_american_football = ModelType(FeatureStat)
phq_viewership_sports_american_football_ncaa = ModelType(FeatureStat)
phq_viewership_sports_american_football_nfl = ModelType(FeatureStat)
phq_viewership_sports_baseball = ModelType(FeatureStat)
phq_viewership_sports_baseball_mlb = ModelType(FeatureStat)
phq_viewership_sports_basketball = ModelType(FeatureStat)
phq_viewership_sports_basketball_ncaa = ModelType(FeatureStat)
phq_viewership_sports_basketball_nba = ModelType(FeatureStat)
phq_viewership_sports_ice_hockey = ModelType(FeatureStat)
phq_viewership_sports_ice_hockey_nhl = ModelType(FeatureStat)
phq_viewership_sports_soccer = ModelType(FeatureStat)
phq_viewership_sports_soccer_mls = ModelType(FeatureStat)


class FeatureResultSet(ResultSet):
Expand Down
112 changes: 112 additions & 0 deletions tests/endpoints/v1/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,115 @@ def test_obtain_features(self, client, responses):
},
phq_rank_public_holidays=True)
assert isinstance(result, FeatureResultSet)

@with_mock_client()
def test_viewership_request_params_underscores(self, client):
feature_stats = ['avg', 'count', 'max', 'median', 'min', 'sum', 'std_dev']
client.features.obtain_features(
active__gte="2017-12-31",
active__lte="2018-01-02",
hour_of_day_active__gt=10,
hour_of_day_active__lte=19,
location__place_id=[4671654],
phq_viewership_sports_american_football__stats=feature_stats,
phq_viewership_sports_american_football__phq_rank={
"gt": 50
},
phq_viewership_sports_baseball_mlb__stats=feature_stats,
phq_viewership_sports_basketball__stats=feature_stats,
phq_viewership_sports_ice_hockey_nhl__stats=feature_stats,
phq_viewership_sports_soccer__stats=feature_stats,
)

client.request.assert_called_once_with(
'post', '/v1/features/',
json={
"active": {
"gte": "2017-12-31",
"lte": "2018-01-02"
},
"hour_of_day_active": {
"gt": 10,
"lte": 19
},
"location": {
"place_id": [
"4671654"
]
},
"phq_viewership_sports_american_football": {
"stats": feature_stats,
"phq_rank": {
"gt": 50
}
},
"phq_viewership_sports_baseball_mlb": {
"stats": feature_stats
},
"phq_viewership_sports_basketball": {
"stats": feature_stats
},
"phq_viewership_sports_ice_hockey_nhl": {
"stats": feature_stats
},
"phq_viewership_sports_soccer": {
"stats": feature_stats
}
}
)

@with_mock_client()
def test_viewership_request_params_dicts(self, client):
feature_criteria = {
"stats": ['avg', 'count', 'max', 'median', 'min', 'sum', 'std_dev'],
"phq_rank": {
"gt": 50
}
}
client.features.obtain_features(
active={
"gte": "2017-12-31",
"lte": "2018-01-02"
},
hour_of_day_start={"gte": 10, "lt": 11},
location={
"geo": {
"lon": -71.49978,
"lat": 41.75038,
"radius": "30km"
}
},
phq_viewership_sports_american_football_ncaa_men=feature_criteria,
phq_viewership_sports_baseball=feature_criteria,
phq_viewership_sports_basketball_ncaa_men=feature_criteria,
phq_viewership_sports_basketball_nba=feature_criteria,
phq_viewership_sports_ice_hockey_nhl=feature_criteria,
phq_viewership_sports_soccer_mls=feature_criteria,
)

client.request.assert_called_once_with(
'post', '/v1/features/',
json={
"active": {
"gte": "2017-12-31",
"lte": "2018-01-02"
},
"hour_of_day_start": {
"gte": 10,
"lt": 11
},
"location": {
"geo": {
"lat": 41.75038,
"lon": -71.49978,
"radius": "30km"
}
},
"phq_viewership_sports_american_football_ncaa_men": feature_criteria,
"phq_viewership_sports_baseball": feature_criteria,
"phq_viewership_sports_basketball_ncaa_men": feature_criteria,
"phq_viewership_sports_basketball_nba": feature_criteria,
"phq_viewership_sports_ice_hockey_nhl": feature_criteria,
"phq_viewership_sports_soccer_mls": feature_criteria
}
)
26 changes: 21 additions & 5 deletions usecases/features/get_features_for_criteria.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

phq = Client(access_token=ACCESS_TOKEN)

# Get sport attendnace and public holiday rank features in a 150km radius around the given geo point (lat, lon)
# Do the aggreations over the period [2019-11-28 TO 2020-01-05]
# Get sport attendance and public holiday rank features in a 150km radius around the given geo point (lat, lon)
# Do the aggregations over the period [2019-11-28 TO 2020-01-05]
# Filter the sports to only account for those events with a rank > 50
for feature in phq.features.obtain_features(
active__gte="2019-11-28",
Expand All @@ -27,9 +27,8 @@
print(feature.date, feature.phq_attendance_sports.stats.count,
feature.phq_rank_public_holidays.rank_levels)


# Get sport attendnace and public holiday rank features across the places [Rhode Island(6295630), Spokane County(5811704), Chicago(4887398)]
# Do the aggreations over the period [2019-11-28 TO 2020-01-05]
# Get sport attendance and public holiday rank features across the places [Rhode Island(6295630), Spokane County(5811704), Chicago(4887398)]
# Do the aggregations over the period [2019-11-28 TO 2020-01-05]
# Filter the sports to only account for those events with a rank > 50
for feature in phq.features.obtain_features(
active__gte="2019-11-28",
Expand All @@ -43,3 +42,20 @@
):
print(feature.date, feature.phq_attendance_sports.stats.count,
feature.phq_rank_public_holidays.rank_levels)

# Get viewership features across the places [Rhode Island(6295630), Spokane County(5811704), Chicago(4887398)]
# Do the aggregations over the period [2019-11-28 TO 2020-01-05]
# Using broadcasts which starts between 9am - 11am
for feature in phq.features.obtain_features(
active__gte="2019-11-28",
active__lte="2020-01-05",
hour_of_day_start__gte=9,
hour_of_day_start__lt=11,
location__place_id=[5224323, 5811704, 4887398],
phq_viewership_sports_american_football__stats=["count", "median"],
phq_viewership_sports_american_football__phq_rank={
"gt": 50
}
):
print(feature.date, feature.phq_viewership_sports_american_football.stats.count,
feature.phq_viewership_sports_american_football.stats.median)

0 comments on commit cc69032

Please sign in to comment.