Skip to content

Commit

Permalink
use same format for overlaps as venn.js
Browse files Browse the repository at this point in the history
  • Loading branch information
stereobooster committed Jun 1, 2024
1 parent ceb63c0 commit 7be3e93
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
1 change: 0 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Fork of https://github.com/mjwybrow/edeap

## TODO

- [ ] use same format as `venn.js`
- [ ] old demo (host online)
- [ ] publish npm package
- update readme (uasgae example)
Expand Down
18 changes: 8 additions & 10 deletions packages/edeap/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,30 @@ export function parse(str: string): SetOverlaps {
.trim()
.split("\n")
.map((row) => {
const parsedRow: (string | number)[] = row.trim().split(/\s+/);
parsedRow[parsedRow.length - 1] = parseFloat(
parsedRow[parsedRow.length - 1] as string
);
return parsedRow;
const sets: string[] = row.trim().split(/\s+/);
const size = parseFloat(sets.pop()!);
return { sets, size };
});
}

export function check(sets: SetOverlaps, silent = true): SetOverlaps {
const seenBefore = new Set<string>();
return sets
.filter((row) => {
const test = row.length > 1;
const test = row.sets.length > 0;
if (!test && !silent)
throw new Error("Each row need to contain at least 2 elements");
return test;
})
.filter((row) => {
const proportion = row[row.length - 1] as number;
const proportion = row.size;
const test = !isNaN(proportion) && proportion > 0;
if (!test && !silent)
throw new Error("Set proportion suppose to be positive");
return test;
})
.filter((row) => {
const combination = row.slice(0, -1);
const combination = row.sets;
const combinationUnique = [...new Set(combination)];
const test1 = combination.length === combinationUnique.length;
if (!test1 && !silent) throw new Error("Duplicates in combination");
Expand All @@ -52,8 +50,8 @@ export function transform(sets: SetOverlaps): TransformedSets {
const proportions: number[] = [];

sets.forEach((row) => {
const zone = [...row];
const proportion = zone.pop() as number;
const zone = row.sets;
const proportion = row.size;

zone.forEach((contour) => contours.add(contour as string));
zones.push(zone as string[]);
Expand Down
7 changes: 6 additions & 1 deletion packages/edeap/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ export type State = {
zoneStrings: string[];
};

export type SetOverlaps = Array<Array<string | number>>;
export interface ISetOverlap {
sets: string[];
size: number;
}

export type SetOverlaps = ISetOverlap[];

export type TransformedSets = {
contours: string[];
Expand Down
36 changes: 18 additions & 18 deletions packages/edeap/tests/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,38 @@ test("parses basic example", () => {
c d 2
`;
expect(parse(str)).toEqual([
["a", "b", 1],
["c", "d", 2],
{ sets: ["a", "b"], size: 1 },
{ sets: ["c", "d"], size: 2 },
]);
});

test("frequency misses", () => {
const str = `a b`;
expect(parse(str)).toEqual([["a", NaN]]);
expect(parse(str)).toEqual([{ sets: ["a"], size: NaN }]);
});

test("filters out rows with less than 2 elements", () => {
const str: SetOverlaps = [[], ["a"]];
const str: SetOverlaps = [{ sets: [], size: 1 }];
expect(check(str)).toEqual([]);
});

test("filters out rows with wrong sizes", () => {
const str: SetOverlaps = [
["a", NaN],
["b", 0],
["c", -1],
{ sets: ["a"], size: NaN },
{ sets: ["b"], size: 0 },
{ sets: ["c"], size: -1 },
];
expect(check(str)).toEqual([]);
});

test.skip("filters out duplicate rows", () => {
const str: SetOverlaps = [
["a", 1],
["a", 2],
["a", "b", 3],
["a", "b", 4],
["b", "a", 5],
["a", "b", "b", 6],
{ sets: ["a"], size: 1 },
{ sets: ["a"], size: 2 },
{ sets: ["a", "b"], size: 3 },
{ sets: ["a", "b"], size: 4 },
{ sets: ["b", "a"], size: 5 },
{ sets: ["a", "b", "b"], size: 6 },
];
expect(check(str)).toEqual([
["a", 1],
Expand All @@ -50,8 +50,8 @@ test.skip("filters out duplicate rows", () => {
test("transforms sets", () => {
expect(
transform([
["a", "b", 1],
["c", "d", 2],
{ sets: ["a", "b"], size: 1 },
{ sets: ["c", "d"], size: 2 },
])
).toEqual({
contours: ["a", "b", "c", "d"],
Expand Down Expand Up @@ -141,9 +141,9 @@ test("example2", () => {

test("calculateInitial", () => {
const x = transform([
["a", 1],
["a", "b", 1],
["a", "b", "c", 1],
{ sets: ["a"], size: 1 },
{ sets: ["a", "b"], size: 1 },
{ sets: ["a", "b", "c"], size: 1 },
]);
expect(calculateInitial(x)).toEqual({
contourAreas: [1, 0.6666666666666666, 0.3333333333333333],
Expand Down

0 comments on commit 7be3e93

Please sign in to comment.