Ejemplo n.º 1
0
 def setUp(self):
     dimensions = [
         numpy.array(range(1, 5), dtype=numpy.float64),
         numpy.array(range(1, 6), dtype=numpy.float64),
         numpy.array(range(1, 4), dtype=numpy.float64)
     ]
     values = numpy.ones(60)
     self.lut = LookupTable(values, dimensions)
Ejemplo n.º 2
0
 def test_get_values_big(self):
     dimensions = [
         numpy.array(range(1, 5), dtype=numpy.float64),
         numpy.array(range(1, 6), dtype=numpy.float64),
         numpy.array(range(1, 4), dtype=numpy.float64)
     ]
     values = numpy.array(range(0, 120))
     self.lut = LookupTable(values, dimensions, 2)
     result = self.lut.getValues(
         [numpy.ones(300000),
          numpy.ones(300000),
          numpy.ones(300000)])
     self.assertEqual((2, 300000), result.shape)
Ejemplo n.º 3
0
 def test_get_value_length_2(self):
     dimensions = [
         numpy.array(range(1, 5), dtype=numpy.float64),
         numpy.array(range(1, 6), dtype=numpy.float64),
         numpy.array(range(1, 4), dtype=numpy.float64)
     ]
     values = numpy.array(range(0, 120))
     self.lut = LookupTable(values, dimensions, 2)
     result = self.lut.getValue([1, 1, 1])
     self.assertEqual((2, ), result.shape)
     [a, b] = result
     self.assertEqual(0, a)
     self.assertEqual(1, b)
Ejemplo n.º 4
0
 def __init__(self, a0, a1, needs_lut):
     self.needs_lut = needs_lut
     if not needs_lut:
         self.a0 = a0
         self.a1 = a1
     else:
         season = numpy.array(range(
             0, 4), dtype=numpy.float64)  # spring, summer, autumn, winter
         height = numpy.array([400, 800, 1000])  # height in meter
         zenith = numpy.array(range(
             5, 56, 5), dtype=numpy.float64)  # 5 - 55deg in 5deg steps
         dimensions = [season, height, zenith]
         mono_coeff = Mono()
         values = numpy.array(mono_coeff.values, dtype=numpy.float64)
         self.lut = LookupTable(values, dimensions, 2)
         self.check = None
Ejemplo n.º 5
0
 def __init__(self, a0, a1, a2, a3, needs_lut):
     if needs_lut:
         self.has_lut = True
         season = numpy.array(range(
             0, 4), dtype=numpy.float64)  # spring, summer, autumn, winter
         height = numpy.array([400, 800, 1000])  # height in meter
         zenith = numpy.array(range(
             5, 56, 5), dtype=numpy.float64)  # 5 - 55deg in 5deg steps
         dimensions = [season, height, zenith]
         #  TODO needs to be loaded from file!
         values = numpy.tile([3.079654, 0.988938, 1.867209, -0.434251],
                             len(season) * len(zenith) * len(height))
         self.lut = LookupTable(values, dimensions, 4)
     else:
         self.has_lut = False
         self.a0 = a0
         self.a1 = a1
         self.a2 = a2
         self.a3 = a3
     self.check = None
Ejemplo n.º 6
0
 def test_get_values_length_2(self):
     dimensions = [
         numpy.array(range(1, 5), dtype=numpy.float64),
         numpy.array(range(1, 6), dtype=numpy.float64),
         numpy.array(range(1, 4), dtype=numpy.float64)
     ]
     values = numpy.array(range(0, 120))
     self.lut = LookupTable(values, dimensions, 2)
     result = self.lut.getValues([
         numpy.array([1, 1, 1]),
         numpy.array([1, 1, 1]),
         numpy.array([1, 1, 1])
     ])
     self.assertEqual((2, 3), result.shape)
     [[a, b, c], [d, e, f]] = result
     self.assertEqual(0, a)
     self.assertEqual(0, b)
     self.assertEqual(0, c)
     self.assertEqual(1, d)
     self.assertEqual(1, e)
     self.assertEqual(1, f)
