@mapequation/infomap 2.0.2 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/network.d.ts CHANGED
@@ -1,12 +1,12 @@
1
- interface Node {
1
+ export interface Node {
2
2
  id: number;
3
3
  name?: string;
4
4
  weight?: number;
5
5
  }
6
- interface StateNode extends Omit<Node, "weight"> {
6
+ export interface StateNode extends Omit<Node, "weight"> {
7
7
  stateId: number;
8
8
  }
9
- interface Link {
9
+ export interface Link {
10
10
  source: number;
11
11
  target: number;
12
12
  weight?: number;
@@ -22,14 +22,14 @@ export interface StateNetwork extends Required<Network> {
22
22
  states: StateNode[];
23
23
  }
24
24
  export declare type BipartiteStateNetwork = Required<BipartiteNetwork> & StateNetwork;
25
- interface MultilayerLink extends Link {
25
+ export interface MultilayerLink extends Link {
26
26
  sourceLayer: number;
27
27
  targetLayer: number;
28
28
  }
29
- interface IntraLink extends Link {
29
+ export interface IntraLink extends Link {
30
30
  layerId: number;
31
31
  }
32
- interface InterLink extends Omit<MultilayerLink, "source" | "target"> {
32
+ export interface InterLink extends Omit<MultilayerLink, "source" | "target"> {
33
33
  id: number;
34
34
  }
35
35
  export declare type MultilayerNetwork = Network<MultilayerLink>;
@@ -39,4 +39,3 @@ export interface MultilayerIntraInterNetwork extends Omit<Network, "links"> {
39
39
  }
40
40
  export declare type NetworkTypes = Network | BipartiteNetwork | StateNetwork | BipartiteStateNetwork | MultilayerNetwork | MultilayerIntraInterNetwork;
41
41
  export default function toString(network: NetworkTypes): string | undefined;
42
- export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mapequation/infomap",
3
- "version": "2.0.2",
3
+ "version": "2.3.0",
4
4
  "description": "Infomap network clustering algorithm",
5
5
  "browser": "index.js",
6
6
  "publishConfig": {
package/parser.d.ts CHANGED
@@ -1,17 +1,21 @@
1
- import type { NodeBase, CluNode, TreeNode as JsonTreeNode } from "./filetypes";
1
+ import type { CluNode, NodeBase, TreeNode as JsonTreeNode } from "./filetypes";
2
2
  import type { Header as JsonHeader, Module } from "./index";
3
3
  declare type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
4
4
  declare type Header = Optional<JsonHeader, "directed">;
5
- declare type TreeNode = Omit<JsonTreeNode, "modules" | "mec">;
5
+ declare type Path = {
6
+ path: string | number[];
7
+ };
8
+ declare type TreeNode = Omit<JsonTreeNode, "modules" | "mec" | "path"> & Path;
6
9
  export declare type Result<NodeType extends NodeBase> = Header & {
7
10
  nodes: NodeType[];
8
11
  modules?: Module[];
9
12
  };
10
13
  export declare type Extension = "clu" | "tree";
11
14
  export declare function extension(filename: string): string | null;
12
- export declare function readFile(file: File): Promise<unknown>;
13
- export declare function parse(file: string | string[], type?: Extension, parseLinks?: boolean): Result<CluNode<"moduleId">> | Result<TreeNode> | null;
14
- export declare function parseClu<NodeType extends CluNode>(file: string | string[], nodeHeader?: string[]): Result<NodeType> | null;
15
- export declare function parseTree<NodeType extends TreeNode>(file: string | string[], nodeHeader?: string[], parseLinks?: boolean): Result<NodeType> | null;
16
- export declare function parseHeader(file: string | string[]): Header | null;
15
+ export declare function lines(file: string): string[];
16
+ export declare function readFile(file: File): Promise<string>;
17
+ export declare function parse(file: string | string[], type?: Extension, parseLinks?: boolean, strictHeader?: boolean): Result<CluNode<"moduleId">> | Result<TreeNode>;
18
+ export declare function parseClu<NodeType extends CluNode>(file: string | string[], nodeHeader?: string[], strictHeader?: boolean): Result<NodeType>;
19
+ export declare function parseTree<NodeType extends TreeNode>(file: string | string[], nodeHeader?: string[], parseLinks?: boolean, strictHeader?: boolean): Result<NodeType>;
20
+ export declare function parseHeader(file: string | string[], strict?: boolean): Header;
17
21
  export {};
package/parser.js CHANGED
@@ -1 +1 @@
1
- !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.infomap=t():e.infomap=t()}(self,(function(){return(()=>{"use strict";var e={};return(()=>{var t=e;Object.defineProperty(t,"__esModule",{value:!0}),t.parseHeader=t.parseTree=t.parseClu=t.parse=t.readFile=t.extension=void 0,t.extension=function(e){var t;return null!==(t=e.split(".").pop())&&void 0!==t?t:null},t.readFile=function(e){return new Promise(((t,n)=>{const o=new FileReader;o.onload=()=>t(o.result),o.onerror=n,o.readAsText(e)}))},t.parse=function(e,t,n=!1){"string"==typeof e&&(e=e.split("\n"));const s=r(e);if(null===s)return null;if(t&&"clu"===t||s.includes("module")){if("node_id"===s[0])return o(e,s);if("state_id"===s[0])return o(e,s)}else if(t&&"tree"===t||s.includes("path")){if("name"===s[s.length-2])return i(e,s,n);if("state_id"===s[s.length-2]||"layer_id"===s[s.length-1])return i(e,s,n)}return null};const n={node_id:"id",module:"moduleId",flow:"flow",name:"name",state_id:"stateId",layer_id:"layerId",path:"path"};function o(e,t){var o;"string"==typeof e&&(e=e.split("\n"));const i=l(e);if(!i)return console.warn("No header"),null;if(!t&&0===(t=null!==(o=r(e))&&void 0!==o?o:[]).length)return console.warn("No node header"),null;const a=[];for(const[o,i]of s(e)){const e=i.split(" ").map(Number);if(e.length<t.length)continue;const o={};for(let i=0;i<t.length;++i){const s=t[i];o[n[s]]=e[i]}a.push(o)}return Object.assign(Object.assign({},i),{nodes:a})}function i(e,t,o=!1){var i,a;"string"==typeof e&&(e=e.split("\n"));const u=l(e);if(!u)return console.warn("No header"),null;if(!t&&0===(t=null!==(i=r(e))&&void 0!==i?i:[]).length)return console.warn("No node header"),null;const c=[];let d=0;for(const[o,i]of s(e)){d=o;const e=i.match(/[^\s"']+|"([^"]*)"/g);if(null===e||e.length<t.length)continue;const s={};for(let o=0;o<t.length;++o){const i=t[o],r=n[i];switch(i){case"path":s.path=e[o].split(":").map(Number);break;case"name":s.name=e[o].slice(1,-1);break;case"node_id":s.id=Number(e[o]);break;case"state_id":case"layer_id":case"flow":s[r]=Number(e[o]);break;default:console.warn(`Unknown field ${i}`)}}c.push(s)}if(++d,e.length<=d+1)return Object.assign(Object.assign({},u),{nodes:c});const f=[];let p=null;const h=e[d];let m=!1;if(null==h?void 0:h.startsWith("*Links")){const e=h.trim().split(" ");m=e.length>1&&"directed"===e[1]}for(const[t,n]of function*(e,t=0){if(!(e.length<=t)&&e[t].startsWith("*Links"))for(let n=++t;n<e.length;++n){const t=e[n];0!==t.length&&(yield[n,t])}}(e,d)){if(n.startsWith("*Links")){const[e,t,...i]=n.split(" "),s="root"===t?[0]:t.split(":").map(Number),[r,l,a,u]=i.map(Number);p={path:s,enterFlow:r,exitFlow:l,numEdges:a,numChildren:u,links:o?[]:void 0},f.push(p);continue}if(!o||!p)continue;const[e,t,i]=n.split(" ").map(Number);null===(a=p.links)||void 0===a||a.push({source:e,target:t,flow:i})}return Object.assign(Object.assign({},u),{directed:m,nodes:c,modules:f})}function*s(e){for(let t=0;t<e.length;++t){const n=e[t];if(!n.startsWith("#")&&0!==n.length){if(n.startsWith("*"))break;yield[t,n]}}}function r(e){const t=["# path","# node_id","# state_id"];"string"==typeof e&&(e=e.split("\n"));for(const n of e){if(!n.startsWith("#"))break;if(t.some((e=>n.startsWith(e))))return n.slice(2).split(" ")}return null}function l(e){var t,n,o,i,s,r,l;const a={};if("string"==typeof e&&(e=e.split("\n")),e.length<8)return null;const u=null===(t=e[0].match(/^# (v\d+\.\d+\.\d+)/))||void 0===t?void 0:t[1];if(!u)return null;a.version=u;const c=null===(n=e[1].match(/^# (.+)/))||void 0===n?void 0:n[1];if(!c)return null;a.args=c;for(let t=2;t<e.length;++t){const n=e[t];if(!n.startsWith("#"))break;const u=null===(o=n.match(/^# started at (.+)/))||void 0===o?void 0:o[1];if(u){a.startedAt=u;continue}const c=null===(i=n.match(/^# completed in (.+) s/))||void 0===i?void 0:i[1];if(c){a.completedIn=Number(c);continue}const d=n.match(/^# partitioned into (\d+) levels with (\d+) top modules/);if(d){a.numLevels=Number(d[1]),a.numTopModules=Number(d[2]);continue}const f=null===(s=n.match(/^# codelength (.+) bits/))||void 0===s?void 0:s[1];if(f){a.codelength=Number(f);continue}const p=null===(r=n.match(/^# relative codelength savings (.+)%/))||void 0===r?void 0:r[1];if(p){a.relativeCodelengthSavings=Number(p)/100;continue}const h=null===(l=n.match(/^# bipartite start id (\d+)/))||void 0===l?void 0:l[1];h&&(a.bipartiteStartId=Number(h))}return a}t.parseClu=o,t.parseTree=i,t.parseHeader=l})(),e})()}));
1
+ !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.infomap=t():e.infomap=t()}(self,(function(){return(()=>{"use strict";var e={};return(()=>{var t=e;function n(e){return e.split(/\r?\n/)}Object.defineProperty(t,"__esModule",{value:!0}),t.parseHeader=t.parseTree=t.parseClu=t.parse=t.readFile=t.lines=t.extension=void 0,t.extension=function(e){var t;return null!==(t=e.split(".").pop())&&void 0!==t?t:null},t.lines=n,t.readFile=function(e){return new Promise(((t,n)=>{const o=new FileReader;o.onload=()=>t(o.result),o.onerror=n,o.readAsText(e)}))},t.parse=function(e,t,o=!1,s=!0){"string"==typeof e&&(e=n(e));const d=l(e);if(t&&"clu"===t||d.includes("module")){if("node_id"===d[0])return i(e,d,s);if("state_id"===d[0])return i(e,d,s)}else if(t&&"tree"===t||d.includes("path")){if("name"===d[d.length-2])return r(e,d,o,s);if("state_id"===d[d.length-2]||"layer_id"===d[d.length-1])return r(e,d,o,s)}throw new Error("File must be either a tree or a clu file")};const o={node_id:"id",module:"moduleId",flow:"flow",name:"name",state_id:"stateId",layer_id:"layerId",path:"path"};function i(e,t,i=!0){"string"==typeof e&&(e=n(e));const r=d(e,i);t||(t=l(e));const a=[];for(const[n,i]of s(e)){const e=i.split(" ").map(Number);if(e.length<t.length)continue;const r={};for(let n=0;n<t.length;++n){const i=t[n];r[o[i]]=e[n]}if(void 0===r.id||void 0===r.moduleId||void 0===r.flow)throw new Error(`Invalid node at line ${n+1}: ${i}`);a.push(r)}return Object.assign(Object.assign({},r),{nodes:a})}function r(e,t,i=!1,r=!0){var a;"string"==typeof e&&(e=n(e));const c=d(e,r);t||(t=l(e));const u=[];let f=0;for(const[n,i]of s(e)){f=n;const e=i.match(/[^\s"']+|"([^"]*)"/g);if(null===e||e.length<t.length)continue;const r={};for(let n=0;n<t.length;++n){const i=t[n],s=o[i];switch(i){case"path":r.path=e[n];break;case"name":r.name=e[n].slice(1,-1);break;case"node_id":r.id=Number(e[n]);break;case"state_id":case"layer_id":case"flow":r[s]=Number(e[n]);break;default:console.warn(`Unknown field ${i}`)}}if(void 0===r.path||void 0===r.flow||void 0===r.name||void 0===r.id)throw new Error(`Invalid node at line ${n+1}: ${i}`);u.push(r)}if(++f,e.length<=f+1)return Object.assign(Object.assign({},c),{nodes:u});const h=[];let p=null;const m=e[f];let v=!1;if(null==m?void 0:m.startsWith("*Links")){const e=m.trim().split(" ");v=e.length>1&&"directed"===e[1]}for(const[t,n]of function*(e,t=0){if(!(e.length<=t)&&e[t].startsWith("*Links"))for(let n=++t;n<e.length;++n){const t=e[n];0!==t.length&&(yield[n,t])}}(e,f)){if(n.startsWith("*Links")){const[e,t,...o]=n.split(" "),r="root"===t?[0]:t.split(":").map(Number),[s,l,d,a]=o.map(Number);p={path:r,enterFlow:s,exitFlow:l,numEdges:d,numChildren:a,links:i?[]:void 0},h.push(p);continue}if(!i||!p)continue;const[e,t,o]=n.split(" ").map(Number);null===(a=p.links)||void 0===a||a.push({source:e,target:t,flow:o})}return Object.assign(Object.assign({},c),{directed:v,nodes:u,modules:h})}function*s(e){for(let t=0;t<e.length;++t){const n=e[t];if(!n.startsWith("#")&&0!==n.length){if(n.startsWith("*"))break;yield[t,n]}}}function l(e){const t=/^#\s*(path|node_id|state_id)/;"string"==typeof e&&(e=n(e));for(const n of e){if(!n.startsWith("#"))break;if(t.test(n))return n.slice(1).trim().split(" ")}function o(e){return e.every((e=>!isNaN(Number(e))))}function i(e){throw new Error(`Invalid node line: ${e}`)}for(const t of e)if(!t.startsWith("#")){if(t.includes('"')){const e=t.match(/[^\s"']+|"([^"]*)"/g);e||i(t);const n=0,r=2,s=e.slice();s.splice(r,1),s.splice(n,1);const l=e[n].split(":");switch(o(s)&&o(l)||i(t),e.length){case 4:return["path","flow","name","node_id"];case 5:return["path","flow","name","state_id","node_id"];case 6:return["path","flow","name","state_id","node_id","layer_id"];default:i(t)}}else{const e=t.split(" ");switch(o(e)||i(t),e.length){case 4:return["node_id","module","flow"];case 5:return["state_id","module","flow","node_id"];case 6:return["state_id","module","flow","node_id","layer_id"];default:i(t)}}break}throw new Error("Could not parse node header")}function d(e,t=!0){var o,i,r,s,l,d,a,c,u,f;const h={};if("string"==typeof e&&(e=n(e)),e.length<8&&t)throw new Error("Expected file header to have at least 8 lines");const p=e[0]&&(null===(o=e[0].match(/^# (v\d+\.\d+\.\d+)/))||void 0===o?void 0:o[1]);if(p)h.version=p;else if(t)throw new Error("Could not parse version");const m=e[1]&&(null===(i=e[1].match(/^# (.+)/))||void 0===i?void 0:i[1]);if(m)h.args=m;else if(t)throw new Error("Could not parse args");for(let t=2;t<e.length;++t){const n=e[t];if(!n.startsWith("#"))break;const o=null===(r=n.match(/^# started at (.+)/))||void 0===r?void 0:r[1];if(o){h.startedAt=o;continue}const i=null===(s=n.match(/^# completed in (.+) s/))||void 0===s?void 0:s[1];if(i){h.completedIn=Number(i);continue}const p=n.match(/^# partitioned into (\d+) levels with (\d+) top modules/);if(p){h.numLevels=Number(p[1]),h.numTopModules=Number(p[2]);continue}const m=null===(l=n.match(/^# codelength (.+) bits/))||void 0===l?void 0:l[1];if(m){h.codelength=Number(m);continue}const v=null===(d=n.match(/^# relative codelength savings (.+)%/))||void 0===d?void 0:d[1];if(v){h.relativeCodelengthSavings=Number(v)/100;continue}const g=null===(a=n.match(/^# flow model (.+)/))||void 0===a?void 0:a[1];if(g){h.flowModel=g;continue}(null===(c=n.match(/^# higher order/))||void 0===c?void 0:c[1])&&(h.higherOrder=!0);const w=null===(u=n.match(/^# (state|physical) level/))||void 0===u?void 0:u[1];w&&(h.stateLevel="state"===w);const b=null===(f=n.match(/^# bipartite start id (\d+)/))||void 0===f?void 0:f[1];b&&(h.bipartiteStartId=Number(b))}if(h.higherOrder||(h.higherOrder=!1),t&&!(h.startedAt&&h.completedIn&&h.numLevels&&h.numTopModules&&h.codelength&&h.relativeCodelengthSavings))throw new Error("Could not parse file header");return h}t.parseClu=i,t.parseTree=r,t.parseHeader=d})(),e})()}));