Mercurial > repos > imgteam > 3d_tensor_feature_dimension_reduction
comparison 3d_tensor_feature_dimension_reduction.py @ 0:e8f64a98cbc6 draft default tip
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/3d_tensor_feature_dimension_reduction/ commit e82400162e337b36c29d6e79fb2deb9871475397"
author | imgteam |
---|---|
date | Thu, 20 Jan 2022 00:45:01 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e8f64a98cbc6 |
---|---|
1 """ | |
2 Copyright 2022 Biomedical Computer Vision Group, Heidelberg University. | |
3 | |
4 Distributed under the MIT license. | |
5 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT | |
6 | |
7 """ | |
8 | |
9 import argparse | |
10 import warnings | |
11 | |
12 import h5py | |
13 import numpy as np | |
14 import tifffile | |
15 import umap | |
16 | |
17 | |
18 def feature_dimension_reduction(tensor_fn, tiff_fn, nCh=5): | |
19 with h5py.File(tensor_fn, 'r') as hf: | |
20 ts = np.array(hf[list(hf.keys())[0]]) | |
21 | |
22 assert len(ts.shape) == 3 and ts.shape[-1] > nCh, \ | |
23 'the input tensor data must be three-dimensional' | |
24 | |
25 embedding = umap.UMAP(n_components=nCh).fit_transform(np.reshape(ts, (-1, ts.shape[-1]))) | |
26 img = np.reshape(embedding, (ts.shape[0], ts.shape[1], -1)).astype(np.float32) | |
27 | |
28 with warnings.catch_warnings(): | |
29 warnings.simplefilter("ignore") | |
30 tifffile.imwrite(tiff_fn, np.transpose(img, (2, 0, 1)), imagej=True) | |
31 | |
32 | |
33 if __name__ == "__main__": | |
34 parser = argparse.ArgumentParser(description="Dimensionality reduction for features (channels) of 3D tensor using UMAP") | |
35 parser.add_argument("tensor_fn", help="Path to the 3D tensor data") | |
36 parser.add_argument("nCh", type=int, help="The reduced dimension of features") | |
37 parser.add_argument("tiff_fn", help="Path to the output file") | |
38 args = parser.parse_args() | |
39 feature_dimension_reduction(args.tensor_fn, args.tiff_fn, args.nCh) |