Ejemplo n.º 7
0
 def create_wrs_lut(self):
     paths = numpy.array(range(1, 234), dtype=numpy.float64)
     rows = numpy.array(range(1, 249), dtype=numpy.float64)
     # points=['CTR LAT', 'CTR LON', 'UL LAT', 'UL LON', 'UR LAT', 'UR LON', 'LL LAT', 'LL LON', 'LR LAT', 'LR LON']
     points = numpy.array(range(0, 10), dtype=numpy.float64)
     dimensions = [paths, rows, points]
     # file = File('wrs.py')
     # props = Properties()
     # inStream = file.toURI().toURL().openStream()
     # props.clear()
     # props.load(inStream)
     # values = numpy.array(props.getProperty('values').split(','), dtype=numpy.float64)
     wrs = Wrs()
     values = numpy.array(wrs.values, dtype=numpy.float64)
     return LookupTable(values, dimensions)
Ejemplo n.º 8
0
class MonoWindowAlgo:
    def __init__(self, a0, a1, needs_lut):
        self.needs_lut = needs_lut
        if not needs_lut:
            self.a0 = a0
            self.a1 = a1
        else:
            season = numpy.array(range(
                0, 4), dtype=numpy.float64)  # spring, summer, autumn, winter
            height = numpy.array([400, 800, 1000])  # height in meter
            zenith = numpy.array(range(
                5, 56, 5), dtype=numpy.float64)  # 5 - 55deg in 5deg steps
            dimensions = [season, height, zenith]
            mono_coeff = Mono()
            values = numpy.array(mono_coeff.values, dtype=numpy.float64)
            self.lut = LookupTable(values, dimensions, 2)
            self.check = None

    def get_season(self, start_date):
        utils = Utils()
        return utils.get_season(start_date)

    def compute_lswt(self, data, season, height, zenith_data):
        if not self.needs_lut:
            return self.a0 * data + self.a1
        else:
            lswt = numpy.zeros(numpy.shape(data))
            lswt[numpy.where(numpy.isnan(data))] = Float.NaN

            season = season[numpy.where(~numpy.isnan(data))]
            height = height[numpy.where(~numpy.isnan(data))]
            zenith_data = zenith_data[numpy.where(~numpy.isnan(data))]

            size = numpy.shape(season)
            if size != (0, ):
                if len(size) == 2:
                    (shape_x, shape_y) = size
                    result = self.lut.getValues([
                        season[::2, ::2], height[::2, ::2],
                        zenith_data[::2, ::2]
                    ])
                    [a0, a1] = result
                    a0 = numpy.repeat(numpy.repeat(a0, 2, axis=0), 2, axis=1)
                    a1 = numpy.repeat(numpy.repeat(a1, 2, axis=0), 2, axis=1)
                    a0 = a0[:shape_x, :shape_y]
                    a1 = a1[:shape_x, :shape_y]
                elif len(size) == 1:
                    (shape_x, ) = size
                    result = self.lut.getValues(
                        [season[::2], height[::2], zenith_data[::2]])
                    [a0, a1] = result
                    a0 = numpy.repeat(a0, 2)
                    a1 = numpy.repeat(a1, 2)
                    a0 = a0[:shape_x]
                    a1 = a1[:shape_x]

                lswt[numpy.where(~numpy.isnan(
                    data))] = a0 * data[numpy.where(~numpy.isnan(data))] + a1
            return lswt

    def compute_flags(self, lswt, visible, nir, bt, sat_za, sun_za, rel_az,
                      lwm, cmsk, height, width, sattype):
        self.check = QualityCheck(lswt, visible, nir, None, bt, None, sat_za,
                                  sun_za, rel_az, lwm, cmsk, height, width,
                                  sattype)
        return self.check.check_quality()

    def compute_quality_index(self, flags):
        return self.check.get_quality_mask(flags)
