Skip to content

Commit

Permalink
add curated sample places (#1426)
Browse files Browse the repository at this point in the history
  • Loading branch information
chejennifer authored Jan 29, 2022
1 parent 653be6e commit 087cefe
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 13 deletions.
16 changes: 12 additions & 4 deletions static/js/tools/map/stat_var_chooser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { getStatVarInfo } from "../../shared/stat_var";
import { StatVarHierarchyType } from "../../shared/types";
import { DrawerToggle } from "../../stat_var_hierarchy/drawer_toggle";
import { StatVarHierarchy } from "../../stat_var_hierarchy/stat_var_hierarchy";
import { getSamplePlaces } from "../../utils/place_utils";
import {
Context,
DisplayOptionsWrapper,
Expand All @@ -37,15 +38,22 @@ import {
getMapPointPlaceType,
} from "./util";

const SAMPLE_SIZE = 3;

export function StatVarChooser(): JSX.Element {
const { statVar, placeInfo, display } = useContext(Context);
const [samplePlaces, setSamplePlaces] = useState(
_.sampleSize(placeInfo.value.enclosedPlaces, SAMPLE_SIZE)
getSamplePlaces(
placeInfo.value.enclosingPlace.dcid,
placeInfo.value.enclosedPlaceType,
placeInfo.value.enclosedPlaces
)
);
useEffect(() => {
setSamplePlaces(_.sampleSize(placeInfo.value.enclosedPlaces, SAMPLE_SIZE));
const samplePlaces = getSamplePlaces(
placeInfo.value.enclosingPlace.dcid,
placeInfo.value.enclosedPlaceType,
placeInfo.value.enclosedPlaces
);
setSamplePlaces(samplePlaces);
}, [placeInfo.value.enclosedPlaces]);
useEffect(() => {
const svWithInfo = _.isNull(statVar.value.info)
Expand Down
21 changes: 12 additions & 9 deletions static/js/tools/scatter/statvar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,9 @@ import { getStatVarInfo, StatVarInfo } from "../../shared/stat_var";
import { StatVarHierarchyType } from "../../shared/types";
import { DrawerToggle } from "../../stat_var_hierarchy/drawer_toggle";
import { StatVarHierarchy } from "../../stat_var_hierarchy/stat_var_hierarchy";
import { getSamplePlaces } from "../../utils/place_utils";
import { AxisWrapper, Context } from "./context";

// Number of enclosed places to sample when filtering the stat vars in the
// stat var menu
// Use a large sample size here. Performance wise this is okay. When the filtered
// stat vars are set, mixer can use the existence cache to check if stat vars
// exist for the place, which is only a point read.
const SAMPLE_SIZE = 50;

interface StatVar {
// Always contains a single statvar.
info: StatVarInfo;
Expand Down Expand Up @@ -78,10 +72,19 @@ function StatVarChooser(): JSX.Element {
const [modalSelected, setModalSelected] = useState(defaultModalSelected);
const [modalOpen, setModalOpen] = useState(false);
const [samplePlaces, setSamplePlaces] = useState(
_.sampleSize(place.value.enclosedPlaces, SAMPLE_SIZE)
getSamplePlaces(
place.value.enclosingPlace.dcid,
place.value.enclosedPlaceType,
place.value.enclosedPlaces
)
);
useEffect(() => {
setSamplePlaces(_.sampleSize(place.value.enclosedPlaces, SAMPLE_SIZE));
const samplePlaces = getSamplePlaces(
place.value.enclosingPlace.dcid,
place.value.enclosedPlaceType,
place.value.enclosedPlaces
);
setSamplePlaces(samplePlaces);
}, [place.value.enclosedPlaces]);
const menuSelected = [
x.value.statVarDcid,
Expand Down
62 changes: 62 additions & 0 deletions static/js/utils/place_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import _ from "lodash";

import { EARTH_NAMED_TYPED_PLACE } from "../shared/constants";
import { NamedPlace } from "../shared/types";

const DEFAULT_SAMPLE_SIZE = 50;
const CURATED_SAMPLE_PLACES = {
[EARTH_NAMED_TYPED_PLACE.dcid]: {
Country: [
{ dcid: "country/USA", name: "United States of America" },
{ dcid: "country/MEX", name: "Mexico" },
{ dcid: "country/BRA", name: "Brazil" },
{ dcid: "country/DEU", name: "Germany" },
{ dcid: "country/POL", name: "Poland" },
{ dcid: "country/RUS", name: "Russia" },
{ dcid: "country/ZAF", name: "South Africa" },
{ dcid: "country/ZWE", name: "Zimbabwe" },
{ dcid: "country/CHN", name: "People's Republic of China" },
{ dcid: "country/IND", name: "India" },
{ dcid: "country/AUS", name: "Australia" },
],
},
};

/**
* Get a subset of the list of places that are of childPlaceType and are
* children of the parentPlace
* @param parentPlace
* @param childPlaceType
* @param childrenPlaces
* @param sampleSize
*/
export function getSamplePlaces(
parentPlace: string,
childPlaceType: string,
childrenPlaces: Array<NamedPlace>,
sampleSize?: number
): Array<NamedPlace> {
if (
parentPlace in CURATED_SAMPLE_PLACES &&
childPlaceType in CURATED_SAMPLE_PLACES[parentPlace]
) {
return CURATED_SAMPLE_PLACES[parentPlace][childPlaceType];
}
return _.sampleSize(childrenPlaces, sampleSize || DEFAULT_SAMPLE_SIZE);
}

0 comments on commit 087cefe

Please sign in to comment.