Diff of /scvae/analyses/images.py [000000] .. [5ad33c]

Switch to unified view

a b/scvae/analyses/images.py
1
# ======================================================================== #
2
#
3
# Copyright (c) 2017 - 2020 scVAE authors
4
#
5
# Licensed under the Apache License, Version 2.0 (the "License");
6
# you may not use this file except in compliance with the License.
7
# You may obtain a copy of the License at
8
#
9
#    http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
16
#
17
# ======================================================================== #
18
19
import os
20
21
import numpy
22
import PIL
23
import scipy.sparse
24
25
from scvae.analyses.figures import saving
26
27
IMAGE_EXTENSION = ".png"
28
DEFAULT_NUMBER_OF_RANDOM_EXAMPLES_FOR_COMBINED_IMAGES = 100
29
30
31
def combine_images_from_data_set(data_set, indices=None,
32
                                 number_of_random_examples=None, name=None):
33
34
    image_name = saving.build_figure_name("random_image_examples", name)
35
    random_state = numpy.random.RandomState(13)
36
37
    if indices is not None:
38
        n_examples = len(indices)
39
        if number_of_random_examples is not None:
40
            n_examples = min(n_examples, number_of_random_examples)
41
            indices = random_state.permutation(indices)[:n_examples]
42
    else:
43
        if number_of_random_examples is not None:
44
            n_examples = number_of_random_examples
45
        else:
46
            n_examples = DEFAULT_NUMBER_OF_RANDOM_EXAMPLES_FOR_COMBINED_IMAGES
47
        indices = random_state.permutation(
48
            data_set.number_of_examples)[:n_examples]
49
50
    if n_examples == 1:
51
        image_name = saving.build_figure_name("image_example", name)
52
    else:
53
        image_name = saving.build_figure_name("image_examples", name)
54
55
    width, height = data_set.feature_dimensions
56
57
    examples = data_set.values[indices]
58
    if scipy.sparse.issparse(examples):
59
        examples = examples.A
60
    examples = examples.reshape(n_examples, width, height)
61
62
    column = int(numpy.ceil(numpy.sqrt(n_examples)))
63
    row = int(numpy.ceil(n_examples / column))
64
65
    image = numpy.zeros((row * width, column * height))
66
67
    for m in range(n_examples):
68
        c = int(m % column)
69
        r = int(numpy.floor(m / column))
70
        rows = slice(r*width, (r+1)*width)
71
        columns = slice(c*height, (c+1)*height)
72
        image[rows, columns] = examples[m]
73
74
    return image, image_name
75
76
77
def save_image(image, name, directory):
78
79
    if not os.path.exists(directory):
80
        os.makedirs(directory)
81
82
    minimum = image.min()
83
    maximum = image.max()
84
    if 0 < minimum and minimum < 1 and 0 < maximum and maximum < 1:
85
        rescaled_image = 255 * image
86
    else:
87
        rescaled_image = (255 / (maximum - minimum) * (image - minimum))
88
89
    image = PIL.Image.fromarray(rescaled_image.astype(numpy.uint8))
90
91
    name += IMAGE_EXTENSION
92
    image_path = os.path.join(directory, name)
93
    image.save(image_path)