-
Notifications
You must be signed in to change notification settings - Fork 73
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome work! Thank you!
Looks good modulo some minor comments.
@piscisaureus please review this due to the sensitive nature of the patch.
src/dataset_test.ts
Outdated
assertAllEqual, | ||
assertEqual, | ||
assertShapesEqual | ||
} | ||
from "./tensor_util"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style nit: Please have the closing curly brace and from "./tensor_util";
on the same line.
src/dl/util.ts
Outdated
@@ -17,6 +17,7 @@ import { DataType, DataTypeMap, NDArray } from "./math/ndarray"; | |||
* ============================================================================= | |||
*/ | |||
|
|||
export { assertEqual } from "../util"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although this might have been violated elsewhere, the intention is that src/dl
is standalone and imports should not reach outside of it. Please remove.
tools/jasmine_shim.ts
Outdated
try { | ||
assertEqual(value, expected); | ||
return true; | ||
} catch (e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the try catch needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I put it there because assertEqual throws if its arguments aren't equal, whereas toEqual returns a boolean
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah makes sense - In that case can you add another routine to util.ts called equal
which returns boolean, and use that here and in assertEqual
, so the try block can be avoided?
return true; | ||
} | ||
if (a && typeof a === "object" && b && typeof b === "object") { | ||
if (seen.get(a) === b) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this file there is a method called objectsEqual
- can that be reused here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thanks. Am I right in thinking that assertObjectsEqual (https://github.com/propelml/propel/blob/master/src/util.ts#L140) isn't needed anymore as assertEqual deals with objects?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Please replace assertObjectsEqual calls with asserEqual
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried using objectsEqual
but it doesnt work with nested objects so I kept equal
as it is here (and replaced objectsEqual
with equal
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great - thank you!
src/util.ts
Outdated
export function assertEqual(actual: any, expected: any, msg = null) { | ||
if (!msg) { msg = `actual: ${actual} expected: ${expected}`; } | ||
if (!equal(actual, expected)) { | ||
throw new Error(msg); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For debugging on web, in the case where the actual and expected are objects - it would be helpful to have an additional output here. Something like this:
console.error("assertEqual failed. actual = ", actual, "expected =", expected)
(This way the nice inspector interface can be used to inspect deep objects)
src/dl/util.ts
Outdated
return false; | ||
} | ||
seen.set(a, b); | ||
return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's hard for me to tell if this algorithm is correct. Could you add some tests to util_test.ts
?
(You can copy some examples from tools/jasmine_shim_test)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure no problem. Just to check first, is this already covered by the tests in jasmine_shim_test.ts
? As toEqual
is tested in jasmine_shim.ts
and it just returns the value from this function. Shall i move the tests from jasmine_shim_test.ts
to util_test.ts
or copy them?
Thanks for your help, hope you don't mind all the questions!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, kind of. The jasmine_shim_test.ts
is covering the jasmine wrapper around assertEqual
(called toEqual
)... but we also want to unit test assertEqual
by itself. So just duplicating the tests is fine. No, thank you! :)
src/dl/util.ts
Outdated
} | ||
} | ||
|
||
function equal(c: any, d: any): boolean { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be in src/util.ts
not src/dl/util.ts
src/dl/util.ts
Outdated
@@ -73,6 +73,52 @@ export function assert(expr: boolean, msg: string) { | |||
} | |||
} | |||
|
|||
export function assertEqual(actual: any, expected: any, msg = null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be in src/util.ts not src/dl/util.ts
src/dl/math/ndarray.ts
Outdated
@@ -222,7 +222,7 @@ export class NDArray<D extends DataType = DataType, R extends Rank = Rank> { | |||
|
|||
asScalar(): Scalar<D> { | |||
this.throwIfDisposed(); | |||
util.assert(this.size === 1, "The array must have only 1 element."); | |||
util.assertEqual(this.size, 1, "The array must have only 1 element."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Files in src/dl should not be changed.
f144375
to
b4b026b
Compare
Works with objects. jasmine_shim.ts and tensor_util.ts use equal/assertEqual from util.ts Relates propelml#423
as assertEqual and equal work with objects (including nested objects). Relates propelml#423
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Elegantly done - thank you!
I attempted to address #423 , adding assertEqual to util.ts and sharing its code with tensor_util.ts and jasmine_shim.ts
Any guidance or feedback would be great @ry @piscisaureus