Blame view

flaskr/laws/travel_emission_lerp_fit.py 1.9 KB
8a74d49c   Antoine Goutenoir   Add the model usi...
1
2
import numpy as np
from .travel_emission_linear_fit import EmissionModel as BaseEmissionModel
8a74d49c   Antoine Goutenoir   Add the model usi...
3
4
5
6
7


class EmissionModel(BaseEmissionModel):

    def apply_scaling_law(self, distance, config):
24345b90   Antoine Goutenoir   Add some explanat...
8
9
10
        """
        This interpolation algorithm will handle duplicates
        as one would expect it to do.
e34a06a0   Antoine Goutenoir   Review.
11
12
        For triplicates and more it will only use the outmost values
        in the list and ignore the middle ones.
24345b90   Antoine Goutenoir   Add some explanat...
13

e34a06a0   Antoine Goutenoir   Review.
14
        And you do not have to provide them in order,
24345b90   Antoine Goutenoir   Add some explanat...
15
16
        as this list is also sorted, and thankfully :

e34a06a0   Antoine Goutenoir   Review.
17
18
        > In Python, when you sort equal values,
        > they will retain their original order in the output.
24345b90   Antoine Goutenoir   Add some explanat...
19
20
21
22
23

        :param distance: float
        :param config:
        :return: float
        """
8a74d49c   Antoine Goutenoir   Add the model usi...
24
25
26
27
28
29
30
31

        assert config.points
        assert len(config.points) > 0

        footprint = None

        sample_points = sorted(config.points, key=lambda p: float(p[0]))

0daf0dc8   Antoine Goutenoir   Review.
32
33
        # Numpy!

8a74d49c   Antoine Goutenoir   Add the model usi...
34
35
36
37
38
39
        previous_point = [0, 0]
        for i, sample_point in enumerate(sample_points):
            if distance <= sample_point[0]:
                t = 0
                if sample_point[0] != previous_point[0]:
                    t = (distance - previous_point[0]) * 1.0 \
24345b90   Antoine Goutenoir   Add some explanat...
40
                                        / \
8a74d49c   Antoine Goutenoir   Add the model usi...
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
                        (sample_point[0] - previous_point[0])

                footprint = previous_point[1] + t * (sample_point[1] - previous_point[1])
                previous_point = sample_point
                break

            previous_point = sample_point

        if footprint is None:
            if len(config.points) == 1:
                last = sample_points[0]
                penu = [0, 0]
            else:
                last = sample_points[-1]
                penu = sample_points[-2]

            t = 0
            if last[0] != penu[0]:
                t = (distance - last[0]) * 1.0 / (last[0] - penu[0])

            footprint = last[1] + t * (last[1] - penu[1])

        return footprint