Skip to content

Instantly share code, notes, and snippets.

View otaviomguerra's full-sized avatar

Otávio Guerra otaviomguerra

  • CE, Brasil
View GitHub Profile
@otaviomguerra
otaviomguerra / debug-pod.yml
Created June 21, 2022 19:34
Dummy Kubernetes Pod for debugging inside the cluster.
apiVersion: v1
kind: Pod
metadata:
name: ubuntu-debug
spec:
containers:
- name: ubuntu
image: ubuntu:latest
# Just spin & wait forever
command: [ "/bin/bash", "-c", "--" ]
@otaviomguerra
otaviomguerra / limit_gpu_tf.py
Last active December 14, 2021 18:26
Limit GPU memory usage in Tensorflow training scripts
import tensorflow as tf
def set_gpu_memory_limit(memory_limit=6096):
"""Set limit to GPU memory usage.
from https://github.com/tensorflow/tensorflow/issues/43174#issuecomment-782222166
Parameters
----------
memory_limit : int, optional
Amount of memory to use in MegaBytes, by default 6096 MB (6GB)

MongoDB Cheat Sheet

Show All Databases

show dbs

Show Current Database

@otaviomguerra
otaviomguerra / sqlite3-in-python.py
Created May 24, 2021 19:49 — forked from 7aman/sqlite3-in-python.py
sqlite3 cheatsheet for python 3
#!/usr/bin/env python3
'''
Thanks to Andres Torres
Source: https://www.pythoncentral.io/introduction-to-sqlite-in-python/
'''
import sqlite3
# Create a database in RAM
@otaviomguerra
otaviomguerra / quantize.py
Created April 22, 2021 16:32
Apply quantization to NN model by converting to TFLite
import tensorflow as tf
import argh
# see: https://github.com/tensorflow/tensorflow/issues/46107
def quantize_model(
original_model_path='models/best-model.h5',
quantized_model_path='models/quantized_model.tflite'):
"""Converts .h5 model to .tflite by applying
quantization.
@otaviomguerra
otaviomguerra / timeout.py
Created November 16, 2020 10:49
Time out function execution in Python
# pip install func-timeout
from func_timeout import func_timeout, FunctionTimedOut
try:
func_result = func_timeout(10, func, args=(arg1, arg2))
except FunctionTimedOut:
print("The function could not complete within 10 seconds, hence terminated.\n")
except Exception as e:
print(f"ERROR: {e} on executing the function")
@otaviomguerra
otaviomguerra / compare_df.py
Created August 6, 2020 15:45
Check equality of 2 given pandas DataFrames
from pandas.testing import assert_frame_equal
assert_frame_equal(df1, df2, check_dtype=False)
@otaviomguerra
otaviomguerra / count_missing_spark.py
Created July 18, 2020 18:49
Count missing rows by column in pyspark
import pyspark.sql.functions as F
def count_missings(spark_df, sort=True):
"""
Counts number of nulls and nans in each column
"""
df = spark_df.select(
[
F.count(F.when(F.isnan(c) | F.isnull(c), c)).alias(c)
@otaviomguerra
otaviomguerra / mllib_regression_pipeline.py
Created July 15, 2020 10:45
Simple MLlib Regression Pipeline definition
from pyspark.ml import Pipeline
from pyspark.ml.regression import GBTRegressor
from pyspark.ml.feature import VectorAssembler, StandardScaler
from pyspark.ml.evaluation import RegressionEvaluator
# Get the names of the input features
input_cols = df.columns[:-1]
# Rename target col and split the dataset
df = df.withColumnRenamed('target_column_original_name', 'label')