Skip to content

Commit 838d2ef

Browse files
committed
Test clean-up
1 parent 940a5ca commit 838d2ef

File tree

9 files changed

+214
-101
lines changed

9 files changed

+214
-101
lines changed

Makefile

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Default Python version to use for tests
2-
PYTHON=python3.6
2+
PYTHON=python3.8
33
PYTHON_VER=$(subst python,,$(PYTHON))
44

55
# Python versions to test
@@ -11,6 +11,8 @@ PYTHONVERS=python2.7 python3.5 python3.6 python3.7 python3.8
1111
VERSION=0.1.5b4
1212
SHELL:= /bin/bash
1313

14+
LONG_TESTS=false
15+
1416
# Select this to have anaconda installed for you.
1517
CONDA=./anaconda3
1618
# CONDA=/opt/anaconda3
@@ -95,13 +97,15 @@ else
9597
make $(CONDA)/envs/$(PYTHON) PYTHON=$(PYTHON)
9698
endif
9799

98-
$(CONDA)/envs/$(PYTHON): /tmp/$(CONDA_PKG)
100+
$(CONDA)/envs/$(PYTHON): ./anaconda3
99101
$(CONDA_ACTIVATE); \
100102
$(CONDA)/bin/conda create -y --name $(PYTHON) python=$(PYTHON_VER)
101103

104+
./anaconda3: /tmp/$(CONDA_PKG)
105+
bash /tmp/$(CONDA_PKG) -b -p $(CONDA)
106+
102107
/tmp/$(CONDA_PKG):
103108
curl https://repo.anaconda.com/miniconda/$(CONDA_PKG) > /tmp/$(CONDA_PKG)
104-
bash /tmp/$(CONDA_PKG) -b -p $(CONDA)
105109

106110
pythonw=$(PYTHON)
107111

@@ -128,12 +132,15 @@ repository-test-data:
128132
# 'python setup.py develop' creates symlinks in system package directory.
129133
# $(CONDA_ACTIVATE) $(PYTHON); $(PYTHON) setup.py develop | grep "Best"
130134

131-
$(CONDA_ACTIVATE) $(PYTHON); $(pythonw) -m pytest -v -m 'not long' hapiclient/test/test_hapi.py
132-
$(CONDA_ACTIVATE) $(PYTHON); $(pythonw) -m pytest -v -m 'long' hapiclient/test/test_hapi.py
135+
$(CONDA_ACTIVATE) $(PYTHON); $(pythonw) -m pytest -v -m 'short' hapiclient/test/test_hapi.py
133136
$(CONDA_ACTIVATE) $(PYTHON); $(pythonw) -m pytest -v hapiclient/test/test_hapitime2datetime.py
134137
$(CONDA_ACTIVATE) $(PYTHON); $(pythonw) -m pytest -v hapiclient/test/test_hapitime2datetime.py
135138
$(CONDA_ACTIVATE) $(PYTHON); $(pythonw) -m pytest -v hapiclient/test/test_hapitime_reformat.py
136139
$(CONDA_ACTIVATE) $(PYTHON); $(pythonw) -m pytest -v hapiclient/test/test_chunking.py
140+
ifeq (LONG_TESTS,true)
141+
$(CONDA_ACTIVATE) $(PYTHON); $(pythonw) -m pytest -v -m 'long' hapiclient/test/test_hapi.py
142+
$(CONDA_ACTIVATE) $(PYTHON); $(pythonw) -m pytest -v -m 'verylong' hapiclient/test/test_hapi.py
143+
endif
137144

138145
# These require visual inspection.
139146
repository-test-plots:
@@ -231,7 +238,7 @@ version-tag:
231238
# Install package in local directory (symlinks made to local dir)
232239
install-local:
233240
# python setup.py -e .
234-
source ~/.bashrc; $(CONDA_ACTIVATE) $(PYTHON); pip install --editable .
241+
pre = sys._getframe(1).f_code.co_name + '(): '$(CONDA_ACTIVATE) $(PYTHON); pip install --editable .
235242

236243
install:
237244
pip install 'hapiclient==$(VERSION)' --index-url $(URL)/simple

hapiclient/hapi.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -482,10 +482,8 @@ def hapi(*args, **kwargs):
482482
opts['dt_chunk'] = None
483483