Ejemplo n.º 9
0
class SplitWindowAlgo:
    def __init__(self, a0, a1, a2, a3, needs_lut):
        if needs_lut:
            self.has_lut = True
            season = numpy.array(range(
                0, 4), dtype=numpy.float64)  # spring, summer, autumn, winter
            height = numpy.array([400, 800, 1000])  # height in meter
            zenith = numpy.array(range(
                5, 56, 5), dtype=numpy.float64)  # 5 - 55deg in 5deg steps
            dimensions = [season, height, zenith]
            #  TODO needs to be loaded from file!
            values = numpy.tile([3.079654, 0.988938, 1.867209, -0.434251],
                                len(season) * len(zenith) * len(height))
            self.lut = LookupTable(values, dimensions, 4)
        else:
            self.has_lut = False
            self.a0 = a0
            self.a1 = a1
            self.a2 = a2
            self.a3 = a3
        self.check = None

    def get_season(self, start_date):
        utils = Utils()
        return utils.get_season(start_date)

    def compute_lswt_lut(self, lower_data, upper_data, zenith_data, season,
                         height):
        lswt = numpy.zeros(numpy.shape(lower_data))
        lswt[numpy.where(
            numpy.logical_or(numpy.isnan(lower_data),
                             numpy.isnan(upper_data)))] = Float.NaN

        season = season[numpy.where(
            numpy.logical_and(~numpy.isnan(lower_data),
                              ~numpy.isnan(upper_data)))]
        zenith_data = zenith_data[numpy.where(
            numpy.logical_and(~numpy.isnan(lower_data),
                              ~numpy.isnan(upper_data)))]
        height = height[numpy.where(
            numpy.logical_and(~numpy.isnan(lower_data),
                              ~numpy.isnan(upper_data)))]

        size = numpy.shape(season)
        if size != (0, ):
            [self.a0, self.a1, self.a2,
             self.a3] = self.lut.getValues([season, height, zenith_data])
            lswt[numpy.where(
                numpy.logical_and(
                    ~numpy.isnan(lower_data),
                    ~numpy.isnan(upper_data)))] = self.compute_lswt(
                        lower_data[numpy.where(
                            numpy.logical_and(~numpy.isnan(lower_data),
                                              ~numpy.isnan(upper_data)))],
                        upper_data[numpy.where(
                            numpy.logical_and(~numpy.isnan(lower_data),
                                              ~numpy.isnan(upper_data)))],
                        zenith_data)
        return lswt

    def compute_lswt(self, lower_data, upper_data, zenith_data):
        lswt = self.a0 + self.a1 * lower_data + self.a2 * (lower_data - upper_data) + \
               self.a3 * (1 - 1 / (numpy.cos(zenith_data * (numpy.math.pi / 180.0)))) * (lower_data - upper_data)
        return lswt

    def compute_flags(self, lswt, visible, nir, bt3, lower_bt, upper_bt,
                      sat_za, sun_za, rel_az, lwm, cmsk, height, width,
                      sattype):
        self.check = QualityCheck(lswt, visible, nir, bt3, lower_bt, upper_bt,
                                  sat_za, sun_za, rel_az, lwm, cmsk, height,
                                  width, sattype)
        return self.check.check_quality()

    def compute_quality_index(self, flags):
        return self.check.get_quality_mask(flags)
        print('Mean game performance: %f' % mean_performance)
    return Q


# create a matrix of all input states for blackjack
X = []
for player in range(2, 22):
    for dealer in range(1, 22):
        for ace in range(2):
            for action in range(2):
                X.append([player, dealer, ace, action])
X = np.array(X)

# ## Incremental training
# # create and initialize Q (state-action value) function
Q_random = LookupTable()
Q_random.fit(X, np.random.random(X.shape[0]))
print('Mean reward per game for random agent (Q before training): %f' %
      np.mean([play(env, Q_random) for i in range(100000)]))
