Skip to content

Unsorted List of Python Snippets

holzkohlengrill edited this page Dec 15, 2023 · 2 revisions
# magic line
#!/usr/bin/env python3

# debugger
import pdb; pdb.set_trace()

# parallel debugger
class ForkedPdb(pdb.Pdb):
    """A Pdb subclass that may be used
    from a forked multiprocessing child

    """
    def interaction(self, *args, **kwargs):
        _stdin = sys.stdin
        try:
            sys.stdin = open('/dev/stdin')
            pdb.Pdb.interaction(self, *args, **kwargs)
        finally:
            sys.stdin = _stdin
ForkedPdb().set_trace()

# filepath
os.path.dirname(os.path.realpath(__file__))

# parallel running
from multiprocessing import Pool
import multiprocessing

def run_parallel(items, function, arguments, num_cpus=8):
    """
    divides items to num_cpus groups and
    starts function with each group + arguments in parallel threads
    """
    items = sorted(items)
    # build group of items that are matching number of cpus
    groups = []
    length = len(items) // multiprocessing.cpu_count()
    for x in range(0, len(items), length):
        groups.append(items[x: x + length])
    # add for each group the shared arguments
    params = [tuple([item_group] + list(arguments)) for item_group in groups]
    # run parallel
    pool = Pool(processes=num_cpus)
    return pool.starmap(function, params)

# ipython
%matplotlib inline
from IPython.display import display as d
import pandas as pd
import numpy as np
import seaborn as sns
sns.set(rc={'figure.figsize':(12,12)})

# argument parsing
import argparse
import sys

def main(_):
    # argument parsing
    parser = argparse.ArgumentParser(description='whatever',
                                     epilog="stg7 2018",
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("database", type=str, help="training database csv file (consists of video segment and rating value) or restricted yaml file")
    parser.add_argument("--feature_folder", type=str, default="features", help="folder for storing the features")
    parser.add_argument("--skip_feature_extraction", action="store_true", help="skip feature extraction step")
    parser.add_argument("--feaure_backtrack", action="store_true", help="backtrack all feature sets")
    parser.add_argument("--train_repetitions", type=int, default=1, help="number of repeatitions for training")
    parser.add_argument("--use_features_subset", action="store_true", help="use only a defined subset of features ({})".format(features_subset()))
    parser.add_argument("--model", type=str, default="models/hyfu.npz", help="output model")
    parser.add_argument("--mode", choices=[0,1], type=int, default=0, help="mode of model")
    parser.add_argument('--cpu_count', type=int, default=multiprocessing.cpu_count() // 2, help='thread/cpu count')
    parser.add_argument("--validation_database", type=str, help="database that is used for validation")
    parser.add_argument("--feature_folder_validation", type=str, default="features", help="folder where validation features are stored")

    a = vars(parser.parse_args())


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))

Clone this wiki locally