484484
backend = 'sequential'
485-
486485
if opts['parallel']:
487486
backend = 'threading'
488-
489487
# multiprocessing was not tested. It may work, but will
490488
# need a speed comparison with threading.
491489
# backend = 'multiprocessing'
@@ -529,13 +527,21 @@ def nhapi(SERVER, DATASET, PARAMETERS, pSTART, pDELTA, i, **opts):
529527

530528
resD = list(resD)
531529

530+
import sys
532531
from hapiclient.hapi import hapitime_reformat
533532

534-
START = hapitime_reformat(resD[0]['Time'][0].decode('UTF-8'), START)
535-
resD[0] = resD[0][resD[0]['Time'] >= bytes(START, 'UTF-8')]
533+
if sys.version_info < (3, ):
534+
START = hapitime_reformat(str(resD[0]['Time'][0]), START)
535+
resD[0] = resD[0][resD[0]['Time'] >= START]
536+
537+
STOP = hapitime_reformat(str(resD[-1]['Time'][0]), STOP)
538+
resD[-1] = resD[-1][resD[-1]['Time'] < STOP]
539+
else:
540+
START = hapitime_reformat(resD[0]['Time'][0].decode('UTF-8'), START)
541+
resD[0] = resD[0][resD[0]['Time'] >= bytes(START, 'UTF-8')]
536542

537-
STOP = hapitime_reformat(resD[-1]['Time'][0].decode('UTF-8'), STOP)
538-
resD[-1] = resD[-1][resD[-1]['Time'] < bytes(STOP, 'UTF-8')]
543+
STOP = hapitime_reformat(resD[-1]['Time'][0].decode('UTF-8'), STOP)
544+
resD[-1] = resD[-1][resD[-1]['Time'] < bytes(STOP, 'UTF-8')]
539545

540546
data = np.concatenate(resD)
541547

@@ -716,7 +722,6 @@ def nhapi(SERVER, DATASET, PARAMETERS, pSTART, pDELTA, i, **opts):
716722
log('Reading %s' % fnamebin.replace(urld + '/', ''), opts)
717723
tic = time.time()
718724
data = np.fromfile(fnamebin, dtype=dt)
719-
toc = time.time() - tic
720725
else:
721726
from io import BytesIO
722727
log('Creating buffer: %s' % urlbin, opts)
@@ -726,7 +731,6 @@ def nhapi(SERVER, DATASET, PARAMETERS, pSTART, pDELTA, i, **opts):
726731
log('Parsing buffer.', opts)
727732
tic = time.time()
728733
data = np.frombuffer(buff.read(), dtype=dt)
729-
toc = time.time() - tic
730734
else:
731735
# HAPI CSV
732736
if opts["cache"]:
@@ -748,7 +752,6 @@ def nhapi(SERVER, DATASET, PARAMETERS, pSTART, pDELTA, i, **opts):
748752
tic = time.time()
749753
if opts['method'] == 'numpy':
750754
data = np.genfromtxt(fnamecsv, dtype=dt, delimiter=',')
751-
toc = time.time() - tic
752755
if opts['method'] == 'pandas':
753756
# Read file into Pandas DataFrame
754757
df = pandas.read_csv(fnamecsv, sep=',', header=None)
@@ -757,17 +760,13 @@ def nhapi(SERVER, DATASET, PARAMETERS, pSTART, pDELTA, i, **opts):
757760
# as computed to pandas.read_csv; pandas dtype is different
758761
# from numpy's dtype.)
759762
data = np.ndarray(shape=(len(df)), dtype=dt)
760-
print(df)
761763
# Insert data from dataframe 'df' columns into N-D array 'data'
762764
for i in range(0, len(pnames)):
763765
shape = np.append(len(data), psizes[i])
764766
# In numpy 1.8.2 and Python 2.7, this throws an error
765767
# for no apparent reason. Works as expected in numpy 1.10.4
766-
print(cols)
767768
data[pnames[i]] = np.squeeze(
768769
np.reshape(df.values[:, np.arange(cols[i][0], cols[i][1] + 1)], shape))
769-
770-
toc = time.time() - tic
771770
else:
772771
# At least one requested string or isotime parameter does not
773772
# have a length in metadata. More work to do to read.
@@ -866,7 +865,7 @@ def nhapi(SERVER, DATASET, PARAMETERS, pSTART, pDELTA, i, **opts):
866865
# Save memory by not copying (does this help?)
867866
# data2[pnames[i]] = np.array(data[pnames[i]],copy=False)
868867

