Skip to content

Instantly share code, notes, and snippets.

@emrul
emrul / heap-sample.py
Created May 28, 2024 10:35 — forked from timvieira/heap-sample.py
Fast sampling from an evolving distribution
import numpy as np
from numpy.random import uniform
def update(S, k, v):
"Update value position `k` in time O(log n)."
d = S.shape[0]
i = d//2 + k
S[i] = v
while i > 0:
@emrul
emrul / rainbow_dqn.py
Last active August 25, 2023 13:45
Tianshou DQN with Temporarlly-extended epsilon greedy exploration
from typing import Union
from tianshou.data import Batch
from tianshou.policy import RainbowPolicy
import numpy as np
import argparse
# See https://arxiv.org/abs/2006.01782 for paper - Temporally-Extended ε-Greedy Exploration
# See https://www.youtube.com/watch?v=Gi_B0IqscBE for video explaining where it's helpful
# See https://github.com/thu-ml/tianshou/blob/master/examples/atari/atari_rainbow.py for full example of how to setup args/net/collectors/etc.
def get_args():
parser = argparse.ArgumentParser()
@emrul
emrul / Consumer.kt
Created January 31, 2022 23:25 — forked from cyberdelia/Consumer.kt
Kafka + Kotlin + Coroutines
package com.lapanthere.bohemia
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import org.apache.kafka.clients.consumer.ConsumerRecord
import org.apache.kafka.clients.consumer.KafkaConsumer
import java.time.Duration
fun <K, V> KafkaConsumer<K, V>.asFlow(timeout: Duration = Duration.ofMillis(500)): Flow<ConsumerRecord<K, V>> =
@emrul
emrul / ComponentBag.kt
Created February 26, 2021 23:25 — forked from avwie/ComponentBag.kt
Usage of companion objects
package nl.avwie
interface Tag<T>
interface Tagged<T> {
val tag: Tag<T>
}
class ComponentBag {
private val components = mutableMapOf<Tag<*>, Any>()
@emrul
emrul / GraphileTestPlugin.js
Created May 11, 2020 21:53
Attempt at creating graphile plugin
const myOwnEscapeFn = (sql, sqlFragment, type) => {
const actualType = type.domainBaseType || type;
if (actualType.category === "N") {
if (actualType.id === 20) {
return sql.fragment `to_b64_extid(${sqlFragment})`;
}
if (["21" /* int2 */,
"23" /* int4 */,
"700" /* float4 */,
"701" /* float8 */
@emrul
emrul / P2Engine.cs
Created July 13, 2019 20:25
C#/.NET implementation of P2 (P-Squared) algorithm for calculating percentiles on streaming data
using System;
using System.Collections.Generic;
/*
* References:
* Initial idea: https://www.cse.wustl.edu/~jain/papers/ftp/psqr.pdf
* Based on implementation: https://github.com/skycmoon/P2
*
* Author:
* Emrul Islam <emrul@emrul.com>
import torch
from flair.data import Sentence, Dictionary
from flair.data_fetcher import NLPTaskDataFetcher, NLPTask
from flair.embeddings import TokenEmbeddings, WordEmbeddings, StackedEmbeddings, CharacterEmbeddings, \
PooledFlairEmbeddings, FlairEmbeddings
from flair.visual.training_curves import Plotter
from flair.trainers import ModelTrainer
from flair.models import SequenceTagger
from flair.datasets import ColumnCorpus
@emrul
emrul / useful_pandas_snippets.py
Created March 18, 2018 10:29 — forked from bsweger/useful_pandas_snippets.md
Useful Pandas Snippets
# List unique values in a DataFrame column
# h/t @makmanalp for the updated syntax!
df['Column Name'].unique()
# Convert Series datatype to numeric (will error if column has non-numeric values)
# h/t @makmanalp
pd.to_numeric(df['Column Name'])
# Convert Series datatype to numeric, changing non-numeric values to NaN
# h/t @makmanalp for the updated syntax!
@emrul
emrul / enum.sql
Created February 9, 2018 10:41 — forked from d11wtq/enum.sql
Renaming an ENUM label in PostgreSQL
/*
Assuming you have an enum type like this.
You want to rename 'pending' to 'lodged'
*/
CREATE TYPE dispute_status AS ENUM('pending', 'resolved', 'open', 'cancelled');
BEGIN;
ALTER TYPE dispute_status ADD VALUE 'lodged';
UPDATE dispute SET status = 'lodged' WHERE status = 'pending';
@emrul
emrul / Example.kt
Last active February 8, 2018 19:08
Hack to get Jsoniter to recognise JSONProperty annotations on Kotlin data classes
data class User(@JsonProperty("userName") val name: String)
val userObj = User("John")
JsoniterKotlinSupport.enable()
JsoniterAnnotationSupport.enable()
val jsonUserString = JsonStream.serialize(userObj)
// jsonUserString will contain: {"userName":"John"}