Skip to content

Commit

Permalink
now the pvts function is interoperable with earth engine
Browse files Browse the repository at this point in the history
  • Loading branch information
ytarazona committed Dec 25, 2021
1 parent 62feeae commit 9e6462c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
6 changes: 3 additions & 3 deletions forestools/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
import numpy as np
import pandas
import pandas as pd

def plot(x, title = None, xlabel = None, ylabel = None, **kwargs):

Expand Down Expand Up @@ -42,7 +42,7 @@ def plot(x, title = None, xlabel = None, ylabel = None, **kwargs):
p = x['Monitoring_period']['end']
cons = 1/3

elif isinstance(ts, (pandas.core.series.Series)):
elif isinstance(ts, (pd.core.series.Series)):
value = x['Breakpoint']['value']
pos = x['Monitoring_period']['end']
post = ts.index.get_loc(pos)
Expand Down Expand Up @@ -71,7 +71,7 @@ def plot(x, title = None, xlabel = None, ylabel = None, **kwargs):
# xlabel
if xlabel is None:
xlabel = "Index"
if isinstance(x['Ts'], (pandas.core.series.Series)):
if isinstance(x['Ts'], (pd.core.series.Series)):
xlabel = 'Time'
xlabel = xlabel

Expand Down
48 changes: 36 additions & 12 deletions forestools/pvts.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env python
# coding: utf-8
# %%
import numpy
import pandas
import numpy as np
import pandas as pd

def pvts(x, startm, endm, threshold = 5):
'''
Expand All @@ -12,7 +12,8 @@ def pvts(x, startm, endm, threshold = 5):
Parameters:
x: Can be numpy.ndarray with 1d or 2d without NaN's or pandas.core.series.Series.
x: Can be numpy.ndarray with 1d or 2d without NaN's, pandas.core.series.Series or
pandas.core.indexes.datetimes.DatetimeIndex.
startm: The start of the monitoring time.
Expand All @@ -25,28 +26,51 @@ def pvts(x, startm, endm, threshold = 5):
'''

if any(numpy.isnan(x)):
raise Exception('The object cannot contain NaN: {}'.format(x))
if isinstance (x, (np.ndarray)):

if isinstance (x, (numpy.ndarray)):
if any(np.isnan(x)):
raise Exception('The object cannot contain NaN: {}'.format(x))

if x.ndim == 1:

mean_pvts = numpy.mean(x[0:(startm-1)])
std_pvts = numpy.std(x[0:(startm-1)])
mean_pvts = np.mean(x[0:(startm-1)])
std_pvts = np.std(x[0:(startm-1)])
li = mean_pvts - threshold*std_pvts
value = x[endm]

else:
raise Exception('2d ndarray not supported')

elif isinstance (x, (pandas.core.series.Series)):
elif isinstance (x, (pd.core.series.Series)):

if any(x.isnull().values):
raise Exception('The object cannot contain NaN: {}'.format(x))

startm_n = x.index.get_loc(startm)
endm_n = x.index.get_loc(endm)

mean_pvts = np.mean(x[0:(startm_n -1)])
std_pvts = np.std(x[0:(startm_n -1)])
li = mean_pvts - threshold*std_pvts
value = x[endm_n]

elif isinstance (x, (pd.core.frame.DataFrame)):

if any(x.isnull().values):
raise Exception('The object cannot contain NaN: {}'.format(x))

# whether the time series comes from google earth engine
if isinstance (x.index, (pd.core.indexes.datetimes.DatetimeIndex)):
#x = x.squeeze()
x.index = x.index.date
x.index = pd.to_datetime(x.index)
x = pd.Series(x.iloc[:, 0], index = x.index)

startm_n = x.index.get_loc(startm)
endm_n = x.index.get_loc(endm)

mean_pvts = numpy.mean(x[0:(startm_n-1)])
std_pvts = numpy.std(x[0:(startm_n-1)])
mean_pvts = np.mean(x[0:(startm_n -1)])
std_pvts = np.std(x[0:(startm_n -1)])
li = mean_pvts - threshold*std_pvts
value = x[endm_n]

Expand All @@ -63,7 +87,7 @@ def pvts(x, startm, endm, threshold = 5):
else:
output = {'Ts': x,
'Monitoring_period': {'start': startm, 'end': endm},
'Breakpoint' : {'Year_index': numpy.nan, 'value': numpy.nan},
'Breakpoint' : {'Year_index': np.nan, 'value': np.nan},
'Threshold' : {'Threshold': threshold, 'Lower_limit': li}}
return output

0 comments on commit 9e6462c

Please sign in to comment.