Brain.js: Migrate to TypeScript

Created on 17 May 2020  ยท  136Comments  ยท  Source: BrainJS/brain.js

We plan to gradually migrate brain.js to TypeScript, code base is pretty large, so we would love your help! ๐Ÿ’ช

How to contribute?

  • Convert a file from .js to .ts
  • Add types, fix all type errors.
  • Submit a PR! ๐ŸŽ‰

Here you can find a guide on how to contribute.

Want to convert something, let us know in the comment and go ahead! ๐Ÿ˜Ž

To avoid duplicate work please comment on which part you want to work on (as long as nobody else is working on it) so we can mark it as taken.

Reach out to us!
Feel free to reach if you have questions or need help getting started. You can leave comments here or you can tag me in your PR if you need any help or you're not sure about something!

You can also get in touch on our Gitter & Slack.

Happy Coding! ๐ŸคŸ


UPDATE:

Wohoooo!!! ๐ŸŽ‰

All files inside src directory are migrated to typescript. (Except few ones which are already taken up and being worked on), though we are still looking on improvements to types in these files and removing any types from the source. You are welcome to contribute. ๐Ÿ˜Š

__tests__ directory has still some files left that needs migration to typescript, so feel free to pick em up! ๐Ÿญ


UPDATE

For anyone looking to contribute, here is the list of files that still needs typescript migration:

https://github.com/BrainJS/brain.js/search?l=javascript&p=1

enhancement good first issue hacktoberfest help wanted

Most helpful comment

With https://github.com/BrainJS/brain.js/commit/6c86dce270ae0a0414a85cced0ff3c24ac8f30cd I believe we are now a full on typescript library. Ty everyone for your hard work!

All 136 comments

I'd like to start working on this, I have some experience with Ts, can you tell me which module is up for grabs?

@yashshah1 You are welcome to contribute, please pick any module or portion for conversion and mention it here, so I can mark it in-progress (to avoid any duplication effort).

I'd like to work on this too, any recommendations on where to start?

@nabeelvalley here is the short guide, super simple to start. Please pick any module or portion for conversion and mention it here, so I can mark it in-progress (to avoid any duplication effort).

Here you can find a guide on how to contribute.

@mubaidr I'm working from the utilities up, to avoid having untyped dependencies

The tests seem to be failing when I convert the file to JS because it looks like Jest is running against the TS files instead of the compiled JS - has the TS Compile been configured?

Looks like the tests are also currently failing:

Test Suites: 1 failed, 5 passed, 6 of 64 total
Tests:       9 failed, 114 passed, 123 total
Snapshots:   0 total
Time:        206.723 s

It appears that the soft-max tests are receiving a Float32Array instead of a plain array - should I look where the Float32 is coming from or should I update the unit tests to expect a Floar32Array instead?

The data returned looks exactly the same, it's just that the object types are different so the deep equality is failing:

Example for one of the tests below:

  โ— SoftMax โ€บ .compare2D โ€บ can run on a simple matrix

    assert.deepEqual(received, expected)

    Expected value to deeply equal to:
      [[-0, 2], [3, 4]]
    Received:
      [[-0, 2], [3, 4]]

    Difference:

    - Expected
    + Received

      Array [
    -   Array [
    +   Float32Array [
          -0,
          2,
        ],
    -   Array [
    +   Float32Array [
          3,
          4,
        ],
      ]

Yes, you can update test to expect Float32Array.

In the mean-time some tests might still fail, because they have not yet been updated recently. You can can continue working and make sure build process is successful and conversion does not cause increase in no. of failed tests.

Hi! I want to help. If I convert one file, do I need to convert related files too or something? Or just a single file?

You don't need to updated all the related files, just go file by file and make sure build is successful.

Utilities is taken, isnt it?

Activation functions seem like a good place to start, may I work on it?

Utilities is taken, isnt it?

Hi @HarshKhandeparkar, I haven't had a chance to work on this you're welcome to take utilities if you want, just note that needs to be updated above

Np @nabeelvalley. I think I'll work on activation functions :)

@nabeelvalley I think I managed to fix the jest error you were facing in #582. You may copy paste my changes to jest.config.json or wait for the PR to be merged :)

estimator/ has a single file. Looks like a nice :dart:.

I think I'll snipe some of the utilities next, if you don't mind @nabeelvalley.

You would have probably started in alphabetical order(I am assuming), so I am going to start from the bottom.

Going to snipe utilities/values* tonight :)

Huzzah!

Build systems seems to be broken, I am looking into this issue. ๐Ÿงฏ

Can I continue or should I wait?

You should continue your contributions! I will try to fix it asap.

Hi All, taking a look at doing some of these as well today, starting from the top of utilities seeing as @HarshKhandeparkar has started from the bottom

Note, I haven't done utilities/array-lookup-table as it looks like it's working with some dynamic types and a few different cases and it's a bit difficult to infer all of these (in my opinion) until recurrent/rnn-time-step is updated. So doing it after that seems to make sense

I'm also not sure what's going on with the builds/tests as I had some issues from when I pulled the code to begin with, and the errors seem to be inconsistent (I assume due to the order in which tests are running etc.)

I am going to stop targeting utilitites for a while now. Apparently some of them refer to layer etc. So I am starting with layer/ today. I am going to drop a nuke on it :grin:

Love it!

I'll actually work on utilities/kernel first because it is referenced in layer/base. I'll do base after that.

NOTE: layer/base is a HOT MESS.

I also wanted to underline that but apparently underline isn't supported on github.

I'm taking over the layer folder from @HarshKhandeparkar (we coordinated already), and will need utilities either done soon, or will need to go ahead and handle:

  • utilities/kernel.js
  • utilities/random.js
  • utilities/randos.js
  • utilities/randos-2d.js
  • utilities/randos-3d.js

I'd also eventually like to consolidate these into a single file, but for a later task.

Going to continue working on utilities as well, will update on here as I go:

  • utilities/to-array
  • utilities/max

__tests__/utilities/mse.js

  test('should return an array if object is passed', () => {
    const collection = {
      name: 'Steve Jobs',
      alive: false,
    };
    const temp = toArray(collection);

    expect(temp.constructor).toBe(Float32Array);
    expect(temp.length).toBe(Object.keys(collection).length);
  });

@robertleeplummerjr I remember doing utilities/kernel.

I can take utilities/random and utilities/random-weight if no one else wants to take it.

@mubaidr The imports are broken and the tests are failing, am I supposed to ignore all of that and force commit?

@HarshKhandeparkar I have fixed the issue. You can create pull request now.

I'll convert the randos kernel tomorrow :)

I have randos done already, but if younknocknitnout, it all good.

On Thu, Sep 10, 2020, 12:58 PM Harsh Khandeparkar notifications@github.com
wrote:

I'll convert the randos kernel tomorrow :)

โ€”
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/BrainJS/brain.js/issues/551#issuecomment-690507706,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAFFZO63XW4THPXV4XGVTNLSFEATPANCNFSM4NDROIUQ
.

I have randos done already, but if younknocknitnout, it all good.

Oops, sorry but I opened a PR while you were deep asleep ;)

@mubaidr
1) Which file(s) can I work on?
2) is it expected that the many tests fail at the moment?

I am have lots of free time in the next 1-2 weeks and years of experience working with typescript, so if you give me the "go ahead" I can probably convert the whole thing.

@ofirgeller

  1. we've been handling them sort of from utilities up, because a lot of files depend on those, just keep us updated on here for whichever file you're doing and that should be fine
  2. i'd noticed some tests are failing at the moment too, think @mubaidr was looking into that

the type checker and linter shouldn't have any issues though - i think there are just two type checker warnings from what i last saw

@ofirgeller You can select any files from the the repo, and mention it here so that no effort is duplicated. Yes, we do have some failing tests, and we are working on this issue, but as @nabeelvalley said, you can start from utilities and we do have lint and typescript scripts (npm run lint) to help catch errors during conversion process.

Converted "array-lookup-table.js" (short and sweet), moving on to DataFormatter which will take a bit longer.

@ofirgeller I recommend creating a pull request per file (and related changes i.e. imports etc) to make it easier for code review and merge.

@mubaidr If each atomic change is a a separate commit on the same branch, do you still want each commit to be its own PR?

No. multiple commits per PR is good.

@mubaidr and @HarshKhandeparkar, layers are here: https://github.com/BrainJS/brain.js/compare/ts-layers

They are not done, but I've laid the foundation for what they'll look like. I just pushed it, and will merge test into it shortly and resolve conflicts. This includes some pretty heavy upgrades from typescript typings from gpu.js.

Tons more layer conversion, including pool.ts and convolution.ts!

All layers have now been converted. Opened PR here: https://github.com/BrainJS/brain.js/pull/603.

Merged the above. Picking up src/lookup.js

Hi, I'd like to work on this as well. Can you tell me which file is not being worked on?

I'm working on src/feed-forward.js, src/utilities/lookup-table.js, and src/praxis/momentum-root-mean-squared-propagation.js, and their tests.

Can I work on utils/range.js and utils/mse.js?

Sure!

Hello, can I work on utilities/array-lookup-table.js? If not, tell me a peding file to convert

