From 86ce26acb86a7c19cb4392e55746849306da4d2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dandelion=20Man=C3=A9?= Date: Fri, 10 Aug 2018 19:29:01 -0700 Subject: [PATCH] Improve weight config (#644) This modifies WeightConfig to properly use the fallback node type, as created in #640 and merged in #642. As an additional change, it now displays type names, rather than the address parts. For example, the issue type is now displayed as `Issue`, not `["sourcecred", "github", "issue"]`. The WeightConfig is an untested mess, and I will likely re-write it entirely. (See a bevy of WeightConfig related issues: #604, #595, #588). So, not too much effort was invested in keeping high code quality in this commit. Test plan: The weight config has no tests, so I manually tested: - weights persist after page reload - node weights influence cred attribution - edge weights influence cred attribution - edge directionality influences cred attribution - weights have reasonably pretty description messages --- src/app/credExplorer/App.js | 2 +- src/app/credExplorer/WeightConfig.js | 155 +++++++++++++-------------- src/app/credExplorer/edgeWeights.js | 34 +++--- 3 files changed, 94 insertions(+), 97 deletions(-) diff --git a/src/app/credExplorer/App.js b/src/app/credExplorer/App.js index 68fa288..2cfe9c8 100644 --- a/src/app/credExplorer/App.js +++ b/src/app/credExplorer/App.js @@ -19,7 +19,7 @@ import { export default class AppPage extends React.Component<{||}> { static _LOCAL_STORE = new CheckedLocalStore( new BrowserLocalStore({ - version: "1", + version: "2", keyPrefix: "cred-explorer", }) ); diff --git a/src/app/credExplorer/WeightConfig.js b/src/app/credExplorer/WeightConfig.js index 7a8daa5..9111f08 100644 --- a/src/app/credExplorer/WeightConfig.js +++ b/src/app/credExplorer/WeightConfig.js @@ -1,53 +1,42 @@ // @flow import React from "react"; - -import { - type EdgeAddressT, - type NodeAddressT, - EdgeAddress, - NodeAddress, -} from "../../core/graph"; +import sortBy from "lodash.sortby"; import type {LocalStore} from "../localStore"; import {type EdgeEvaluator} from "../../core/attribution/pagerank"; import {byEdgeType, byNodeType} from "./edgeWeights"; -import * as MapUtil from "../../util/map"; import * as NullUtil from "../../util/null"; import {defaultStaticAdapters} from "../adapters/defaultPlugins"; +import type {NodeType, EdgeType} from "../adapters/pluginAdapter"; type Props = {| +localStore: LocalStore, +onChange: (EdgeEvaluator) => void, |}; -type EdgeWeights = Map; -type UserEdgeWeight = {|+logWeight: number, +directionality: number|}; - +type WeightedEdgeType = {| + +type: EdgeType, + +logWeight: number, + +directionality: number, +|}; +type EdgeWeights = WeightedEdgeType[]; const EDGE_WEIGHTS_KEY = "edgeWeights"; const defaultEdgeWeights = (): EdgeWeights => { - const result = new Map(); - for (const {prefix} of defaultStaticAdapters().edgeTypes()) { - if (prefix === EdgeAddress.empty) { - // We haven't decided how to deal with the FallbackAdapter's fallback type. - continue; - } - result.set(prefix, {logWeight: 0, directionality: 0.5}); + const result = []; + for (const type of defaultStaticAdapters().edgeTypes()) { + result.push({type, logWeight: 0, directionality: 0.5}); } return result; }; -type NodeWeights = Map; -type UserNodeWeight = number /* in log space */; +type NodeWeights = WeightedNodeType[]; +type WeightedNodeType = {|+type: NodeType, +logWeight: number|}; const NODE_WEIGHTS_KEY = "nodeWeights"; const defaultNodeWeights = (): NodeWeights => { - const result = new Map(); - for (const {prefix, defaultWeight} of defaultStaticAdapters().nodeTypes()) { - if (prefix === NodeAddress.empty) { - // We haven't decided how to deal with the FallbackAdapter's fallback type. - continue; - } - result.set(prefix, Math.log2(defaultWeight)); + const result = []; + for (const type of defaultStaticAdapters().nodeTypes()) { + result.push({type, logWeight: type.defaultWeight}); } return result; }; @@ -74,11 +63,11 @@ export class WeightConfig extends React.Component { (state) => { return { edgeWeights: NullUtil.orElse( - NullUtil.map(localStore.get(EDGE_WEIGHTS_KEY), MapUtil.fromObject), + localStore.get(EDGE_WEIGHTS_KEY), state.edgeWeights ), nodeWeights: NullUtil.orElse( - NullUtil.map(localStore.get(NODE_WEIGHTS_KEY), MapUtil.fromObject), + localStore.get(NODE_WEIGHTS_KEY), state.nodeWeights ), }; @@ -127,21 +116,19 @@ export class WeightConfig extends React.Component { fire() { const {localStore} = this.props; const {edgeWeights, nodeWeights} = this.state; - localStore.set(EDGE_WEIGHTS_KEY, MapUtil.toObject(edgeWeights)); - localStore.set(NODE_WEIGHTS_KEY, MapUtil.toObject(nodeWeights)); - const edgePrefixes = Array.from(edgeWeights.entries()).map( - ([prefix, {logWeight, directionality}]) => ({ - prefix, + localStore.set(EDGE_WEIGHTS_KEY, edgeWeights); + localStore.set(NODE_WEIGHTS_KEY, nodeWeights); + const edgePrefixes = edgeWeights.map( + ({type, logWeight, directionality}) => ({ + prefix: type.prefix, weight: 2 ** logWeight, directionality, }) ); - const nodePrefixes = Array.from(nodeWeights.entries()).map( - ([prefix, logWeight]) => ({ - prefix, - weight: 2 ** logWeight, - }) - ); + const nodePrefixes = nodeWeights.map(({type, logWeight}) => ({ + prefix: type.prefix, + weight: 2 ** logWeight, + })); const edgeEvaluator = byNodeType(byEdgeType(edgePrefixes), nodePrefixes); this.props.onChange(edgeEvaluator); } @@ -152,49 +139,55 @@ class EdgeConfig extends React.Component<{ onChange: (EdgeWeights) => void, }> { weightControls() { - return Array.from(this.props.edgeWeights.entries()).map(([key, datum]) => ( -