So I was looking at the code here: https://github.com/biopython/biopython/blob/master/Bio/Cluster/cluster.c
And saw that the function "double median(int n, double x[])" can be improved. I wonder if this file is still in use or is it outdated?
This C implementation is ~~3.5 times faster on average when using g++ -O2 (2-5 times, depending on the input). And it can still be optimized more. The version for N-even is pretty much the same. https://drive.google.com/file/d/1uElcWd-Yf2pKdXP-azvFVUnS9AxmTcsX/view?usp=sharing
void medianOddCore(int n, double a[], int middle, int l, int r)
{
if (l >= r) return;
int i = l, j = r;
int x = (l + r) >> 1;
while (i<=j)
{
while (a[i] < a[x]) i++;
while (a[j] > a[x]) j--;
if (i<=j) {
std::swap(a[i], a[j]);
i++;
j--;
}
}
if (l <= middle && middle <= j)
medianOddCore(n, a, middle, l, j);
else
return medianOddCore(n, a, middle, i, r);
}
double medianOdd(int n, double a[])
{
medianOddCore(n, a, n / 2, 0, n-1);
return a[n / 2];
}
The google drive link contains all the sources code, which includes unit-testing for the function (using Biopython's median function as a baseline). It's in C++ to use the timer, file output for debug, ..; but the algorithm itself is purely C and does not need any library.
This is my first time looking at an open-source project, so I wonder if it can be added to Biopython? Thank you.
0 answers
No answers yet.
Log in to answer this question.
I think you probably should pose that question to the biopython devs at the above github address. Maybe fork the whole thing, add your changes and make a pull request.
Ah okay, the Biopython github linked to this page so I thought they also discuss here.
As others have said, you can offer your changes via opening a PR to the
biopythongithub repository.if its a drop-in replacement and doesn't break any of their tests, then they will likely accept it. You may need to write your own specific tests too.
If it does break anything however, it strikes me as unlikely that this function gets a huge amount of use, and the runtime savings given the frequency of its use may not justify a big refactor.
It's just a raw C function that takes an array and output a number. No extra library/big file/etc.
Thanks, so I'll fork and open a PR