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

Add choice of agg type #4953

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion locale/en_US/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -4652,7 +4652,7 @@ msgid "X_VERSUS_Y"
msgstr "{x_axis_label} vs. {y_axis_label}"

msgid "X_VERSUS_Y_AGGREGATED"
msgstr "{x_axis_label} vs. {y_axis_label} (Averaged)"
msgstr "{x_axis_label} vs. {y_axis_label} ({aggregationType})"

msgid "Y Axis"
msgstr "Y Axis"
Expand Down
2 changes: 1 addition & 1 deletion locale/es/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -6025,7 +6025,7 @@ msgstr "{x_axis_label} vs. {y_axis_label}"

#, fuzzy
msgid "X_VERSUS_Y_AGGREGATED"
msgstr "{x_axis_label} vs. {y_axis_label} (Agregado)"
msgstr "{x_axis_label} vs. {y_axis_label} ({aggregationType})"

#, fuzzy
msgid "Y Axis"
Expand Down
2 changes: 1 addition & 1 deletion locale/fr_CA/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -4692,7 +4692,7 @@ msgstr "{x_axis_label} contre {y_axis_label}"

#, fuzzy
msgid "X_VERSUS_Y_AGGREGATED"
msgstr "{x_axis_label} contre {y_axis_label} (agrégé)"
msgstr "{x_axis_label} contre {y_axis_label} ({aggregationType})"

msgid "Y Axis"
msgstr "Axe Y"
Expand Down
11 changes: 8 additions & 3 deletions seed/static/seed/js/controllers/inventory_reports_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@

const localStorageXAxisKey = `${base_storage_key}.xaxis`;
const localStorageYAxisKey = `${base_storage_key}.yaxis`;
const localStorageAggregationTypeKey = `${base_storage_key}.aggregationType`;
const localStorageALIndex = `${base_storage_key}.ALIndex`;
const localStorageALIID = `${base_storage_key}.ALIID`;
const localStorageReportConfigID = `${base_storage_key}.RCID`;
Expand All @@ -300,6 +301,7 @@
// Currently selected x and y variables - check local storage first, otherwise initialize to first choice
$scope.yAxisSelectedItem = JSON.parse(localStorage.getItem(localStorageYAxisKey)) || $scope.yAxisVars[0];
$scope.xAxisSelectedItem = JSON.parse(localStorage.getItem(localStorageXAxisKey)) || $scope.xAxisVars[0];
$scope.aggregationType = JSON.parse(localStorage.getItem(localStorageAggregationTypeKey)) || $scope.xAxisVars[0];

$scope.level_name_index = JSON.parse(localStorage.getItem(localStorageALIndex)) || '0';
const new_level_instance_depth = parseInt($scope.level_name_index, 10) + parseInt(users_depth, 10);
Expand Down Expand Up @@ -547,13 +549,15 @@
try {
interpolationParams = {
x_axis_label: $translate.instant($scope.xAxisSelectedItem.label),
y_axis_label: $translate.instant($scope.yAxisSelectedItem.label)
y_axis_label: $translate.instant($scope.yAxisSelectedItem.label),
aggregationType: $translate.instant( $scope.aggregationType)

Check failure on line 553 in seed/static/seed/js/controllers/inventory_reports_controller.js

View workflow job for this annotation

GitHub Actions / formatting (lint)

There should be no space after this paren
};
} catch (e) {
$log.error('$sce issue... missing translation');
interpolationParams = {
x_axis_label: $scope.xAxisSelectedItem.label,
y_axis_label: $scope.yAxisSelectedItem.label
y_axis_label: $scope.yAxisSelectedItem.label,
aggregationType: $scope.aggregationType
};
}
$scope.chart1Title = $translate.instant('X_VERSUS_Y', interpolationParams);
Expand Down Expand Up @@ -707,7 +711,7 @@

