Skip to content

Instantly share code, notes, and snippets.

@xujiboy
xujiboy / rocker_ml_metaflow.Dockerfile
Last active December 7, 2020 20:13
Dockerfile to install Netflix/metaflow on top of rocker/ml
FROM rocker/ml:4.0.2
USER rstudio
WORKDIR /home/rstudio/
RUN Rscript -e 'devtools::install_github("Netflix/metaflow", subdir="R")'
RUN Rscript -e 'metaflow::install_metaflow(method="conda", prompt=FALSE)'
ENV USERNAME mfuser
RUN Rscript -e 'metaflow::test()'
USER root
@xujiboy
xujiboy / test_foreach_flow.py
Last active May 22, 2020 21:22
A foreach metaflow to test
from metaflow import FlowSpec, step, Parameter
def script_path(filename):
"""
A convenience function to get the absolute path to a file in this
tutorial's directory. This allows the tutorial to be launched from any
directory.
"""
@xujiboy
xujiboy / multiclass_ORC.py
Created September 16, 2019 16:20
plot ORC with AUC information for multi-class classification models
# Compute ROC curve and ROC area for each class in multi-class classification tasks
from sklearn.metrics import roc_curve, auc
from scipy import interp
from itertools import cycle
from typing import Tuple, List
def ROC(y_val: np.ndarray, y_score: np.ndarray) -> Tuple[dict, dict, dict]:
fpr = dict()
@xujiboy
xujiboy / find_max_depth_b_tree_2.py
Last active June 15, 2019 23:19
[B-Tree] find maximum depth of a binary tree
# ask the node to always return the maxinum depth
# of the subtree that it is leading
Class MaxDepth:
def __init__(self):
pass
def max_depth(self, node):
# remember 1-2-3:
# here pre-order traversal is applied
@xujiboy
xujiboy / max_depth_b_tree.py
Last active June 15, 2019 22:46
[B-Tree] find maximum depth of a binary tree
## ENTERING: if leaf, update self.max_depth & return
## Traversal: carries depth+1 around
Class MaxDepth:
def __init__(self):
self.max_depth = 0
def max_depth(node, depth=0):
if not node: # empty tree or Null
return
@xujiboy
xujiboy / query_unique_value.py
Last active December 4, 2018 22:29
A method to query the unique values of a partition (name) from a `ParquetDataset`, using `pieces`.
import re
import pyarrow.parquet as pq
def query_unique_value(dataset: pq.ParquetDataset,
partition: str
) -> set:
''' query the unique values of a given partition name from a give `ParquetDataset`, returns a set.
Parameters
----------
@xujiboy
xujiboy / python_built_in_decorator_example.py
Last active August 26, 2018 19:07
Example code for class "Circle" showing the difference between decorator function @Property, @classmethod, and @staticmethod
# example class Circle showing the difference between
# decorator function @property, @classmethod, and
# @staticmethod
# source: https://realpython.com/primer-on-python-decorators/
# pls credit the source if you choose to use this snippet
class Circle:
def __init__(self, radius):
self._radius = radius
@xujiboy
xujiboy / decorator_example.py
Created August 25, 2018 19:15
a code snippet from https://realpython.com/primer-on-python-decorators as an example of decorator class in python
# source: https://realpython.com/primer-on-python-decorators/
# please credit the source if you choose to use this snippet.
import functools
import time
def timer(func):
"""Print the runtime of the decorated function"""
@functools.wraps(func)
def wrapper_timer(*args, **kwargs):