Source code for tile_processing.helpers
# Copyright (c) Fraunhofer MEVIS, Germany. All rights reserved.
# **InsertLicense** code
#
# Comprises helper methods that do not depend on TileProcessor or TileProcessorProperties
#-------------------------------------------------------------------------------
from typing import Sequence, Any, List
NUM_ML_VECTOR_DIMENSIONS = 6
[docs]def toUpperCase( word:str ) -> str:
"""
Converts only the first character to upper case and leaves the rest untouched
**Example**::
toUpperCase( "myPropertyName" ) -> "MyPropertyName"
"""
return word[0].upper() + word[1:] if word else ""
[docs]def toFullVec( seq:Sequence, fillValue:Any=0, typeToEnsure:type=None, fullNumComponents=NUM_ML_VECTOR_DIMENSIONS ) -> List:
"""
Extends the given tuple or list to TileProcessor.NUM_IMAGE_VECTOR_COMPONENTS components by appending the given `fillValue`
as often as necessary, optionally converting all elements to typeToEnsure at the end.
:param seq: List or tuple with <= TileProcessor.NUM_IMAGE_VECTOR_COMPONENTS elements
:param fillValue: Value used for the elements appended to partialSequence
:param typeToEnsure: If not None, used to convert all elements (including fillValue) to the given type
:param fullNumComponents: "Full" number of components expected, i.e. number of components in the result list
:returns: Completed sequence
:raises: TypeError if seq is not a tuple or list
:raises: ValueError if seq has more than TileProcessor.NUM_IMAGE_VECTOR_COMPONENTS elements
"""
def ___validateInputSequence():
if not isinstance( seq, (tuple, list) ):
raise TypeError( "Given sequence must be of type tuple or list, but was {}".format(
None if seq is None else seq.__class__.__name__ ) )
if len( seq ) > fullNumComponents:
raise ValueError( "Given list has {}>{} (= TileProcessor.NUM_IMAGE_VECTOR_COMPONENTS) components!".format(
len( seq ), fullNumComponents ) )
#
def ___getExtendedListCopy():
listCopy = list( seq )
for _ in range( len( seq ), fullNumComponents ):
listCopy.append( fillValue )
return listCopy
#
def ___ensureComponentTypes( vec ):
if typeToEnsure is not None:
vec = [ typeToEnsure( i ) for i in vec ]
return vec
#
___validateInputSequence()
result = ___getExtendedListCopy()
return ___ensureComponentTypes( result )
[docs]def stripStartAndEnd( seq: Sequence, searchVal:Any ) -> Sequence:
"""
Strips leading or trailing occurrences of `searchVal` from seq.
:param seq: Sequence to strip components from
:param searchVal: Component value to strip
"""
def ___stripStart( seqOrig ):
seqStripped = type( seqOrig )()
for i in range( len( seqOrig ) ):
if seqOrig[ i ] != searchVal:
seqStripped = seqOrig[ i: ]
break
return seqStripped
#
def ___stripEnd( seqOrig ):
seqStripped = type( seqOrig )()
for i in range( len( seqOrig ) ):
if seqOrig[ -i - 1 ] != searchVal:
seqStripped = seqOrig[ : -i if i != 0 else None ]
break
return seqStripped
#
return ___stripEnd( ___stripStart( seq ) )