相当于Python中的R runmin和runmax函数?

时间:2017-01-17 22:22:41

标签: python r python-2.7 numpy

我正在尝试将一些R脚本翻译为Python,但我找不到与runmin runmax caTools相同的shift <- 250 ... min <- runmin(Array, k = shift, align = "right") max <- runmax(Array, k = shift, align = "right") "http.proxyStrictSSL": false

这是R代码:

/*
 * get_cpu_position_in_core(cpu)
 * return the position of the CPU among its HT siblings in the core
 * return -1 if the sibling is not in list
 */
int get_cpu_position_in_core(int cpu)
{
    char path[64];
    FILE *filep;
    int this_cpu;
    char character;
    int i;

    sprintf(path,
        "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
        cpu);
    filep = fopen(path, "r");
    if (filep == NULL) {
        perror(path);
        exit(1);
    }

    for (i = 0; i < topo.num_threads_per_core; i++) {
        fscanf(filep, "%d", &this_cpu);
        if (this_cpu == cpu) {
            fclose(filep);
            return i;
        }

        /* Account for no separator after last thread*/
        if (i != (topo.num_threads_per_core - 1))
            fscanf(filep, "%c", &character);
    }

    fclose(filep);
    return -1;
}

1 个答案:

答案 0 :(得分:3)

我认为你的意思是runmin and runmax from caTools

在Python中,您可以使用scipy中的minfiltermaxfilterk中的runmin参数等同于移动窗口的size参数。

>>> from scipy.ndimage import minimum_filter, maximum_filter
>>> minimum_filter([5, 6, 4, 7, 8, 9, 10], size=3)
array([5, 4, 4, 4, 7, 8, 9])

>>> maximum_filter([5, 6, 4, 7, 8, 9, 10], size=3)
array([ 6,  6,  7,  8,  9, 10, 10])

但是它不支持align。应该可以使用origin代替:

>>> maximum_filter([5, 6, 4, 7, 8, 9, 10], size=3, origin=1)
array([ 6,  6,  6,  7,  8,  9, 10])
>>> maximum_filter([5, 6, 4, 7, 8, 9, 10], size=3, origin=-1)
array([ 6,  7,  8,  9, 10, 10, 10])

我会说右对齐等于origin=(size//2)(或否定),但您可能需要自己验证。