Skip to content

Commit

Permalink
math_basic module documented and following PEP8 standards. validation…
Browse files Browse the repository at this point in the history
… module completed.
  • Loading branch information
mcarbajo committed Dec 4, 2017
1 parent e0985de commit 88b7b3f
Show file tree
Hide file tree
Showing 3 changed files with 295 additions and 21 deletions.
8 changes: 4 additions & 4 deletions fda/FDataGrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def derivative(self, order=1):

def __add__(self, other):
if not isinstance(other, FDataGrid):
raise TypeError("Object type is not FDataGrid.")
raise NotImplementedError("Object type is not FDataGrid.")
if self.data_matrix.shape[1] != other.data_matrix.shape[1]:
raise ValueError("Error in columns dimensions")
if self.sample_points != other.sample_points:
Expand All @@ -265,7 +265,7 @@ def __add__(self, other):

def __sub__(self, other):
if not isinstance(other, FDataGrid):
raise TypeError("Object type is not FDataGrid.")
raise NotImplementedError("Object type is not FDataGrid.")
if self.data_matrix.shape[1] != other.data_matrix.shape[1]:
raise ValueError("Error in columns dimensions")
if self.sample_points != other.sample_points:
Expand All @@ -276,7 +276,7 @@ def __sub__(self, other):

def __mul__(self, other):
if not isinstance(other, FDataGrid):
raise TypeError("Object type is not FDataGrid.")
raise NotImplementedError("Object type is not FDataGrid.")
if self.data_matrix.shape[1] != other.data_matrix.shape[1]:
raise ValueError("Error in columns dimensions")
if self.sample_points != other.sample_points:
Expand All @@ -287,7 +287,7 @@ def __mul__(self, other):

def __truediv__(self, other):
if not isinstance(other, FDataGrid):
raise TypeError("Object type is not FDataGrid.")
raise NotImplementedError("Object type is not FDataGrid.")
if self.data_matrix.shape[1] != other.data_matrix.shape[1]:
raise ValueError("Error in columns dimensions")
if self.sample_points != other.sample_points:
Expand Down
173 changes: 161 additions & 12 deletions fda/math_basic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""This module defines the basic mathematic operations for classes defined in this package.
"""This module defines the basic mathematic operations for classes defined
in this package.
"""
from fda.FDataGrid import FDataGrid
Expand All @@ -15,51 +16,199 @@


