Skip to content

Instantly share code, notes, and snippets.

#!/bin/zsh
# A shell script that generates files provided in the sphinx tutorial:
# https://www.sphinx-doc.org/en/master/tutorial/getting-started.html
# If you want start from a clean folder run `rm -rf lumache` first
# run as ./sphinx_tutorial.sh
# Ensure you have the following packages installed
# uv pip install sphinx pydata-sphinx-theme numpy sphinx-rtd-theme furo
mkdir lumache
cd lumache
"""
Reference solution:
https://www.geeksforgeeks.org/estimating-value-pi-using-monte-carlo/
Accelerated examples:
https://bede-documentation.readthedocs.io/en/latest/guides/wanderings/Estimating-pi-in-CUDALand.html
https://dev.to/joaomcteixeira/parallelized-vectorization-with-dask-a-monte-carlo-example-3hg0
I tinker with N_ITERATIONS to keep the time around one second. This could be
improved by adding a stop after one second.
"""
problem is defined in https://leetcode.com/problems/number-of-islands/
Depth-first = explore each possible branch of tree before traversing
Breadth-first = explore each node at current depth before continuing
Don't re-invent the wheel solution:
from skimage.measure import label
import numpy as np
grid = [
from __future__ import annotations
import typing
from dataclasses import dataclass
from statistics import mean
from typing import Callable, Final, List, Literal, Tuple, Union
Tickers = Literal["IMB", "APL"]
DataCols = Literal["Open", "Close"]
Cols = Literal["Ticker", "Date", "Open", "Close"]
>>> schema = {
"a": pa.timestamp("s"),
"b": pa.int32(),
"c": pa.string(),
"d": pa.int32(),
"e": pa.int32(),
"f": pa.int32(),
}
>>> df.to_parquet("df.parquet", engine="pyarrow", schema=schema)
import dask
import dask.array as da
import numpy as np
from ..core import histogram
def empty_dask_array(shape, dtype=float, chunks=None):
# a dask array that errors if you try to compute it
def raise_if_computed():
netcdf TwoD {
dimensions:
altitude_above_msl = 1 ;
height_above_ground = 1 ;
height_above_ground1 = 1 ;
height_above_ground_layer = 1 ;
height_above_ground_layer_bounds_1 = 2 ;
reftime = 10 ;
reftime1 = 4 ;
reftime2 = 3 ;
>>> import s3fs
>>> import xarray as xr
>>> s3 = s3fs.S3FileSystem(anon=True)
>>> file = s3.open('s3://fmi-opendata-rcrhirlam-surface-grib/2021/02/03/00/numerical-hirlam74-forecast-MaximumWind-20210203T000000Z.grb2')
>>> ds = xr.open_dataset(file, engine="cfgrib")
Can't create file '<File-like object S3FileSystem, fmi-opendata-rcrhirlam-surface-grib/2021/02/03/00/numerical-hirlam74-forecast-MaximumWind-20210203T000000Z.grb2>.90c91.idx'
Traceback (most recent call last):
File "/Users/ray.bell/miniconda/envs/test_env/lib/python3.8/site-packages/cfgrib/messages.py", line 342, in from_indexpath_or_filestream
with compat_create_exclusive(indexpath) as new_index_file:
import numpy as np
import pandas as pd
from sklearn.svm import OneClassSVM
X = np.random.rand(100, 1)
nu = np.geomspace(0.0001, 1, num=100)
df = pd.DataFrame(data={'nu': nu})
for i in range(0, len(X)):
import pandas as pd
import numpy as np
data = {"policy": ["1-3 a", "1-3 a", "2-9 c"], "pw": ["abcde", "cdefg", "ccccccccc"]}
df = pd.DataFrame.from_dict(data)
df["min"] = df.policy.str[0].astype(int)
df["max"] = df.policy.str[2].astype(int)
df["let"] = df.policy.str[4]