869-
toc = time.time() - tic
868+
toc = time.time() - tic
870869

871870
# Extra metadata associated with request will be saved in
872871
# a pkl file with same base name as npy data file.
@@ -919,10 +918,12 @@ def nhapi(SERVER, DATASET, PARAMETERS, pSTART, pDELTA, i, **opts):
919918
else:
920919
return data, meta
921920

922-
def hapitime_reformat(form_to_match, given_form):
921+
922+
def hapitime_reformat(form_to_match, given_form, logging=False):
923923
"""Reformat a given HAPI ISO 8601 time to match format of another HAPI ISO 8601 time."""
924924

925-
print('ref: {}\ngiven: {}'.format(form_to_match, given_form))
925+
log('ref: {}'.format(form_to_match), {'logging': logging})
926+
log('given: {}'.format(given_form), {'logging': logging})
926927

927928
if 'T' in given_form:
928929
dt_given = isodate.parse_datetime(given_form)
@@ -962,10 +963,14 @@ def hapitime_reformat(form_to_match, given_form):
962963

963964
if len(converted) > len(form_to_match):
964965
converted = converted[0:len(form_to_match)-1] + "Z"
965-
print('converted: {}\nref fmt: {}'.format(converted, format_ref))
966-
print('----')
966+
967+
log('converted: {}'.format(converted), {'logging': logging})
968+
log('ref fmt: {}'.format(format_ref), {'logging': logging})
969+
log('----', {'logging': logging})
970+
967971
return converted
968972

973+
969974
def hapitime_format_str(Time):
970975
"""Determine the time format string for a HAPI ISO 8601 time"""
971976

@@ -1093,9 +1098,7 @@ def hapitime2datetime(Time, **kwargs):
10931098
except:
10941099
tzinfo = datetime.timezone.utc
10951100

1096-
opts = {'logging': False}
1097-
1098-
opts = setopts(opts, kwargs)
1101+
opts = kwargs.copy()
10991102

11001103
if type(Time) == list:
11011104
Time = np.asarray(Time)
@@ -1138,13 +1141,16 @@ def hapitime2datetime(Time, **kwargs):
11381141
# have trailing Z, in some cases, infer_datetime_format will not return
11391142
# a timezone-aware Timestamp.
11401143
# TODO: Use hapitime_format_str() and pass this as format=...
1141-
Time = pandas.to_datetime(Time, infer_datetime_format=True).tz_localize(tzinfo).to_pydatetime()
1144+
#import pdb;pdb.set_trace()
1145+
Timeo = Time[0]
1146+
Time = pandas.to_datetime(Time, infer_datetime_format=True).tz_convert(tzinfo).to_pydatetime()
11421147
if reshape:
11431148
Time = np.reshape(Time, shape)
11441149
toc = time.time() - tic
1145-
log("Pandas processing time = %.4fs, Input = %s\n" % (toc, Time[0]), opts)
1150+
log("Pandas processing time = %.4fs, first time = %s" % (toc, Timeo), opts)
11461151
return Time
11471152
except:
1153+
log("Pandas processing failed, first time = %s" % Time[0], opts)
11481154
pass
11491155

11501156
# Convert from Python byte literals to unicode strings
@@ -1174,7 +1180,7 @@ def hapitime2datetime(Time, **kwargs):
11741180
error("HAPI Times must have trailing Z. Time[" + str(i) + "] = " + Time[i] + " does not have trailing Z.")
11751181

11761182
toc = time.time() - tic
1177-
log("Manual processing time = %.4fs, Input = %s, fmt = %s\n" % (toc, Time[0], fmt), opts)
1183+
log("Manual processing time = %.4fs, Input = %s, fmt = %s" % (toc, Time[0], fmt), opts)
11781184

11791185
if reshape:
11801186
pythonDateTime = np.reshape(pythonDateTime, shape)

0 commit comments

Comments
 (0)