Created
May 28, 2019 12:25
-
-
Save arnobaer/c4c058a04f5a2e4331342c58927ef40c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--orbits', type=int, default=1) | |
parser.add_argument('--precision', type=int, default=1) | |
parser.add_argument('--prescale', type=float, default=2.0) | |
parser.add_argument('--show-ticks', action='store_true') | |
args = parser.parse_args() | |
def find_period(sequence): | |
"""Find period of repeating sequence.""" | |
for period in range(1, len(sequence)): | |
success = True | |
pattern = sequence[:period] | |
i = period | |
while i < int(len(sequence)/period) * period: | |
if sequence[i:i+period] != pattern: | |
success = False | |
break | |
i += period | |
if success: | |
return period | |
ORBIT_SIZE = 3564 | |
PREC = args.precision | |
PREC_MULT = 10**PREC | |
PREC_WIDTH = len(format(PREC_MULT)) | |
ORBIT_COUNT = args.orbits | |
prescale = args.prescale | |
prescale_ticks = int(prescale * PREC_MULT) | |
prescale_counter = 0 | |
data = [] | |
for orbit in range(ORBIT_COUNT): | |
for bx in range(ORBIT_SIZE): | |
signal = 0 | |
for tick in range(PREC_MULT): | |
prescale_counter += 1 | |
if prescale_counter >= prescale_ticks: | |
signal = 1 | |
prescale_counter = 0 | |
if args.show_ticks: | |
print("{:>6} {:>4} {:>4} {}".format(orbit, bx, tick, signal)) | |
else: | |
if args.show_ticks: | |
print("{:>6} {:>4} {:>4} {}".format(orbit, bx, tick, signal)) | |
data.append(signal) | |
if not args.show_ticks: | |
print("{:>6} {:>4} {}".format(orbit, bx, signal)) | |
period = find_period(data) | |
pattern = data[:period] | |
data2 = [] | |
i = 0 | |
for orbit in range(ORBIT_COUNT): | |
for bx in range(ORBIT_SIZE): | |
signal = pattern[i] | |
data2.append(signal) | |
i += 1 | |
if i >= len(pattern): | |
i = 0 | |
def compress_sequence(sequence): | |
pattern = [] | |
c = 0 | |
for i in range(len(sequence)): | |
c += 1 | |
if sequence[i] == 1: | |
pattern.append(c) | |
c = 0 | |
return pattern | |
from collections import Counter | |
compressed = compress_sequence(pattern) | |
print("chunks:", compressed) | |
print("compressed:", len(compressed)) | |
print("keys:", set(compressed)) | |
print("--") | |
print("ratio:", len(data) / data.count(1)) | |
print("sequence size:", period) | |
print("--") | |
print("ratio:", len(data2) / data2.count(1)) | |
print("sequence size:", find_period(data2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment