Skip to content

Instantly share code, notes, and snippets.

View hellpanderrr's full-sized avatar
🎯
Focusing

hellpanderrr

🎯
Focusing
View GitHub Profile
@hellpanderrr
hellpanderrr / Python concave hull ( alpha shape ) .md
Last active April 27, 2022 22:51
Concave hull in python using scipy and networkx
from scipy.spatial import Delaunay, ConvexHull
import networkx as nx
 
points = [ [0,0],[0,50],[50,50],[50,0],[0,400],[0,450],[50,400],[50,450],[700,300],[700,350],[750,300],[750,350],
          [900,600],[950,650],[950,600],[900,650]
]
def concave(points,alpha_x=150,alpha_y=250):
    points = [(i[0],i[1]) if type(i) <> tuple else i for i in points]
    de = Delaunay(points)
@hellpanderrr
hellpanderrr / python polygon scale .md
Last active April 21, 2022 11:24
python convex polygon scaling
import itertools as IT
def scale_polygon(path,offset):
    center = centroid_of_polygon(path)
    for i in path:
        if i[0] > center[0]:
            i[0] += offset
        else:
            i[0] -= offset
        if i[1] > center[1]: 
@hellpanderrr
hellpanderrr / clear_encoding.md
Last active August 29, 2015 14:21
Python pandas clean bad encoding in a data frame
def clear(df):
    array = df.values
    for n,i in enumerate(array):
        for k,j in enumerate(i):
            if type(j) == str:
                try:
                     j.decode('ascii')
                except:        
 array[n][k] = j.decode('utf-8')
@hellpanderrr
hellpanderrr / Python read Excel XML Table.md
Last active March 24, 2021 04:03
Read Excel XML table in a python list
from bs4 import BeautifulSoup
 
def read_excel_xml(path):
    file = open(path).read()
    soup = BeautifulSoup(file,'xml')
    workbook = []
    for sheet in soup.findAll('Worksheet'): 
        sheet_as_list = []
 for row in sheet.findAll('Row'):
@hellpanderrr
hellpanderrr / python elapsed time context .md
Last active August 29, 2015 14:22
Print time needed for code execution
import time
class print_time:
    def __enter__(self):
        self.time = time.time()
        return self.time
    def __exit__(self, type, value, traceback):
        time_elapsed = time.time() - self.time
        print time_elapsed
@hellpanderrr
hellpanderrr / Pandas + Ipython Notebook dataframe css.py
Last active August 29, 2015 14:23
Pandas highlight even rows in Ipython Notebook
from IPython.display import HTML
HTML('''
<style>
.df tbody tr:nth-child(even) { background-color: lightblue; }
</style>
''' + df.to_html(classes='df'))
@hellpanderrr
hellpanderrr / context_exception.md
Last active August 29, 2015 14:24
Python handling exception in a context manager
from contextlib import contextmanager

@contextmanager
def catch(*exceptions, **kwargs):
    try:
        yield kwargs.get("default", None)
    except exceptions or Exception:
        print kwargs
 
@hellpanderrr
hellpanderrr / vector_intersection.md
Last active April 2, 2023 18:45
Python find intersection of two vectors using matplotlib and numpy
from numpy import dot,array,empty_like
from matplotlib.path import Path

def make_path(x1,y1,x2,y2):
    return Path([[x1,y1],[x1,y2],[x2,y2],[x2,y1]])

def perp( a ) :
    b = empty_like(a)
 b[0] = -a[1]
@hellpanderrr
hellpanderrr / get_filelist.py
Created July 15, 2015 13:54
python get files in a folder
def get_filelist(path, extension=None,only_folders=False):
'''Returns list of files in a given folder, without going further
Parameters
---------
extension: Collect only files with this extension
only_folders: Collect only folders names
'''
filenames = []
if not only_folders:
@hellpanderrr
hellpanderrr / sort_df.py
Created July 27, 2015 12:26
Pandas sort dataframe using custom function
def sort_df(df, column_idx, key):
'''Takes dataframe, column index and custom function for sorting,
returns dataframe sorted by this column using this function'''
col = df.ix[:,column_idx]
temp = pd.DataFrame([])
temp[0] = col
temp[1] = df.index
temp = temp.values.tolist()
df = df.ix[[i[1] for i in sorted(temp, key=key)]]