def mean(fdatagrid):
return FDataGrid([numpy.mean(fdatagrid.data_matrix, 0)], fdatagrid.sample_points, fdatagrid.sample_range,
""" Computes the mean of all the samples in a FDataGrid object.
Args:
fdatagrid (FDataGrid): Object containing all the samples whose mean
is wanted.
Returns:
FDataGrid: A FDataGrid object with just one sample representing the
mean of all the samples in the original FDataGrid object.
"""
return FDataGrid([numpy.mean(fdatagrid.data_matrix, 0)],
fdatagrid.sample_points, fdatagrid.sample_range,
fdatagrid.names)


def var(fdatagrid):
return FDataGrid([numpy.var(fdatagrid.data_matrix, 0)], fdatagrid.sample_points, fdatagrid.sample_range, fdatagrid.names)
""" Computes the variance of a set of samples in a FDataGrid object.
Args:
fdatagrid (FDataGrid): Object containing all the set of samples
whose variance is desired.
Returns:
FDataGrid: A FDataGrid object with just one sample representing the
mean of all the samples in the original FDataGrid object.
"""
return FDataGrid([numpy.var(fdatagrid.data_matrix, 0)],
fdatagrid.sample_points, fdatagrid.sample_range,
fdatagrid.names)


def gmean(fdatagrid):
return FDataGrid([scipy.stats.mstats.gmean(fdatagrid.data_matrix, 0)], fdatagrid.sample_points, fdatagrid.sample_range,
""" Computes the geometric mean of all the samples in a FDataGrid object.
Args:
fdatagrid (FDataGrid): Object containing all the samples whose
geometric mean is wanted.
Returns:
FDataGrid: A FDataGrid object with just one sample representing the
geometric mean of all the samples in the original FDataGrid object.
"""
return FDataGrid([scipy.stats.mstats.gmean(fdatagrid.data_matrix, 0)],
fdatagrid.sample_points, fdatagrid.sample_range,
fdatagrid.names)


def cov(fdatagrid):
""" Calculates the covariance matrix representing the covariance of the
functional samples at the observation points.
Args:
fdatagrid (FDataGrid): Object containing different samples of a
functional variable.
Returns:
numpy.darray: Matrix of covariances.
"""
return numpy.cov(fdatagrid.data_matrix)


def sqrt(fdatagrid):
return FDataGrid(numpy.sqrt(fdatagrid.data_matrix), fdatagrid.sample_points, fdatagrid.sample_range, fdatagrid.names)
""" Performs a element wise square root operation.
Args:
fdatagrid (FDataGrid): Object to whose elements the square root
operations is going to be applied.
Returns:
FDataGrid: Object whose elements are the square roots of the original.
"""
return FDataGrid(numpy.sqrt(fdatagrid.data_matrix),
fdatagrid.sample_points, fdatagrid.sample_range,
fdatagrid.names)


def absolute(fdatagrid):
return FDataGrid(numpy.absolute(fdatagrid.data_matrix), fdatagrid.sample_points, fdatagrid.sample_range, fdatagrid.names)
""" Gets the absolute value of all elements in the FDataGrid object.
Args:
fdatagrid (FDataGrid): Object from whose elements the absolute value
is going to be retrieved.
Returns:
FDataGrid: Object whose elements are the absolute values of the
original.
"""
return FDataGrid(numpy.absolute(fdatagrid.data_matrix),
fdatagrid.sample_points, fdatagrid.sample_range,
fdatagrid.names)


def around(fdatagrid, decimals=0):
return FDataGrid(numpy.around(fdatagrid.data_matrix, decimals), fdatagrid.sample_points, fdatagrid.sample_range,
""" Rounds all elements of the object.
Args:
fdatagrid (FDataGrid): Object to whose elements are going to be
rounded.
decimals (int, optional): Number of decimals wanted. Defaults to 0.
Returns:
FDataGrid: Object whose elements are rounded.
"""
return FDataGrid(numpy.around(fdatagrid.data_matrix, decimals),
fdatagrid.sample_points, fdatagrid.sample_range,
fdatagrid.names)


def exp(fdatagrid):
return FDataGrid(numpy.exp(fdatagrid.data_matrix), fdatagrid.sample_points, fdatagrid.sample_range, fdatagrid.names)
""" Performs a element wise exponential operation.
Args:
fdatagrid (FDataGrid): Object to whose elements the exponential
operations is going to be applied.
Returns:
FDataGrid: Object whose elements are the result of exponentiating
the elements of the original.
"""
return FDataGrid(numpy.exp(fdatagrid.data_matrix),
fdatagrid.sample_points, fdatagrid.sample_range,
fdatagrid.names)


def log(fdatagrid):
return FDataGrid(numpy.log(fdatagrid.data_matrix), fdatagrid.sample_points, fdatagrid.sample_range, fdatagrid.names)
""" Performs a element wise logarithm operation.
Args:
fdatagrid (FDataGrid): Object to whose elements the logarithm
operations is going to be applied.
Returns:
FDataGrid: Object whose elements are the logarithm of the original.
"""
return FDataGrid(numpy.log(fdatagrid.data_matrix),
fdatagrid.sample_points, fdatagrid.sample_range,
fdatagrid.names)


def log10(fdatagrid):
return FDataGrid(numpy.log10(fdatagrid.data_matrix), fdatagrid.sample_points, fdatagrid.sample_range, fdatagrid.names)
""" Performs a element wise base 10 logarithm operation.
Args:
fdatagrid (FDataGrid): Object to whose elements the base 10 logarithm
operations is going to be applied.
Returns:
FDataGrid: Object whose elements are the base 10 logarithm of the
original.
"""
return FDataGrid(numpy.log10(fdatagrid.data_matrix),
fdatagrid.sample_points, fdatagrid.sample_range,
fdatagrid.names)


def log2(fdatagrid):
return FDataGrid(numpy.log2(fdatagrid.data_matrix), fdatagrid.sample_points, fdatagrid.sample_range, fdatagrid.names)
""" Performs a element wise binary logarithm operation.
Args:
fdatagrid (FDataGrid): Object to whose elements the binary logarithm
operations is going to be applied.
Returns:
FDataGrid: Object whose elements are the binary logarithm of the
original.
"""
return FDataGrid(numpy.log2(fdatagrid.data_matrix),
fdatagrid.sample_points, fdatagrid.sample_range,
fdatagrid.names)


def cumsum(fdatagrid):
return FDataGrid(numpy.cumsum(fdatagrid.data_matrix), fdatagrid.sample_points, fdatagrid.sample_range, fdatagrid.names)
""" Returns the cumulative sum of the samples.
Args:
fdatagrid (FDataGrid): Object over whose samples the cumulative sum is
going to be calculated.
Returns:
FDataGrid: Object with the sample wise cumulative sum.
"""
return FDataGrid(numpy.cumsum(fdatagrid.data_matrix, axis=0),
fdatagrid.sample_points, fdatagrid.sample_range,
fdatagrid.names)

0 comments on commit 88b7b3f

Please sign in to comment.