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