Skip to content

Commit

Permalink
format python code using ruff (#83)
Browse files Browse the repository at this point in the history
* format python code using ruff

* add test for component
  • Loading branch information
wwrechard authored Sep 7, 2024
1 parent 704cfca commit b283a84
Show file tree
Hide file tree
Showing 46 changed files with 1,887 additions and 1,838 deletions.
84 changes: 42 additions & 42 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#
import os
import sys
sys.path.insert(0, os.path.abspath('../..'))

sys.path.insert(0, os.path.abspath("../.."))

# -- General configuration ------------------------------------------------

Expand All @@ -29,54 +30,54 @@

html_theme = "sphinx_rtd_theme"
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
#collapse_navigation = True
# collapse_navigation = True

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.mathjax',
'sphinx.ext.githubpages',
'sphinx.ext.napoleon',
"sphinx.ext.autodoc",
"sphinx.ext.mathjax",
"sphinx.ext.githubpages",
"sphinx.ext.napoleon",
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
templates_path = ["_templates"]

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
# source_suffix = ['.rst', '.md']
source_suffix = '.rst'
source_suffix = ".rst"

# The encoding of source files.
#
# source_encoding = 'utf-8-sig'

# The master toctree document.
master_doc = 'index'
master_doc = "index"

# General information about the project.
project = u'PyDLM'
copyright = u'2024, Xiangyu Wang'
author = u'Xiangyu Wang'
project = "PyDLM"
copyright = "2024, Xiangyu Wang"
author = "Xiangyu Wang"

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = u'0.1.1.13'
version = "0.1.1.13"
# The full version, including alpha/beta/rc tags.
release = u'0.1.1.13'
release = "0.1.1.13"

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = 'en'
language = "en"

# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
Expand Down Expand Up @@ -112,7 +113,7 @@
# show_authors = False

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'
pygments_style = "sphinx"

# A list of ignored prefixes for module index sorting.
# modindex_common_prefix = []
Expand Down Expand Up @@ -163,7 +164,7 @@
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
html_static_path = ["_static"]

# Add any extra paths that contain custom files (such as robots.txt or
# .htaccess) here, relative to this directory. These files are copied
Expand Down Expand Up @@ -243,34 +244,30 @@
# html_search_scorer = 'scorer.js'

# Output file base name for HTML help builder.
htmlhelp_basename = 'PyDLMdoc'
htmlhelp_basename = "PyDLMdoc"

# -- Options for LaTeX output ---------------------------------------------

latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#
# 'papersize': 'letterpaper',

# The font size ('10pt', '11pt' or '12pt').
#
# 'pointsize': '10pt',

# Additional stuff for the LaTeX preamble.
#
# 'preamble': '',

# Latex figure (float) alignment
#
# 'figure_align': 'htbp',
# The paper size ('letterpaper' or 'a4paper').
#
# 'papersize': 'letterpaper',
# The font size ('10pt', '11pt' or '12pt').
#
# 'pointsize': '10pt',
# Additional stuff for the LaTeX preamble.
#
# 'preamble': '',
# Latex figure (float) alignment
#
# 'figure_align': 'htbp',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(master_doc, 'PyDLM.tex', u'PyDLM Documentation',
u'Xiangyu Wang', 'manual'),
(master_doc, "PyDLM.tex", "PyDLM Documentation", "Xiangyu Wang", "manual"),
]

# The name of an image file (relative to this directory) to place at the top of
Expand Down Expand Up @@ -310,10 +307,7 @@

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
(master_doc, 'pydlm', u'PyDLM Documentation',
[author], 1)
]
man_pages = [(master_doc, "pydlm", "PyDLM Documentation", [author], 1)]

# If true, show URL addresses after external links.
#
Expand All @@ -326,9 +320,15 @@
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'PyDLM', u'PyDLM Documentation',
author, 'PyDLM', 'One line description of project.',
'Miscellaneous'),
(
master_doc,
"PyDLM",
"PyDLM Documentation",
author,
"PyDLM",
"One line description of project.",
"Miscellaneous",
),
]

# Documents to append as an appendix to all manuals.
Expand Down
56 changes: 30 additions & 26 deletions examples/unemployment_insurance/main.py
Original file line number Diff line number Diff line change
@@ -1,82 +1,86 @@
#==================================================================
# ==================================================================
#
# This example use the data from Google data science blog post
# http://www.unofficialgoogledatascience.com/2017/07/fitting-bayesian-structural-time-series.html
#
# Make sure your working directly is pointing to the folder where
# `main.py` locates and `data.csv` is in the same folder.
#
#==================================================================
# ==================================================================

