File size: 3,540 Bytes
5ad11ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import json
from typing import List, Union
import numpy as np


class BaseReport:
    """The Abstract base class for all reports
    """

    def __init__(self, imgstack_files: str, coord_infos: str):

        self.imgstack = self._read_imgstack(imgstack_files)

        if isinstance(coord_infos, list):
            merged_json = dict()
            for coord_info in coord_infos:
                a_file = open(coord_info, "r")
                _contents = json.load(a_file)

                for k, v in _contents.items():
                    # print(v)
                    if not (k in merged_json):
                        # if the key isnt in the merged dictionary, then add and initialise it. This is python 3.6+ behaviour
                        merged_json[k] = v
                        continue

                    if not isinstance(merged_json[k], list) or isinstance(
                        merged_json[k], str
                    ):
                        # If there is unique data and the value at k in merged dict is not a list already, make it one
                        merged_json[k] = [merged_json[k]]
                    _test_dict = merged_json[k].copy()
                    _base_set = set(_test_dict)

                    if isinstance(v, list):
                        _test_dict.extend(v)
                    else:
                        _test_dict.append(v)

                    _test_set = set(_test_dict)
                    # print(_test_set)
                    # print(_base_set)
                    if len(_test_set) == len(_base_set):
                        # If there is nothing unique when making the set of the new content at that key and the old content, then skip
                        continue
                    if isinstance(v, list):
                        merged_json[k].extend(v)
                    else:
                        merged_json[k].append(v)

            self.coords = merged_json
            # print(self.coords)
        else:
            a_file = open(coord_infos, "r")
            self.coords = json.load(a_file)

        self.pdf = None

    # PDF controls--------------------------------------------
    def set_pdf(self, pdf) -> None:
        """Takes a PdfPages object and sets that as the internal variable 

        Args:
            pdf : the pdf pages object
        """
        self.pdf = pdf

    def get_pdf(self):
        """Return the pdfpages object

        Returns:
            _type_: _description_
        """
        return self.pdf

    def isPdf(self) -> bool:
        """Checks if the pdf has been set

        Returns:
            bool: _description_
        """
        if self.pdf is None:
            return False
        return True

    def closePdf(self) -> None:
        """Closes the pdf
        """
        if self.isPdf():
            self.pdf.close()
            self.pdf = None

    def _read_imgstack(self, imgstack_files: Union[List[str], str]):
        """Read in an image stack created by utils.fileIO function

        Args:
            imgstack_files (str): The 

        Returns:
            _type_: Loaded Image stack
        """
        if isinstance(imgstack_files, list):
            # If there is a list of images, then extend the image stack along the last dimension
            _imgstack = np.array(
                [np.load(image_stack) for image_stack in imgstack_files]
            )
            imgstack = np.stack(_imgstack, axis=-1)
        else:
            imgstack = np.load(imgstack_files)
        return imgstack