Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate session.volview.zip state files from 2.1.0 to 3.0.0 #524

Merged
merged 5 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/core/remote/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { Maybe } from '@/src/types';
import { Deferred, defer } from '@/src/utils';
import { debug } from '@/src/utils/loggers';
import pipe from '@/src/utils/pipe';
import { flow } from '@/src/utils/functional';
import { nanoid } from 'nanoid';
import { Socket, io } from 'socket.io-client';
import { z } from 'zod';
Expand Down Expand Up @@ -160,13 +160,8 @@ export default class RpcClient {
this.socket.on(STREAM_RESULT_EVENT, this.onStreamResultEvent);
}

protected serialize = (obj: any) => {
return pipe(...this.serializers)(obj);
};

protected deserialize = (obj: any) => {
return pipe(...this.deserializers)(obj);
};
protected serialize = flow(...this.serializers);
protected deserialize = flow(...this.deserializers);

async connect(uri: string) {
await this.disconnect();
Expand Down
135 changes: 131 additions & 4 deletions src/io/import/processors/restoreStateFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from '@/src/io/import/dataSource';
import { MANIFEST, isStateFile } from '@/src/io/state-file';
import { ensureError, partition } from '@/src/utils';
import { pipe } from '@/src/utils/functional';
import Pipeline, { PipelineContext } from '@/src/core/pipeline';
import { Awaitable } from '@vueuse/core';
import doneWithDataSource from '@/src/io/import/processors/doneWithDataSource';
Expand All @@ -26,14 +27,140 @@ import {
makeImageSelection,
useDatasetStore,
} from '@/src/store/datasets';
import { useSegmentGroupStore } from '@/src/store/segmentGroups';
import {
makeDefaultSegmentGroupName,
useSegmentGroupStore,
} from '@/src/store/segmentGroups';
import { useToolStore } from '@/src/store/tools';
import { useLayersStore } from '@/src/store/datasets-layers';
import { extractFilesFromZip } from '@/src/io/zip';
import downloadUrl from '@/src/io/import/processors/downloadUrl';
import updateFileMimeType from '@/src/io/import/processors/updateFileMimeType';
import extractArchiveTarget from '@/src/io/import/processors/extractArchiveTarget';

const LABELMAP_PALETTE_2_1_0 = {
'1': {
value: 1,
name: 'Segment 1',
color: [153, 153, 0, 255],
},
'2': {
value: 2,
name: 'Segment 2',
color: [76, 76, 0, 255],
},
'3': {
value: 3,
name: 'Segment 3',
color: [255, 255, 0, 255],
},
'4': {
value: 4,
name: 'Segment 4',
color: [0, 76, 0, 255],
},
'5': {
value: 5,
name: 'Segment 5',
color: [0, 153, 0, 255],
},
'6': {
value: 6,
name: 'Segment 6',
color: [0, 255, 0, 255],
},
'7': {
value: 7,
name: 'Segment 7',
color: [76, 0, 0, 255],
},
'8': {
value: 8,
name: 'Segment 8',
color: [153, 0, 0, 255],
},
'9': {
value: 9,
name: 'Segment 9',
color: [255, 0, 0, 255],
},
'10': {
value: 10,
name: 'Segment 10',
color: [0, 76, 76, 255],
},
'11': {
value: 11,
name: 'Segment 11',
color: [0, 153, 153, 255],
},
'12': {
value: 12,
name: 'Segment 12',
color: [0, 255, 255, 255],
},
'13': {
value: 13,
name: 'Segment 13',
color: [0, 0, 76, 255],
},
'14': {
value: 14,
name: 'Segment 14',
color: [0, 0, 153, 255],
},
};

const migrateOrPass =
(versions: Array<string>, migrationFunc: (manifest: any) => any) =>
(inputManifest: any) => {
if (versions.includes(inputManifest.version)) {
return migrationFunc(inputManifest);
}
return inputManifest;
};

const migrateBefore210 = (inputManifest: any) => {
const manifest = JSON.parse(JSON.stringify(inputManifest));
manifest.version = '2.1.0';
return manifest;
};

const migrate210To300 = (inputManifest: any) => {
const manifest = JSON.parse(JSON.stringify(inputManifest));
manifest.tools.paint.activeSegmentGroupID =
inputManifest.tools.paint.activeLabelmapID;
delete manifest.tools.paint.activeLabelmapID;

const order = Object.keys(LABELMAP_PALETTE_2_1_0).map((key) => Number(key));
manifest.labelMaps = inputManifest.labelMaps.map(
(labelMap: any, index: number) => ({
id: labelMap.id,
path: labelMap.path,
metadata: {
parentImage: labelMap.parent,
name: makeDefaultSegmentGroupName('My Image', index),
segments: {
order,
byValue: LABELMAP_PALETTE_2_1_0,
},
},
})
);

manifest.version = '3.0.0';
return manifest;
};

const migrateManifest = (manifestString: string) => {
const inputManifest = JSON.parse(manifestString);
return pipe(
inputManifest,
migrateOrPass(['1.1.0', '1.0.0', '0.5.0'], migrateBefore210),
migrateOrPass(['2.1.0'], migrate210To300)
);
};

const resolveUriSource: ImportHandler = async (dataSource, { extra, done }) => {
const { uriSrc } = dataSource;

Expand Down Expand Up @@ -234,9 +361,9 @@ const restoreStateFile: ImportHandler = async (
throw new Error('State file does not have exactly 1 manifest');
}

const manifest = ManifestSchema.parse(
JSON.parse(await manifests[0].file.text())
);
const manifestString = await manifests[0].file.text();
const migrated = migrateManifest(manifestString);
const manifest = ManifestSchema.parse(migrated);

// We restore the view first, so that the appropriate watchers are triggered
// in the views as the data is loaded
Expand Down
42 changes: 42 additions & 0 deletions src/utils/functional.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
type FlowFunction<T> = (o: T) => T;

export function flow<T>(...fns: Array<FlowFunction<T>>) {
return (input: T) => fns.reduce((result, fn) => fn(result), input);
}

// Pipe code from
// https://dev.to/ecyrbe/how-to-use-advanced-typescript-to-define-a-pipe-function-381h
// Changed second parameter to rest/spread argument.
type AnyFunc = (...arg: any) => any;

type LastFnReturnType<F extends Array<AnyFunc>, Else = never> = F extends [
...any[],
(...arg: any) => infer R
]
? R
: Else;

type PipeArgs<F extends AnyFunc[], Acc extends AnyFunc[] = []> = F extends [
(...args: infer A) => infer B
]
? [...Acc, (...args: A) => B]
: F extends [(...args: infer A) => any, ...infer Tail]
? Tail extends [(arg: infer B) => any, ...any[]]
? PipeArgs<Tail, [...Acc, (...args: A) => B]>
: Acc
: Acc;

// Example:
// const myNumber = pipe(
// "1",
// (a: string) => Number(a),
// (c: number) => c + 1,
// (d: number) => `${d}`,
// (e: string) => Number(e)
// );
export function pipe<F extends AnyFunc[]>(
arg: Parameters<F[0]>[0],
...fns: PipeArgs<F> extends F ? F : PipeArgs<F>
): LastFnReturnType<F, ReturnType<F[0]>> {
return (fns.slice(1) as AnyFunc[]).reduce((acc, fn) => fn(acc), fns[0](arg));
}
5 changes: 0 additions & 5 deletions src/utils/pipe.ts

This file was deleted.

Loading
Loading