Skip to content

Commit

Permalink
Merge pull request #172 from sot/pep8
Browse files Browse the repository at this point in the history
Fix PEP8 and flake8 issues and add flake8 checking
  • Loading branch information
taldcroft authored Sep 24, 2019
2 parents d9da591 + 1dffc7f commit 9fa6993
Show file tree
Hide file tree
Showing 29 changed files with 356 additions and 301 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/flake8.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Python flake8 check

on: [push]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: Set up Python 3.6
uses: actions/setup-python@v1
with:
python-version: 3.6
- name: Lint with flake8
run: |
pip install flake8
# Note: only check files in Ska.engarchive package. Many other files in
# the repo are not maintained as PEP8 compliant.
flake8 Ska --count --exclude=Ska/engarchive/file_defs.py --ignore=W503,W504 --max-line-length=100 --show-source --statistics
2 changes: 1 addition & 1 deletion Ska/engarchive/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from .version import __version__, __git_version__
from .version import __version__, __git_version__ # noqa


def test(*args, **kwargs):
Expand Down
1 change: 1 addition & 0 deletions Ska/engarchive/add_derived.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,6 @@ def main():

add_colname(msid_files['colnames_all'].rel, 'QUALITY')


if __name__ == '__main__':
main()
24 changes: 14 additions & 10 deletions Ska/engarchive/cache.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## {{{ http://code.activestate.com/recipes/498245/ (r6)
# {{{ http://code.activestate.com/recipes/498245/ (r6)
# Licensed under a 3-clause BSD style license - see LICENSE.rst
import collections
import functools
Expand All @@ -7,11 +7,16 @@
from heapq import nsmallest
from operator import itemgetter


class Counter(dict):
'Mapping where default values are zero'

def __missing__(self, key):
return 0


# TODO: replace with std_library version of this in Py3.6 (issue #173)

def lru_cache(maxsize=30):
'''Least-recently-used cache decorator.
Expand All @@ -22,13 +27,13 @@ def lru_cache(maxsize=30):
'''
maxqueue = maxsize * 10

def decorating_function(user_function,
len=len, iter=iter, tuple=tuple, sorted=sorted, KeyError=KeyError):
len=len, iter=iter, tuple=tuple, sorted=sorted, KeyError=KeyError):
cache = {} # mapping of args to results
queue = collections.deque() # order that keys have been used
queue = collections.deque() # order that keys have been used
refcount = Counter() # times each key is in the queue
sentinel = object() # marker for looping around the queue
kwd_mark = object() # separate positional and keyword args

# lookup optimizations (ugly but fast)
queue_append, queue_popleft = queue.append, queue.popleft
Expand All @@ -38,7 +43,7 @@ def decorating_function(user_function,
def wrapper(*args, **kwds):
# cache key records both positional args only
key = args
#if kwds:
# if kwds:
# key += (kwd_mark,) + tuple(sorted(kwds.items()))

# record recent use of this key
Expand Down Expand Up @@ -69,11 +74,10 @@ def wrapper(*args, **kwds):
refcount.clear()
queue_appendleft(sentinel)
for key in filterfalse(refcount.__contains__,
iter(queue_pop, sentinel)):
iter(queue_pop, sentinel)):
queue_appendleft(key)
refcount[key] = 1


return result

def clear():
Expand Down Expand Up @@ -142,7 +146,7 @@ def clear():

@lru_cache(maxsize=20)
def f(x, y):
return 3*x+y
return 3 * x + y

domain = list(range(5))
from random import choice
Expand All @@ -153,12 +157,12 @@ def f(x, y):

@lfu_cache(maxsize=20)
def f(x, y):
return 3*x+y
return 3 * x + y

domain = list(range(5))
from random import choice
for i in range(1000):
r = f(choice(domain), choice(domain))

print((f.hits, f.misses))
## end of http://code.activestate.com/recipes/498245/ }}}
# end of http://code.activestate.com/recipes/498245/ }}}
5 changes: 2 additions & 3 deletions Ska/engarchive/check_integrity.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import pyyaks.context

import Ska.engarchive.fetch as fetch
import Ska.engarchive.file_defs as file_defs
import Ska.DBI

opt = None
Expand Down Expand Up @@ -102,7 +101,7 @@ def check_filetype(filetype):
if archfile['rowstop'] != length:
logger.info('ERROR: inconsistent archfile {}: '
'last rowstop={} MSID length={}'.format(
ft['content'], archfile['rowstop'], length))
ft['content'], archfile['rowstop'], length))
if opt.find_glitch:
find_glitch()

Expand All @@ -118,7 +117,7 @@ def find_glitch():

