This repository has been archived by the owner on Mar 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from BodenmillerGroup/development
Makes the development branch the new master.
- Loading branch information
Showing
14 changed files
with
258 additions
and
139 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import warnings | ||
|
||
CHANGE_DTYPE_LB_WARNING = 'Data minimum trunkated as outside dtype range' | ||
CHANGE_DTYPE_UB_WARNING = 'Data max trunkated as outside dtype range' | ||
|
||
# Checks if numpy is available in the | ||
# Python implementation | ||
|
||
_is_base = False | ||
try: | ||
import numpy as np | ||
except ImportError: | ||
_is_base = True | ||
|
||
if _is_base == False: | ||
def change_dtype(array, dtype): | ||
""" | ||
Changes the dtype of an array | ||
This makes sure that the values are correctly truncated and rounded | ||
to fit into the new dtype. | ||
:param array: a numpy array | ||
:param dtypw: a numpy dtype | ||
:returns: a copy of the array with the correct dtype. | ||
""" | ||
if dtype.kind in ['i', 'u']: | ||
dinf = np.iinfo(dtype) | ||
array = np.around(array) | ||
mina = array.min() | ||
maxa = array.max() | ||
t_min = None | ||
t_max = None | ||
if mina < dinf.min: | ||
t_min = dinf.min | ||
warnings.warn(CHANGE_DTYPE_LB_WARNING) | ||
|
||
if maxa > dinf.max: | ||
t_max = dinf.max | ||
warnings.warn(CHANGE_DTYPE_UB_WARNING) | ||
if (t_min is not None) | (t_max is not None): | ||
# this can be done inplace, as np.around returns array new object. | ||
np.clip(array, a_min=t_min, a_max=t_max, out=array) | ||
array = array.astype(dtype) | ||
return array |
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
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
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
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
Oops, something went wrong.