Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Share default TimelineCredParameters (#1376)
At present, every place in the codebase that needs TimelineCredParameters constructs them ad-hoc, meaning we don't have any shared defaults across different consumers. This commit adds a new type, `PartialTimelineCredParameters`, which is basically `TimelineCredParameters` with every field marked optional. Callers can then choose to override any fields where they want non-default values. A new internal `partialParams` function promotes these partial parameters to full parameters. All the public interfaces for using params (namely, `TimelineCred.compute` and `TimelineCred.reanalyze`) now accept optional partial params. If the params are not specified, default values are used; if partial params are provided, all the explicitly provided values are used, and unspecified values are initialized to default values. Test plan: A simple unit test was added to ensure that weights overrides work as intended. `git grep "intervalDecay: "` reveals that there are no other explicit parameter constructions in the codebase. All existing unit tests pass.
- Loading branch information
1 parent
def1fef
commit 0a0010f
Showing
10 changed files
with
145 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,67 @@ | ||
// @flow | ||
|
||
import {paramsToJSON, paramsFromJSON} from "./params"; | ||
import { | ||
paramsToJSON, | ||
paramsFromJSON, | ||
defaultParams, | ||
partialParams, | ||
type TimelineCredParameters, | ||
DEFAULT_ALPHA, | ||
DEFAULT_INTERVAL_DECAY, | ||
} from "./params"; | ||
import {defaultWeights} from "../weights"; | ||
import {NodeAddress} from "../../core/graph"; | ||
|
||
describe("analysis/timeline/params", () => { | ||
it("JSON round trip", () => { | ||
const customWeights = () => { | ||
const weights = defaultWeights(); | ||
// Ensure it works with non-default weights | ||
weights.nodeManualWeights.set(NodeAddress.empty, 33); | ||
const p = {alpha: 0.5, intervalDecay: 0.5, weights}; | ||
return weights; | ||
}; | ||
it("JSON round trip", () => { | ||
const p: TimelineCredParameters = { | ||
alpha: 0.1337, | ||
intervalDecay: 0.31337, | ||
weights: customWeights(), | ||
}; | ||
const j = paramsToJSON(p); | ||
const p_ = paramsFromJSON(j); | ||
const j_ = paramsToJSON(p_); | ||
expect(j).toEqual(j_); | ||
expect(p).toEqual(p_); | ||
}); | ||
it("defaultParams", () => { | ||
const expected: TimelineCredParameters = { | ||
alpha: DEFAULT_ALPHA, | ||
intervalDecay: DEFAULT_INTERVAL_DECAY, | ||
weights: defaultWeights(), | ||
}; | ||
expect(defaultParams()).toEqual(expected); | ||
}); | ||
describe("partialParams", () => { | ||
it("uses default values if no overrides provided", () => { | ||
const params = partialParams({}); | ||
expect(params).toEqual(defaultParams()); | ||
}); | ||
it("accepts an alpha override", () => { | ||
const params = partialParams({alpha: 0.99}); | ||
expect(params.weights).toEqual(defaultWeights()); | ||
expect(params.alpha).toEqual(0.99); | ||
expect(params.intervalDecay).toEqual(DEFAULT_INTERVAL_DECAY); | ||
}); | ||
it("accepts weights override", () => { | ||
const weights = customWeights(); | ||
const params = partialParams({weights}); | ||
expect(params.weights).toEqual(weights); | ||
expect(params.alpha).toEqual(DEFAULT_ALPHA); | ||
expect(params.intervalDecay).toEqual(DEFAULT_INTERVAL_DECAY); | ||
}); | ||
it("accepts intervalDecay override", () => { | ||
const params = partialParams({intervalDecay: 0.1}); | ||
expect(params.weights).toEqual(defaultWeights()); | ||
expect(params.alpha).toEqual(DEFAULT_ALPHA); | ||
expect(params.intervalDecay).toEqual(0.1); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters