{"id":19267011,"url":"https://github.com/mljs/ngmca","last_synced_at":"2025-09-05T16:09:27.758Z","repository":{"id":43354202,"uuid":"292312754","full_name":"mljs/nGMCA","owner":"mljs","description":"A tool for Non-negative matrix factorization","archived":false,"fork":false,"pushed_at":"2022-03-06T11:06:46.000Z","size":1538,"stargazers_count":0,"open_issues_count":4,"forks_count":1,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-02-05T14:07:35.422Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://mljs.github.io/nGMCA/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mljs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-09-02T14:53:08.000Z","updated_at":"2022-03-06T11:06:51.000Z","dependencies_parsed_at":"2022-08-27T10:22:56.328Z","dependency_job_id":null,"html_url":"https://github.com/mljs/nGMCA","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mljs%2FnGMCA","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mljs%2FnGMCA/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mljs%2FnGMCA/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mljs%2FnGMCA/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mljs","download_url":"https://codeload.github.com/mljs/nGMCA/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240367671,"owners_count":19790294,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T20:09:18.061Z","updated_at":"2025-02-23T19:29:36.914Z","avatar_url":"https://github.com/mljs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# nGMCA - non-negative Generalized Morphological Component Analysis\n\n\u003cp align=\"center\"\u003e\n  \u003cimg alt=\"NMReDATA\" src=\"image/nonNegativeMatrixFactorization.png\"\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n  A tool for non-negative matrix factorization.\n\u003c/p\u003e\n\n## Instalation\n\n`$ npm install ml-ngmca `\n\n## Usage\n\n```js\nimport { nGMCA } from 'ml-ngmca';\n\nconst result = nGMCA(dataMatrix, options);\n```\n\n### As a CommonJS module\n\n```js\nconst { nGMCA } = require('ml-ngmca');\n\nconst result = nGMCA(dataMatrix, options);\n```\n\n## [API Documentation](https://mljs.github.io/nGMCA/)\n\nThis algorithm is based on the article [Jérémy Rapin, Jérôme Bobin, Anthony Larue, Jean-Luc Starck. Sparse and Non-negative BSS for Noisy Data, IEEE Transactions on Signal Processing, 2013.IEEE Transactions on Signal Processing, vol. 61, issue 22, p. 5620-5632, 2013.](https://arxiv.org/pdf/1308.5546.pdf)\n\nIn order to get a general idea of the problem you could also check the [Wikipedia article](https://en.wikipedia.org/wiki/Non-negative_matrix_factorization).\n\n## Examples\n\nYou will be able to separate the components of a mixture if you have a series of measurements correlated by a composition profile e.g NMR or mass spectra coming from a chromatographic coupled technique of two or more close retention times. So you will have a matrix with a number of rows equal or greater than the number of pure components of the mixture.\n\n```js\nimport { Matrix } from 'ml-matrix';\nimport { nGMCA } from 'ml-ngmca';\n\nlet pureSpectra = new Matrix([[1, 0, 1, 0]]);\nlet composition = new Matrix([[1, 2, 3, 2, 1]]);\n\n// matrix = composition.transpose().mmul(pureSpectra)\nlet matrix = new Matrix([\n  [1, 0, 1, 0],\n  [2, 0, 2, 0],\n  [3, 0, 3, 0],\n  [2, 0, 2, 0],\n  [1, 0, 1, 0],\n]);\n\nconst options = {\n  maximumIteration: 200,\n  phaseRatio: 0.4,\n};\nconst result = nGMCA(matrix, 1, options);\nconst { A, S } = result;\nconsole.log(`A = ${A.to2DArray()} S =${S.to2DArray()}`);\n/**\nA = [\n    [ 0.22941573387056177 ],\n    [ 0.45883146774112354 ],\n    [ 0.6882472016116853 ],\n    [ 0.45883146774112354 ],\n    [ 0.22941573387056177 ]\n  ]\nS = [ [ 4.358898943540674, 0, 4.358898943540674, 0 ] ]\n\nif you reescale both S maxS and A with 1/maxS.\n*/\n\nlet maxByRow = [];\nfor (let i = 0; i \u003c S.rows; i++) {\n  maxByRow.push(S.maxRow(i));\n}\n\nS.scale('row', { scale: maxByRow });\nA.scale('column', {\n  scale: maxByRow.map((e) =\u003e 1 / e),\n});\n\n/**\nS = [ [ 1, 0, 1, 0 ] ]\nA = [\n  [1.0000000000000002],\n  [2.0000000000000004],\n  [3.0000000000000004],\n  [2.0000000000000004],\n  [1.0000000000000002]\n  ]\n*/\n\nconst estimatedMatrix = A.mmul(S);\nconst diff = Matrix.sub(matrix, estimatedMatrix);\n```\n\n\nHere is a second example: \n\n```js\nlet matrix = new Matrix([\n  [0, 0, 1, 1, 1],\n  [0, 0, 1, 1, 1],\n  [2, 2, 2, 0, 0],\n  [2, 2, 2, 0, 0],\n]);\n\nconst options = {\n  maximumIteration: 200,\n  phaseRatio: 0.4,\n};\nconst result = nGMCA(matrix, 1, options);\nconst { A, S } = result;\nconsole.log(`A = ${A} S =${S}`);\n/**\n A = [\n  [\n    0.707107 0       \n    0.707107 0       \n    2.26e-17 0.707107\n    2.26e-17 0.707107\n  ]\n]\nS = [\n  [\n    9.86e-32 9.86e-32 1.41421 1.41421 1.41421\n    2.82843  2.82843  2.82843 0       0       \n  ]\n]\nnote: 9.86e-32 and 2.26e-17 is practically zero\nso if you reescale both S maxS and A with 1/maxS.\n*/\n\nlet maxByRow = [];\nfor (let i = 0; i \u003c S.rows; i++) {\n  maxByRow.push(S.maxRow(i));\n}\n\nS.scale('row', { scale: maxByRow });\nA.scale('column', {\n  scale: maxByRow.map((e) =\u003e 1 / e),\n});\n\nconsole.log(`A = ${A} S =${S}`);\n/**\n A = [\n  [\n    1 0       \n    1 0       \n    0 1\n    0 1\n  ]\n]\nS = [\n  [\n    0 0 1 1 1\n    2 2 2 0 0       \n  ]\n]\n*/\n```\n\nThe result has the matrices A and S, the estimated matrices of compositions and pureSpectra respectively. It's possible that the matrices A and S have not the same scale than pureSpectra and composition matrices because of AS has an infinity of combination to get the target matrix.\n\n## License\n\n[MIT](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmljs%2Fngmca","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmljs%2Fngmca","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmljs%2Fngmca/lists"}