incremental_game_means = []
# Q = train_Q_incrementally(Q_random, measure_every=10000, game_means=incremental_game_means)
# print('Mean reward for game with trained agent: %f' % np.mean([play(env, Q) for i in range(100000)]))
# print('Mean reward for play with Q_ideal (Vegas blackjack strategy card): %f' % np.mean([play(env, Q_ideal) for i in range(100000)]))
# # Fit an SVM regression model to the lookup table version of Q to see what's
# # the best we can do with an SVM
# Q_SVM = sklearn.svm.SVR()
# Q_SVM.fit(X, Q.predict(X))
# print('Mean reward for best-fit SVM regression (SVR fit to the lookup table): %f' % np.mean([play(env, Q_SVM) for i in range(100000)]))
#
#
## Batch training
Ejemplo n.º 11
0
class TestLookupTable(unittest.TestCase):
    def setUp(self):
        dimensions = [
            numpy.array(range(1, 5), dtype=numpy.float64),
            numpy.array(range(1, 6), dtype=numpy.float64),
            numpy.array(range(1, 4), dtype=numpy.float64)
        ]
        values = numpy.ones(60)
        self.lut = LookupTable(values, dimensions)

    def test_get_value(self):
        self.assertEqual(1, self.lut.getValue([4, 3, 2]))

    def test_get_values(self):
        coordinates = [
            numpy.array(range(2, 5), dtype=numpy.float64),
            numpy.array(range(2, 5), dtype=numpy.float64),
            numpy.array(range(1, 4), dtype=numpy.float64)
        ]
        result = self.lut.getValues(coordinates)
        self.assertEqual(coordinates[0].shape, result.shape)
        self.assertEqual(1, result[0])

    def test_get_values_mulit_index(self):
        coordinates = [
            numpy.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]]),
            numpy.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]]),
            numpy.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])
        ]
        result = self.lut.getValues(coordinates)
        self.assertEqual(coordinates[0].shape, result.shape)
        self.assertEqual(1, result[0][0])

    def test_get_value_length_2(self):
        dimensions = [
            numpy.array(range(1, 5), dtype=numpy.float64),
            numpy.array(range(1, 6), dtype=numpy.float64),
            numpy.array(range(1, 4), dtype=numpy.float64)
        ]
        values = numpy.array(range(0, 120))
        self.lut = LookupTable(values, dimensions, 2)
        result = self.lut.getValue([1, 1, 1])
        self.assertEqual((2, ), result.shape)
        [a, b] = result
        self.assertEqual(0, a)
        self.assertEqual(1, b)

    def test_get_values_length_2(self):
        dimensions = [
            numpy.array(range(1, 5), dtype=numpy.float64),
            numpy.array(range(1, 6), dtype=numpy.float64),
            numpy.array(range(1, 4), dtype=numpy.float64)
        ]
        values = numpy.array(range(0, 120))
        self.lut = LookupTable(values, dimensions, 2)
        result = self.lut.getValues([
            numpy.array([1, 1, 1]),
            numpy.array([1, 1, 1]),
            numpy.array([1, 1, 1])
        ])
        self.assertEqual((2, 3), result.shape)
        [[a, b, c], [d, e, f]] = result
        self.assertEqual(0, a)
        self.assertEqual(0, b)
        self.assertEqual(0, c)
        self.assertEqual(1, d)
        self.assertEqual(1, e)
        self.assertEqual(1, f)

    def test_get_values_big(self):
        dimensions = [
            numpy.array(range(1, 5), dtype=numpy.float64),
            numpy.array(range(1, 6), dtype=numpy.float64),
            numpy.array(range(1, 4), dtype=numpy.float64)
        ]
        values = numpy.array(range(0, 120))
        self.lut = LookupTable(values, dimensions, 2)
        result = self.lut.getValues(
            [numpy.ones(300000),
             numpy.ones(300000),
             numpy.ones(300000)])
        self.assertEqual((2, 300000), result.shape)