Detecting clusters in a binary sequence I have a binary sequence such as 11111011011110101100000000000100101011011111101111100000000000011010100000010000000011101111
Where clusters of mostly 1's are followed by a larger number of zeros, like in the picture below (black stands for 1):

I would like to apply a technique (preferably in R or in Python) where I can automatically detect these clusters of 1's, and produce spans (denoted as red lines in the image). I know one could do this with a threshold, i.e. saying that two clusters must be seperated by at least n 0's to be clusters, but I wonder if there are other established methods which do not use predefined thresholds. 
Any idea? 
 A: I would avoid calling them "clusters". With this terminology you end up getting distracted into multidimensional techniques from data mining all the time.
Your problem is a much simpler one dimensional setting. And even simpler: you don't even have coordinates but an array of zeros and ones.
There will not be a one-size-fits all solution for your problem ever. Because one user might want to read very high resolution "barcodes", while the other user has a lot of noise.
So in the end, you will need to have one parameter. You have a number of choices: absolute gap sizes, relative gap sizes, kernel bandwidth etc.
A very simple "kernel based" approach would be to map each pixel to the number of pixels set in -10...+10. So that is 21 cells, the value will be 0 to 21. Now look for a local minimum. Increase the window size, if it starts splitting runs that you did not yet want to split.
A: Reference 1 on pages 49-55 has nice section on kernel based methods that might be useful here.  If I were doing it then I would look at some weighted sum of the actual values and their first derivative because it might be a better indicator of "information".
Reference:
http://amzn.com/0198538642  "Neural Networks for Pattern Recognition" by Christopher Bishop.(1995)
A: The problem has some similarity with image processing. You have a binary image with a height of one pixel and want to achieve some sort of segmentation.
The nature of the input image suggests a morphological filter to smooth the regions, e.g. closing. You'd need to choose the structuring element that thereby determines the "linkage" of the clusters. In the end this is pretty similar to your approach. You could also smooth the image using convolution filters, e.g. using blur, or gaussian kernel and apply a chosen threshold to re-binarize it. 
If you can treat every 1 as a point, its position in the sequence as a coordinate, and can make up some distance metric, you could use pretty much every standard clustering algorithm there is. 
For example, you could use hierarchical clustering (choose a linkage criterion and a threshold), you could use k-means or an EM with a gaussian mixture model (choose the number of clusters you are looking for).
But I don't think, you may eventually getting away without having to predefine the sensitivity of the algorithm at least.