# Read data file
import os

this_dir = os.getcwd()
DATA_PATH = os.path.join(this_dir, "data.csv")
data_file = open(DATA_PATH, 'r')
data_file = open(DATA_PATH, "r")

variables = data_file.readline().strip().split(',')
variables = data_file.readline().strip().split(",")
data_map = {}
for var in variables:
data_map[var] = []

for line in data_file:
for i, data_piece in enumerate(line.strip().split(',')):
for i, data_piece in enumerate(line.strip().split(",")):
data_map[variables[i]].append(float(data_piece))

# Extract and store the data.
time_series = data_map[variables[0]]
features = [[data_map[variables[j]][i] for j in range(1, len(variables)) ]
for i in range(len(time_series))]
features = [
[data_map[variables[j]][i] for j in range(1, len(variables))]
for i in range(len(time_series))
]

# Plot the raw data
import matplotlib.pyplot as plt
import pydlm.plot.dlmPlot as dlmPlot
dlmPlot.plotData(range(len(time_series)),
time_series,
showDataPoint=False,
label='raw_data')
plt.legend(loc='best', shadow=True)

dlmPlot.plotData(
range(len(time_series)), time_series, showDataPoint=False, label="raw_data"
)
plt.legend(loc="best", shadow=True)
plt.show()

# Build a simple model
from pydlm import dlm, trend, seasonality

# A linear trend
linear_trend = trend(degree=1, discount=0.95, name='linear_trend', w=10)
linear_trend = trend(degree=1, discount=0.95, name="linear_trend", w=10)
# A seasonality
seasonal52 = seasonality(period=52, discount=0.99, name='seasonal52', w=10)
seasonal52 = seasonality(period=52, discount=0.99, name="seasonal52", w=10)

simple_dlm = dlm(time_series) + linear_trend + seasonal52
simple_dlm.fit()

# Plot the fitted results
simple_dlm.turnOff('data points')
simple_dlm.turnOff("data points")
simple_dlm.plot()
# Plot each component (attribution)
simple_dlm.turnOff('predict plot')
simple_dlm.turnOff('filtered plot')
simple_dlm.plot('linear_trend')
simple_dlm.plot('seasonal52')
simple_dlm.turnOff("predict plot")
simple_dlm.turnOff("filtered plot")
simple_dlm.plot("linear_trend")
simple_dlm.plot("seasonal52")
# Plot the prediction give the first 350 weeks and forcast the next 200 weeks.
simple_dlm.plotPredictN(N=200, date=350)
# Plot the prediction give the first 250 weeks and forcast the next 200 weeks.
simple_dlm.plotPredictN(N=200, date=250)

# Build a dynamic regression model
from pydlm import dynamic
regressor10 = dynamic(features=features, discount=1.0, name='regressor10', w=10)

regressor10 = dynamic(features=features, discount=1.0, name="regressor10", w=10)
drm = dlm(time_series) + linear_trend + seasonal52 + regressor10
drm.fit()

# Plot the fitted results
drm.turnOff('data points')
drm.turnOff("data points")
drm.plot()
# Plot each component (attribution)
drm.turnOff('predict plot')
drm.turnOff('filtered plot')
drm.plot('linear_trend')
drm.plot('seasonal52')
drm.plot('regressor10')
drm.turnOff("predict plot")
drm.turnOff("filtered plot")
drm.plot("linear_trend")
drm.plot("seasonal52")
drm.plot("regressor10")
# Plot the prediction give the first 300 weeks and forcast the next 150 weeks.
drm.plotPredictN(N=150, date=300)
# Plot the prediction give the first 250 weeks and forcast the next 200 weeks.
Expand Down
10 changes: 9 additions & 1 deletion pydlm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# This is the PyDLM package

__all__ = ['dlm', 'trend', 'seasonality', 'dynamic', 'autoReg', 'longSeason', 'modelTuner']
__all__ = [
"dlm",
"trend",
"seasonality",
"dynamic",
"autoReg",
"longSeason",
"modelTuner",
]

from pydlm.dlm import dlm
from pydlm.modeler.trends import trend
Expand Down
Loading

0 comments on commit b283a84

Please sign in to comment.