data.py 3.94 KB
Newer Older
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
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright(C) 2013-2024 Max-Planck-Society
# Author: Matteo Guardiani
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.

import nifty7 as ift
import numpy as np

from binner import Bin1D, Bin2D, data_filter_x
from data_utilities import read_in


class Data:
    def __init__(self, npix_age, npix_ll, setup, reshuffle_iterator, inversion_parameter, data_csv_file):
        if not isinstance(npix_age, int):
            raise TypeError("Number of pixels argument needs to be of type int.")

        if not isinstance(npix_ll, int):
            raise TypeError("Number of pixels argument needs to be of type int.")

        if not isinstance(setup, dict):
            raise TypeError("Setup argument needs to be of type dict.")

        if not isinstance(reshuffle_iterator, int):
            raise TypeError("Reshuffle iterator argument needs to be of type int.")

        if not isinstance(inversion_parameter, bool):
            raise TypeError("Inversion parameter argument needs to be of type bool.")

        self.npix_age = npix_age
        self.npix_ll = npix_ll
        self.setup = setup
        self.reshuffle_iterator = reshuffle_iterator
        self.inversion_par = inversion_parameter
        self.data_csv_file = data_csv_file

    def zero_pad(self):
        ext_npix_age = 2 * self.npix_age
        ext_npix_ll = 2 * self.npix_ll
        return self.create_spaces(ext_npix_age, ext_npix_ll, self.inversion_par)

    def load(self):
        # Loads, filters and reshuffles data
        self.zero_pad()
        threshold = self.setup["threshold"]
        age, ll = read_in(self.data_csv_file)
        ll, age = data_filter_x(threshold, ll, age)  # For Cobas data filter was set at 3.3 for log load FIXXXXX!!!
        # NOW 3.85 (other param 5.4)

        if not self.reshuffle_iterator == 0:
            self.reshuffle_data(ll, self.reshuffle_iterator)
        return age, ll

    def bin(self):
        age, ll = self.load()
        if self.inversion_par:
            data, ll_edges, age_edges = Bin2D(ll, age, self.npix_ll, self.npix_age)
        else:
            data, age_edges, ll_edges = Bin2D(age, ll, self.npix_age, self.npix_ll)
        data = np.array(data, dtype=np.int64)
        return data, age_edges, ll_edges

    def coordinates(self):
        age, ll = self.load()
        if self.inversion_par:
            age_coordinates = self.obtain_coordinates(ll, self.npix_ll)
            ll_coordinates = self.obtain_coordinates(age, self.npix_age)
        else:
            age_coordinates = self.obtain_coordinates(age, self.npix_age)
            ll_coordinates = self.obtain_coordinates(ll, self.npix_ll)
        return age_coordinates, ll_coordinates

    @staticmethod
    def create_spaces(npix_x, npix_y, inv):
        if inv:
            position_space = ift.RGSpace((npix_y, npix_x))
            sp1 = ift.RGSpace(npix_y)
            sp2 = ift.RGSpace(npix_x)
        else:
            position_space = ift.RGSpace((npix_x, npix_y))
            sp1 = ift.RGSpace(npix_x)
            sp2 = ift.RGSpace(npix_y)
        return position_space, sp1, sp2

    @staticmethod
    def reshuffle_data(data, seed):
        from sklearn.utils import shuffle
        return shuffle(data, random_state=seed)

    @staticmethod
    def obtain_coordinates(x, npix):
        binned_x, x_edges = Bin1D(x, npix)
106
        return 0.5 * (x_edges[:-1] + x_edges[1:])