$scope.aggChartIsLoading = true;
inventory_reports_service
.get_aggregated_report_data(xVar, yVar, $scope.selected_cycles, $scope.access_level_instance_id, $scope.filter_group_id)
.get_aggregated_report_data(xVar, yVar, $scope.selected_cycles, $scope.access_level_instance_id, $scope.filter_group_id, $scope.aggregationType)
.then(
(data) => {
data = data.aggregated_data;
Expand Down Expand Up @@ -763,6 +767,7 @@
// Save axis and cycle selections
localStorage.setItem(localStorageXAxisKey, JSON.stringify($scope.xAxisSelectedItem ?? ''));
localStorage.setItem(localStorageYAxisKey, JSON.stringify($scope.yAxisSelectedItem ?? ''));
localStorage.setItem(localStorageAggregationTypeKey, JSON.stringify($scope.aggregationType ?? ''));
localStorage.setItem(localStorageSelectedCycles, JSON.stringify($scope.selected_cycles));
localStorage.setItem(localStorageALIndex, JSON.stringify($scope.level_name_index));
localStorage.setItem(localStorageALIID, JSON.stringify($scope.access_level_instance_id));
Expand Down
7 changes: 4 additions & 3 deletions seed/static/seed/js/services/inventory_reports_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ angular.module('SEED.service.inventory_reports', []).factory('inventory_reports_
}
}
*/
const get_aggregated_report_data = (xVar, yVar, cycle_ids, access_level_instance_id, filter_group_id) => {
const get_aggregated_report_data = (xVar, yVar, cycle_ids, access_level_instance_id, filter_group_id, aggregationType) => {
// Error checks
if ([xVar, yVar, cycle_ids].includes(null)) {
if ([xVar, yVar, cycle_ids, aggregationType].includes(null)) {
$log.error('#inventory_reports_service.get_aggregated_report_data(): null parameter');
throw new Error('Invalid Parameter');
}
Expand All @@ -99,7 +99,8 @@ angular.module('SEED.service.inventory_reports', []).factory('inventory_reports_
y_var: yVar,
cycle_ids,
access_level_instance_id,
filter_group_id
filter_group_id,
aggregationType
}
})
.then((response) => response.data)
Expand Down
2 changes: 1 addition & 1 deletion seed/static/seed/locales/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@
"X-Axis Field Options": "X-Axis Field Options",
"XSS_TEST": "Accented téxt. <script>alert(\"XSS FAIL IN BASE TRANSLATION STRING\");<\/script>{injectedText}",
"X_VERSUS_Y": "{x_axis_label} vs. {y_axis_label}",
"X_VERSUS_Y_AGGREGATED": "{x_axis_label} vs. {y_axis_label} (Averaged)",
"X_VERSUS_Y_AGGREGATED": "{x_axis_label} vs. {y_axis_label} ({aggregationType})",
"Y Axis": "Y Axis",
"YEAR_BUILT_THRESHOLD_HELP": "The year built to use as a comparison threshold in the calculations",
"Year": "Year",
Expand Down
2 changes: 1 addition & 1 deletion seed/static/seed/locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@
"X-Axis Field Options": "Opciones de campo del eje X",
"XSS_TEST": "Texto acentuado. <script>alert(\"XSS FAIL IN BASE TRANSLATION STRING\");<\/script>{injectedText}",
"X_VERSUS_Y": "{x_axis_label} vs. {y_axis_label}",
"X_VERSUS_Y_AGGREGATED": "{x_axis_label} vs. {y_axis_label} (Agregado)",
"X_VERSUS_Y_AGGREGATED": "{x_axis_label} vs. {y_axis_label} ({aggregationType})",
"Y Axis": "Eje Y",
"YEAR_BUILT_THRESHOLD_HELP": "El año construido que se utilizará como umbral de comparación en los cálculos",
"Year": "Año",
Expand Down
2 changes: 1 addition & 1 deletion seed/static/seed/locales/fr_CA.json
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@
"X-Axis Field Options": "Options de champ de l'axe X",
"XSS_TEST": "Texte accentée. <script>alert(\"XSS FAIL IN BASE TRANSLATION STRING\");<\/script>{injectedText}",
"X_VERSUS_Y": "{x_axis_label} contre {y_axis_label}",
"X_VERSUS_Y_AGGREGATED": "{x_axis_label} contre {y_axis_label} (agrégé)",
"X_VERSUS_Y_AGGREGATED": "{x_axis_label} contre {y_axis_label} ({aggregationType})",
"Y Axis": "Axe Y",
"YEAR_BUILT_THRESHOLD_HELP": "L'année de construction à utiliser comme seuil de comparaison dans les calculs",
"Year": "An",
Expand Down
6 changes: 5 additions & 1 deletion seed/static/seed/partials/inventory_reports.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,14 @@ <h2 class="property-report-header"><i class="fa-solid fa-bar-chart pull-left"></
<label for="xAxisSelector">{$:: 'X Axis' | translate $}:</label>
<select class="form-control" id="xAxisSelector" ng-change="setModified()" ng-model="xAxisSelectedItem" ng-options="x.name for x in xAxisVars track by x.varName"></select>
</div>
<div class="form-group">
<div class="form-group pad-bottom-10">
<label for="yAxisSelector">{$:: 'Y Axis' | translate $}:</label>
<select class="form-control" id="yAxisSelector" ng-model="yAxisSelectedItem" ng-change="setModified()" ng-options="y.name for y in yAxisVars track by y.varName"></select>
</div>
<div class="form-group">
<label for="aggregationTypeTypeSelector">{$:: 'Aggregation Type' | translate $}:</label>
<select class="form-control" id="aggregationTypeSelector" ng-model="aggregationType" ng-change="setModified()" ng-options="t for t in ['Sum', 'Average']"></select>
</div>
<div class="form-group pad-top-10">
<p>
<span translate>Visit</span>
Expand Down
31 changes: 23 additions & 8 deletions seed/views/v3/organizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,7 @@ def report_aggregated(self, request, pk=None):
params = {
"x_var": request.query_params.get("x_var", None),
"y_var": request.query_params.get("y_var", None),
"aggregation_type": request.query_params.get("aggregationType", None),
"cycle_ids": request.query_params.getlist("cycle_ids", None),
"access_level_instance_id": request.query_params.get("access_level_instance_id", None),
}
Expand Down Expand Up @@ -1083,7 +1084,9 @@ def report_aggregated(self, request, pk=None):
for datum in data:
buildings = datum["chart_data"]
yr_e = datum["property_counts"]["yr_e"]
chart_data.extend(aggregate_data(yr_e, buildings, bins, count=params["x_var"] == "Count"))
chart_data.extend(
aggregate_data(yr_e, buildings, bins, count=params["x_var"] == "Count", aggregation_type=params["aggregation_type"])
)
property_counts.append(datum["property_counts"])

# Send back to client
Expand All @@ -1094,34 +1097,46 @@ def report_aggregated(self, request, pk=None):

return Response(result, status=status.HTTP_200_OK)

def continuous_aggregate_data(self, yr_e, buildings, bins, count=False):
def continuous_aggregate_data(self, yr_e, buildings, bins, count=False, aggregation_type="Average"):
buildings = [b for b in buildings if b["x"] is not None and b["y"] is not None]
binplace = np.digitize([b["y"] for b in buildings], bins)
xs = [b["x"] for b in buildings]

if count or aggregation_type == "Sum":
aggregate = sum
elif aggregation_type == "Average":

def average(x):
return np.average(x).item()

aggregate = average

results = []
for i in range(len(bins) - 1):
bin = f"{round(bins[i], 2)} - {round(bins[i+1], 2)}"
values = np.array(xs)[np.where(binplace == i + 1)]
x = sum(values) if count else np.average(values).item()
x = aggregate(values)
results.append({"y": bin, "x": None if np.isnan(x) else x, "yr_e": yr_e})

return results

def discrete_aggregate_data(self, yr_e, buildings, bins, count=False):
def discrete_aggregate_data(self, yr_e, buildings, bins, count=False, aggregation_type="Average"):
xs_by_bin = {bin: [] for bin in bins}

for building in buildings:
xs_by_bin[building["y"]].append(building["x"])

results = []
for bin, xs in xs_by_bin.items():
if count:
if count or aggregation_type == "Sum":
x = sum(xs)
elif len(xs) == 0:
x = None
elif aggregation_type == "Average":
if len(xs) == 0:
x = None
else:
x = sum(xs) / len(xs)
else:
x = sum(xs) / len(xs)
raise ValueError(f"Bad aggregation_type: {aggregation_type}")

results.append({"y": bin, "x": x, "yr_e": yr_e})

Expand Down
Loading