for archfile in archfiles:
logger.verbose('archfile {} {} {}'.format(
archfile['filename'], archfile['year'], archfile['doy']))
archfile['filename'], archfile['year'], archfile['doy']))
tstart = archfile['tstart']
rowstart = archfile['rowstart']
if abs(tstart - times[rowstart]) > opt.max_tstart_mismatch:
Expand Down
10 changes: 7 additions & 3 deletions Ska/engarchive/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ def _convert(dat):

return _convert


orbitephem0 = generic_converter('orbitephem0', add_quality=True)
lunarephem0 = generic_converter('lunarephem0', add_quality=True)
solarephem0 = generic_converter('solarephem0', add_quality=True)
Expand All @@ -180,6 +181,7 @@ def parse_alias_str(alias_str, invert=False):
aliases[cxcmsid] = msid
return aliases


ALIASES = {'simdiag': """
RAMEXEC 3SDSWELF SEA CSC Exectuting from RAM
DSTACKPTR 3SDPSTKP SEA Data Stack Ptr
Expand Down Expand Up @@ -526,7 +528,7 @@ def acisdeahk(dat):
vals = tuple((0.0 if bad else query_vals[id_idxs[query_id]])
for bad, query_id in zip(bads, col_query_ids))
val_tus = tuple((0 if bad else query_val_tus[id_idxs[query_id]])
for bad, query_id in zip(bads, col_query_ids))
for bad, query_id in zip(bads, col_query_ids))

# Now have another pass at finding bad values. Take these out now so the
# 5min and daily stats are not frequently corrupted.
Expand All @@ -537,11 +539,12 @@ def acisdeahk(dat):
outs.append((times[i0], quality) + vals)

dtype = [('TIME', numpy.float64),
('QUALITY', numpy.bool, (len(col_names) + 2,))]
('QUALITY', numpy.bool, (len(col_names) + 2,))]
dtype += [(col_name, numpy.float32) for col_name in col_names]

return numpy.rec.fromrecords(outs, dtype=dtype)


def _get_deahk_cols():
out = [
{
Expand Down Expand Up @@ -746,9 +749,10 @@ def _get_deahk_cols():
"unit": "V",
"descr": "Ground"
}
]
]
return out


def pyfits_to_recarray(dat):
dtypes = []
colnames = dat.dtype.names
Expand Down
14 changes: 7 additions & 7 deletions Ska/engarchive/derived/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
MSIDs.
"""

from .base import *
from .base import * # noqa

from .thermal import *
from .test import *
from .acispow import *
from .pcad import *
from .orbit import *
from .eps import *
from .thermal import * # noqa
from .test import * # noqa
from .acispow import * # noqa
from .pcad import * # noqa
from .orbit import * # noqa
from .eps import * # noqa
7 changes: 3 additions & 4 deletions Ska/engarchive/derived/acispow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
Derived parameter MSIDs related to ACIS power.
"""

import numpy as np
from . import base


class DP_DPA_POWER(base.DerivedParameter):
"""ACIS total DPA-A and DPA-B power"""
rootparams = ['1dp28avo', '1dpicacu', '1dp28bvo', '1dpicbcu']
time_step = 32.8
content_root = 'acispow'

def calc(self, data):
power = (data['1dp28avo'].vals * data['1dpicacu'].vals +
power = (data['1dp28avo'].vals * data['1dpicacu'].vals +
data['1dp28bvo'].vals * data['1dpicbcu'].vals)
return power

Expand All @@ -36,8 +36,7 @@ class DP_PSMC_POWER(base.DerivedParameter):
content_root = 'acispow'

def calc(self, data):
power = (data['1dp28avo'].vals * data['1dpicacu'].vals +
power = (data['1dp28avo'].vals * data['1dpicacu'].vals +
data['1dp28bvo'].vals * data['1dpicbcu'].vals +
data['1de28avo'].vals * data['1deicacu'].vals)
return power

20 changes: 11 additions & 9 deletions Ska/engarchive/derived/base.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import print_function, division, absolute_import

from Chandra.Time import DateTime
from .. import fetch
from .. import fetch, fetch_eng
import Ska.Numpy
import numpy as np
from .. import cache

__all__ = ['MNF_TIME', 'times_indexes', 'DerivedParameter']

MNF_TIME = 0.25625 # Minor Frame duration (seconds)


def times_indexes(start, stop, dt):
index0 = DateTime(start).secs // dt
index1 = DateTime(stop).secs // dt + 1
indexes = np.arange(index0, index1, dtype=np.int64)
times = indexes * dt
return times, indexes


@cache.lru_cache(20)
def interpolate_times(keyvals, len_data_times, data_times=None, times=None):
return Ska.Numpy.interpolate(np.arange(len_data_times),
data_times, times, method='nearest')


class DerivedParameter(object):
max_gap = 66.0 # Max allowed data gap (seconds)
max_gaps = {}
Expand All @@ -41,9 +43,9 @@ def fetch(self, start, stop):
# Translate state codes "ON" and "OFF" to 1 and 0, respectively.
for data in dataset.values():
if (data.vals.dtype.name == 'str96'
and set(data.vals).issubset(set(['ON ', 'OFF']))):
and set(data.vals).issubset(set(['ON ', 'OFF']))):
data.vals = np.where(data.vals == 'OFF', np.int8(0), np.int8(1))

times, indexes = times_indexes(start, stop, self.time_step)
bads = np.zeros(len(times), dtype=np.bool) # All data OK (false)

Expand All @@ -59,9 +61,9 @@ def fetch(self, start, stop):
.format(msidname, DateTime(start).date, DateTime(stop).date))
keyvals = (data.content, data.times[0], data.times[-1],
len(times), times[0], times[-1])
idxs = interpolate_times(keyvals, len(data.times),
idxs = interpolate_times(keyvals, len(data.times),
data_times=data.times, times=times)

# Loop over data attributes like "bads", "times", "vals" etc and
# perform near-neighbor interpolation by indexing
for attr in data.colnames:
Expand Down Expand Up @@ -92,9 +94,9 @@ def __call__(self, start, stop):
# Translate state codes "ON" and "OFF" to 1 and 0, respectively.
for data in dataset.values():
if (data.vals.dtype.name == 'string24'
and set(data.vals) == set(('ON ', 'OFF'))):
and set(data.vals) == set(('ON ', 'OFF'))):
data.vals = np.where(data.vals == 'OFF', np.int8(0), np.int8(1))

dataset.interpolate(dt=self.time_step)

# Return calculated values. Np.asarray will copy the array only if
Expand Down
12 changes: 6 additions & 6 deletions Ska/engarchive/derived/eps.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class DerivedParameterEps(base.DerivedParameter):
content_root = 'eps'


#--------------------------------------------
# --------------------------------------------
class DP_BATT1_TAVE(DerivedParameterEps):
"""Battery 1 Average Temperature. Derived from average of all three battery temperature sensors.
Telemetry 16x / MF
Expand All @@ -30,7 +30,7 @@ def calc(self, data):
return BATT1_TAVE


#--------------------------------------------
# --------------------------------------------
class DP_BATT2_TAVE(DerivedParameterEps):
"""Battery 2 Average Temperature. Derived from average of all three battery temperature sensors.
Telemetry 16x / MF
Expand All @@ -44,7 +44,7 @@ def calc(self, data):
return BATT2_TAVE


#--------------------------------------------
# --------------------------------------------
class DP_BATT3_TAVE(DerivedParameterEps):
"""Battery 3 Average Temperature. Derived from average of all three battery temperature sensors.
Telemetry 16x / MF
Expand All @@ -58,7 +58,7 @@ def calc(self, data):
return BATT3_TAVE


#--------------------------------------------
# --------------------------------------------
class DP_EPOWER1(DerivedParameterEps):
"""Bus Power = ELBI_LOW * ELBV
Telemetry 8x / MF
Expand All @@ -71,7 +71,7 @@ def calc(self, data):
return EPOWER1


#--------------------------------------------
# --------------------------------------------
class DP_MYSAPOW(DerivedParameterEps):
"""-Y Solar Array Power = ESAMYI * ELBV
Telemetry 8x / MF
Expand All @@ -84,7 +84,7 @@ def calc(self, data):
return MYSAPOW


#--------------------------------------------
# --------------------------------------------
class DP_PYSAPOW(DerivedParameterEps):
"""+Y Solar Array Power = ESAPYI * ELBV
Telemetry 8x / MF
Expand Down
9 changes: 6 additions & 3 deletions Ska/engarchive/derived/orbit.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ def calc_orbital_elements(x, y, z, vx, vy, vz):
def arccos_2pi(arg, reflect):
"""
Return arccos(arg) where reflect is False, 2 * pi - arccos(arg) where reflect is True
:param arg: float, np.ndarray: input arg (radians)
:param reflect: bool, np.ndarray of bool: use reflected value
"""
np_err_handling = np.geterr()
np.seterr(all='raise')
Expand All @@ -81,9 +84,9 @@ def arccos_2pi(arg, reflect):
np.seterr(**np_err_handling)

try:
# Check if arg is a non-scalar numpy array
len(arg) and isinstance(arg, np.ndarray)
except:
# Check if arg has a length
len(arg)
except TypeError:
if reflect:
out = 2 * pi - out
else:
Expand Down
Loading

0 comments on commit 9fa6993

Please sign in to comment.