Skip to content

Instantly share code, notes, and snippets.

@mmechtley
mmechtley / ConvertLiftGammaGain.cs
Created December 16, 2022 05:21
Convert Lift Gamma Gain coefficients from PostProcessing V2 to URP 12.x
// URP has a tool to convert postprocessing volumes but it doesn't transform the coefficients so the visuals won't look the same
// This does the transformation to make them match.
private static void ConvertLiftGammaGainValues( VolumeProfile profile )
{
if( profile != null && profile.TryGet( out LiftGammaGain lgg ) && lgg.active )
{
if( lgg.lift.overrideState )
{
lgg.lift.value = new Vector4( Mathf.LinearToGammaSpace( lgg.lift.value.x * 0.2f / 0.15f ),
@mmechtley
mmechtley / EditorLayoutUtil.cs
Last active July 3, 2021 20:05
Generic MonoBehaviour inspector for Unity that adds type-specific color-coding to the inspector header for visual distinction. Doesn't work when inspectors are collapsed though. ):
using UnityEngine;
namespace Code.Editor.Utilities
{
public static class EditorLayoutUtil
{
public enum RectDirection
{
Horizontal,
Vertical
@mmechtley
mmechtley / PingableGameObjectList.cs
Last active April 2, 2021 18:57
A reusable Unity editor window to show a list of gameobjects, with buttons to ping them in the scene / prefab stage / project / etc.
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace Code.Utilities.Editor
{
public class PingableGameObjectList : EditorWindow
{
@mmechtley
mmechtley / AppScript.json
Last active June 7, 2024 00:31
Unity Google Sheets Localization
{
"webapp": {
"access": "ANYONE_ANONYMOUS",
"executeAs": "USER_DEPLOYING"
},
"exceptionLogging": "STACKDRIVER",
"oauthScopes": ["https://www.googleapis.com/auth/spreadsheets"],
"runtimeVersion": "V8"
}
@mmechtley
mmechtley / gist:6c30d557e3d68c234b67
Created June 8, 2015 11:27
TouchKO Loading Level
#pragma strict
#pragma implicit
#pragma downcast
var destroyOnLevelLoad:GameObject[];
// For tracking sessions for Blurst
static var startRecorded:boolean = false;
function Start()
@mmechtley
mmechtley / gist:b292733d76b9700d52dc
Created March 20, 2015 16:34
Python SequenceMatcher for finding a similarly-named file
"""
Here's a cute example of using Python's builtin difflib support to find a file with the closest matching name
"""
from difflib import SequenceMatcher
# Suppose we have some files (databases here) with a certain naming scheme.
db_files = ['out_NDWFS_1425+3254_J_db.hdf5', 'out_NDWFS_1425+3254_H_db.hdf5']
# Now we have several other files (model definitions here) that have a similar naming scheme
py_files = ['model_NDWFS_1425+3254_J.py', 'model_NDWFS_1425+3254_H.py']
for db_file in db_files:
import pymc as mc
import numpy as np
import pyfits as pf
arr = pf.getdata('stack3_gold.fits')
x=y=np.arange(0,71)
x,y=np.meshgrid(x,y)
err_map = pf.getdata('stack3wht_gold.fits')
def model((x,y),arr):
amp = mc.Uniform('amp',lower=0,upper=np.amax(arr),doc='Amplitude')
@mmechtley
mmechtley / shuffle.py
Created July 10, 2013 06:02
Numpy shuffled axis example
import numpy as np
arr = np.arange(100).reshape(4,5,5)
indexes = np.arange(5)
np.random.shuffle(indexes)
dim2shuff = arr[:,indexes,:]
dim3shuff = arr[:,:,indexes]
@mmechtley
mmechtley / gist:5786263
Last active December 18, 2015 12:58
Naive SVD-based image compression
from pylab import *
def makeApprox(channel,n=20):
u,eigVals,vt = svd(channel)
approx = zeros_like(channel)
# for i in xrange(n):
# approx += outer(u[:,i],vt[i,:])*eigVales[i]
approx = dot(u[:,:n]*eigVals[:n], vt[:n,:])
## Fractional error in matrix approximation
error = sqrt(sum(eigVals[n+1:]**2)/sum(eigVals**2))
@mmechtley
mmechtley / model_config.py
Last active April 13, 2022 05:31
Python Metaprogramming: An example of how to use Abstract Syntax Trees (ast module) in Python to turn bare expressions (not bound to any target variable names) in an end-user configuration file into elements of a list.
"""
This file is made by the user, to set up model components for a Markov Chain
Monte Carlo galaxy simulator.
For simplicity, I didn't want to force the user to bind everything to variables,
or follow some particular naming scheme, or remember to add them to a list, etc.
YAML or such seemed like overkill, since python keyword arguments already make it
pretty human-readable
"""
from psfMC.ModelComponents import Sky, PSF, Sersic
from psfMC.distributions import Normal, Uniform