Skip to content

Commit

Permalink
Merge pull request #155 from AustinSanders/master
Browse files Browse the repository at this point in the history
Updated for PVL 1.0
  • Loading branch information
AustinSanders committed Sep 30, 2020
2 parents 3930bb6 + bd5a7b8 commit 6155fa2
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 31 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ after_success:
- source deactivate
- conda install -q conda-build anaconda-client
- conda config --set anaconda_upload no
- conda build recipe -q
- builddir=(`conda build recipe --output`)
- travis_wait conda build recipe -q
- travis_wait builddir=(`conda build recipe --output`)
- |
if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then
anaconda -t="$CONDA_UPLOAD_TOKEN" upload $builddir --force;
Expand Down
28 changes: 14 additions & 14 deletions plio/io/io_controlnetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ class MeasureMessageType(IntEnum):
MaximumPixelZScore = 4
PixelShift = 5
WholePixelCorrelation = 6
SubPixelCorrelation = 7
SubPixelCorrelation = 7

class MeasureLog():

def __init__(self, messagetype, value):
"""
A protobuf compliant measure log object.
Parameters
----------
messagetype : int or str
Either the integer or string representation from the MeasureMessageType enum
value : int or float
The value to be stored in the message log
"""
Expand All @@ -64,19 +64,19 @@ def __init__(self, messagetype, value):
else:
# by name
self.messagetype = MeasureMessageType[messagetype]

if not isinstance(value, (float, int)):
raise TypeError(f'{value} is not a numeric type')
self.value = value

def __repr__(self):
return f'{self.messagetype.name}: {self.value}'

def to_protobuf(self, version=2):
"""
Return protobuf compliant measure log object representation
of this class.
Returns
-------
log_message : obj
Expand Down Expand Up @@ -148,7 +148,7 @@ def to_isis(obj, path, mode='wb', version=2,
buffer_header_size, points_bytes,
creation_date, modified_date)

store.write(header)
store.write(header.encode('utf-8'))

class IsisStore(object):
"""
Expand Down Expand Up @@ -283,7 +283,7 @@ def read(self):

# Munge the MeasureLogData into Python objs
df['measureLog'] = df['measureLog'].apply(lambda x: [MeasureLog.from_protobuf(i) for i in x])

df.header = pvl_header
return df

