Skip to content

Commit

Permalink
update rscript
Browse files Browse the repository at this point in the history
  • Loading branch information
fenke committed Jul 19, 2024
1 parent f3a8fd3 commit 640a9e6
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 25 deletions.
2 changes: 1 addition & 1 deletion corebridge/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.2.15"
__version__ = "0.2.16"
4 changes: 2 additions & 2 deletions corebridge/rscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
'check_script_inputs', 'check_script_output', 'generate_checksum_file', 'run_script']

# %% ../nbs/02_rscriptbridge.ipynb 4
import json, os, fcntl, time
import json, os, fcntl
import subprocess
import hashlib

from functools import reduce
#from corebridge.aicorebridge import AICoreModule

# %% ../nbs/02_rscriptbridge.ipynb 7
def get_asset_path(script_name, assets_dir:str):
Expand Down
21 changes: 16 additions & 5 deletions nbs/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"outputs": [],
"source": [
"#| export\n",
"import typing\n",
"import typing, logging\n",
"import os\n",
"import numpy as np\n",
"import pandas as pd"
Expand All @@ -49,8 +49,9 @@
"outputs": [],
"source": [
"#| export\n",
"syslog = logging.getLogger(__name__)\n",
"try:\n",
" print(f\"Loading {__name__} from {__file__}\")\n",
" syslog.info()(f\"Loading {__name__} from {__file__}\")\n",
"except:\n",
" pass"
]
Expand Down Expand Up @@ -287,7 +288,7 @@
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"#| exports\n",
"def timeseries_dataframe_from_datadict(\n",
" data:dict, \n",
" timecolumns=None,\n",
Expand All @@ -302,18 +303,28 @@
" Returns:\n",
" - df: A pandas DataFrame with a DatetimeIndex representing the converted data.\n",
" \"\"\"\n",
"\n",
" orient = recordformat.lower()\n",
" assert orient in ['records', 'table', 'split', 'index', 'tight']\n",
" \n",
" assert timecolumns, 'No time columns specified'\n",
"\n",
" if orient == 'records':\n",
" # data is a structured ndarray, sequence of tuples or dicts, or DataFrame\n",
" df = pd.DataFrame.from_records(data)\n",
" time_column = [C for C in df.columns if C in timecolumns][0]\n",
" time_columns_in_df = [C for C in df.columns if C in timecolumns]\n",
" if not time_columns_in_df:\n",
" syslog.error(f\"No column in records {df.columns} matches specification in time columns {timecolumns}, assuming first column is time\")\n",
" time_column = df.columns[0]\n",
" else:\n",
" time_column = time_columns_in_df[0]\n",
"\n",
" elif orient == 'table':\n",
" # data is in pandas table format\n",
" time_column = data['schema']['primaryKey'][0]\n",
" df = pd.DataFrame.from_dict(data['data']).set_index(data['schema']['primaryKey'])\n",
" df.index.name = 'time'\n",
" else:\n",
" # data is formatted according to 'orient' parameter (pandas)\n",
" df = pd.DataFrame.from_dict(data, orient=orient)\n",
" time_column = df.index.name\n",
"\n",
Expand Down
24 changes: 20 additions & 4 deletions nbs/01_aicorebridge.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loading corebridge.core from /home/fenke/repos/corebridge/corebridge/core.py\n"
]
}
],
"source": [
"#| export\n",
"\n",
Expand Down Expand Up @@ -843,7 +851,7 @@
"]\n",
"Result Message\n",
" [\n",
" \"Startup time: 2024-07-08T13:46:30.324776+00:00\",\n",
" \"Startup time: 2024-07-17T09:51:57.026890+00:00\",\n",
" \"test_function((data: pandas.core.frame.DataFrame, anumber: float | numpy.ndarray = 0))\",\n",
" \"init_args: (1, 2), init_kwargs: {'num_1': 3, 'num_2': 4, 'assets_dir': None, 'save_dir': '/home/fenke/repos/corebridge/nbs/cache'}\",\n",
" \"lastSeen: False, recordformat: records, timezone: Europe/Amsterdam\",\n",
Expand Down Expand Up @@ -947,7 +955,15 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loading corebridge.aicorebridge 0.2.16 from /home/fenke/repos/corebridge/corebridge/aicorebridge.py\n"
]
}
],
"source": [
"\n",
"from corebridge.aicorebridge import AICoreModule\n"
Expand Down Expand Up @@ -994,7 +1010,7 @@
"]\n",
"Result Message\n",
" [\n",
" \"Startup time: 2024-07-08T13:46:30.367098+00:00\",\n",
" \"Startup time: 2024-07-17T09:51:57.093724+00:00\",\n",
" \"test_function((data: pandas.core.frame.DataFrame, anumber: float | numpy.ndarray = 0))\",\n",
" \"init_args: (1, 2), init_kwargs: {'num_1': 3, 'num_2': 4, 'assets_dir': None, 'save_dir': '/home/fenke/repos/corebridge/nbs/cache'}\",\n",
" \"lastSeen: False, recordformat: records, timezone: Europe/Amsterdam\",\n",
Expand Down
44 changes: 32 additions & 12 deletions nbs/02_rscriptbridge.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"outputs": [],
"source": [
"from nbdev.showdoc import *\n",
"import addroot"
"#import addroot"
]
},
{
Expand All @@ -46,11 +46,11 @@
"source": [
"#| export\n",
"\n",
"import json, os, fcntl, time\n",
"import json, os, fcntl\n",
"import subprocess\n",
"import hashlib\n",
"\n",
"from functools import reduce"
"#from corebridge.aicorebridge import AICoreModule"
]
},
{
Expand Down Expand Up @@ -93,6 +93,9 @@
"outputs": [],
"source": [
"#| hide\n",
"\n",
"from functools import reduce\n",
"\n",
"from IPython.display import display\n",
"from IPython.display import Markdown\n",
"\n",
Expand Down Expand Up @@ -186,7 +189,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Sapflow prediction scripts\n"
"## Example: sapflow prediction scripts\n"
]
},
{
Expand Down Expand Up @@ -509,7 +512,7 @@
{
"data": {
"text/plain": [
"['lubridate', 'zoo', 'stringr', 'mgcv']"
"['lubridate', 'stringr', 'mgcv', 'zoo']"
]
},
"execution_count": null,
Expand Down Expand Up @@ -592,6 +595,12 @@
" date, intersect, setdiff, union\n",
"\n",
"\n",
"Library stringr already installed\n",
"\n",
"Library mgcv already installed\n",
"Loading required package: nlme\n",
"This is mgcv 1.9-1. For overview type 'help(\"mgcv-package\")'.\n",
"\n",
"Library zoo already installed\n",
"\n",
"Attaching package: ‘zoo’\n",
Expand All @@ -600,12 +609,6 @@
"\n",
" as.Date, as.Date.numeric\n",
"\n",
"\n",
"Library stringr already installed\n",
"\n",
"Library mgcv already installed\n",
"Loading required package: nlme\n",
"This is mgcv 1.9-1. For overview type 'help(\"mgcv-package\")'.\n",
"\n"
]
}
Expand Down Expand Up @@ -1112,7 +1115,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Putting it together\n",
"## Putting it together\n",
"\n",
"We need to run a script when either any of it's inputs have changed or any \n",
"of it's outputs do not exist. Return True if a follow-up script should be \n",
Expand Down Expand Up @@ -1202,6 +1205,23 @@
"run_script(data_file_flow[0], assets_dir, save_dir) and run_script(data_file_flow[1], assets_dir, save_dir) and run_script(data_file_flow[2], assets_dir, save_dir)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### AICore module class"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class AICoreRModule(AICoreModule):\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
2 changes: 1 addition & 1 deletion settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
### Python library ###
repo = corebridge
lib_name = %(repo)s
version = 0.2.15
version = 0.2.16
min_python = 3.10
license = apache2
black_formatting = False
Expand Down

0 comments on commit 640a9e6

Please sign in to comment.