Welcome! That one & reccurrent/matrix is already being worked on (https://github.com/BrainJS/brain.js/issues/551#issuecomment-702838155).

You can pick any other js files from the src or even __tests__. Looking forward!

I'm working on converting utilities/traverse-layers-from.js, utilities/traverse-layers-excluding-from.js, and utilities/to-svg.js.

Sure, so can I work on src/likely.js and src/utilities/flatten-layers.js?

Sounds good!

Can I work on src/utilities/layer-from-json.js? I don't think anyone is working on it.

If you are ok with this, I will take all JS files into src/recurrent/matrix

src/recurrent/matrix is already taken up: https://github.com/BrainJS/brain.js/pull/607

You can pick any other js files from the src or even __tests__. Looking forward!

What about __tests__/activation ?

Sure, go ahead!

Hello! Can i convert some js files in src/recurrent ?

@mubaidr Is anyone else working on __test__ folder? In the case of no one: I will convert the whole folder

@mberger75 src/recurrent/matrix is already taken up: https://github.com/BrainJS/brain.js/pull/607 but you are free to choose some other files in recurrent directory or even pick some subdirectory from __tests__ directory.

@lomby92 I recommend picking up one sub directory from __tests__ one at a time. This will help you focus more, help us in the review process and give others some content to contribute too. ๐Ÿ˜‹

Hello, can I get any module to convert it to Typescript ?

You can start with /__tests__/utilities, simple to start with. I recommend pull request per file if you are new to typescript.

Hello! Can I work on /__tests__/utilities/random.js?

Starting on src/recurrent/rnn.js. What a bear!

Hey! I'd like to start working on (probably two different PRs):

  • src/recurrent/gru-time-step.js
  • src/recurrent/gru.js
  • src/recurrent/lstm-time-step.js
  • src/recurrent/lstm.js

or any of those files have been already taken for someone else?

@magicmarvman sure pls

@vikhyat Hi! src/recurrent/lstm.js & src/recurrent/lstm-time-step.js is being worked on, you can work on gru.js & gru-time-step.js

Good luck!

Sure @mubaidr , I'll work on those files, thanks.

Any particular files I should work on?

@DarinDev1000 please check __tests__/utilities

Can someone pickup src/recurrent/matrix/copy.js?

It's already ts ยฏ\_(ใƒ„)_/ยฏ

Ah, I was looking at the export, not extension. I'll take care of converting to named export.

Hello! I want to contribute, can I convert __tests__/utilities/layer-size.js to typescript?

@LuanSilveiraSouza sure, go ahead!

I'd like to start working on this, can you tell me which module i can start with?

@rohya8 Please select any file(s) from __tests__ directory and just mention it here.

I'll contribute to /__tests__/utilities/data-formatter.js!

@dhairyagada /__tests__/utilities/ is fully migrated to typescript as per this pull request: https://github.com/BrainJS/brain.js/pull/630/files

You can pick other files please.

@mubaidr Can I know which one's are remaining?

All other js files inside __tests__ are available. You can start with __tests__/neural-network/options.js

@mubaidr Thanks! I'll start with it ๐Ÿ‘

@mubaidr Has src\neural-network.js already been converted to Typescript already? Because that is required for __tests__/neural-network/options.js

I have a locally converted __tests__/utilities/data-formatter.js

Hello again! I will convert /__tests__/neural-network/test.js to ts

Hey, I would like to convert /__tests__/neural-network/to-function.js to ts

@dhairyagada No, its in progress. But I don't think you would need it.
@LuanSilveiraSouza @Satyam-kumar-yadav Sure, go ahead!

@mubaidr
Let me know if I can pick any of test files for converting to ts.

@mubaidr I will convert __tests__/cross-validate.js to ts

@vikgmdev https://github.com/BrainJS/brain.js/blob/master/tests/neural-network/to-function.js is already in progress, you can continue with https://github.com/BrainJS/brain.js/blob/master/tests/neural-network/trainopts.js. ๐Ÿ‘๐Ÿผ

@JCMais Sure, please

@mubaidr
which files can I take?

We have following available:

  • https://github.com/BrainJS/brain.js/blob/master/__tests__/praxis/arthur-deviation-biases/end-to-end.js
  • https://github.com/BrainJS/brain.js/blob/master/__tests__/praxis/arthur-deviation-weights/end-to-end.js
  • https://github.com/BrainJS/brain.js/blob/master/__tests__/recurrent/end-to-end.js

@mubaidr I'll be taking up https://github.com/BrainJS/brain.js/blob/master/__tests__/recurrent/end-to-end.js

Hey! Would like to pick up some work here. I can help with removing any type or converting the file to typescript(although I don't see any file pending). You can direct me to "what is still pending"

@shubhi23994 That would be great! You can run npm run lint to identify any usages. Looking forward!

For anyone looking to contribute, here is the list of files that still needs typescript migration:

https://github.com/BrainJS/brain.js/search?l=javascript&p=1

Looks like childrens-book.js has already been converted. I'm going to convert https://github.com/BrainJS/brain.js/blob/370502c92ce228148524bc3bff95eac304a559a5/examples/javascript/predict-tense.js if that's not being worked on atm?

@luisgregson @dhairyagada Please wait for input from @robertleeplummerjr I think he was working on these, if otherwise, you can then work on these, please.

Yea, nearly done, but was away last week. Look for by end of week something for review. @dhairyagada I'd love to get your help on it, however.

@mubaidr @robertleeplummerjr
Sorry, i couldn't see neural-network.js mentioned here so i thought it was free. I'll stop working on it. But let me know if you want any help ๐Ÿ™‚

Sorry, neural-network.js is free, I am working on rnn.js.

@luisgregson You can continue working on neural-network.js, please ๐Ÿ‘๐Ÿผ

Yea, nearly done, but was away last week. Look for by end of week something for review. @dhairyagada I'd love to get your help on it, however.

Sure @robertleeplummerjr , I would love to help! Please let me know how can I contribute to it

I have pushed up the RNN changes to https://github.com/BrainJS/brain.js/tree/rnn-ts @dhairyagada. If you want to check it out. It is nearly complete in script conversion, but I've not tested it entirely yet. The typescript fails around serializing and deserializing from JSON.

I have pushed up the RNN changes to https://github.com/BrainJS/brain.js/tree/rnn-ts @dhairyagada. If you want to check it out. It is nearly complete in script conversion, but I've not tested it entirely yet. The typescript fails around serializing and deserializing from JSON.

Sure @robertleeplummerjr I'll check it out

Wow, this has made a lot of progress! Is there anything left where I can help?

I've got rnn.ts nearly done, upgrades to the data-formatter.ts (because they are so heavily linked) and some minor fixes along with the to-svg.ts fixes. I even found a (semi naughty) way of fixing some weirdness we were seeing in equation.ts. I'll have them pushed tomorrow.

Pushed rnn.ts up, with the related changes. Going to go ahead and merge, and pick up rnn-time-step.js.

Hi there @mubaidr and everyone! Wondering if I could help anywhere? Could I work on cross-validate.js conversion to cross-validate.ts? (along with fixing the test bugs in there right now caused by RNNTimeStep class), doesn't seem like anyone is working on that right now.

@StoneT2000 I am working on this right now. In fact, yesterday got the RNNTimeStep mostly converted to typescipt, but tests failing. Now working on those here: https://github.com/BrainJS/brain.js/tree/rnn-time-step-ts.

Here are a list of files that you can choose from:
src/cross-validate.js
src/neural-network.js
src/neural-network-gpu.js
src/recurrent.js

GUYS! Four files (and their tests ๐Ÿ˜„)?!?! That is all we have left!

@robertleeplummerjr I'll tackle src/cross-validate.js on my fork of master

Thanks! Ya'lls work on this looks amazing ๐Ÿ’ฏ and lets make that final push!

@robertleeplummerjr @mubaidr just made the cross-validation PR and if it looks good and is merged, I can tackle the neural-networks.js file next (and get rid of the million todos caused by the missing types :) )

Awesome work @StoneT2000!

Starting on src/recurrent.js while https://github.com/BrainJS/brain.js/pull/677 is waiting.

I'll get started on src/neural-networks.js

@StoneT2000, how is it going?
src/recurrent.js is now done: https://github.com/BrainJS/brain.js/pull/679 and https://github.com/BrainJS/brain.js/pull/677 is merged.

@robertleeplummerjr Uni just started again so put a short pause on what I was attempting with neural-network.js

There's also a few confusing typings for data that blocked me last I worked on it

I'll probably pick it up again this weekend

@StoneT2000 if you need me to take over, just let me know. Meanwhile, since we have just the two files remaining src/neural-network.js and src/neural-network-gpu.js I'm going to shift to fixing all the broken unit tests on branch: https://github.com/BrainJS/brain.js/tree/ts-tests.

Actually yeah, now that i think of it I don't think I'll have much time in the next few weeks to make this a good conversion, feel free to take over @robertleeplummerjr

You can try and use some of the conversion i've made although tbf I think i might've interpreted some functions incorrectly.

I'll be around anyhow and good luck!

Started on src/neural-network.js last night, maybe I'll have something next week.

With https://github.com/BrainJS/brain.js/commit/6c86dce270ae0a0414a85cced0ff3c24ac8f30cd I believe we are now a full on typescript library. Ty everyone for your hard work!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SCMorgan picture SCMorgan  ยท  3Comments

AkashGutha picture AkashGutha  ยท  5Comments

panzad3 picture panzad3  ยท  5Comments

tyt34 picture tyt34  ยท  5Comments

ximik753 picture ximik753  ยท  3Comments