Tag Archives: code

Updating your local forked project by a commit to the main project?

This is from my stackoverflow question. Thanks to "bitoiu". Here is the real thread.

How to pick up a single commit from a remote repo

Assuming you have a local clone of the repo you forked if you type in the following you should get a single origin:

> git show remote
origin

Unless you've added the original's repo location, you won't have access to the commit you want to pick into your local one. So we need to add that, let's assume this repo ishttps://github.com/GitbookIO/gitbook.git. Notice this is an HTTPS clone URL because you won't have write access to this repo. Let's name it original_repo:

> git remote add original_repo https://github.com/GitbookIO/gitbook.git

And now let's get all the refs back:

> git fetch origina_repo

At this point you have all you need locally, you'll just need to merge the commit into one of your branches, let's assume your local master.

Find the commit you want to merge. This implies finding it in one of the branches the team used. Could be already merged to master or you could be picking it up from the branch that was used for the pull request. Either way, just run a series of git log to check what commit you want if you don't know the reference. When you do simply go to the branch where you want to merge the commit to and run:

> git cherry-pick COMMIT_ID

This will bring the commit to whatever branch you are at the moment.

How to merge a branch from a remote repo

The only difference in this steps is that instead of doing the cherry-pick you will be doing a merge. So imagine the contents of the pull request are in a branch named so-pr, you would simply do:

> git merge original_repo/so-pr

And that would merge the contents of so-pr into your working branch.

Kohonen Learning Procedure K-Means vs Lloyd's K-means

K-means maybe the most common data quantization method, used widely for many different domain of problems. Even it relies on very simple idea, it proposes satisfying results in a computationally efficient environment.

Underneath of the formula of K-means optimization, the objective is to minimize the distance between data points to its closest centroid (cluster center). Here we can write the objective as;

argmin sum_{i=1}^{k}sum_{x_j in S_i} ||x_j - mu_i||^2

mu_i is the closest centroid to instance x_j.

Continue reading Kohonen Learning Procedure K-Means vs Lloyd's K-means

Passing multiple arguments for Python multiprocessing.pool

Python is a very bright language that is used by variety of users and mitigates many of pain.

One of the core functionality of Python that I frequently use is multiprocessing module. It is very efficient way of distribute your computation  embarrassingly.

If you read about the module and got used, at some point you will realize, there is no way proposed to pass multiple arguments to parallelized function. Now, I will present a way to achieve in a very Pythonized way.

For our instance, we have two lists with same number of arguments but they need to be fed into the function which is pooling.

Here we have self cover code:

Fundamental Sort Algorithms in Python

As a rusty researcher at coding, I spend some to revise my algorithm knowledge. At some part, I coded basic sorting algorithms in Python as  a good and concise practice (even the result is not very efficient as C or C++). However you would like to check the code and clean up your memory or edit the code in some efficient manner. Continue reading Fundamental Sort Algorithms in Python

Two way python dictionary

Here there is a simple class implementation of Two way Dictionary that uses from native dictionary class of Python. The main idea of that kind of data structure to reach, but side of the data by using other side as the key. It is like bi-directional relation between items.

A dumb use case:

d = TwoWayDict()
d['erogol'] = 13
print d['erogol']
# outputs 13
print d[13]
# outputs erogol

Here is the class implementation. However, keep in mid that this class uses more x2 memory to keep the data with that functionality.

class TwoWayDict(dict):
    def __len__(self):
        return dict.__len__(self) / 2

    def __setitem__(self, key, value):
        dict.__setitem__(self, key, value)
        dict.__setitem__(self, value, key)

Hope this helps in some hinge 🙂