Expand All @@ -292,8 +292,8 @@ def write(self, data, offset=0):
"""
Parameters
----------
data : str
to be written to the file
data : bytes
Encoded header to be written to the file
offset : int
The byte offset into the output binary
"""
Expand Down Expand Up @@ -461,7 +461,7 @@ def create_pvl_header(self, version, headerstartbyte,
An ISIS compliant PVL header object
"""

encoder = pvl.encoder.IsisCubeLabelEncoder
encoder = pvl.encoder.ISISEncoder(end_delimiter=False)

header_bytes = buffer_header_size
points_start_byte = HEADERSTARTBYTE + buffer_header_size
Expand Down Expand Up @@ -489,4 +489,4 @@ def create_pvl_header(self, version, headerstartbyte,
)
])

return pvl.dumps(header, cls=encoder)
return pvl.dumps(header, encoder=encoder)
4 changes: 2 additions & 2 deletions plio/io/io_spectral_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ def __init__(self, input_data, label=None, cleaned=True, qa_threshold=2000):
"^SP_SPECTRUM_REF1", "^SP_SPECTRUM_QA", "^L2D_RESULT_ARRAY",
"^SP_SPECTRUM_RAD"]:
continue
if isinstance(v, pvl._collections.Units):
if isinstance(v, pvl.collections.Quantity):
k = "{}_{}".format(k, v.units)
v = v.value
keys.append(k)
vals.append(v)

vals = [vals] * len(self.ancillary_data)
new_anc = pd.DataFrame(vals, index=self.ancillary_data.index, columns=keys)
self.ancillary_data = self.ancillary_data.join(new_anc, how='inner')
Expand Down
2 changes: 1 addition & 1 deletion plio/io/io_tes.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def expand_bitstrings(df, dataset): # pragma: no cover
with open(var_file, "rb") as var:
buffer = var.read()
def process_rad(index):
if index is -1:
if index == -1:
return None

length = np.frombuffer(buffer[index:index+2], dtype='>u2')[0]
Expand Down
76 changes: 67 additions & 9 deletions plio/io/isis_serial_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pvl
from pvl.collections import PVLModule
from itertools import chain

import plio
from plio.data import get_data
Expand Down Expand Up @@ -70,7 +71,7 @@ def generate_serial_number(label):
The ISIS compatible serial number
"""
if not isinstance(label, PVLModule):
label = pvl.load(label, cls=SerialNumberDecoder)
label = pvl.load(label, decoder=SerialNumberDecoder())
# Get the translation information
translation = get_isis_translation(label)

Expand Down Expand Up @@ -99,6 +100,7 @@ def generate_serial_number(label):
serial_number.append(serial_entry)
except:
pass

return '/'.join(serial_number)


Expand All @@ -108,13 +110,69 @@ class SerialNumberDecoder(pvl.decoder.PVLDecoder):
serial number. Inherits from the PVLDecoder in planetarypy's pvl module.
"""

def cast_unquoated_string(self, value):
"""
Overrides the parent class's method so that any un-quoted string type value found in the
parsed pvl will just return the original value. This is needed so that keyword values
are not re-formatted from what is originally in the ISIS cube label.
def decode_simple_value(self, value: str):
"""Returns a Python object based on *value*, assuming
that *value* can be decoded as a PVL Simple Value::
Note: This affects value types that are recognized as null, boolean, number, datetime,
et at.
<Simple-Value> ::= (<Numeric> | <String>)
Modified from https://pvl.readthedocs.io/en/stable/_modules/pvl/decoder.html#PVLDecoder.decode_simple_value
Modification entails stripping datetime from list of functions.
"""
for d in (
self.decode_quoted_string,
self.decode_non_decimal,
self.decode_decimal,
):
try:
return d(value)
except ValueError:
pass

if value.casefold() == self.grammar.none_keyword.casefold():
return None

if value.casefold() == self.grammar.true_keyword.casefold():
return True

if value.casefold() == self.grammar.false_keyword.casefold():
return False

return self.decode_unquoted_string(value)

def decode_unquoted_string(self, value: str) -> str:
"""Returns a Python ``str`` if *value* can be decoded
as an unquoted string, based on this decoder's grammar.
Raises a ValueError otherwise.
Modified from: https://pvl.readthedocs.io/en/stable/_modules/pvl/decoder.html#PVLDecoder.decode_unquoted_string
Modification entails removal of decode_datetime call
"""
return value.decode('utf-8')
for coll in (
("a comment", chain.from_iterable(self.grammar.comments)),
("some whitespace", self.grammar.whitespace),
("a special character", self.grammar.reserved_characters),
):
for item in coll[1]:
if item in value:
raise ValueError(
"Expected a Simple Value, but encountered "
f'{coll[0]} in "{self}": "{item}".'
)

agg_keywords = self.grammar.aggregation_keywords.items()
for kw in chain.from_iterable(agg_keywords):
if kw.casefold() == value.casefold():
raise ValueError(
"Expected a Simple Value, but encountered "
f'an aggregation keyword: "{value}".'
)

for es in self.grammar.end_statements:
if es.casefold() == value.casefold():
raise ValueError(
"Expected a Simple Value, but encountered "
f'an End-Statement: "{value}".'
)

return str(value)
6 changes: 3 additions & 3 deletions plio/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def is_number(s):
return True
except ValueError:
return False

def convert_string_to_float(s):
"""
Attempt to convert a string to a float.
Expand All @@ -44,7 +44,7 @@ def convert_string_to_float(s):
If successful, the converted value, else the argument is passed back
out.
"""

try:
return float(s)
except TypeError:
Expand Down Expand Up @@ -240,7 +240,7 @@ def split_all_ext(path):
"""
base, ext = os.path.splitext(path)

while len(ext) is not 0:
while len(ext) != 0:
base, ext = os.path.splitext(base)

return base

0 comments on commit 6155fa2

Please sign in to comment.