Skip to content

Instantly share code, notes, and snippets.

@kondors1995
Created August 5, 2019 14:05
Show Gist options
  • Save kondors1995/7a0078029562bcc9527823e7113a13ae to your computer and use it in GitHub Desktop.
Save kondors1995/7a0078029562bcc9527823e7113a13ae to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#
# EAS cluster cost interpolator for energy model construction
# by @kdrag0n
#
# This program is licensed under the MIT License (MIT)
#
# Copyright (c) 2019 Danny "kdrag0n" Lin <danny@kdrag0n.dev>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
import numpy as np
from scipy import interpolate
# Source data - cluster costs
# Format: "FREQ COST"
# The data below is from sd625: https://github.com/jayant-b/andorid_kernel_xiaomi_oxygen_1/commit/2339503e70d250b0debc7b82cba95fdebc17a065#diff-820be5108263e0a28cea0cba1d3744a8
src_cluster_costs0 = '''
652800 5
1036800 10
1401600 16
1689600 95
1804800 105
1958400 125
2016000 135'''
src_cluster_costs1 = '''
652800 69
1036800 74
1401600 80
1689600 100
1804800 110
1958400 130
2016000 140'''
# Target frequencies to interpolate the costs for
target_freqs0 = [400457, 480000, 576000, 652800, 729600, 806400, 883200, 960000, 1036800, 1113600, 1248000, 1305600, 1401600, 1536000, 1689600, 1758400, 1804800, 1843200, 1958400, 2016000]
target_freqs1 = [400457, 480000, 576000, 652800, 729600, 806400, 883200, 960000, 1036800, 1113600, 1248000, 1305600, 1401600, 1536000, 1689600, 1758400, 1804800, 1843200, 1958400, 2016000]
# Interpolate and show cluster costs for the given source and target datasets
def int_cluster(src_str, target_freqs):
src_data = src_str.split()
src_freqs = [] # x
src_costs = [] # y
# Populate source frequency (x) and cost (y) lists
for freq, cost in zip(*[iter(src_data)]*2):
src_freqs.append(freq)
src_costs.append(cost)
# Perform cubic-spline interpolation on the source and target datasets
tck = interpolate.splrep(src_freqs, src_costs, s=0)
xnew = target_freqs
ynew = interpolate.splev(xnew, tck, der=0)
new_costs = list(ynew)
# Print the new interpolated costs
for idx, cost in enumerate(new_costs):
freq = target_freqs[idx]
print('%7d %.0f' % (freq, cost))
def main():
# Interpolate and show little cluster costs
print('Little cluster')
int_cluster(src_cluster_costs0, target_freqs0)
# Interpolate and show big cluster costs
print('\n\nBig cluster')
int_cluster(src_cluster_costs1, target_freqs1)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment