Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions Python_Engine/Python/src/python_toolkit/bhom/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,38 @@
import uuid
from functools import wraps
from typing import Any, Callable, Dict, List, Union
from datetime import datetime
from datetime import datetime, timezone

# pylint: enable=E0401

from .logging import ANALYTICS_LOGGER, CONSOLE_LOGGER
from .util import bson_unix_ticks, bson_unix_ticks_to_datetime
from . import BHOM_VERSION, TOOLKIT_NAME, BHOM_LOG_FOLDER, DISABLE_ANALYTICS

def _selected_item_factory():
return {"MethodName": "", "Parameters": [], "TypeName": ""}

def _custom_data_factory():
return {"interpreter", sys.executable}

def _time_factory():
return {"$date": 0}

@dataclass
class UsageLogEntry():
BHoMVersion:str = BHOM_VERSION
BHoM_Guid:uuid.UUID = uuid.uuid4()
BHoM_Guid:uuid.UUID = field(default_factory=uuid.uuid4)
CallerName:str = ""
ComponentId:uuid.UUID = uuid.uuid4()
CustomData:Dict = field(default_factory = {"interpreter", sys.executable})
Errors:List[str] = field(default_factory = [])
ComponentId:uuid.UUID = field(default_factory=uuid.uuid4)
CustomData:Dict = field(default_factory = _custom_data_factory)
Errors:List[str] = field(default_factory = list)
FileId:str = ""
FileName:str = ""
Fragments:List[str] = field(default_factory = [])
Fragments:List[str] = field(default_factory = list)
Name:str = ""
ProjectID:str = ""
SelectedItem:Dict = field(default_factory = {"MethodName": "", "Parameters": [], "TypeName": ""})
Time:Dict = field(default_factory = {"$date": 0})
SelectedItem:Dict = field(default_factory = _selected_item_factory)
Time:Dict = field(default_factory = _time_factory)
UI:str = "Python"
UiVersion:str = sys.version
_t:str = "BH.oM.Base.UsageLogEntry"
Expand Down Expand Up @@ -107,7 +116,7 @@ def summarise_usage_logs(usage_log_entries:List[UsageLogEntry]) -> List[Dict]:
return db_entries

def convert_exc_info_to_bhom_error(exc_info):
time = bson_unix_ticks(datetime.now(), short=True)
time = bson_unix_ticks(datetime.now(timezone.utc), short=True)
utcTime = bson_unix_ticks(short=True)
stack_trace = traceback.extract_tb(exc_info[2])
message = str(exc_info[1])
Expand Down
26 changes: 15 additions & 11 deletions Python_Engine/Python/src/python_toolkit/bhom/bhom_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ def deserialise_unknown(self, obj: Union['IObject', 'BHoMObject', dict]):

class BHoMJSONEncoder(JSONEncoder):
def default(self, o):
"""
default encoder for BHoMObject and other primitive types to BHoM style json.

The converter for datetime and pandas.Timestamp require timezone aware timestamps,
these are automatically converted to UTC where applicable and throw an error if they are not timezone aware.
"""
if isinstance(o, BHoMObject):
#initialise special BHoMObject properties

Expand Down Expand Up @@ -146,9 +152,7 @@ def default(self, o):
return [date.isoformat() for date in o]
elif isinstance(o, np.ndarray):
return o.tolist()
elif isinstance(o, pd.Timestamp):
return {"$date": bson_unix_ticks(o.to_pydatetime(), True)}
elif isinstance(o, datetime):
elif isinstance(o, datetime) or isinstance(o, pd.Timestamp):
return {"$date": bson_unix_ticks(o, True)}
elif isinstance(o, pd.Series):
return dict(zip(o.index.astype(str), o))
Expand Down Expand Up @@ -237,19 +241,19 @@ def __init__(
self,
_t: str, #don't make default, as subclasses of this class should set this with super().__init__
name: str = "",
bhom_guid: uuid.UUID = uuid.uuid4(),
tags: List[str] = [],
fragments: List[Dict[str, object]] = [],
custom_data: Dict[str, object] = {},
bhom_guid: uuid.UUID = None,
tags: List[str] = None,
fragments: List[Dict[str, object]] = None,
custom_data: Dict[str, object] = None,
_bhom_version: str = None,
**kwargs
) -> 'BHoMObject':
self._t = _t
self.name = name
self.bhom_guid = bhom_guid
self.fragments = fragments
self.tags = tags
self.custom_data = custom_data
self.bhom_guid = bhom_guid or uuid.uuid4()
self.fragments = fragments or []
self.tags = tags or []
self.custom_data = custom_data or {}
self._bhom_version = _bhom_version

#set non-CustomData properties with reflection.
Expand Down
18 changes: 13 additions & 5 deletions Python_Engine/Python/src/python_toolkit/bhom/util.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
"""General utility functions."""
# pylint: disable=E0401
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
import pandas as pd
from typing import Union
# pylint: enable=E0401


def bson_unix_ticks(date_time: datetime = datetime.utcnow(), short: bool = False) -> int:
def bson_unix_ticks(date_time: Union[datetime, pd.Timestamp] = None, short: bool = False) -> int:
"""Python implementation of unix ticks.

Args:
date_time (datetime, optional): The datetime to convert to ticks. Defaults to datetime.utcnow().
date_time (datetime, optional): The datetime to convert to ticks. Defaults to datetime.now(timezone.utc).
short (bool, optional): Whether to return the short ticks. Defaults to False.

Returns:
int: The ticks.
"""

_ticks = (date_time - datetime(1970, 1, 1)).total_seconds() * 10**3
if isinstance(date_time, pd.Timestamp):
date_time = date_time.tz_convert(None) #This throws an error if the pandas timestamp is not timezone aware. If no error the resultant date_time will be in UTC.
date_time = date_time.to_pydatetime().replace(tzinfo=timezone.utc) #Convert to python datetime and ensure it is a timezone aware with timestamp set to UTC.

date_time = date_time or datetime.now(timezone.utc)

_ticks = (date_time - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds() * 10**3

if short:
return int(_ticks)
Expand All @@ -27,5 +35,5 @@ def bson_unix_ticks_to_datetime(ticks: int, short:bool = False) -> datetime:
if not short:
ticks = int(ticks / (10**3))

return datetime(1970, 1, 1) + timedelta(milliseconds=ticks)
return datetime(1970, 1, 1, tzinfo=timezone.utc) + timedelta(milliseconds=ticks)

6 changes: 4 additions & 2 deletions Python_Engine/Python/src/python_toolkit/plot/cmap_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
def cmap_sample_plot(
cmap: Union[str, Colormap],
bounds: Optional[tuple] = None,
figsize: tuple = (9, 1),
figsize: Optional[tuple] = None,
bins: int = 256
) -> Figure:

Expand All @@ -19,12 +19,14 @@ def cmap_sample_plot(
Args:
cmap: Either a colormap string (e.g., 'viridis') or a custom Colormap object
bounds: Optional tuple of (vmin, vmax) for normalization. If None, uses (0, 1)
figsize: Figure size as (width, height)
figsize: Figure size as (width, height). If None, defaults to (9, 1)
bins: Number of discrete bins for the gradient. Defaults to 256.
Returns:
Matplotlib Figure object
"""

figsize = figsize or (9, 1)

# Set bounds
if bounds is None:
bounds = (0, 1)
Expand Down
2 changes: 1 addition & 1 deletion Python_Engine/Python/src/python_toolkit/plot/diurnal.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def diurnal(
minor_ticks = target_idx
major_ticklabels = [f"{i:02d}:00" for i in major_ticks]
elif period == "weekly":
group = series.groupby([series.index.dayofweek, series.index.hour])
group = series.groupby([series.index.day_of_week, series.index.hour])
target_idx = pd.MultiIndex.from_product([range(7), range(24)])
major_ticks = range(len(target_idx))[::12]
minor_ticks = range(len(target_idx))[::3]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def set_dimensions(df: pd.DataFrame, tick_mark_count: int, dp:int) -> List[Dict[
@bhom_analytics()
def parallel_coordinate_plot(

df: pd.DataFrame = pd.DataFrame(),
df: pd.DataFrame,
variables_to_show: list = None,
decimal_places: int = 0,
tick_mark_count: int = 11,
Expand Down
Loading