Skip to content

Commit

Permalink
Merge branch 'dev-kja' of https://github.com/OpenDendro/dplPy into de…
Browse files Browse the repository at this point in the history
…v-kja
  • Loading branch information
kanchukaitis committed Jan 5, 2024
2 parents 2335055 + 04e02dc commit a38d04f
Show file tree
Hide file tree
Showing 13 changed files with 9,328 additions and 14,830 deletions.
File renamed without changes.
File renamed without changes.
68 changes: 0 additions & 68 deletions dplpy/GitHub Copilot

This file was deleted.

14,558 changes: 0 additions & 14,558 deletions dplpy/output.rwl

This file was deleted.

3 changes: 3 additions & 0 deletions dplpy/smoothingspline.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def get_param(amp, period):
def spline(x, y, period=None):
if period == None:
period = len(x) * 0.67
elif period < 0:
# Use the absolute value of period as a percentage
period = len(x) * (abs(period) / 100.0)

p = get_param(0.5, period)
yi = csaps(x, y, x, smooth=p)
Expand Down
72 changes: 72 additions & 0 deletions dplpy/transform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
__copyright__ = """
dplPy for tree ring width time series analyses
Copyright (C) 2023 OpenDendro
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""

__license__ = "GNU GPLv3"

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Date: 12/15/2023
# Author: Kevin Anchukaitis
# Title: transform.py
# Description: Transforms the raw series using one of several options.
# Doing so gives the series better statistical properties
# prior to detrending and standardization
# example usage:
# >>> import dplpy as dpl
# >>> data = dpl.readers("../tests/data/csv/file.csv")
# >>> dpl.transform(data,transform_type='power')

from tkinter import Y
import pandas as pd
import numpy as np

def transform_series(data: pd.DataFrame | pd.Series, transform_type='power'):
transformed_data = data.copy()

for col in transformed_data.columns:
series = transformed_data[col]

if transform_type == 'log':
series[series == 0] = 0.01
transformed_data[col] = np.log(series)

elif transform_type == 'log6':
series = series + (1/6)
series[series <= 0] = (1/6)
transformed_data[col] = np.log(series)

elif transform_type == 'inversehypersine':
valid_idx = ~series.isna()
transformed_data.loc[valid_idx, col] = np.log(series[valid_idx] + np.sqrt(series[valid_idx] + 1.0))

elif transform_type == 'power':
valid_idx = ~series.isna()
x = series[valid_idx]
original_mean = x.mean()
original_stdev = x.std()

M = (x[:-1] + x[1:]) / 2
S = np.abs(x[:-1] - x[1:])
b = np.polyfit(M, S, 1)

transformed_data.loc[valid_idx, col] = series[valid_idx] ** (1 - b[0])

# Optional: reset mean to original

return transformed_data
8 changes: 4 additions & 4 deletions dplpy/writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ def write_rwl(data, file):
file.write("\n")


def write_crn(data, site_id, file, site_name="Unnamed object", species_code="UNKN", location="Unknown", species="Plantae", elevation="", lat_long="", investigator="", comp_date=""):
rwi_data = chron(detrend(data, fit="spline", method="residual", plot=False), prewhiten=True)
def write_crn(data, site_id, file, site_name="Unnamed site", species_code="UNKN", location="Unknown", species="Plantae", elevation="", lat_long="", investigator="", comp_date=""):
rwi_data = data
first = rwi_data.first_valid_index()
last = rwi_data.last_valid_index()

Expand All @@ -159,8 +159,8 @@ def write_crn(data, site_id, file, site_name="Unnamed object", species_code="UNK
file.write(str(rwi_data.first_valid_index()).rjust(4))

for year in rwi_data.index.to_numpy():
file.write(str(round(rwi_data["Mean RWI"][year], 2)).replace(".", "").replace("0", "").rjust(4))
file.write(str(rwi_data["Sample depth"][year]).rjust(3))
file.write(str(round(rwi_data["Standard Chronology"][year], 2)).replace(".", "").replace("0", "").rjust(4))
file.write(str(rwi_data["Sample Depth"][year]).rjust(3))

if year % 10 == 9:
# write TRL ID#(optional) which takes columns 82-88
Expand Down
File renamed without changes.
Loading

0 comments on commit a38d04f

Please sign in to comment.