File size: 389,612 Bytes
0a2e9f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
# from .cloud_storage import create_cloud_fuse
import argparse
import base64
import copy
import cv2
import glob
import imghdr
import inspect
import json
import logging
import math
import numpy as np
import os
import os.path as op
import pymongo
import random
import re
import shutil
from collections import OrderedDict
import sys
import time
import unicodedata
import yaml
from .tsv_io import TSVSplitProperty
from .tsv_io import CompositeTSVFile
from .common import pilimg_from_base64
from .common import dict_get_all_path
from .tsv_io import get_tsv_associates
from .common import dict_get_path_value
from .common import qd_tqdm
from .common import qd_tqdm as tqdm
from .common import parallel_map
from .common import split_to_chunk_to_range
from .common import ensure_remove_file
from .common import get_tmp_folder

from collections import defaultdict
from deprecated import deprecated
from pathos.multiprocessing import ProcessingPool as Pool
from datetime import datetime
from pprint import pformat
try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO
from future.utils import viewitems
from .cloud_storage import CloudStorage
from .process_image import draw_bb, show_image, save_image
from .process_image import show_images
from .process_image import draw_rects
from .common import calculate_image_ap
from .common import calculate_iou
from .common import dict_to_list
from .common import ensure_directory
from .common import normalize_to_str
from .common import generate_lineidx
from .common import get_mpi_rank, get_mpi_size
from .common import hash_sha1
from .common import ensure_copy_file
from .common import img_from_base64
from .common import init_logging
from .common import json_dump
from .common import list_to_dict, list_to_dict_unique
from .common import parse_test_data
from .common import read_to_buffer
from .tsv_io import load_list_file
from .common import worth_create
from .tsv_io import write_to_file
from .common import write_to_yaml_file, load_from_yaml_file
from .common import float_tolorance_equal
from .common import is_positive_uhrs_verified, is_negative_uhrs_verified
from .tsv_io import concat_files
from .common import try_delete
from .common import encoded_from_img
from .common import get_frame_info
from .common import calc_mean
from .taxonomy import child_parent_print_tree2
from .taxonomy import create_markdown_url
from .taxonomy import disambibuity_noffsets
from .taxonomy import gen_noffset
from .taxonomy import gen_term_list
from .taxonomy import get_nick_name
from .taxonomy import is_noffset
from .taxonomy import LabelToSynset, synset_to_noffset
from .taxonomy import load_all_tax, merge_all_tax
from .taxonomy import noffset_to_synset
from .taxonomy import populate_cum_images
from .taxonomy import populate_url_for_offset
from .taxonomy import Taxonomy
from .tsv_io import create_inverted_list
from .tsv_io import create_inverted_list2
from .tsv_io import extract_label
from .tsv_io import get_meta_file
from .tsv_io import load_labels
from .tsv_io import TSVDataset, TSVFile
from .tsv_io import tsv_reader, tsv_writer
from .tsv_io import is_verified_rect
from .tsv_io import tsv_copy
from .tsv_io import tsv_writers
from .tsv_io import get_default_splits
from .db import create_mongodb_client
from .db import create_bbverification_db
from .tsv_io import get_tsv_lineidx_8b
from .tsv_io import File
import torch


def delete_tsv_files(tsvs):
    for t in tsvs:
        if op.isfile(t):
            try_delete(t)
        line = op.splitext(t)[0] + '.lineidx'
        if op.isfile(line):
            try_delete(line)
        line = get_tsv_lineidx_8b(t)
        if op.isfile(line):
            try_delete(line)

def concat_tsv_files_mem_efficient(tsvs, out_tsv):
    # prefer concat_tsv_files
    if len(tsvs) == 1 and tsvs[0] == out_tsv:
        return
    concat_files(tsvs, out_tsv)
    sizes = [os.stat(t).st_size for t in tsvs]
    sizes = np.cumsum(sizes)
    debug_check = {'n': 0, 'n8b': 0}
    def gen_lineidx():
        for i, t in enumerate(tsvs):
            with File.open(op.splitext(t)[0] + '.lineidx') as fp:
                for idx in fp:
                    debug_check['n'] += 1
                    if i == 0:
                        yield int(idx)
                    else:
                        yield int(idx) + sizes[i - 1]
    with File.open(op.splitext(out_tsv)[0] + '.lineidx', 'w') as fp:
        for x in tqdm(gen_lineidx()):
            fp.write(str(x) + '\n')

    def gen_lineidx_8b():
        for i, t in enumerate(tsvs):
            with File.open(op.splitext(t)[0] + '.lineidx.8b', 'rb') as fp:
                while True:
                    x = fp.read(8)
                    if x != b'':
                        idx = int.from_bytes(x, 'little')
                        debug_check['n8b'] += 1
                        if i == 0:
                            yield idx
                        else:
                            yield int(idx) + sizes[i - 1]
                    else:
                        break
    with File.open(op.splitext(out_tsv)[0] + '.lineidx.8b', 'wb') as fp:
        for x in tqdm(gen_lineidx_8b()):
            fp.write(int(x).to_bytes(8, 'little'))
    assert debug_check['n'] == debug_check['n8b']

def concat_tsv_files(tsvs, out_tsv, gen_lineidx=True):
    if len(tsvs) == 1 and tsvs[0] == out_tsv:
        return
    File.prepare(tsvs)
    concat_files(tsvs, out_tsv)
    sizes = [File.get_file_size(t) for t in tsvs]
    sizes = np.cumsum(sizes)
    sizes = [0] + sizes[:-1].tolist()

    if gen_lineidx:
        # the generation process is much slower than that for lineidx.8b. As
        # this format is going to be deprecated, we have a flag to disable this
        # ggeneration proces
        concate_lineidx(sizes, tsvs, out_tsv)
    concate_lineidx_8b(sizes, tsvs, out_tsv)

def concate_lineidx(sizes, tsvs, out_tsv):
    File.prepare(tsvs)
    def row_processor(row):
        offset, in_tsv, out_tsv = row
        fbar = qd_tqdm(unit_scale=True)
        with File.open(in_tsv) as fp:
            with File.open(out_tsv, 'w') as fpout:
                for idx in fp:
                    fbar.update(1)
                    fpout.write(str(int(idx) + offset) + '\n')
    all_info = [(sizes[i], op.splitext(t)[0] + '.lineidx') for i, t in enumerate(tsvs)]
    folder = get_tmp_folder()
    all_info = [(offset, in_tsv, op.join(folder, in_tsv)) for offset, in_tsv in all_info]
    #for _, __, f in all_info:
        #ensure_directory(op.dirname(f))
    parallel_map(row_processor, all_info, 64)
    concat_files([i[2] for i in all_info], op.splitext(out_tsv)[0] + '.lineidx')
    for d in all_info:
        ensure_remove_file(d[2])

def concate_lineidx_8b(sizes, tsvs, out_tsv):
    File.prepare(tsvs)
    folder = get_tmp_folder()
    def row_processor_8b(row):
        offset, in_tsv, out_tsv = row
        fbar = qd_tqdm(unit_scale=True)
        bulk_size = 1024
        with File.open(in_tsv, 'rb') as fp:
            with File.open(out_tsv, 'wb') as fpout:
                while True:
                    x = fp.read(8 * bulk_size)
                    fbar.update(len(x) // 8)
                    if x != b'':
                        import struct
                        fmt = '<{}q'.format(len(x) // 8)
                        x = [i + offset for i in struct.unpack(fmt, x)]
                        fpout.write(b''.join([i.to_bytes(8, 'little') for i in
                                              x]))
                    else:
                        break
    all_info_8b = [(sizes[i], op.splitext(t)[0] + '.lineidx.8b') for i, t in enumerate(tsvs)]
    File.prepare([in_tsv for _, in_tsv in all_info_8b])
    # op.join(folder, in_tsv) may also be equal to in_tsv, although it is fine
    all_info_8b = [(offset, in_tsv, '{}/{}'.format(folder, in_tsv + '.lineidx.8b')) for offset, in_tsv
                   in all_info_8b]
    parallel_map(row_processor_8b, all_info_8b, 64)
    concat_files([i[2] for i in all_info_8b], op.splitext(out_tsv)[0] + '.lineidx.8b')
    for d in all_info_8b:
        ensure_remove_file(d[2])

def concat_tsv_files_old(tsvs, out_tsv):
    # prefer concat_tsv_files
    if len(tsvs) == 1 and tsvs[0] == out_tsv:
        return
    concat_files(tsvs, out_tsv)
    sizes = [os.stat(t).st_size for t in tsvs]
    sizes = np.cumsum(sizes)
    all_idx = []
    for i, t in enumerate(tsvs):
        for idx in load_list_file(op.splitext(t)[0] + '.lineidx'):
            if i == 0:
                all_idx.append(idx)
            else:
                all_idx.append(str(int(idx) + sizes[i - 1]))
    write_to_file('\n'.join(all_idx), op.splitext(out_tsv)[0] + '.lineidx')
    with File.open(get_tsv_lineidx_8b(out_tsv), 'wb') as fp:
        for i in all_idx:
            fp.write(int(i).to_bytes(8, 'little'))

def merge_data_feature_tsv(from_data, to_data, feature_version):
    assert from_data.startswith(to_data)
    from_dataset = TSVDataset(from_data)
    to_dataset = TSVDataset(to_data)
    src_tsvs = load_list_file(from_dataset.get_data(
        'trainX', 'feature', feature_version))
    dest_tsvs = load_list_file(to_dataset.get_data(
        'trainX',
        'feature',
        feature_version))
    assert len(dest_tsvs) == 1
    dest_tsv = dest_tsvs[0]
    logging.info(pformat(src_tsvs))
    logging.info(pformat(dest_tsv))
    if op.isfile(dest_tsv):
        logging.info('skip to merge as exists')
    else:
        concat_tsv_files(src_tsvs, dest_tsv)

def train_test_split(data_from, data_to, split_from='train', num_test=5000):
    populate_dataset_details(data_from)
    dataset_from = TSVDataset(data_from)
    dataset_to = TSVDataset(data_to)

    num_total_image = dataset_from.num_rows(split_from)
    total_idx = list(range(num_total_image))
    random.seed(666)
    random.shuffle(total_idx)
    split_names = ['test', 'train']
    all_split_idx = [total_idx[: num_test], total_idx[num_test:]]
    for split_idx, split_name in zip(all_split_idx, split_names):
        dataset_to.write_data(dataset_from.iter_data(split_from,
            filter_idx=split_idx), split_name)
        # all label file
        v = 0
        while dataset_from.has(split_from, 'label', version=v):
            dataset_to.write_data(dataset_from.iter_data(split_from,
                t='label', version=v, filter_idx=split_idx),
                split_name, t='label', version=v)
            v = v + 1

def find_best_matched_rect_idx(target, rects, check_class=True):
    target_class_lower = target['class'].lower()
    if check_class:
        same_class_rects = [r for r in rects if r['class'].lower() == target_class_lower]
    else:
        same_class_rects = rects
    idx_ious = [(i, calculate_iou(r['rect'], target['rect']))
        for i, r in enumerate(same_class_rects)]
    if len(idx_ious) == 0:
        return None, -1
    return max(idx_ious, key=lambda r: r[-1])

def find_best_matched_rect(target, rects, check_class=True, check_iou=True):
    target_class_lower = target['class'].lower()
    if check_class:
        same_class_rects = [r for r in rects if r['class'].lower() == target_class_lower]
    else:
        same_class_rects = rects
    if check_iou:
        rect_ious = [(r, calculate_iou(r['rect'], target['rect']))
            for r in same_class_rects]
    else:
        rect_ious = [(r, 1) for r in same_class_rects]
    if len(rect_ious) == 0:
        return None, -1
    return max(rect_ious, key=lambda r: r[-1])

def create_tsvdataset_from_image_folder(root_folder, data):
    from .process_image import load_image
    dataset = TSVDataset(data)
    all_full_file_name = [op.join(root, f) for root, dirnames, filenames in os.walk(root_folder)
        for f in filenames]
    def row_processor(full_file_name):
        im = load_image(full_file_name)
        if im is not None:
            key = full_file_name.replace(root_folder, '')
            return key, base64.b64encode(read_to_buffer(full_file_name))
    parallel_tsv_process(
        row_processor,
        all_full_file_name,
        dataset.get_data('train'),
        64
    )

def create_new_image_tsv_if_exif_rotated(data, split):
    dataset = TSVDataset(data)
    key_no_rotate_in_image = 'no_rotate_in_image'
    if dataset.has(split, key_no_rotate_in_image):
        return
    info = {'changed': 0, 'total': 0}
    def gen_rows():
        logging.info('{}-{}'.format(data, split))
        for key, str_rects, str_im in tqdm(dataset.iter_data(split)):
            info['total'] = info['total'] + 1
            im = img_from_base64(str_im)
            from .common import img_from_base64_ignore_rotation
            im2 = img_from_base64_ignore_rotation(str_im)
            if im.shape[0] != im2.shape[0] or im.shape[1] != im2.shape[1]:
                assert im.shape[0] == im2.shape[1]
                assert im.shape[1] == im2.shape[0]
                info['changed'] = info['changed'] + 1
                yield key, str_rects, encoded_from_img(im)
            else:
                yield key, str_rects, str_im
    tmp_tsv = dataset.get_data(split, 'tmp')
    tsv_writer(gen_rows(), tmp_tsv)
    logging.info(pformat(info))
    if info['changed'] != 0:
        from .tsv_io import tsv_mv
        tsv_mv(tmp_tsv, dataset.get_data(split))
    else:
        from .tsv_io import tsv_rm
        tsv_rm(tmp_tsv)
    dataset.write_data([], split, key_no_rotate_in_image)

def get_data_distribution(data, split, version):
    # return a dictionary to represent the distribution of the data
    # only composite dataset requires the following two information
    result = {}

    populate_dataset_details(data)
    dataset = TSVDataset(data)
    tsvsource_to_imageratio = {src[5:]:
            (float(numdestimages) / float(numsrcimages))
            for src, numdestimages, numsrcimages in
            dataset.iter_data(split + 'X', 'tsvsource.numdestimages.numsrcimages')}
    result['tsvsource_to_imageratio'] = tsvsource_to_imageratio

    tsvsource_to_boxratio = {src[5:]:
            (float(numdest) / float(numsrc))
            for src, numdest, numsrc in
            dataset.iter_data(split + 'X', 'tsvsource.numdestbbs.numsrcbbs')}
    result['tsvsource_to_boxratio'] = tsvsource_to_boxratio

    tsvsource_to_num_image = {source[5:]: int(num_image) for source, num_image in dataset.iter_data(split + 'X', 'numImagesPerSource')}
    result['tsvsource_to_num_image'] = tsvsource_to_num_image

    tsvsource_to_num_category = {source[5:]: int(num_image) for source, num_image
            in dataset.iter_data(split + 'X', 'numCategoriesPerSource')}
    result['tsvsource_to_num_category'] = tsvsource_to_num_category

    label_to_count = {l: int(c) for l, c in dataset.iter_data(split, 'inverted.label.count', version=version)}
    unique_labels = set([l if not l.startswith('-') else l[1:] for l in label_to_count])
    import math
    all_label_pos_negs = [[l, [label_to_count.get(l, 0), -label_to_count.get('-' + l, 0)]] for l in unique_labels]
    all_label_pos_negs = [[l, [math.log10(label_to_count.get(l, 0) + 1),
        -math.log10(label_to_count.get('-' + l, 0) + 1)]] for l in unique_labels]
    all_label_pos_negs = sorted(all_label_pos_negs, key=lambda x: x[1][0])
    result['all_label_pos_negs'] = all_label_pos_negs

    result['num_labels_with_neg'] = len([l for l, (pos, neg) in all_label_pos_negs if neg != 0])
    result['num_labels_with_pos'] = len([l for l, (pos, neg) in all_label_pos_negs if pos != 0])

    label_to_count = {l: int(c) for l, c in dataset.iter_data(split, 'inverted.label.count',
            version=version)}
    _, c = next(dataset.iter_data(split, 'inverted.background.count',
        version=version))
    label_to_count['__background'] = int(c)
    result['label_to_count'] = label_to_count

    return result

def convert_pred_to_dataset_label(full_expid, predict_file,
        th_file, min_value):
    pred_file = op.join('output',
            full_expid,
            'snapshot',
            predict_file)

    from .common import parse_test_data
    data, split = parse_test_data(predict_file)

    from process_tsv import load_key_rects

    dataset = TSVDataset(data)
    populate_dataset_details(data)
    latest_version = dataset.get_latest_version(split, 'label')
    gt_key_rects = load_key_rects(dataset.iter_data(split, 'label',
        latest_version))
    pred_key_rects = load_key_rects(tsv_reader(pred_file))
    pred_key_to_rects = {key: rects for key, rects in pred_key_rects}
    if th_file:
        th_file = op.join('output', full_expid, 'snapshot', th_file)
        per_cat_th = {l: max(float(th), min_value) for l, th, _ in tsv_reader(th_file)}
    else:
        per_cat_th = {}
    def gen_rows():
        for key, rects in gt_key_rects:
            pred_rects = pred_key_to_rects.get(key, [])
            pred_rects = [r for r in pred_rects
                    if r['conf'] >= per_cat_th.get(r['class'], 0)]
            yield key, json.dumps(pred_rects)
    info = [('full_expid', full_expid),
            ('predict_file', pred_file),
            ('th_file', th_file),
            ('min_value', min_value)]
    dataset.update_data(gen_rows(), split, 'label',
            generate_info=info)

    populate_dataset_details(data)

def ensure_inject_expid(full_expid):
    all_predict = []
    all_predict.extend(glob.glob(op.join('output', full_expid, 'snapshot',
        '*.predict')))
    all_predict.extend(glob.glob(op.join('output', full_expid, 'snapshot',
        '*.predict.tsv')))
    for p in all_predict:
        ensure_inject_expid_pred(full_expid, op.basename(p))

def ensure_inject_dataset(data, **kwargs):
    for split in get_default_splits():
        ensure_upload_image_to_blob(data, split)
        ensure_inject_image(data, split)
        ensure_inject_gt(data, split, **kwargs)

def ensure_inject_decorate(func):
    def func_wrapper(*args, **kwargs):
        client = create_mongodb_client()
        db = client['qd']
        task = db['task']
        # .func_name is ok for python2, but not for python3. .__name__ is ok
        # for both
        func_name = func.__name__
        if func_name.startswith('ensure_'):
            func_name = func_name[len('ensure_'): ]
        argnames, ins_varargs, ins_kwargs, ins_defaults = inspect.getargspec(func)
        # we have not supported if other paramseters is not None
        assert ins_varargs is None and ins_kwargs is None
        query = {'task_type': func_name}
        for n, v in zip(argnames[: len(args)], args):
            assert n not in query
            query[n] = v
        for k in kwargs:
            assert k not in query
            query[k] = kwargs[k]

        # create the unique index to make the update_one atomic
        logging.info('make sure the index is created: {}'.format(
            ', '.join(query.keys())))
        task.create_index([(k, pymongo.ASCENDING) for k in query])

        while True:
            result = task.update_one(filter=query,
                                    update={'$setOnInsert': query},
                                    upsert=True)
            if result.matched_count == 0:
                task.update_one(filter=query,
                        update={'$set': {'status': 'started',
                                         'create_time': datetime.now()}})
                func(*args, **kwargs)
                task.update_many(filter=query,
                        update={'$set': {'status': 'done'}})
                return True
            else:
                assert result.matched_count == 1
                existing_entries = list(task.find(query))
                if any(e for e in existing_entries if e['status'] == 'done'):
                    logging.info('ignore to inject since it is done: \n{}'.format(query))
                    break
                else:
                    logging.info('waiting to finish \n{}'.format(
                        pformat(existing_entries)))
                    time.sleep(10)
                # return False if not done
    return func_wrapper

def ensure_composite_key_url(data, split):
    dataset = TSVDataset(data)
    if not dataset.has(split):
        logging.info('data tsv does not exist')
        return
    if dataset.has(split, 'key.url'):
        logging.info('it exists')
        return
    data_to_dataset = {}
    uploaded_key_url = set()
    def gen_rows():
        for key, _ in dataset.iter_data(split, 'label'):
            c_data, c_split, c_key = parse_combine(key)
            if c_data in data_to_dataset:
                c_dataset = data_to_dataset[c_data]
            else:
                c_dataset = TSVDataset(c_data)
                data_to_dataset[c_data] = c_dataset
            if (c_data, c_split) not in uploaded_key_url:
                ensure_upload_image_to_blob(c_data, c_split)
                uploaded_key_url.add((c_data, c_split))
            _, url = c_dataset.seek_by_key(c_key, c_split, 'key.url')
            yield key, url
    dataset.write_data(gen_rows(), split, 'key.url')

def upload_image_to_blob(data, split, config=None):
    dataset = TSVDataset(data)
    if not dataset.has(split):
        logging.info('{} - {} does not exist'.format(data, split))
        return
    logging.info('{} - {}'.format(data, split))
    if op.isfile(dataset.get_data(split + 'X')):
        ensure_composite_key_url(data, split)
        logging.info('ignore to upload images for composite dataset')
        return
    parallel = True
    if not parallel:
        s = CloudStorage(config=config)
        def gen_rows():
            for key, _, str_im in tqdm(dataset.iter_data(split)):
                url_key = map_image_key_to_url_key(data, split, key)
                if sys.version_info.major == 3:
                    url = s.upload_stream(base64.b64decode(str_im),
                            'images/' + url_key + '.jpg')
                else:
                    url = s.upload_stream(StringIO(base64.b64decode(str_im)),
                            'images/' + url_key)
                yield key, url
        dataset.write_data(gen_rows(), split, 'key.url')
    else:
        from .common import split_to_chunk
        from .tsv_io import rm_tsv
        num_rows = dataset.num_rows(split)
        num_chunk = num_rows // 1000
        num_chunk = max(1, num_chunk)
        tasks = split_to_chunk(range(num_rows), num_chunk)
        tasks = [(data, split, t, i, len(tasks), config) for i, t in enumerate(tasks)]
        parallel_map(upload_image_to_blob_by_idx, tasks)
        # merge the result
        def gen_rows():
            for idx_task in range(len(tasks)):
                for key, url in dataset.iter_data(split,
                        'key.url.{}.{}'.format(idx_task, len(tasks))):
                    yield key, url
        dataset.write_data(gen_rows(), split, 'key.url')
        # remove intermediate files
        for idx_task in range(len(tasks)):
            rm_tsv(dataset.get_data(split, 'key.url.{}.{}'.format(idx_task, len(tasks))))

def upload_image_to_blob_by_idx(args):
    data, split, idxes, idx_task, num_task, config = args
    t = 'key.url.{}.{}'.format(idx_task, num_task)
    dataset = TSVDataset(data)
    if dataset.has(split, t):
        logging.info('return since exist')
        return
    s = CloudStorage(config=config)
    def gen_rows():
        for key, _, str_im in tqdm(dataset.iter_data(split, filter_idx=idxes)):
            url_key = map_image_key_to_url_key(data, split, key)
            if sys.version_info.major == 3:
                url = s.upload_stream(base64.b64decode(str_im),
                        'images/' + url_key + '.jpg')
            else:
                url = s.upload_stream(StringIO(base64.b64decode(str_im)),
                        'images/' + url_key)
            if s.sas_token:
                url += s.sas_token
            yield key, url
    dataset.write_data(gen_rows(), split, t)

@ensure_inject_decorate
def ensure_upload_image_to_blob(data, split, config=None):
    upload_image_to_blob(data, split, config=config)

@ensure_inject_decorate
def ensure_inject_image(data, split):
    dataset = TSVDataset(data)
    if not dataset.has(split):
        return

    client = create_mongodb_client()
    db = client['qd']
    images = db['image']
    images.delete_many(filter={'data': data, 'split': split})
    images.create_index([
        ('data', pymongo.ASCENDING),
        ('split', pymongo.ASCENDING),
        ('key', pymongo.ASCENDING),
        ],
        unique=True)
    all_data = []

    logging.info('injecting {} - {}'.format(data, split))
    injected = set()
    key_to_hw = None
    if dataset.has(split, 'hw'):
        key_to_hw = {key: [int(x) for x in hw.split(' ')] for key, hw in
                dataset.iter_data(split, 'hw')}
    assert dataset.has(split, 'key.url')
    key_to_url = {key: url for key, url in dataset.iter_data(split, 'key.url')}
    logging.info('injecting image for {} - {}'.format(data, split))
    for i, (key, _) in tqdm(enumerate(dataset.iter_data(split,
        'label'))):
        if key in injected:
            continue
        else:
            injected.add(key)
        url = key_to_url[key]
        doc = {'data': data,
                'split': split,
                'key': key,
                'idx_in_split': i,
                'url': url,
                'create_time': datetime.now(),
                }
        if key_to_hw:
            doc['height'], doc['width'] = key_to_hw[key]
        all_data.append(doc)
        if len(all_data) > 1000:
            images.insert_many(all_data)
            all_data = []
    if len(all_data) > 0:
        images.insert_many(all_data)
        all_data = []

@ensure_inject_decorate
def ensure_update_pred_with_correctness(data, split,
        full_expid, predict_file):
    ensure_inject_gt(data, split)
    ensure_inject_pred(full_expid, predict_file, data, split)

    update_pred_with_correctness(data, split,
        full_expid,
        predict_file)


def update_pred_with_correctness(test_data, test_split,
        full_expid,
        predict_file):
    client = create_mongodb_client()
    db = client['qd']
    _pred = db['predict_result']

    # get the latest version of the gt
    pipeline = [{'$match': {'data': test_data,
                            'split': test_split,
                            'version': {'$lte': 0}}},
                {'$group': {'_id': {'data': '$data',
                                    'split': '$split',
                                    'key': '$key',
                                    'action_target_id': '$action_target_id'},
                            'contribution': {'$sum': '$contribution'},
                            'class': {'$first': '$class'},
                            'rect': {'$first': '$rect'}}},
                {'$match': {'contribution': {'$gte': 1}}}, # if it is 0, it means we removed the box
                {'$addFields': {'data': '$_id.data',
                                'split': '$_id.split',
                                'key': '$_id.key'}},
                ]
    def get_query_pipeline(data, split, key, class_name):
        return [{'$match': {'$expr': {'$and': [{'$eq': ['$data', '$${}'.format(data)]},
                                               {'$eq': ['$split', '$${}'.format(split)]},
                                               {'$eq': ['$key', '$${}'.format(key)]},
                                               {'$eq': ['$class', '$${}'.format(class_name)]}]}}},
                 {'$group': {'_id': '$action_target_id',
                             'contribution': {'$sum': '$contribution'},
                             'create_time': {'$max': '$create_time'},
                             'rect': {'$first': '$rect'}}},
                 {'$match': {'contribution': {'$gte': 1}}},
                ]

    # get the target prediction bounding boxes
    pipeline = [{'$match': {'full_expid': full_expid,
                            'pred_file': predict_file}},
                {'$group': {'_id': {'data': '$data',
                                   'split': '$split',
                                   'key': '$key',
                                   'class': '$class'},
                           'pred_box': {'$push': {'rect': '$rect',
                                                  'conf': '$conf',
                                                  'pred_box_id': '$_id'}}}},
                {'$lookup': {'from': 'ground_truth',
                             'let': {'target_data': '$_id.data',
                                     'target_split': '$_id.split',
                                     'target_key': '$_id.key',
                                     'target_class': '$_id.class'},
                             'pipeline': get_query_pipeline('target_data',
                                                            'target_split',
                                                            'target_key',
                                                            'target_class'),
                             'as': 'gt_box'}},
            ]
    all_correct_box, all_wrong_box = [], []
    for row in tqdm(_pred.aggregate(pipeline, allowDiskUse=True)):
        curr_pred = row['pred_box']
        curr_gt = row['gt_box']
        curr_pred = sorted(curr_pred, key=lambda x: -x['conf'])
        for p in curr_pred:
            matched = [g for g in curr_gt if
                    not g.get('used', False) and
                    ('rect' not in p or
                        not p['rect'] or
                        'rect' not in g or
                        not g['rect'] or
                        calculate_iou(g['rect'], p['rect']) > 0.3)]
            if len(matched) == 0:
                all_wrong_box.append(p['pred_box_id'])
            else:
                all_correct_box.append(p['pred_box_id'])
                matched[0]['used'] = True
        if len(all_correct_box) > 1000:
            _pred.update_many({'_id': {'$in': all_correct_box}},
                    {'$set': {'correct': 1}})
            all_correct_box = []
        if len(all_wrong_box) > 1000:
            _pred.update_many({'_id': {'$in': all_wrong_box}},
                    {'$set': {'correct': 0}})
            all_wrong_box = []
    if len(all_correct_box) > 0:
        _pred.update_many({'_id': {'$in': all_correct_box}},
                {'$set': {'correct': 1}})
        all_correct_box = []
    if len(all_wrong_box) > 0:
        _pred.update_many({'_id': {'$in': all_wrong_box}},
                {'$set': {'correct': 0}})
        all_wrong_box = []

def ensure_inject_expid_pred(full_expid, predict_file):
    try:
        data, split = parse_test_data(predict_file)
    except:
        logging.info('ignore to inject {} - {}'.format(full_expid, predict_file))
        return
    ensure_upload_image_to_blob(data, split)
    ensure_inject_image(data, split)
    ensure_inject_gt(data, split)
    ensure_inject_pred(full_expid,
            predict_file,
            data,
            split)
    ensure_update_pred_with_correctness(data, split,
        full_expid, predict_file)

@ensure_inject_decorate
def ensure_inject_pred(full_expid, pred_file, test_data, test_split):
    client = create_mongodb_client()
    db = client['qd']
    pred_collection = db['predict_result']
    logging.info('cleaning {} - {}'.format(full_expid, pred_file))
    pred_collection.delete_many({'full_expid': full_expid, 'pred_file': pred_file})
    pred_file = op.join('output', full_expid, 'snapshot', pred_file)
    all_rect = []
    for key, label_str in tqdm(tsv_reader(pred_file)):
        rects = json.loads(label_str)
        rects = [r for r in rects if r['conf'] > 0.05]
        for i, r in enumerate(rects):
            r['full_expid'] = full_expid
            r['pred_file'] = op.basename(pred_file)
            r['data'] = test_data
            r['split'] = test_split
            r['key'] = key
        all_rect.extend(rects)
        if len(all_rect) > 10000:
            pred_collection.insert_many(all_rect)
            all_rect = []
    if len(all_rect) > 0:
        pred_collection.insert_many(all_rect)

def ensure_inject_gt(data, split, **kwargs):
    client = create_mongodb_client()
    db = client['qd']
    task = db['task']
    assert split is not None
    dataset = TSVDataset(data)
    version = 0
    set_previous_key_rects = False
    while dataset.has(split, 'label', version=version):
        query = {'task_type': 'inject_gt',
                                'data': data,
                                'split': split,
                                'version': version}
        result = task.update_one(filter=query,
                                update={'$setOnInsert': query},
                                upsert=True)
        if result.matched_count == 0:
            logging.info('start to inserting {}-{}-{}'.format(data,
                split, version))
            # no process is working on inserting current version
            task.update_one(filter=query,
                    update={'$set': {'status': 'started',
                                     'create_time': datetime.now()}})
            if not set_previous_key_rects:
                if version == 0:
                    previous_key_to_rects = {}
                else:
                    key_rects = load_key_rects(dataset.iter_data(split, 'label',
                        version=version-1))
                    previous_key_to_rects = {key: rects for key, rects in key_rects}
                set_previous_key_rects = True
            inject_gt_version(data, split, version, previous_key_to_rects,
                    **kwargs)
            task.update_many(filter={'task_type': 'inject_gt',
                'data': data,
                'split': split,
                'version': version}, update={'$set': {'status': 'done'}})
        else:
            # it is done or it is started by anohter process. let's skip
            # this version
            assert result.matched_count == 1
            while True:
                existing_entries = list(task.find(query))
                if any(e for e in existing_entries if e['status'] == 'done'):
                    logging.info('ignore to inject since it is done: \n{}'.format(query))
                    break
                elif len(existing_entries) == 0:
                    logging.info('we will do it')
                    version = version - 1
                    break
                else:
                    logging.info('waiting to finish \n{}'.format(
                        pformat(existing_entries)))
                    time.sleep(10)
            set_previous_key_rects = False
        version = version + 1

def inject_gt_version(data, split, version, previous_key_to_rects,
        delete_existing=True):
    client = create_mongodb_client()
    db = client['qd']
    gt = db['ground_truth']
    dataset = TSVDataset(data)
    if delete_existing:
        logging.info('deleting data={}, split={}, version={}'.format(
            data, split, version))
        gt.delete_many({'data': data, 'split': split, 'version': version})
    dataset = TSVDataset(data)

    if not dataset.has(split, 'label', version):
        return False
    all_rect = []
    logging.info('{}-{}-{}'.format(data, split, version))
    total_inserted = 0
    for idx_in_split, (key, label_str) in tqdm(enumerate(dataset.iter_data(
        split, 'label', version=version))):
        rects = json.loads(label_str)
        def add_to_all_rect(r, extra_info):
            r2 = copy.deepcopy(r)
            r2.update(extra_info)
            r2['idx_in_split'] = idx_in_split
            r2['data'] = data
            r2['split'] = split
            r2['key'] = key
            r2['action_target_id'] = hash_sha1([data, split, key, r['class'],
                r.get('rect', [])])
            r2['version'] = version
            r2['create_time'] = datetime.now()
            all_rect.append(r2)
        if version == 0:
            assert key not in previous_key_to_rects
            for r in rects:
                add_to_all_rect(r, {'contribution': 1})
        else:
            previous_rects = previous_key_to_rects[key]
            # use strict_rect_in_rects rather than rect_in_rects: if the higher
            # version contains more properies, we want to have the properies in
            # teh database also.
            previous_not_in_current = copy.deepcopy([r for r in previous_rects if
                    not strict_rect_in_rects(r, rects)])
            current_not_in_previous = copy.deepcopy([r for r in rects if
                    not strict_rect_in_rects(r, previous_rects)])
            for r in previous_not_in_current:
                add_to_all_rect(r, {'contribution': -1})
            for r in current_not_in_previous:
                add_to_all_rect(r, {'contribution': 1})
        previous_key_to_rects[key] = rects
        if len(all_rect) > 1000:
            db_insert_many(gt, all_rect)
            total_inserted = total_inserted + len(all_rect)
            logging.info('inserting data={}, split={}, version={}, curr_insert={}, total={}'.format(
                data, split, version, len(all_rect), total_inserted))
            all_rect = []

    if len(all_rect) > 0:
        total_inserted = total_inserted + len(all_rect)
        logging.info('inserting data={}, split={}, version={}, curr_insert={}, total={}'.format(
            data, split, version, len(all_rect), total_inserted))
        db_insert_many(gt, all_rect)
        all_rect = []
    return True

def db_insert_many(collection, all_info):
    for a in all_info:
        if '_id' in a:
            del a['_id']
    collection.insert_many(all_info)

class VisualizationDatabaseByMongoDB():
    def __init__(self):
        self._client = create_mongodb_client()
        self._db = self._client['qd']
        self._pred = self._db['predict_result']
        self._gt = self._db['ground_truth']

    def _get_positive_start(self, start_id, max_item):
        if start_id < 0:
            rank = pymongo.DESCENDING
            start_id = min(0, start_id + max_item)
            start_id = abs(start_id)
        else:
            rank = pymongo.ASCENDING
        return rank, start_id

    def query_pipeline(self, pipeline, collection, db_name):
        image_info = list(self._client[db_name][collection].aggregate(
            pipeline, allowDiskUse=True))
        logging.info(len(image_info))
        if len(image_info) > 0:
            logging.info(pformat(image_info[0]))
        image_info = [{'key': x['key'],
            'url': x.get('url', ''),
            'gt': x.get('gt', []),
            'pred': x.get('pred', [])} for x in image_info]
        return image_info

    def insert(self, dic, collection, db_name):
        return self._client[db_name][collection].insert(dic)

    def query_by_id(self, _id, collection, db_name):
        from bson.objectid import ObjectId
        return list(self._client[db_name][collection].find({'_id':
            ObjectId(_id)}))[0]

    def query_predict_recall(self, full_expid, pred_file, class_name, threshold, start_id, max_item):
        rank, start_id = self._get_positive_start(start_id, max_item)
        # from the predict file
        row = self._pred.find_one({'full_expid': full_expid, 'pred_file':
            pred_file})
        if row is None:
            logging.info('no prediction data in db')
            return []
        data = row['data']
        split = row['split']
        logging.info(data)
        logging.info(split)
        pipeline = [{'$match': {'data': data, 'split': split, 'class': class_name}},
                    {'$group': {'_id': {'key': '$key', 'target_action_id': '$target_action_id'},
                                'contribution': {'$sum': '$contribution'},
                                'num_target_label': {'$sum': 1}}},
                    {'$match': {'contribution': {'$gte': 1}}},
                    {'$group': {'_id': {'key': '$_id.key'},
                                'num_contribution': {'$sum': 1}}},
                    ## add the number of correct predicted
                    {'$lookup': {
                        'from': 'predict_result',
                        'let': {'key': '$_id.key'},
                        'pipeline': [{'$match': {'full_expid': full_expid,
                                                 'pred_file': pred_file,
                                                 'class': class_name,
                                                 'conf': {'$gte': threshold}}},
                                     {'$group': {'_id': {'key': '$key'},
                                                 'correct': {'$sum': '$correct'}}},
                                      {'$match': {'$expr': {'$and': [{'$eq': ['$_id.key', '$$key']}]}}},
                                      ],
                        'as': 'recall',
                        }},
                    {'$unwind': {'path': '$recall', 'preserveNullAndEmptyArrays': True}},
                    {'$addFields': {'recall': {'$divide': ['$recall.correct', '$num_target_label']}}},
                    {'$sort': {'recall': rank}},
                    {'$skip': start_id},
                    {'$limit': max_item},
                    # add the field of url
                    {'$lookup': {
                        'from': 'image',
                        'let': {'key': '$_id.key'},
                        'pipeline': [{'$match': {'$expr': {'$and': [{'$eq': ['$data', data]},
                                                                    {'$eq': ['$split', split]},
                                                                    {'$eq': ['$key', '$$key']},],}}},
                                     {'$project': {'url': True, '_id': 0}}],
                        'as': 'url',
                        }},
                    {'$addFields': {'url': {'$arrayElemAt': ['$url', 0]}}},
                    {'$addFields': {'url': '$url.url'}},
                    ## add the field of pred
                    {'$lookup': {
                        'from': 'predict_result',
                        'let': {'key': '$_id.key'},
                        'pipeline': [{'$match': {'$expr': {'$and': [{'$eq': ['$full_expid', full_expid]},
                                                                    {'$eq': ['$pred_file', pred_file]},
                                                                    {'$eq': ['$key', '$$key']},
                                                                    {'$gte': ['$conf', threshold]}]}}},
                                     {'$project': {'conf': True, 'rect': True, 'class': True, '_id': 0}}],
                        'as': 'pred',
                        }
                    },
                    ## add the gt field
                    {'$lookup': {
                        'from': 'ground_truth',
                        'let': {'key': '$_id.key'},
                        'pipeline': [{'$match': {'$expr': {'$and': [{'$eq': ['$data', data]},
                                                                    {'$eq': ['$split', split]},
                                                                    {'$eq': ['$key', '$$key']},],}}},
                                    {'$group': {'_id': {'action_target_id': '$action_target_id'},
                                                'contribution': {'$sum': '$contribution'},
                                                'rect': {'$first': '$rect'},
                                                'class': {'$first': '$class'},
                                                'conf': {'$first': '$conf'}}, },
                                    {'$match': {'contribution': {'$gte': 1}}},
                                    ],
                        'as': 'gt',
                        }}
                    ]
        image_info = list(self._gt.aggregate(pipeline))
        logging.info(len(image_info))
        image_info = [{'key': x['_id']['key'],
            'url': x['url'],
            'gt': x['gt'],
            'pred': x['pred']} for x in image_info]
        return image_info

    def query_predict_precision(self, full_expid, pred_file, class_name, threshold, start_id, max_item):
        rank, start_id = self._get_positive_start(start_id, max_item)
        filter_pred = {'full_expid': full_expid,
                            'pred_file': pred_file,
                            'conf': {'$gte': threshold}}
        if class_name != 'None' and class_name:
            filter_pred['class'] = class_name
        pipeline = [
                {'$match': filter_pred},
                {'$group': {'_id': {'data': '$data',
                                    'split': '$split',
                                    'key': '$key'},
                            'correct': {'$sum': '$correct'},
                            'total': {'$sum': 1}}},
                {'$addFields': {'precision': {'$divide': ['$correct', '$total']}}},
                {'$sort': {'precision': rank}},
                {'$skip': start_id},
                {'$limit': max_item},
                # add the field of url
                {'$lookup': {
                    'from': 'image',
                    'let': {'data': '$_id.data',
                            'split': '$_id.split',
                            'key': '$_id.key'},
                    'pipeline': [{'$match': {'$expr': {'$and': [{'$eq': ['$data', '$$data']},
                                                                {'$eq': ['$split', '$$split']},
                                                                {'$eq': ['$key', '$$key']},],}}},
                                 {'$project': {'url': True, '_id': 0}}],
                    'as': 'url',
                    }},
                {'$unwind': '$url'},
                {'$addFields': {'url': '$url.url'}},
                ## add the field of pred
                {'$lookup': {
                    'from': 'predict_result',
                    'let': {'data': '$_id.data',
                            'split': '$_id.split',
                            'key': '$_id.key'},
                    'pipeline': [{'$match': {'$expr': {'$and': [{'$eq': ['$full_expid', full_expid]},
                                                                {'$eq': ['$pred_file', pred_file]},
                                                                {'$eq': ['$key', '$$key']},
                                                                {'$gte': ['$conf', threshold]}]}}},
                                 {'$project': {'conf': True, 'rect': True, 'class': True, '_id': 0}}],
                    'as': 'pred',
                    }
                },
                ## add the gt field
                {'$lookup': {
                    'from': 'ground_truth',
                    'let': {'data': '$_id.data',
                            'split': '$_id.split',
                            'key': '$_id.key'},
                    'pipeline': [{'$match': {'$expr': {'$and': [{'$eq': ['$data', '$$data']},
                                                                {'$eq': ['$split', '$$split']},
                                                                {'$eq': ['$key', '$$key']},],}}},
                                {'$group': {'_id': {'action_target_id': '$action_target_id'},
                                            'contribution': {'$sum': '$contribution'},
                                            'rect': {'$first': '$rect'},
                                            'class': {'$first': '$class'},
                                            'conf': {'$first': '$conf'}}, },
                                {'$match': {'contribution': {'$gte': 1}}},
                                ],
                    'as': 'gt',
                    }}
                ]

        logging.info(pformat(pipeline))
        image_info = list(self._pred.aggregate(pipeline))
        image_info = [{'key': x['_id']['key'],
            'url': x['url'],
            'gt': x['gt'],
            'pred': x['pred']} for x in image_info]
        return image_info

    def query_ground_truth(self, data, split, version, label, start_id, max_item):
        pipeline = self.get_ground_truth_pipeline(data, split, version, label, start_id, max_item)
        image_info = list(self._gt.aggregate(pipeline['pipeline']))
        logging.info(len(image_info))
        image_info = [{'key': x['key'],
            'url': x['url'],
            'gt': x['gt']} for x in image_info]
        return pipeline, image_info

    def get_ground_truth_pipeline(self, data, split, version, label, start_id, max_item):
        rank, start_id = self._get_positive_start(start_id, max_item)

        match_pairs = {'data': data}
        gt_match = []
        gt_match.append({'$eq': ['$data', data]})
        url_match = []
        url_match.append({'$eq': ['$data', data]})
        if split is not None:
            match_pairs['split'] = split
            gt_match.append({'$eq': ['$split', split]})
            url_match.append({'$eq': ['$split', split]})
        if label is not None:
            match_pairs['class'] = label
        if version is not None:
            match_pairs['version'] = {'$lte': version}
            gt_match.append({'$lte': ['$version', version]})
        gt_match.append({'$eq': ['$key', '$$key']})
        url_match.append({'$eq': ['$key', '$$key']})
        pipeline = [{'$match': match_pairs},
                    {'$group': {'_id': {'key': '$key'},
                                'contribution': {'$sum': '$contribution'}}},
                    {'$match': {'contribution': {'$gte': 1}}},
                    {'$sort': {'_id.key': rank}},
                    {'$skip': start_id},
                    {'$limit': max_item},
                    # add gt boxes
                    {'$lookup': {
                        'from': 'ground_truth',
                        'let': {'key': '$_id.key'},
                        'pipeline': [{'$match': {'$expr': {'$and': gt_match}}},
                                     {'$group': {'_id': {'action_target_id': '$action_target_id'},
                                                 'contribution': {'$sum': '$contribution'},
                                                 'rect': {'$first': '$rect'},
                                                 'class': {'$first': '$class'},
                                                 'conf': {'$first': '$conf'}}, },
                                     {'$match': {'contribution': {'$gte': 1}}},
                                    ],
                        'as': 'gt',
                        }},
                    # add url
                    {'$lookup': {
                        'from': 'image',
                        'let': {'key': '$_id.key'},
                        'pipeline': [{'$match': {'$expr': {'$and': url_match}}},
                                     {'$project': {'url': True, '_id': 0}}],
                        'as': 'url',
                        }},
                    {'$unwind': '$url'},
                    {'$addFields': {'url': '$url.url',
                                    'key': '$_id.key'}},
                    ]

        return {'pipeline': pipeline,
                'database': 'qd',
                'collection': 'ground_truth'}

def get_class_count(data, splits):
    dataset = TSVDataset(data)
    result = {}
    for split in splits:
        result[split] = {row[0]: int(row[1])
                for row in dataset.iter_data(
                    split, 'inverted.label.count', -1)}
    return result

def get_colored_name(node, colors):
    name = node.name
    if hasattr(node, 'sub_group'):
        sub_group = node.sub_group
    else:
        sub_group = -1
    idx = sub_group + 1
    while idx >= len(colors):
        colors.append('rgb({},{},{})'.format(
            int(random.random() * 255),
            int(random.random() * 255),
            int(random.random() * 255)))
    return "<span style='color:{}'>{}</span>".format(colors[idx],
            name)

def get_vis_url(data, l, split=None):
    if split is None:
        return '/detection/view_image?data={}&label={}&start_id=0'.format(
                data, l)
    else:
        return '/detection/view_image?data={}&split={}&label={}&start_id=0'.format(
                data, split, l)


def get_readable_label(l):
    if is_noffset(l):
        return '{}({})'.format(l, get_nick_name(noffset_to_synset(l)))
    else:
        return l

def get_node_key(node, k, default_value=-1):
    r = default_value
    if hasattr(node, k):
        r = node.__getattribute__(k)
    return r

def get_node_info(data, node, colors, all_data):
    ss = []
    if hasattr(node, 'ap'):
        ss.append('ap({0:.2f})'.format(node.ap))
    if hasattr(node, 'images_with_bb') and \
            hasattr(node, 'images_no_bb'):
        keys = ['with_bb', 'no_bb']
        for k in keys:
            ss.append("{}({},<span><a href='{}' target='_blank'>{}</a></span>,<span><a href='{}' target='_blank'>{}</a></span>)".format(
                k,
                get_node_key(node, 'images_' + k, -1),
                get_vis_url(data + '_' + k, node.name, split='train'),
                get_node_key(node, k + '_train', -1),
                get_vis_url(data + '_' + k, node.name, split='test'),
                get_node_key(node, k + '_test', -1)))
    ignore_keys = ['support', 'dist', 'name', 'url',
            'with_bb_train',
            'with_bb_test',
            'no_bb_train',
            'no_bb_test',
            'ap',
            'images_with_bb',
            'images_no_bb',
            'noffset',
            'sub_group',
            'cum_images_with_bb',
            'cum_images_no_bb']
    for key in node.features:
        if any(x == key for x in ignore_keys):
            continue
        if key.endswith('_readable') or \
                key.endswith('_toTrain') or \
                key.endswith('_toTest') or \
                key.endswith('_total'):
            continue
        value = node.__getattribute__(key)
        if key in all_data and value != None:
            total = get_node_key(node, '{}_total'.format(key))
            toTrain = get_node_key(node, '{}_toTrain'.format(key))
            toTest = get_node_key(node, '{}_toTest'.format(key))
            extra_info = '({},{},{})'.format(total, toTrain, toTest)
            labels = value.split(',')
            value = ','.join(["<a href='{}' target='_blank'>{}</a>".format(get_vis_url(key, l),
                get_readable_label(l)) for l in labels])
            value = '{}{}'.format(value, extra_info)
        ss.append('{}[{}]'.format(key, value))
    if len(ss) > 0:
        return "{}: {}".format(
                get_colored_name(node, colors),
                '; '.join(ss))
    else:
        return get_colored_name(node, colors)

def gen_html_tree_view(data, full_expid=None,
        predict_file=None):
    colors=['rgb(0,0,0)',
                'rgb(255,0,0)',
                'rgb(0,0,255)']
    dataset = TSVDataset(data)
    all_data = os.listdir('./data')
    file_name = op.join(dataset._data_root, 'root_enriched.yaml')
    logging.info('loading {}'.format(file_name))
    with open(file_name, 'r') as fp:
        config_tax = yaml.load(fp)
    tax = Taxonomy(config_tax)
    if full_expid is not None and \
            predict_file is not None:
        map_file = op.join('output',full_expid, 'snapshot',
                op.splitext(predict_file)[0] + '.report.class_ap.json')
        if op.isfile(map_file):
            class_ap = json.loads(read_to_buffer(map_file))
            class_ap = class_ap['overall']['0.3']['class_ap']
            for node in tax.root.iter_search_nodes():
                node.add_feature('ap',
                        class_ap.get(node.name, -1))

    def gen_html_tree_view_rec(root):
        '''
        include itself
        '''
        if len(root.children) == 0:
            s = u"<li data-jstree='{{\"icon\":\"glyphicon glyphicon-leaf\"}}'><span>{}</span></li>".format(
                    get_node_info(data, root, colors, all_data))
            return s
        else:
            result = []
            # we cannot remove span tag here
            result.append("<li data-jstree='{{\"opened\":true}}' ><span>{}</span>".format(
                get_node_info(data, root, colors, all_data)))
            result.append('<ul>')
            all_child = sorted(root.children, key=lambda c: c.sub_group)
            for c in all_child:
                r = gen_html_tree_view_rec(c)
                result.append(r)
            result.append('</ul>')
            result.append('</li>')
            return '\n'.join(result)
    s = gen_html_tree_view_rec(tax.root)
    return s


def gt_predict_images(predicts, gts, test_data, target_images, label, start_id, threshold,
        label_to_idx, image_aps, test_data_split='test'):
    test_dataset = TSVDataset(test_data)
    test_tsv = TSVFile(test_dataset.get_data(test_data_split))
    for i in xrange(start_id, len(target_images)):
        key = target_images[i]
        logging.info('key = {}, ap = {}'.format(key, image_aps[i][1]))
        idx = label_to_idx[key]
        row = test_tsv.seek(idx)
        origin = img_from_base64(row[2])
        im_gt = np.copy(origin)
        draw_bb(im_gt, [g['rect'] for g in gts[key]],
                [g['class'] for g in gts[key]])
        im_gt_target = np.copy(origin)
        gts_target = [g for g in gts[key] if g['class'] == label]
        draw_bb(im_gt_target, [g['rect'] for g in gts_target],
                [g['class'] for g in gts_target])
        im_pred = np.copy(origin)
        rects = [p for p in predicts[key] if p['conf'] > threshold]
        draw_bb(im_pred, [r['rect'] for r in rects],
                [r['class'] for r in rects],
                [r['conf'] for r in rects])
        im_pred_target = np.copy(origin)
        rects = [p for p in rects if p['class'] == label]
        draw_bb(im_pred_target, [r['rect'] for r in rects],
                [r['class'] for r in rects],
                [r['conf'] for r in rects])
        yield key, origin, im_gt_target, im_pred_target, im_gt, im_pred, image_aps[i][1]

def get_confusion_matrix_by_predict_file_label(full_expid,
        predict_file, label, threshold):
    '''
    get confusion matrix for specific label
    '''
    test_data, test_data_split = parse_test_data(predict_file)

    # load the gt
    logging.info('loading {} - {}'.format(test_data, test_data_split))
    test_dataset = TSVDataset(test_data)
    rows = test_dataset.iter_data(test_data_split, 'label', -1)
    gts = {}
    label_to_idx = {}
    keys_with_label = []
    for i, row in enumerate(rows):
        rects = json.loads(row[1])
        gts[row[0]] = rects
        label_to_idx[row[0]] = i
        if any(r['class'] == label for r in rects):
            keys_with_label.append(row[0])

    predict_full_path = op.join('output', full_expid, 'snapshot', predict_file)
    if not full_expid.startswith('Tax'):
        logging.info('loading {}'.format(predict_file))
        predicts, _ = load_labels(predict_full_path)
    else:
        # create the inverted index
        pred_label_file = '{}.labelmap.tsv'.format(predict_full_path)
        pred_inverted_file = '{}.inverted.tsv'.format(predict_full_path)
        pred_key_file = '{}.key.tsv'.format(predict_full_path)
        if not op.isfile(pred_label_file) or \
                not op.isfile(pred_inverted_file) or \
                not op.isfile(pred_key_file):
            pred_inverted = {}
            pred_keys = []
            rows = tsv_reader(predict_full_path)
            logging.info('loading data and creating index')
            inverted, pred_keys = create_inverted_list2(
                    tsv_reader(predict_full_path))
            logging.info('done loading data and creating index')
            pred_labels = inverted.keys()
            tsv_writer(([l] for l in pred_labels), pred_label_file)
            tsv_writer(((l, ' '.join(map(str, inverted[l]))) for l in
                    pred_labels), pred_inverted_file)
            tsv_writer([[k] for k in pred_keys], pred_key_file)
        # find out data whose prediction has the target label
        all_labels = load_list_file(pred_label_file)
        if label in all_labels:
            row = TSVFile(pred_inverted_file).seek(all_labels.index(label))
            assert row[0] == label
            idx = map(int, row[1].split(' '))
        else:
            idx = []
        # find out the index from the ground truth
        key_to_predidx = {k: i for i, k in enumerate(load_list_file(pred_key_file))}
        idx.extend([key_to_predidx[k] for k in keys_with_label])
        idx = set(idx)
        tsv = TSVFile(predict_full_path)
        predicts = {}
        logging.info('loading')
        for i in idx:
            row = tsv.seek(i)
            assert len(row) == 2
            predicts[row[0]] = json.loads(row[1])
        logging.info('done')
        # load data from the inverted index
    logging.info('done loading {}'.format(predict_file))

    return {'predicts': predicts,
            'gts': gts,
            'label_to_idx': label_to_idx}

def get_confusion_matrix_by_predict_file(full_expid,
        predict_file, threshold):
    '''
    get confusion matrix for all classes
    '''

    test_data, test_data_split = parse_test_data(predict_file)

    # load the gt
    logging.info('loading {} - {}'.format(test_data, test_data_split))
    test_dataset = TSVDataset(test_data)
    rows = test_dataset.iter_data(test_data_split, 'label', -1)
    gts = {}
    label_to_idx = {}
    for i, row in enumerate(rows):
        gts[row[0]] = json.loads(row[1])
        label_to_idx[row[0]] = i

    logging.info('loading {}'.format(predict_file))
    predicts, _ = load_labels(op.join('output', full_expid, 'snapshot', predict_file))
    logging.info('done loading {}'.format(predict_file))

    # calculate the confusion matrix
    confusion_pred_gt = {}
    confusion_gt_pred = {}
    update_confusion_matrix(predicts, gts, threshold,
            confusion_pred_gt,
            confusion_gt_pred)

    return {'predicts': predicts,
            'gts': gts,
            'confusion_pred_gt': confusion_pred_gt,
            'confusion_gt_pred': confusion_gt_pred,
            'label_to_idx': label_to_idx}

def inc_one_dic_dic(dic, c1, c2):
    if c1 not in dic:
        dic[c1] = {}
    if c2 not in dic[c1]:
        dic[c1][c2] = 0
    dic[c1][c2] = dic[c1][c2] + 1

def update_confusion_matrix(predicts, gts, threshold,
            confusion_pred_gt,
            confusion_gt_pred):
    for key in predicts:
        curr_pred = [p for p in predicts[key] if p['conf'] > threshold]
        if key not in gts:
            continue
        curr_gt = gts[key]
        if len(curr_pred) == 0 and len(curr_gt) > 0:
            for g in curr_gt:
                inc_one_dic_dic(confusion_gt_pred, g['class'], 'None')
            continue
        elif len(curr_pred) > 0 and len(curr_gt) == 0:
            for p in curr_pred:
                inc_one_dic_dic(confusion_pred_gt, p['class'], 'None')
            continue
        elif len(curr_pred) == 0 and len(curr_gt) == 0:
            continue
        ious = np.zeros((len(curr_pred), len(curr_gt)))
        for i, p in enumerate(curr_pred):
            for j, g in enumerate(curr_gt):
                iou = calculate_iou(p['rect'], g['rect'])
                ious[i, j] = iou
        gt_idx = np.argmax(ious, axis=1)
        for i, p in enumerate(curr_pred):
            j = gt_idx[i]
            predict_class = p['class']
            gt_class = curr_gt[j]['class']
            if ious[i, j] > 0.3:
                inc_one_dic_dic(confusion_pred_gt,
                        predict_class, gt_class)
            else:
                inc_one_dic_dic(confusion_pred_gt,
                        predict_class, 'None')
        pred_idx = np.argmax(ious, axis=0)
        for j, g in enumerate(curr_gt):
            i = pred_idx[j]
            predict_class = curr_pred[i]['class']
            gt_class = g['class']
            if ious[i, j] > 0.3:
                inc_one_dic_dic(confusion_gt_pred,
                        gt_class, predict_class)
            else:
                inc_one_dic_dic(confusion_gt_pred,
                        gt_class, 'None')

def normalize_str_in_rects(data, out_data):
    '''
    normalize all the unicode to string
    '''
    dataset = TSVDataset(data)
    dest_dataset = TSVDataset(out_data)
    splits = get_default_splits()
    for split in splits:
        if not op.isfile(dataset.get_data(split)):
            continue
        def gen_rows():
            for key, label_str, im_str in tsv_reader(dataset.get_data(split)):
                rects = json.loads(label_str)
                for rect in rects:
                    s = rect['class']
                    rect['class'] = normalize_to_str(s)
                    if s != rect['class']:
                        logging.info(u'{}->{}'.format(s, rect['class']))
                label_str = json.dumps(rects)
                yield key, label_str, im_str
        tsv_writer(gen_rows(), dest_dataset.get_data(split))

def tsv_details(row_hw, row_label, num_rows):
    label_count = {}
    sizes = []
    logging.info('tsv details...')
    for r_hw, r_label in tqdm(zip(row_hw, row_label), total=num_rows):
        if r_label[1] == 'd':
            # this is the deleted label
            rects = []
        else:
            rects = json.loads(r_label[1])
        assert r_hw[0] == r_label[0]
        height, width = map(int, r_hw[1].split(' '))
        if type(rects) is list:
            # this is the detection dataset
            # convert it to str. if it is unicode, in yaml, there will be some
            # special tags, which is annoying
            curr_labels = set(normalize_to_str(rect['class']) for rect in rects)
            for rect in rects:
                if 'rect' not in rect or all(x == 0 for x in rect['rect']):
                    # it is image-level annotation
                    continue
                r = rect['rect']
                # this should be a valid bounding box
                cx, cy = (r[0] + r[2]) / 2., (r[1] + r[3]) / 2.
                rw, rh = r[2] - r[0], r[3] - r[1]
                #assert cx >= 0 and cx < width \
                        #and cy >= 0 and cy < height
                if rw < 1 or rh < 1:
                    logging.warn('rw or rh too small: {} - {}'.format(r_label[0],
                        ','.join(map(str, r))))
        else:
            # this is classification dataset
            assert type(rects) is int
            curr_labels = [rects]
        for c in curr_labels:
            if c in label_count:
                label_count[c] = label_count[c] + 1
            else:
                label_count[c] = 1
        sizes.append((height, width))
    size_counts = [s[0] * s[1] for s in sizes]
    min_size = sizes[np.argmin(size_counts)]
    max_size = sizes[np.argmax(size_counts)]
    min_size = map(float, min_size)
    max_size = map(float, max_size)
    mean_size = (np.mean([s[0] for s in sizes]),
            np.mean([s[1] for s in sizes]))
    mean_size = map(float, mean_size)

    return {'label_count': label_count,
            'min_image_size': min_size,
            'max_image_size': max_size,
            'mean_image_size': mean_size}


def detect_duplicate_key(tsv, duplicate_tsv):
    rows = tsv_reader(tsv)
    key_to_idx = {}
    for i, row in enumerate(rows):
        key = row[0]
        if key in key_to_idx:
            key_to_idx[key].append(i)
        else:
            key_to_idx[key] = [i]
    def gen_rows():
        for key in key_to_idx:
            idxs = key_to_idx[key]
            if len(idxs) > 1:
                logging.info('duplicate key: {}: {}'.format(key, ', '.join(map(str,
                    idxs))))
                yield key, ','.join(map(str, idxs))
    tsv_writer(gen_rows(), duplicate_tsv)
    return TSVFile(duplicate_tsv).num_rows()

def populate_all_dataset_details():
    all_data = os.listdir('data/')
    for data in all_data:
        try:
            populate_dataset_details(data)
        except:
            continue

def update_labelmap(rows, num_rows, labelmap):
    '''
    labelmap is a hashset and can be added
    '''
    logging.info('updating labelmap')
    for row in tqdm(rows, total=num_rows):
        assert len(row) == 2
        try:
            labelmap.update(set([rect['class'] for rect in
                json.loads(row[1])]))
        except:
            labelmap.add(row[1])
    logging.info('done')

def derive_composite_meta_data(data, split, t):
    data_to_dataset = {}
    dataset = TSVDataset(data)
    all_source_data_split = dataset.load_composite_source_data_split(split)
    def gen_rows():
        assert dataset.has(split, 'label')
        iter_label = dataset.iter_data(split, 'label')
        iter_shuffle = tsv_reader(dataset.get_shuffle_file(split))
        for (idx_source, idx_row), (label_row) in zip(iter_shuffle,
                iter_label):
            idx_source = int(idx_source)
            idx_row = int(idx_row)
            c_data, c_split = all_source_data_split[idx_source]
            if c_data in data_to_dataset:
                c_dataset = data_to_dataset[c_data]
            else:
                c_dataset = TSVDataset(c_data)
                data_to_dataset[c_data] = c_dataset
            assert c_dataset.has(c_split, t)
            key, t_data = c_dataset.seek_by_idx(idx_row, c_split, t)
            yield label_row[0], t_data
    assert not dataset.has(split, t)
    dataset.write_data(gen_rows(), split, t)

def ensure_create_inverted_tsvs(dataset, splits):
    # for each data tsv, generate the inverted file, and the labelmap
    is_parallel = True
    for split in splits:
        if not dataset.has(split):
            continue
        latest = dataset.get_latest_version(split, 'label')
        if not is_parallel:
            for v in range(latest + 1):
                assert dataset.has(split, 'label', v)
                ensure_create_inverted_tsv_for_each((dataset, split, v))
        else:
            params = [(dataset, split, v) for v in range(latest + 1)]
            params = [param for param in params if
                    not has_inverted(param)]
            if len(params) > 0:
                p = Pool()
                p.map(ensure_create_inverted_tsv_for_each, params)

    # generate the inverted tsv for background images without any labels
    for split in splits:
        if not dataset.has(split):
            continue
        latest = dataset.get_latest_version(split, 'label')
        if not is_parallel:
            for v in range(latest + 1):
                assert dataset.has(split, 'label', v)
                ensure_create_inverted_tsv_background_for_each((dataset, split, v))
        else:
            params = [(dataset, split, v) for v in range(latest + 1)]
            params = [param for param in params if
                    not has_inverted_background(param)]
            if len(params) > 0:
                p = Pool()
                p.map(ensure_create_inverted_tsv_background_for_each, params)

def has_inverted(param):
    dataset, split, v = param
    inverted_keys = ['inverted.label',
            'inverted.label.with_bb',
            'inverted.label.no_bb',
            'inverted.label.with_bb.verified',
            'inverted.label.with_bb.noverified',]
    return all(dataset.has(split, k, v) for k in inverted_keys)

def has_inverted_background(args):
    dataset, split, v = args
    if dataset.has(split, 'inverted.background', version=v) and \
            dataset.has(split, 'inverted.background.count'):
        return True
    else:
        return False

def ensure_create_inverted_tsv_background_for_each(args):
    dataset, split, v = args
    if dataset.has(split, 'inverted.background', version=v) and \
            dataset.has(split, 'inverted.background.count'):
        return
    label_to_indices = dataset.load_inverted_label(split, version=v)
    all_idx = [i for l in label_to_indices for i in label_to_indices[l]]
    all_idx = set(all_idx)
    num = dataset.num_rows(split)
    background_idx = set(range(num)).difference(all_idx)

    def gen_rows():
        yield 'background', ' '.join(map(str, background_idx))
    dataset.write_data(gen_rows(), split, 'inverted.background', version=v)

    def gen_row_count():
        yield 'background', len(background_idx)
    dataset.write_data(gen_row_count(), split, 'inverted.background.count',
            version=v)

def ensure_create_inverted_tsv_for_each(args):
    dataset, split, v = args
    if not dataset.has(split, 'labelmap', v) or \
        dataset.last_update_time(split, 'labelmap', v) < dataset.last_update_time(split, 'label', v):
        curr_labelmap = set()
        update_labelmap(dataset.iter_data(split, 'label', v),
                dataset.num_rows(split),
                curr_labelmap)
        curr_labelmap = sorted(list(curr_labelmap))
        dataset.write_data([[l] for l in curr_labelmap], split, 'labelmap', v)
    else:
        curr_labelmap = None
    inverted_keys = ['inverted.label',
            'inverted.label.with_bb',
            'inverted.label.no_bb',
            'inverted.label.with_bb.verified',
            'inverted.label.with_bb.noverified',]
    if any(not dataset.has(split, k, v) for k in inverted_keys):
        logging.info('version = {}'.format(v))
        if curr_labelmap is None:
            curr_labelmap = []
            for row in dataset.iter_data(split, 'labelmap', v):
                assert len(row) == 1
                curr_labelmap.append(row[0])
        def gen_inverted_rows(inv):
            logging.info('re-orderring')
            set_labelmap = set(curr_labelmap)
            for label in tqdm(inv):
                assert label in set_labelmap
            for label in tqdm(curr_labelmap):
                i = inv[label] if label in inv else []
                yield label, ' '.join(map(str, i))
        inverted_result = create_inverted_list(
                dataset.iter_data(split, 'label', v))
        for k in inverted_keys:
            dataset.write_data(gen_inverted_rows(inverted_result[k]),
                    split, k, v)

def ensure_extract_from_data_source(data, split, t):
    assert t != 'label'
    dataset = TSVDataset(data)
    if dataset.has(split, t):
        logging.info('ignore to generate {}, {}, {}'.format(data, split, t))
        return
    all_data_split = dataset.load_composite_source_data_split(split)
    source_data_to_dataset = {}
    label_iter = dataset.iter_data(split, 'label')
    shuffle_iter = tsv_reader(dataset.get_shuffle_file(split))
    def gen_rows():
        for (idx_source, idx_row), label_row in zip(shuffle_iter, label_iter):
            idx_source, idx_row = int(idx_source), int(idx_row)
            source_data, source_split = all_data_split[idx_source]
            if source_data not in source_data_to_dataset:
                source_data_to_dataset[source_data] = TSVDataset(source_data)
            source_dataset = source_data_to_dataset[source_data]
            source_key, str_t = source_dataset.seek_by_idx(idx_row, source_split, t)
            assert label_row[0] == '_'.join([source_data, source_split, source_key])
            yield label_row[0], str_t
    dataset.write_data(gen_rows(), split, t)

def populate_dataset_hw(data,
                        splits=('train', 'trainval', 'test', 'val'),
                        img_t=None,
                        multi_thread=True,
                        ):
    dataset = TSVDataset(data)
    for split in splits:
        if dataset.has(split, img_t) and not dataset.has(split, 'hw'):
            success = False
            if op.isfile(dataset.get_data(split + 'X', img_t)):
                try:
                    derive_composite_meta_data(data, split, 'hw')
                    success = True
                except:
                    pass
            if not success:
                if not multi_thread:
                    logging.info('generating hw')
                    rows = dataset.iter_data(split, img_t, progress=True)
                    dataset.write_data(((row[0], ' '.join(map(str,
                        img_from_base64(row[-1]).shape[:2]))) for
                        row in rows), split, 'hw')
                else:
                    num_images = dataset.num_rows(split, img_t)
                    num_tasks = min(num_images, 128 * 3)
                    num_worker = (num_tasks + 2) // 3
                    num_image_per_worker = (num_images + num_tasks - 1) // num_tasks
                    assert num_image_per_worker > 0
                    all_idx = []
                    for i in range(num_tasks):
                        curr_idx_start = i * num_image_per_worker
                        if curr_idx_start >= num_images:
                            break
                        curr_idx_end = curr_idx_start + num_image_per_worker
                        curr_idx_end = min(curr_idx_end, num_images)
                        if curr_idx_end > curr_idx_start:
                            all_idx.append(range(curr_idx_start, curr_idx_end))
                    logging.info('creating pool')
                    m = Pool(num_worker)
                    def get_hw(filter_idx):
                        dataset = TSVDataset(data)
                        rows = dataset.iter_data(split, img_t, progress=True,
                                filter_idx=filter_idx)
                        return [(row[0], ' '.join(map(str,
                            img_from_base64(row[-1]).shape[:2])))
                            for row in rows]
                    all_result = m.map(get_hw, all_idx)
                    x = []
                    for r in all_result:
                        x.extend(r)
                    dataset.write_data(x, split, 'hw')

def ensure_label_extract(data, splits):
    dataset = TSVDataset(data)
    for split in splits:
        if (not dataset.has(split, 'label')) and \
                dataset.has(split):
            def gen_rows():
                if op.isfile(dataset.get_data(split)):
                    iter_row = dataset.iter_data(split)
                else:
                    iter_row = dataset.iter_composite(split, t=None, version=0)
                for row in tqdm(iter_row):
                    if len(row) == 3:
                        str_rects = row[1]
                    else:
                        assert len(row) == 2
                        str_rects = json_dump([])
                    yield row[0], str_rects
            dataset.write_data(gen_rows(), split, 'label')

def ensure_labelmap_extract(data):
    splits = get_default_splits()
    dataset = TSVDataset(data)
    # generate the label map if there is no
    if not op.isfile(dataset.get_labelmap_file()) and \
            not op.islink(dataset.get_labelmap_file()):
        logging.info('no labelmap, generating...')
        labelmap = []
        for split in splits:
            label_tsv = dataset.get_data(split, 'label', version=0)
            if not op.isfile(label_tsv):
                continue
            for row in tqdm(tsv_reader(label_tsv)):
                try:
                    labelmap.extend(set([rect['class'] for rect in
                        json.loads(row[1])]))
                except:
                    labelmap.append(row[1])
        if len(labelmap) == 0:
            logging.warning('there are no labels!')
        labelmap = sorted(list(set(labelmap)))
        logging.info('find {} labels'.format(len(labelmap)))
        need_update = False
        if op.isfile(dataset.get_labelmap_file()):
            origin_labelmap = dataset.load_labelmap()
            if len(origin_labelmap) == len(labelmap):
                for o in origin_labelmap:
                    if o not in labelmap:
                        need_update = True
                        break
            else:
                need_update = True
        else:
            need_update = True
        if need_update:
            logging.info('updating {}'.format(dataset.get_labelmap_file()))
            write_to_file('\n'.join(labelmap), dataset.get_labelmap_file())

def populate_dataset_details(data, check_image_details=False,
        splits=None, check_box=False, data_root=None):
    logging.info(data)
    dataset = TSVDataset(data)

    if not splits:
        splits = get_default_splits()

    # populate the height and with
    if check_image_details:
        populate_dataset_hw(data, splits)

    ensure_label_extract(data, splits)

    for split in splits:
        tsv_file = dataset.get_data(split)
        out_file = get_meta_file(tsv_file)
        if not op.isfile(out_file) and \
                dataset.has(split, 'hw') and \
                dataset.has(split):
            row_hw = dataset.iter_data(split, 'hw')
            row_label = dataset.iter_data(split, 'label')
            num_rows = dataset.num_rows(split)
            if check_image_details:
                details = tsv_details(row_hw, row_label, num_rows)
                write_to_yaml_file(details, out_file)

    ensure_labelmap_extract(data)

    labelmap = []
    if not op.isfile(dataset.get_pos_labelmap_file()):
        ls = dataset.load_labelmap()
        write_to_file('\n'.join([l for l in ls if not l.startswith('-')]),
                dataset.get_pos_labelmap_file())

    # generate the rows with duplicate keys
    for split in splits:
        label_tsv = dataset.get_data(split, 'label')
        duplicate_tsv = dataset.get_data(split, 'key_duplicate')
        if op.isfile(label_tsv) and not op.isfile(duplicate_tsv):
            detect_duplicate_key(label_tsv, duplicate_tsv)

    # generate lineidx if it is not generated
    for split in splits:
        lineidx = dataset.get_lineidx(split)
        full_tsv = dataset.get_data(split)
        if not op.isfile(lineidx) and op.isfile(full_tsv):
            logging.info('no lineidx for {}. generating...'.format(split))
            generate_lineidx(full_tsv, lineidx)

    ensure_create_inverted_tsvs(dataset, splits)
    # check if the number of rows from the label tsv are equal to the number of rows in image tsv
    for split in splits:
        v = 0
        num_rows = None
        while True:
            if not dataset.has(split, 'label', v):
                break
            if dataset.has(split, 'check_count', v):
                v = v + 1
                continue
            if num_rows is None:
                num_rows = dataset.num_rows(split)
            num_rows_in_label = dataset.num_rows(split, 'label', v)
            # we can remove the assert
            assert num_rows == num_rows_in_label
            if num_rows != num_rows_in_label:
                dataset.write_data([['num_rows', num_rows], ['num_rows_in_label', num_rows_in_label]],
                        split, 'check_count', v)
            else:
                dataset.write_data([], split, 'check_count', v)
            v = v + 1

    if not op.isfile(dataset.get_noffsets_file()):
        logging.info('no noffset file. generating...')
        labelmap = dataset.load_labelmap()
        mapper = LabelToSynset(True)
        ambigous = []
        ss = [mapper.convert(l) for l in labelmap]
        for l, (success, s) in zip(labelmap, ss):
            if not success and len(s) > 1:
                d = create_info_for_ambigous_noffset(l, [synset_to_noffset(s1)
                    for s1 in s])
                ambigous.append(d)
        if len(ambigous) > 0:
            logging.info('ambigous term which has no exact noffset: {}'.format(
                dataset.name))
            write_to_yaml_file(ambigous, dataset.get_noffsets_file() +
                    '.ambigous.yaml')
        noffsets = []
        for success, s in ss:
            if not success:
                noffsets.append('')
            else:
                noffsets.append(','.join([synset_to_noffset(o) for o in s]))
        write_to_file('\n'.join(noffsets), dataset.get_noffsets_file())

    if not op.isfile(dataset.get_labelmap_of_noffset_file()):
        noffsets = dataset.load_noffsets()
        all_line = []
        for noffset in noffsets:
            if len(noffset) == 0:
                all_line.append('unkown')
            else:
                ss = [noffset_to_synset(n) for n in noffset.split(',')]
                all_line.append(','.join([get_nick_name(s) for s in ss]))
        write_to_file('\n'.join(all_line),
                dataset.get_labelmap_of_noffset_file())

    # generate the label -> count tsv
    for split in splits:
        v = 0
        while True:
            label_idx_file = dataset.get_data(split, 'inverted.label', v)
            label_count_file = dataset.get_data(split, 'inverted.label.count', v)
            if op.isfile(label_idx_file):
                if not op.isfile(label_count_file):
                    label_idx = dataset.load_inverted_label_as_list(split, v)
                    tsv_writer(((l, str(len(i))) for l, i in label_idx), label_count_file)
            else:
                break
            v = v + 1

    if op.isfile(dataset.get_data('trainX')):
        # composite dataset.
        create_index_composite_dataset(dataset)

    populate_num_images_composite(dataset)

    add_node_to_ancestors(dataset)

    populate_all_label_counts(dataset)

    if check_box:
        populate_bbcount(dataset)

def create_label_bbcount(dataset, split, v):
    class_to_bbcount = defaultdict(int)
    logging.info('reading {}, {}, {}'.format(split, 'label', v))
    for key, str_rects in dataset.iter_data(split, 'label',
        v, progress=True):
        rects = json.loads(str_rects)
        for r in rects:
            if 'rect' in r and not all(x == 0 for x in r['rect']):
                c = r['class']
                class_to_bbcount[c] = class_to_bbcount[c] + 1
    labelmap = [l for l, in dataset.iter_data(split, 'labelmap',
            v)]
    dataset.write_data(((l, class_to_bbcount.get(l, 0)) for l in labelmap),
            split, 'label.bbcount', v)

def create_label_verifiedbbcount(dataset, split, v):
    class_to_bbcount = defaultdict(int)
    logging.info('reading {}, {}, {}'.format(split, 'label', v))
    for key, str_rects in dataset.iter_data(split, 'label',
        v, progress=True):
        rects = json.loads(str_rects)
        for r in rects:
            if 'rect' in r and \
                    not all(x == 0 for x in r['rect']) and \
                    is_verified_rect(r):
                c = r['class']
                class_to_bbcount[c] = class_to_bbcount[c] + 1
    labelmap = [l for l, in dataset.iter_data(split, 'labelmap',
            v)]
    dataset.write_data(((l, class_to_bbcount.get(l, 0)) for l in labelmap),
            split, 'label.verifiedbbcount', v)

def populate_bbcount(dataset):
    for split in get_default_splits():
        v = 0
        while True:
            if dataset.has(split, 'label', v):
                if not dataset.has(split, 'label.verifiedbbcount', v):
                    create_label_verifiedbbcount(dataset, split, v)
                if not dataset.has(split, 'label.bbcount', v):
                    create_label_bbcount(dataset, split, v)
                v = v + 1
            else:
                break

def populate_all_label_counts(dataset):
    for split in get_default_splits():
        v = 0
        while True:
            if dataset.has(split, 'inverted.label.count', v):
                if not dataset.has(split, 'inverted.label.count.total', v):
                    count = sum([int(count) for _, count in dataset.iter_data(split,
                        'inverted.label.count', v)])
                    dataset.write_data([(str(count),)],
                            split,
                            'inverted.label.count.total', v)
                v = v + 1
            else:
                break

def add_node_to_ancestors(dataset):
    tree_file = op.join(dataset._data_root, 'root.yaml')
    out_file = op.join(dataset._data_root, 'treenode_to_ancestors.tsv')
    if op.isfile(tree_file) and worth_create(tree_file, out_file):
        tax = Taxonomy(load_from_yaml_file(tree_file))
        tax.update()
        tsv_writer([[name, ','.join(tax.name_to_ancestors_list[name])] for name in
            tax.name_to_ancestors_list], out_file)

def populate_num_images_composite(dataset):
    data_with_bb = dataset.name + '_with_bb'
    data_no_bb = dataset.name + '_no_bb'
    datas = [data_with_bb, data_no_bb]
    datasets = [TSVDataset(d) for d in datas]
    suffixes = ['with_bb', 'no_bb']
    splits = ['train', 'test']
    dest_tree_file = op.join(dataset._data_root, 'root_enriched.yaml')
    src_tree_file = op.join(dataset._data_root, 'root.yaml')
    if op.isfile(dest_tree_file) or not op.isfile(src_tree_file):
        return
    tax = Taxonomy(load_from_yaml_file(src_tree_file))
    for split in splits:
        for suffix, d in zip(suffixes, datasets):
            if not d.has(split, 'inverted.label.count'):
                continue
            label_to_count_with_bb = {label: int(count) for label, count in d.iter_data(split,
                    'inverted.label.count')}
            for label in label_to_count_with_bb:
                count = label_to_count_with_bb[label]
                if not label.startswith('-'):
                    nodes = tax.root.search_nodes(name=label)
                else:
                    nodes = tax.root.search_nodes(name=label[1:])
                assert len(nodes) == 1, label
                node = nodes[0]
                if not label.startswith('-'):
                    node.add_feature('{}_{}'.format(suffix, split), count)
                else:
                    node.add_feature('{}_{}_neg'.format(suffix, split), count)
    write_to_yaml_file(tax.dump(), dest_tree_file)

def create_index_composite_dataset(dataset):
    split = 'train'
    splitx = split + 'X'
    fname_numImagesPerSource = dataset.get_data(splitx,
            'numImagesPerSource')
    if op.isfile(fname_numImagesPerSource):
        return
    # how many images are contributed from each data source
    trainX_file = dataset.get_data(splitx)
    source_tsvs = load_list_file(trainX_file)

    shuffle_file = dataset.get_shuffle_file(split)
    if not op.isfile(shuffle_file):
        return
    rows = tsv_reader(shuffle_file)
    all_idxSource_idxRow = []
    for idx_source, idx_row in rows:
        all_idxSource_idxRow.append((int(idx_source), int(idx_row)))
    # note, the data may be duplicated.
    all_idxSource_idxRow = list(set(all_idxSource_idxRow))

    idxSource_to_idxRows = list_to_dict(all_idxSource_idxRow, 0)
    num_images_per_datasource = [None] * len(source_tsvs)
    num_srcimages_per_datasource = [TSVFile(s).num_rows() for s in source_tsvs]
    for idxSource in idxSource_to_idxRows:
        assert num_images_per_datasource[idxSource] is None
        num_images_per_datasource[idxSource] = len(idxSource_to_idxRows[idxSource])
    tsv_writer([(name, str(num)) for name, num in zip(source_tsvs,
        num_images_per_datasource)], fname_numImagesPerSource)
    dataset.write_data([(source_tsvs[i], num_images_per_datasource[i], num_srcimages_per_datasource[i])
        for i in range(len(source_tsvs))], splitx,
        'tsvsource.numdestimages.numsrcimages')

    # for each data source, how many labels are contributed and how many are
    # not
    if op.isfile(dataset.get_data(splitx, 'origin.label')):
        source_tsv_label_files = load_list_file(dataset.get_data(splitx,
            'origin.label'))
        source_tsv_labels = [TSVFile(t) for t in source_tsv_label_files]
        trainX_label_file = dataset.get_data(splitx, 'label')
        all_dest_label_file = load_list_file(trainX_label_file)
        dest_labels = [TSVFile(f) for f in all_dest_label_file]
        all_idxSource_sourceLabel_destLabel = []
        logging.info('each datasource and each idx row')
        idxSource_to_numRect = {}
        all_idxSource_numSourceRects_numDestRects = []
        for idx_source, idx_row in tqdm(all_idxSource_idxRow):
            source_rects = json.loads(source_tsv_labels[idx_source].seek(idx_row)[-1])
            dest_rects = json.loads(dest_labels[idx_source].seek(idx_row)[-1])
            if idx_source not in idxSource_to_numRect:
                idxSource_to_numRect[idx_source] = 0
            idxSource_to_numRect[idx_source] = idxSource_to_numRect[idx_source] + \
                len(dest_rects)
            all_idxSource_numSourceRects_numDestRects.append((
                idx_source, len(source_rects), len(dest_rects)))
            for r in dest_rects:
                all_idxSource_sourceLabel_destLabel.append((idx_source,
                    r.get('class_from', r['class']), r['class']))
        idxSource_to_numSourceRects_numDestRects = list_to_dict(
                all_idxSource_numSourceRects_numDestRects, 0)
        idxSource_to_numSourceRects_numDestRect = {idxSource: [sum(x1 for x1, x2 in idxSource_to_numSourceRects_numDestRects[idxSource]),
                 sum(x2 for x1, x2 in idxSource_to_numSourceRects_numDestRects[idxSource])]
            for idxSource in idxSource_to_numSourceRects_numDestRects}

        dataset.write_data([(source_tsvs[idxSource],
            idxSource_to_numSourceRects_numDestRect[idxSource][1],
            idxSource_to_numSourceRects_numDestRect[idxSource][0])
            for idxSource in range(len(source_tsvs))],
            splitx, 'tsvsource.numdestbbs.numsrcbbs')

        sourcetsv_to_num_rect = {source_tsvs[idx_source]: idxSource_to_numRect[idx_source]
                for idx_source in idxSource_to_numRect}
        dataset.write_data([(s, sourcetsv_to_num_rect[s]) for s in source_tsvs],
            splitx, 'numRectsPerSource')

        idxSource_to_sourceLabel_destLabels = list_to_dict(
                all_idxSource_sourceLabel_destLabel, 0)
        source_numSourceLabels = [(s, 0) for s in source_tsvs]
        source_includedSourceLabels = [(s, []) for s in source_tsvs]
        for idxSource in idxSource_to_sourceLabel_destLabels:
            sourceLabel_destLabels = idxSource_to_sourceLabel_destLabels[idxSource]
            sourceLabel_to_destLabels = list_to_dict(sourceLabel_destLabels, 0)
            source_numSourceLabels[idxSource] = (source_tsvs[idxSource],
                    len(sourceLabel_to_destLabels))
            source_includedSourceLabels[idxSource][1].extend(
                    sourceLabel_to_destLabels.keys())

        dataset.write_data([(n, str(i)) for (n, i) in source_numSourceLabels],
                splitx, 'numCategoriesPerSource')

        # save teh list of included labels
        sourceDataset_to_includedSourceLabels = {}
        for source, sourceLabels in source_includedSourceLabels:
            source_dataset_name = op.basename(op.dirname(source))
            if source_dataset_name not in sourceDataset_to_includedSourceLabels:
                sourceDataset_to_includedSourceLabels[source_dataset_name] = []
            sourceDataset_to_includedSourceLabels[source_dataset_name].extend(sourceLabels)
        for source_dataset_name in sourceDataset_to_includedSourceLabels:
            sourceDataset_to_includedSourceLabels[source_dataset_name] = \
                    set(sourceDataset_to_includedSourceLabels[source_dataset_name])

        tsv_writer([(n, ','.join(sourceDataset_to_includedSourceLabels[n])) for n in
            sourceDataset_to_includedSourceLabels], op.join(dataset._data_root,
                'trainX.includeCategoriesPerSourceDataset.tsv'))

        tsv_writer([(n, get_nick_name(noffset_to_synset(s)) if is_noffset(s) else
            s) for n in sourceDataset_to_includedSourceLabels
              for s in sourceDataset_to_includedSourceLabels[n]],
              op.join(dataset._data_root, 'trainX.includeCategoriesPerSourceDatasetReadable.tsv'))

class TSVTransformer(object):
    def __init__(self):
        self._total_rows = 0
        self._row_processor = None

    def ReadProcess(self, source_tsv, row_processor):
        self._row_processor = row_processor
        self._total_rows = 0

        rows = tsv_reader(source_tsv)
        x = [self._over_row_processor(row) for row in rows]

        logging.info('total rows = {}; total_processed = {}'.format(self._total_rows,
                len(x)))

    def Process(self, source_tsv, dst_tsv, row_processor):
        '''
        row_processor: a function whose input should be a list of tsv cols and
                      whose return is also a list of tsv colums (will be saved into dst_tsv)
        '''
        self._row_processor = row_processor
        self._total_rows = 0

        rows = tsv_reader(source_tsv)
        result = (self._over_row_processor(row) for row in rows)
        tsv_writer(result, dst_tsv)

        logging.info('total rows = {}'.format(self._total_rows))

    def _over_row_processor(self, row):
        out = self._row_processor(row)
        self._total_rows = self._total_rows + 1
        if self._total_rows % 500 == 0:
            logging.info('processed = {}'.format(self._total_rows))
        return out

def randomize_tsv_file(tsv_file):
    prefix = os.path.splitext(tsv_file)[0]
    shuffle_file = prefix + '.shuffle'
    if os.path.exists(shuffle_file):
        return shuffle_file
    idx_file = prefix + '.lineidx'
    with open(idx_file, 'r') as fp:
        num = len([line for line in fp.readlines() if len(line.strip()) > 0])
    np.random.seed(777)
    nums = np.random.permutation(num)
    result = '\n'.join(map(str, nums))
    with open(shuffle_file, 'w') as fp:
        fp.write(result)
    return shuffle_file

def gen_tsv_from_labeling(input_folder, output_folder):
    fs = glob.glob(op.join(input_folder, '*'))
    labels = set()
    def gen_rows():
        for f in fs:
            im = cv2.imread(f, cv2.IMREAD_COLOR)
            if im is None:
                continue
            yaml_file = op.splitext(f)[0] + '.yaml'
            if not op.isfile(yaml_file):
                logging.info('{} not exist'.format(yaml_file))
            with open(yaml_file, 'r') as fp:
                bb_labels = yaml.loads(fp.read())
            for bb_label in bb_labels:
                labels.add(bb_label['class'])
            with open(f, 'r') as fp:
                encoded_im = base64.b64encode(fp.read())
            yield op.basename(f), json.dumps(bb_labels), encoded_im

    tsv_writer(gen_rows(), op.join(output_folder, 'train.tsv'))
    write_to_file('\n'.join(labels), op.join(output_folder, 'labelmap.txt'))

def try_json_parse(s):
    try:
        return json.loads(s)
    except ValueError:
        return s

def get_label_type(full_expid, predict_file):
    label_type = 'label'
    for row in tsv_reader(
        op.join('output', full_expid, 'snapshot', predict_file)):
        rects = json.loads(row[-1])
        if len(rects) > 0:
            if 'caption' in rects[0]:
                label_type = 'caption'
                break
    return label_type

def iter_full_expid_pred(
    full_expid, predict_file, start_id, threshold,
        label_type, label_version=None):
    test_data, test_data_split = parse_test_data(predict_file)
    pred_tsv = TSVFile(op.join('output', full_expid, 'snapshot', predict_file))
    num_total = len(pred_tsv)
    while start_id < 0:
        start_id += num_total
    test_dataset = TSVDataset(test_data)
    iter_image = test_dataset.iter_data(
        test_data_split,
        filter_idx=range(start_id, num_total))
    has_gt = test_dataset.has(test_data_split, t=label_type, version=label_version)
    if has_gt:
        iter_gt = test_dataset.iter_data(test_data_split, t=label_type,
                                     version=label_version,
                                     filter_idx=range(start_id, num_total))
    for i, row_image in enumerate(iter_image):
        key = row_image[0]
        if has_gt:
            row_gt = next(iter_gt)
            str_gt = row_gt[-1]
            key = row_gt[0]
            rects_gt = json.loads(str_gt)
            if not isinstance(rects_gt, list):
                rects_gt = [{'class': str(rects_gt)}]
        else:
            rects_gt = []
        str_im = row_image[-1]
        pred_key, str_pred = pred_tsv[i + start_id]
        assert pred_key == key, (i,pred_key,key)
        im = img_from_base64(str_im)
        rects_pred = [r for r in json.loads(str_pred) if r['conf'] > threshold]
        yield {
            'key': key,
            'im': im,
            'rects_gt': rects_gt,
            'rects_pred': rects_pred,
        }

def visualize_predict_no_draw(full_expid, predict_file, label, start_id,
        threshold):
    test_data, test_data_split = parse_test_data(predict_file)
    logging.info('{}->{}/{}'.format(predict_file, test_data, test_data_split))
    pred_full_path = op.join('output', full_expid, 'snapshot', predict_file)
    pred_key_path = '{}.key.tsv'.format(pred_full_path)
    pred_label_path = '{}.labelmap.th{}.tsv'.format(pred_full_path,
            threshold)
    pred_inverted_path = '{}.inverted.th{}.tsv'.format(pred_full_path,
            threshold)
    pred_sorted_cache_path = '{}.key_idxGT_idxPred_ap.th{}.{}.tsv'.format(
            pred_full_path, threshold, label)

    test_dataset = TSVDataset(test_data)
    if not op.isfile(pred_sorted_cache_path):
        if not op.isfile(pred_key_path) or \
                not op.isfile(pred_label_path) or \
                not op.isfile(pred_inverted_path):
            logging.info('loading {}'.format(pred_full_path))
            inverted, pred_keys = create_inverted_list2(
                    tsv_reader(pred_full_path), threshold)
            pred_labels = inverted.keys()
            logging.info('writing {}'.format(pred_key_path))
            tsv_writer([[k] for k in pred_keys], pred_key_path)
            tsv_writer(([l] for l in pred_labels), pred_label_path)
            tsv_writer(((l, ' '.join(map(str, inverted[l]))) for l in
                    pred_labels), pred_inverted_path)
        keys_from_pred = []
        labelmap = load_list_file(pred_label_path)
        pred_keys = load_list_file(pred_key_path)
        if label in labelmap:
            inverted_row = TSVFile(pred_inverted_path).seek(labelmap.index(label))
            assert inverted_row[0] == label
            assert len(inverted_row) == 2
            idx_from_pred = map(int, inverted_row[1].split(' '))
            keys_from_pred = [pred_keys[i] for i in idx_from_pred]
        else:
            keys_from_pred = []
        inverted_test_split = test_dataset.load_inverted_label(test_data_split, version=-1,
                label=label)
        if label in inverted_test_split:
            idx_from_gt = inverted_test_split[label]
        else:
            idx_from_gt = []
        rows = test_dataset.iter_data(test_data_split, t='label', version=-1,
                filter_idx=idx_from_gt, unique=True)
        keys_from_gt = [row[0] for row in rows]
        target_keys = list(set(keys_from_pred + keys_from_gt))
        target_keys = [k for k in target_keys if k in pred_keys]
        target_idx_in_pred = [pred_keys.index(k) for k in target_keys]
        gt_keys = test_dataset.load_keys(test_data_split)
        target_idx_in_gt = [gt_keys.index(k) for k in target_keys]
        rows_in_gt = test_dataset.iter_data(test_data_split, t='label', version=-1,
                filter_idx=target_idx_in_gt)
        pred_tsv = TSVFile(pred_full_path)
        rows_in_pred = (pred_tsv.seek(i) for i in target_idx_in_pred)
        target_aps = []
        for row_in_gt, row_in_pred in zip(rows_in_gt, rows_in_pred):
            assert row_in_gt[0] == row_in_pred[0]
            assert len(row_in_gt) == 2
            assert len(row_in_pred) == 2
            rects_gt = json.loads(row_in_gt[1])
            rects_pred = json.loads(row_in_pred[1])
            rects_gt = [r for r in rects_gt if r['class'] == label]
            rects_pred = [r for r in rects_pred if r['class'] == label]
            ap = calculate_image_ap([r['rect'] for r in rects_gt if 'rect' in r],
                    [r['rect'] for r in rects_pred])
            target_aps.append(ap)
        key_idxGT_idxPred_aps = zip(target_keys, target_idx_in_gt,
                target_idx_in_pred, target_aps)
        key_idxGT_idxPred_aps = sorted(key_idxGT_idxPred_aps, key=lambda x:
                x[-1])
        tsv_writer(key_idxGT_idxPred_aps, pred_sorted_cache_path)

    tsv = TSVFile(pred_sorted_cache_path)
    total_num = tsv.num_rows()
    if total_num == 0:
        return
    while start_id < 0:
        start_id = start_id + total_num
    while start_id >= total_num:
        start_id = start_id - total_num
    i = start_id
    tsv_pred = TSVFile(pred_full_path)
    for i in range(start_id, total_num):
        key, idx_gt, idx_pred, ap = tsv.seek(i)
        idx_gt, idx_pred, ap = int(idx_gt), int(idx_pred), float(ap)
        row_gt = next(test_dataset.iter_data(test_data_split,
            filter_idx=[idx_gt]))
        row_pred = tsv_pred.seek(idx_pred)
        assert row_gt[0] == row_pred[0], (row_gt[0], row_pred[0])

        rects_gt = json.loads(next(test_dataset.iter_data(test_data_split, 'label',
            filter_idx=[idx_gt], version=-1))[1])

        rects_pred = json.loads(row_pred[1])
        rects_pred = [r for r in rects_pred if r['conf'] > threshold]
        im_origin = img_from_base64(row_gt[-1])
        yield key, im_origin, rects_gt, rects_pred, ap

def visualize_predict(full_expid, predict_file, label, start_id, threshold):
    test_data, test_data_split = parse_test_data(predict_file)
    pred_full_path = op.join('output', full_expid, 'snapshot', predict_file)
    pred_key_path = '{}.key.tsv'.format(pred_full_path)
    pred_label_path = '{}.labelmap.th{}.tsv'.format(pred_full_path,
            threshold)
    pred_inverted_path = '{}.inverted.th{}.tsv'.format(pred_full_path,
            threshold)
    pred_sorted_cache_path = '{}.key_idxGT_idxPred_ap.th{}.{}.tsv'.format(
            pred_full_path, threshold, label)

    test_dataset = TSVDataset(test_data)
    if not op.isfile(pred_sorted_cache_path):
        if not op.isfile(pred_key_path) or \
                not op.isfile(pred_label_path) or \
                not op.isfile(pred_inverted_path):
            logging.info('loading {}'.format(pred_full_path))
            inverted, pred_keys = create_inverted_list2(
                    tsv_reader(pred_full_path), threshold)
            pred_labels = inverted.keys()
            logging.info('writing {}'.format(pred_key_path))
            tsv_writer([[k] for k in pred_keys], pred_key_path)
            tsv_writer(([l] for l in pred_labels), pred_label_path)
            tsv_writer(((l, ' '.join(map(str, inverted[l]))) for l in
                    pred_labels), pred_inverted_path)
        keys_from_pred = []
        labelmap = load_list_file(pred_label_path)
        pred_keys = load_list_file(pred_key_path)
        if label in labelmap:
            inverted_row = TSVFile(pred_inverted_path).seek(labelmap.index(label))
            assert inverted_row[0] == label
            assert len(inverted_row) == 2
            idx_from_pred = map(int, inverted_row[1].split(' '))
            keys_from_pred = [pred_keys[i] for i in idx_from_pred]
        else:
            keys_from_pred = []
        inverted_test_split = test_dataset.load_inverted_label(test_data_split, version=-1,
                label=label)
        if label in inverted_test_split:
            idx_from_gt = inverted_test_split[label]
        else:
            idx_from_gt = []
        rows = test_dataset.iter_data(test_data_split, t='label', version=-1,
                filter_idx=idx_from_gt, unique=True)
        keys_from_gt = [row[0] for row in rows]
        target_keys = list(set(keys_from_pred + keys_from_gt))
        target_keys = [k for k in target_keys if k in pred_keys]
        target_idx_in_pred = [pred_keys.index(k) for k in target_keys]
        gt_keys = test_dataset.load_keys(test_data_split)
        target_idx_in_gt = [gt_keys.index(k) for k in target_keys]
        rows_in_gt = test_dataset.iter_data(test_data_split, t='label', version=-1,
                filter_idx=target_idx_in_gt)
        pred_tsv = TSVFile(pred_full_path)
        rows_in_pred = (pred_tsv.seek(i) for i in target_idx_in_pred)
        target_aps = []
        for row_in_gt, row_in_pred in zip(rows_in_gt, rows_in_pred):
            assert row_in_gt[0] == row_in_pred[0]
            assert len(row_in_gt) == 2
            assert len(row_in_pred) == 2
            rects_gt = json.loads(row_in_gt[1])
            rects_pred = json.loads(row_in_pred[1])
            rects_gt = [r for r in rects_gt if r['class'] == label]
            rects_pred = [r for r in rects_pred if r['class'] == label]
            ap = calculate_image_ap([r['rect'] for r in rects_gt],
                    [r['rect'] for r in rects_pred])
            target_aps.append(ap)
        key_idxGT_idxPred_aps = zip(target_keys, target_idx_in_gt,
                target_idx_in_pred, target_aps)
        key_idxGT_idxPred_aps = sorted(key_idxGT_idxPred_aps, key=lambda x:
                x[-1])
        tsv_writer(key_idxGT_idxPred_aps, pred_sorted_cache_path)

    tsv = TSVFile(pred_sorted_cache_path)
    total_num = tsv.num_rows()
    if total_num == 0:
        return
    while start_id < 0:
        start_id = start_id + total_num
    while start_id >= total_num:
        start_id = start_id - total_num
    i = start_id
    tsv_pred = TSVFile(pred_full_path)
    for i in range(start_id, total_num):
        key, idx_gt, idx_pred, ap = tsv.seek(i)
        idx_gt, idx_pred, ap = int(idx_gt), int(idx_pred), float(ap)
        row_gt = next(test_dataset.iter_data(test_data_split,
            filter_idx=[idx_gt]))
        row_pred = tsv_pred.seek(idx_pred)
        assert row_gt[0] == row_pred[0], (row_gt[0], row_pred[0])

        rects_gt = json.loads(row_gt[1])
        rects_pred = json.loads(row_pred[1])
        rects_pred = [r for r in rects_pred if r['conf'] > threshold]
        rects_gt_target = [r for r in rects_gt if r['class'] == label]
        rects_pred_target = [r for r in rects_pred if r['class'] == label]
        if len(rects_gt_target) == 0 and len(rects_pred_target) == 0:
            logging.info('skipping to next')
            continue
        im_origin = img_from_base64(row_gt[-1])
        im_gt_target = np.copy(im_origin)
        draw_bb(im_gt_target, [r['rect'] for r in rects_gt_target],
                [r['class'] for r in rects_gt_target])
        im_pred_target = np.copy(im_origin)
        draw_bb(im_pred_target, [r['rect'] for r in rects_pred_target],
                [r['class'] for r in rects_pred_target])
        im_gt = np.copy(im_origin)
        draw_bb(im_gt, [r['rect'] for r in rects_gt],
                [r['class'] for r in rects_gt])
        im_pred = np.copy(im_origin)
        draw_bb(im_pred, [r['rect'] for r in rects_pred],
                [r['class'] for r in rects_pred])
        yield key, im_origin, im_gt_target, im_pred_target, im_gt, im_pred, ap

def visualize_box_no_draw(data, split, version, label, start_id, color_map={},
                          split_type='label',
        min_conf=-1, max_conf=2):
    dataset = TSVDataset(data)
    if split is None:
        # guess which split should be used. only support non-composite tsv
        candidate_split = get_default_splits()
        for c in candidate_split:
            if not op.isfile(dataset.get_data(c)):
                continue
            inverted = dataset.load_inverted_label(c, version, label)
            if label not in inverted:
                continue
            n = len(inverted[label])
            if n <= start_id:
                start_id = start_id - n
            else:
                logging.info('split = {}'.format(split))
                split = c
                break
        if not split:
            logging.info('cannot find the valid')
            return
    if label is not None:
        logging.info('loading inverted label')
        idx = dataset.load_inverted_label(split, version, label)[label]
        curr_num_image = len(idx)
    else:
        curr_num_image = dataset.num_rows(split, t=split_type, version=version)
    if curr_num_image == 0:
        logging.info('there is no image')
        return
    while start_id > curr_num_image:
        start_id = start_id - curr_num_image
    while start_id < 0:
        start_id = start_id + curr_num_image
    if label is not None:
        filter_idx = idx[start_id:]
    else:
        filter_idx = range(start_id, curr_num_image)
    logging.info('start to read')
    rows_image = dataset.iter_data(split, filter_idx=filter_idx)
    rows_label = dataset.iter_data(split, split_type, version=version,
            filter_idx=filter_idx)
    for idx_row, (row_image, row_label) in enumerate(zip(rows_image, rows_label)):
        key = row_image[0]
        assert key == row_label[0], (key, row_label[0], idx_row)
        assert len(row_label) == 2
        label_str = row_label[-1]
        img_str = row_image[-1]
        im = img_from_base64(img_str)
        assert im is not None
        origin = np.copy(im)
        rects = try_json_parse(label_str)
        if type(rects) is list:
            if label is not None:
                rects = [r for r in rects if \
                        r['class'] == label and \
                        r.get('conf', 1) >= min_conf and \
                        r.get('conf', 1) <= max_conf or r['class'] != label]
                if all(r['class'] != label for r in rects):
                    continue
            yield (key, origin, rects)
        else:
            yield key, origin, [{'class': label_str, 'rect': [0, 0, 0, 0]}]

def visualize_box(data, split, version, label, start_id, color_map={}):
    dataset = TSVDataset(data)
    logging.info('loading inverted label')
    if split is None:
        # guess which split should be used. only support non-composite tsv
        candidate_split = get_default_splits()
        for c in candidate_split:
            if not op.isfile(dataset.get_data(c)):
                continue
            inverted = dataset.load_inverted_label(c, version, label)
            if label not in inverted:
                continue
            n = len(inverted[label])
            if n <= start_id:
                start_id = start_id - n
            else:
                logging.info('split = {}'.format(split))
                split = c
                break
        if not split:
            logging.info('cannot find the valid')
            return
    else:
        inverted = dataset.load_inverted_label(split, version, label)
    logging.info('inverted label loaded')
    logging.info('keys: {}'.format(inverted.keys()))
    if label not in inverted:
        return
    idx = inverted[label]
    if len(idx) == 0:
        return
    while start_id > len(idx):
        start_id = start_id - len(idx)
    while start_id < 0:
        start_id = start_id + len(idx)
    logging.info('start to read')
    rows_image = dataset.iter_data(split, filter_idx=idx[start_id:])
    rows_label = dataset.iter_data(split, 'label', version=version,
            filter_idx=idx[start_id:])
    for row_image, row_label in zip(rows_image, rows_label):
        key = row_image[0]
        assert key == row_label[0]
        assert len(row_image) == 3
        assert len(row_label) == 2
        label_str = row_label[-1]
        img_str = row_image[-1]
        im = img_from_base64(img_str)
        origin = np.copy(im)
        rects = try_json_parse(label_str)
        new_name = key.replace('/', '_').replace(':', '')
        if type(rects) is list:
            #rects = [l for l in rects if 'conf' not in l or l['conf'] > 0.3]
            def get_rect_class(rects):
                all_class = []
                all_rect = []
                for rect in rects:
                    label_class = rect['class']
                    rect = rect['rect']
                    all_class.append(label_class)
                    if not (rect[0] == 0 and rect[1] == 0
                            and rect[2] == 0 and rect[3] == 0):
                        all_rect.append(rect)
                    else:
                        all_rect.append((0, 0, im.shape[1] - 1, im.shape[0] - 1))
                return all_rect, all_class
            all_rect, all_class = get_rect_class(rects)
            draw_bb(im, all_rect, all_class)
            target_rects = [l for l in rects if l['class']
                    == label]
            all_rect, all_class = get_rect_class(target_rects)
            im_label = np.copy(origin)
            draw_bb(im_label, all_rect, all_class)
            yield new_name, origin, im_label, im, pformat(target_rects)
        else:
            yield new_name, origin, im, im, ''


def visualize_tsv2(data, split, label):
    '''
    by default, pass split as 'train'
    TODO: try to refactor it with visualize_box
    '''
    dataset = TSVDataset(data)
    logging.info('loading inverted label')
    inverted = dataset.load_inverted_label(split)
    logging.info('inverted label loaded')
    logging.info('keys: {}'.format(inverted.keys()))
    assert label in inverted
    idx = inverted[label]
    is_composite = False
    if split == 'train' and not op.isfile(dataset.get_data(split)):
        is_composite = True
        tsvs = [TSVFile(f) for f in dataset.get_train_tsvs()]
        shuffle_tsv_rows = tsv_reader(dataset.get_shuffle_file(split))
        shuffle = []
        for row in shuffle_tsv_rows:
            shuffle.append([int(row[0]), int(row[1])])
    else:
        tsv = TSVFile(dataset.get_data(split))
    num_image = 0
    num_rows = 2
    num_cols = 2
    num_image_one_fig = num_rows * num_cols
    idx.extend([0] * (num_image_one_fig - len(idx) % num_image_one_fig))
    idx = np.asarray(idx)
    idx = idx.reshape((-1, num_image_one_fig))
    logging.info('start to read')
    color_map = {}
    for i in idx:
        all_image = []
        for j in i:
            logging.info(j)
            if is_composite:
                row_image = tsvs[shuffle[j][0]].seek(shuffle[j][1])
            else:
                row_image = tsv.seek(j)
            im = img_from_base64(row_image[-1])
            labels = try_json_parse(row_image[1])
            num_image = num_image + 1
            if type(labels) is list:
                labels = [l for l in labels if 'conf' not in l or l['conf'] > 0.3]
                all_class = []
                all_rect = []
                for label in labels:
                    label_class = label['class']
                    rect = label['rect']
                    all_class.append(label_class)
                    if not (rect[0] == 0 and rect[1] == 0
                            and rect[2] == 0 and rect[3] == 0):
                        all_rect.append(rect)
                    else:
                        all_rect.append((0, 0, im.shape[1] - 1, im.shape[0] - 1))
                new_name = row_image[0].replace('/', '_').replace(':', '')
                draw_bb(im, all_rect, all_class, color=color_map)
            all_image.append(im)
        logging.info('start to show')
        show_images(all_image, num_rows, num_cols)

    logging.info('#image: {}'.format(num_image))

def visualize_tsv(tsv_image, tsv_label, out_folder=None, label_idx=1):
    '''
    deprecated, use visualize_tsv2
    '''
    rows_image = tsv_reader(tsv_image)
    rows_label = tsv_reader(tsv_label)
    assert out_folder == None or not op.exists(out_folder)
    source_folder = op.dirname(tsv_image)
    num_image = 0
    for row_image, row_label in zip(rows_image, rows_label):
        assert row_image[0] == row_label[0]
        im = img_from_base64(row_image[-1])
        labels = try_json_parse(row_label[label_idx])
        num_image = num_image + 1
        if type(labels) is list:
            labels = [l for l in labels if 'conf' not in l or l['conf'] > 0.3]
            all_class = []
            all_rect = []
            for label in labels:
                label_class = label['class']
                rect = label['rect']
                all_class.append(label_class)
                if not (rect[0] == 0 and rect[1] == 0
                        and rect[2] == 0 and rect[3] == 0):
                    all_rect.append(rect)
                else:
                    all_rect.append((0, 0, im.shape[1] - 1, im.shape[0] - 1))
            new_name = row_image[0].replace('/', '_').replace(':', '')
            draw_bb(im, all_rect, all_class)
            if out_folder:
                fname = os.path.join(out_folder,
                        '_'.join(set(c.replace(' ', '_') for c in all_class)),
                        new_name +'.png')
        else:
            fname = op.join(out_folder, row_image[0],
                    '{}_{}_{}.png'.format(num_image, labels, row_image[0]))
        if out_folder:
            save_image(im, fname)
        else:
            show_image(im)

    logging.info('#image: {}'.format(num_image))

def iou(wh1, wh2):
    w1, h1 = wh1
    w2, h2 = wh2
    return min(w1, w2) * min(h1, h2) / max(w1, w2) / max(h1, h2)

class ImageTypeParser(object):
    def __init__(self):
        self.m = None
        pass

    def parse_type(self, im_binary):
        return imghdr.what('', im_binary)
        self._ensure_init()
        mime_type = self.m.buffer(im_binary)
        t = op.basename(mime_type)
        return t

    def _ensure_init(self):
        if self.m is None:
            import magic
            #m = magic.open(magic.MAGIC_MIME_TYPE)
            m = magic.from_file(magic.MAGIC_MIME_TYPE)
            m.load()
            self.m = m

def collect_label(row, stat, **kwargs):
    labels = json.loads(row[1])
    labels = [label['class'] for label in labels]
    remove_labels = kwargs['remove_image'].split(',')
    is_remove = False
    for label in labels:
        if label in remove_labels or remove_labels == 'all':
            if random.random() <= kwargs['remove_image_prob']:
                is_remove = True
                break

    stat.append((is_remove, labels))

class TSVDatasetSource(TSVDataset):
    def __init__(self, name, root=None,
            split_infos=None,
            cleaness=10,
            use_all=False,
            use_negative_label=False,
            select_by_verified=False):
        super(TSVDatasetSource, self).__init__(name)
        self._noffset_count = {}
        self._type = None
        self._root = root
        # the list of <datasetlabel, rootlabel>
        self._sourcelabel_to_targetlabels = None
        self._targetlabel_to_sourcelabels = None
        self._initialized = False
        self._type_to_datasetlabel_to_split_idx = None
        self._type_to_datasetlabel_to_count = None
        self._type_to_split_label_idx = None
        if split_infos is not None:
            self._split_infos = split_infos
        else:
            self._split_infos = [{'split': s, 'version': -1}
                    for s in get_default_splits()]
        assert len(set([split_info['split'] for split_info in
            self._split_infos])) == len(self._split_infos)
        self._use_all = use_all
        self._select_by_verified = select_by_verified
        self.cleaness = cleaness
        self.use_negative_label = use_negative_label

    def get_version_by_split(self, split_name):
        for split_info in self._split_infos:
            if split_info['split'] == split_name:
                return split_info['version']
        return -1

    def get_label_tsv(self, split_name):
        version_by_config = self.get_version_by_split(split_name)
        return super(TSVDatasetSource, self).get_data(split_name, 'label',
                version_by_config)

    def populate_info(self, root):
        self._ensure_initialized()
        for node in root.iter_search_nodes():
            if root == node:
                continue
            if node.name in self._targetlabel_to_sourcelabels:
                sourcelabels = self._targetlabel_to_sourcelabels[node.name]
                node.add_feature(self.name, ','.join(sourcelabels))
                if any(is_noffset(l) for l in sourcelabels):
                    node.add_feature(self.name + '_readable',
                            ','.join(get_nick_name(noffset_to_synset(l)) if
                                is_noffset(l) else l for l in sourcelabels))
                total = 0
                for sourcelabel in sourcelabels:
                    for t in self._type_to_datasetlabel_to_count:
                        datasetlabel_to_count = self._type_to_datasetlabel_to_count[t]
                        c = datasetlabel_to_count.get(sourcelabel, 0)
                        total = total + c
                        key = 'images_{}'.format(t)
                        node.add_feature(key,
                                node.__getattribute__(key) + c)
                node.add_feature('{}_total'.format(self.name), total)

    def _ensure_initialized(self):
        if self._initialized:
            return
        populate_dataset_details(self.name)

        self.update_label_mapper()

        self._load_inverted()

        self._initialized = True

    def _load_inverted(self):
        # make sure self.update_label_mapper() is called
        types = ['with_bb', 'no_bb']
        self._type_split_label_idx = []
        for split_info in self._split_infos:
            split = split_info['split']
            version = split_info['version']
            logging.info('loading the inverted file: {}-{}'.format(self.name,
                split))
            if not self.has(split, 'label', version=version):
                continue
            # load inverted list
            type_to_inverted = {}
            for i, t in enumerate(types):
                if self._select_by_verified:
                    inverted_label_type = 'inverted.label.{}.verified'.format(t)
                else:
                    inverted_label_type = 'inverted.label.{}'.format(t)
                rows = self.iter_data(split, inverted_label_type,
                        version=version)
                type_to_inverted[t] = {r[0]: map(int, r[1].split(' '))
                        for r in tqdm(rows) if
                    r[0] in self._sourcelabel_to_targetlabels and
                    len(r[1]) > 0}

            # register the positive labels
            for i, t in enumerate(types):
                inverted = type_to_inverted[t]
                inverted = {l: inverted[l] for l in inverted if not l.startswith('-')}
                label_idx = dict_to_list(inverted, 0)
                for label, idx in label_idx:
                    self._type_split_label_idx.append((t, split, label, idx))
                    # for no_bb, we need to add the with_bb into the list
                    if t == 'with_bb':
                        self._type_split_label_idx.append(('no_bb', split, label, idx))

            if self.use_negative_label:
                # currently, we only have a scenario where we have negative
                # annotations in the no_bb data source and the need to apply it
                # to no_bb target set.
                inverted = type_to_inverted['no_bb']
                inverted = {l: inverted[l] for l in inverted if l.startswith('-')}
                label_idx = dict_to_list(inverted, 0)
                for label, idx in label_idx:
                    #self._type_split_label_idx.append(('with_bb', split, label, idx))
                    self._type_split_label_idx.append(('no_bb', split, label, idx))

        self._type_split_label_idx = list(set(self._type_split_label_idx))
        self._type_to_split_label_idx = list_to_dict(
                self._type_split_label_idx, 0)
        self._type_to_datasetlabel_to_split_idx = {}
        for t in self._type_to_split_label_idx:
            split_label_idx = self._type_to_split_label_idx[t]
            label_to_split_idx = list_to_dict(split_label_idx, 1)
            self._type_to_datasetlabel_to_split_idx[t] = label_to_split_idx
        self._type_to_datasetlabel_to_count = {}
        for t in self._type_to_datasetlabel_to_split_idx:
            datasetlabel_to_split_idx = self._type_to_datasetlabel_to_split_idx[t]
            datasetlabel_to_count = {l: len(datasetlabel_to_split_idx[l]) for l in
                    datasetlabel_to_split_idx}
            self._type_to_datasetlabel_to_count[t] = datasetlabel_to_count

        self._split_to_num_image = {split_info['split']: self.num_rows(split_info['split']) for split_info in
                self._split_infos if self.has(split_info['split'])}

    def update_label_mapper(self):
        root = self._root
        # load the labelmap for all splits, self.load_labelmap is not correct,
        # since we will update the label and will not update the labelmap
        labelmap = []
        for split_info in self._split_infos:
            split, version = split_info['split'], split_info['version']
            if self.has(split, 'labelmap', version):
                for row in self.iter_data(split, 'labelmap', version):
                    labelmap.append(row[0])
        # if it has a prefix of -, it means it has no that tag.
        labelmap = [l for l in labelmap if not l.startswith('-')]
        hash_labelmap = set(labelmap)
        labelmap = list(hash_labelmap)

        tree_noffsets = {}
        for node in root.iter_search_nodes():
            if node == root or not node.noffset:
                continue
            for s in node.noffset.split(','):
                tree_noffsets[s] = node.name
        name_to_targetlabels = {}
        targetlabel_has_whitelist = set()
        invalid_list = []
        any_source_key = 'label_names_in_all_dataset_source'
        for node in root.iter_search_nodes():
            if node == root:
                continue
            if hasattr(node, self.name) or hasattr(node, any_source_key):
                if hasattr(node, self.name):
                    # this is like a white-list
                    values = node.__getattribute__(self.name)
                else:
                    values = node.__getattribute__(any_source_key)
                if values is not None:
                    source_terms = values.split(',')
                    for t in source_terms:
                        t = t.strip()
                        if t not in name_to_targetlabels:
                            name_to_targetlabels[t] = set()
                        if t not in hash_labelmap:
                            invalid_list.append((t, self.name, node.name))
                            continue
                        name_to_targetlabels[t].add(node.name)
                # even if it is None, we will also add it to white-list so that
                # we will not automatically match the term.
                targetlabel_has_whitelist.add(node.name)
            else:
                # we will keep the lower case always for case-insensitive
                # comparison
                all_candidate_src_names = [node.name.lower()]
                if hasattr(node, 'alias_names'):
                    all_candidate_src_names.extend([s.strip() for s in
                        node.alias_names.split(',')])
                for t in set(all_candidate_src_names):
                    if t not in name_to_targetlabels:
                        name_to_targetlabels[t] = set()
                    name_to_targetlabels[t].add(node.name)

        sourcelabel_targetlabel = []
        if len(invalid_list) != 0:
            logging.warn('invalid white list information: {}'.format(pformat(invalid_list)))

        #result = {}
        label_to_synset = LabelToSynset()
        for l in labelmap:
            matched = False
            if l.lower() in name_to_targetlabels:
                matched = True
                for t in name_to_targetlabels[l.lower()]:
                    sourcelabel_targetlabel.append((l, t))
            if l in name_to_targetlabels:
                for t in name_to_targetlabels[l]:
                    sourcelabel_targetlabel.append((l, t))
                matched = True
            if not matched:
                succeed, ns = label_to_synset.convert(l)
                if not succeed:
                    continue
                for n in ns:
                    n = synset_to_noffset(n)
                    if n in tree_noffsets:
                        t = tree_noffsets[n]
                        if t in targetlabel_has_whitelist:
                            # if it has white list, we will not respect the
                            # noffset to do the autmatic matching
                            continue
                        sourcelabel_targetlabel.append((l, t))
                        #result[l] = t
        if self.use_negative_label:
            # we just add the mapping here. no need to check if -s is in the
            # source label list
            sourcelabel_targetlabel.extend([('-' + s, '-' + t) for s, t in
                sourcelabel_targetlabel])

        self._sourcelabel_to_targetlabels = list_to_dict_unique(sourcelabel_targetlabel,
                0)
        self._targetlabel_to_sourcelabels = list_to_dict_unique(sourcelabel_targetlabel,
                1)

        return self._sourcelabel_to_targetlabels

    def select_tsv_rows(self, label_type):
        self._ensure_initialized()
        result = []
        if label_type in self._type_to_split_label_idx:
            split_label_idx = self._type_to_split_label_idx[label_type]
            datasetlabel_to_splitidx = list_to_dict(split_label_idx, 1)
            for datasetlabel in datasetlabel_to_splitidx:
                if datasetlabel in self._sourcelabel_to_targetlabels:
                    split_idxes = datasetlabel_to_splitidx[datasetlabel]
                    targetlabels = self._sourcelabel_to_targetlabels[datasetlabel]
                    for targetlabel in targetlabels:
                        result.extend([(targetlabel, split, idx) for split, idx in
                            split_idxes])
        # must_have_indices
        for split_info in self._split_infos:
            split = split_info['split']
            must_have_indices = split_info.get('must_have_indices', [])
            # we set the target label here as None so that the post-processing
            # will not ignore it. The real labels will also be converted
            # corrected since we do not depend on this target label only.
            result.extend((None, split, i) for i in must_have_indices)
        if self._use_all:
            split_to_targetlabel_idx = list_to_dict(result, 1)
            for s in split_to_targetlabel_idx:
                rootlabel_idxes = split_to_targetlabel_idx[s]
                idx_to_rootlabel = list_to_dict(rootlabel_idxes, 1)
                num_image = self._split_to_num_image[s]
                idxes = set(range(num_image)).difference(set(idx_to_rootlabel.keys()))
                for i in idxes:
                    # for these images, the root label is hard-coded as None
                    result.append((None, s, i))
            for split_info in self._split_infos:
                s = split_info['split']
                if s in split_to_targetlabel_idx:
                    continue
                if s not in self._split_to_num_image:
                    continue
                result.extend([(None, s, i) for i in
                    range(self._split_to_num_image[s])])
        return result

def initialize_images_count(root):
    for node in root.iter_search_nodes():
        node.add_feature('images_with_bb', 0)
        node.add_feature('images_no_bb', 0)

def trainval_split(dataset, num_test_each_label):
    if op.isfile(dataset.get_train_tsv()):
        logging.info('skip to run trainval split for {} because it has been done'.format(
            dataset.name))
        return
    random.seed(777)
    label_to_idx = {}
    for i, row in enumerate(tsv_reader(dataset.get_trainval_tsv('label'))):
        rects = json.loads(row[1])
        if len(rects) == 0:
            logging.info('{} has empty label for {}'.format(dataset.name, i))
            continue
        random.shuffle(rects)
        label = rects[0]['class']
        if label in label_to_idx:
            label_to_idx[label].append(i)
        else:
            label_to_idx[label] = [i]

    test_idx = []
    train_idx = []
    for label in label_to_idx:
        if len(label_to_idx[label]) < num_test_each_label:
            logging.fatal('dataset {} has less than {} images for label {}'.
                    format(dataset.name, num_test_each_label, label))
        random.shuffle(label_to_idx[label])
        test_idx.extend(label_to_idx[label][: num_test_each_label])
        train_idx.extend(label_to_idx[label][num_test_each_label: ])

    trainval = TSVFile(dataset.get_trainval_tsv())

    def gen_train():
        for i in train_idx:
            row = trainval.seek(i)
            yield row
    tsv_writer(gen_train(), dataset.get_train_tsv())

    def gen_test():
        for i in test_idx:
            yield trainval.seek(i)
    tsv_writer(gen_test(), dataset.get_test_tsv_file())

def convert_one_label(rects, label_mapper):
    to_remove = []
    rects2 = []
    for rect in rects:
        if rect['class'] in label_mapper:
            for t in label_mapper[rect['class']]:
                r2 = copy.deepcopy(rect)
                assert type(t) is str or type(t) is unicode
                r2['class'] = t
                rects2.append(r2)
    rects[:] = []
    rects.extend(rects2)

def convert_label_by_dataset(dataset, split, version, idx, label_mapper, with_bb):
    result = None
    for i, row in zip(idx, dataset.iter_data(split, 'label', version=version,
        filter_idx=idx)):
        assert len(row) == 2
        rects = json.loads(row[1])
        def eval_with_bb(r):
            if not r['class'].startswith('-'):
                return 'rect' in r and any(x != 0 for x in r['rect'])
            else:
                return 'rect' not in r or all(x == 0 for x in r['rect'])
        if with_bb:
            # in the case with -, we will not add rect
                                # with all zeros, thus, no need to check if it is all zeros
                                # when it is negative samples
            rects = [r for r in rects if eval_with_bb(r)]
        # all annotations if eval_with_bb(r) is valid for no_bb. Thus, disable
        # the following
        #else:
            #rects = [r for r in rects if not eval_with_bb(r)]
        if result is None:
            # don't use this, because all list will be shared
            #result = [len(row) * ['d']] * tsv.num_rows()
            result = [None] * dataset.num_rows(split)
            for _ in range(len(result)):
                result[_] = ['d'] * len(row)
        rects2 = []
        # the following code should use convert_one_label
        for rect in rects:
            if rect['class'] in label_mapper:
                for t in label_mapper[rect['class']]:
                    r2 = copy.deepcopy(rect)
                    r2['class'] = t
                    if rect['class'] != t:
                        # keep this for logging
                        r2['class_from'] = rect['class']
                    rects2.append(r2)
        row[1] = rects2
        result[i] = row
    return result

def convert_label(label_tsv, idx, label_mapper, with_bb):
    tsv = TSVFile(label_tsv)
    result = None
    for i in tqdm(idx):
        row = tsv.seek(i)
        assert len(row) == 2
        rects = json.loads(row[1])
        def eval_with_bb(r):
            if not r['class'].startswith('-'):
                return 'rect' in r and any(x != 0 for x in r['rect'])
            else:
                return 'rect' not in r or all(x == 0 for x in r['rect'])
        if with_bb:
            # in the case with -, we will not add rect
                                # with all zeros, thus, no need to check if it is all zeros
                                # when it is negative samples
            rects = [r for r in rects if eval_with_bb(r)]
        # all annotations if eval_with_bb(r) is valid for no_bb. Thus, disable
        # the following
        #else:
            #rects = [r for r in rects if not eval_with_bb(r)]
        if result is None:
            # don't use this, because all list will be shared
            #result = [len(row) * ['d']] * tsv.num_rows()
            result = [None] * tsv.num_rows()
            for _ in range(len(result)):
                result[_] = ['d'] * len(row)
        rects2 = []
        # the following code should use convert_one_label
        for rect in rects:
            if rect['class'] in label_mapper:
                for t in label_mapper[rect['class']]:
                    r2 = copy.deepcopy(rect)
                    r2['class'] = t
                    if rect['class'] != t:
                        # keep this for logging
                        r2['class_from'] = rect['class']
                    rects2.append(r2)
        row[1] = rects2
        result[i] = row
    return result

def create_info_for_ambigous_noffset(name, noffsets):
    definitions = [str(noffset_to_synset(n).definition()) for n in noffsets]
    de = [{'noffset': n,
          'definition': d.replace("`", '').replace("'", ''),
          'nick_name': str(get_nick_name(noffset_to_synset(n)))}
            for n, d in zip(noffsets, definitions)]
    d = {'name': name,
            'definitions': de,
            'noffset': None,
            'markdown_url': create_markdown_url(noffsets)}
    return d

def node_should_have_images(root, th, fname):
    enough = True
    few_training_with_bb = []
    for node in root.iter_search_nodes():
        if node == root:
            continue
        if node.cum_images_with_bb < th:
            few_training_with_bb.append({'name': node.name,
                'cum_images_with_bb': node.cum_images_with_bb,
                'parent list': [p.name for p in node.get_ancestors()[:-1]]})
            enough = False
            logging.warn('less images: {} ({})'.format(
                node.name.encode('utf-8'),
                node.cum_images_with_bb))
    if enough:
        logging.info('con. every node has at least {} images'.format(th))
    else:
        write_to_yaml_file(few_training_with_bb, fname)

def clean_dataset2(source_dataset_name, dest_dataset_name):
    source_dataset = TSVDataset(source_dataset_name)
    dest_dataset = TSVDataset(dest_dataset_name)
    splits = get_default_splits()
    for split in splits:
        src_tsv = source_dataset.get_data(split)
        if op.isfile(src_tsv):
            valid_idxs = []
            dest_tsv = dest_dataset.get_data(split)
            def gen_rows():
                rows = tsv_reader(src_tsv)
                num_removed_images = 0
                num_removed_rects = 0
                for i, row in enumerate(tqdm(rows)):
                    if (i % 1000) == 0:
                        logging.info('{} - #removedImages={};#removedRects={}'.format(
                            i, num_removed_images, num_removed_rects))
                    im = img_from_base64(row[-1])
                    if im is None:
                        num_removed_images = num_removed_images + 1
                        continue
                    height, width = im.shape[:2]
                    rects = json.loads(row[1])
                    invalid = False
                    to_remove = []
                    for rect in rects:
                        r = rect['rect']
                        if all(s == 0 for s in r):
                            continue
                        changed = False
                        origin = copy.deepcopy(rect)
                        for j in range(4):
                            if r[j] < 0:
                                r[j] = 0
                                changed = True
                        for j in range(2):
                            if r[2 * j] >= width -1:
                                changed = True
                                r[2 * j] = width - 1
                            if [2 * j + 1] >= height - 1:
                                changed = True
                                r[2 * j + 1] = height - 1
                        if changed:
                            rect['changed_from_rect'] = origin
                        cx, cy = (r[0] + r[2]) / 2., (r[1] + r[3]) / 2.
                        w, h = r[2] - r[0], r[3] - r[1]
                        if cx < 0 or cy < 0 or cx >= width or \
                                cy >= height or w <= 1 or h <= 1 or \
                                w >= width or h >= height:
                            to_remove.append(rect)
                    if len(to_remove) > 0:
                        num_removed_rects = num_removed_rects + len(to_remove)
                    for rect in to_remove:
                        rects.remove(rect)
                    valid_idxs.append(i)
                    if len(to_remove) > 0:
                        yield row[0], json.dumps(rects), row[2]
                    else:
                        yield row
            tsv_writer(gen_rows(), dest_tsv)
            dest_dataset.write_data(source_dataset.iter_data(split, 'label',
                filter_idx=valid_idxs), split, 'label')

def clean_dataset(source_dataset_name, dest_dataset_name):
    '''
    use version 2
    '''
    source_dataset = TSVDataset(source_dataset_name)
    dest_dataset = TSVDataset(dest_dataset_name)
    splits = get_default_splits()
    for split in splits:
        src_tsv = source_dataset.get_data(split)
        if op.isfile(src_tsv):
            dest_tsv = dest_dataset.get_data(split)
            def gen_rows():
                rows = tsv_reader(src_tsv)
                num_removed_images = 0
                num_removed_rects = 0
                for i, row in enumerate(rows):
                    if (i % 1000) == 0:
                        logging.info('{} - #removedImages={};#removedRects={}'.format(
                            i, num_removed_images, num_removed_rects))
                    im = img_from_base64(row[-1])
                    height, width = im.shape[:2]
                    rects = json.loads(row[1])
                    invalid = False
                    to_remove = []
                    for rect in rects:
                        r = rect['rect']
                        if all(s == 0 for s in r):
                            continue
                        cx, cy = (r[0] + r[2]) / 2., (r[1] + r[3]) / 2.
                        w, h = r[2] - r[0], r[3] - r[1]
                        if cx < 0 or cy < 0 or cx >= width or \
                                cy >= height or w <= 1 or h <= 1 or \
                                w >= width or h >= height:
                            to_remove.append(rect)
                    if len(to_remove) > 0:
                        logging.info('before removing {}'.format(len(rects)))
                        num_removed_rects = num_removed_rects + len(to_remove)
                    for rect in to_remove:
                        rects.remove(rect)
                        r = rect['rect']
                        logging.info('removing {}'.format(','.join(map(str,
                                r))))
                    if len(to_remove) > 0:
                        logging.info('after removing {}'.format(len(rects)))
                        if len(rects) > 0:
                            yield row[0], json.dumps(rects), row[2]
                        else:
                            num_removed_images = num_removed_images + 1
                            logging.info('removing image {}'.format(row[0]))
                    else:
                        yield row
            tsv_writer(gen_rows(), dest_tsv)

def parallel_convert_label(func, all_task, num_worker=128):
    num_split = num_worker
    num_task_each_split = (len(all_task) + num_split - 1) / num_split
    all_sub_tasks = []
    for i in range(num_split):
        start_idx = i * num_task_each_split
        if start_idx >= len(all_task):
            break
        end_idx = start_idx + num_task_each_split
        if end_idx > len(all_task):
            end_idx = len(all_task)
        all_sub_tasks.append(all_task[start_idx:end_idx])
    m = Pool(num_worker)
    all_sub_results = m.map(func, all_sub_tasks)
    result = all_sub_results[0]
    for s in all_sub_results[1:]:
        assert len(result) == len(s)
        for i in range(len(result)):
            assert len(result[i]) == 2
            assert len(s[i]) == 2
            if result[i][1] == 'd' and s[i][1] != 'd':
                result[i][0] = s[i][0]
                result[i][1] = s[i][1]
            else:
                assert not (result[i][1] != 'd' and s[i][1] != 'd')
    return result

def parallel_map_to_array(func, all_task, num_worker=128):
    num_split = num_worker * 2
    num_task_each_split = (len(all_task) + num_split - 1) / num_split
    all_sub_tasks = []
    for i in range(num_split):
        start_idx = i * num_task_each_split
        if start_idx > len(all_task):
            break
        end_idx = start_idx + num_task_each_split
        if end_idx > len(all_task):
            end_idx = len(all_task)
        all_sub_tasks.append(all_task[start_idx:end_idx])
    m = Pool(num_worker)
    all_sub_results = m.map(func, all_sub_tasks)
    result = []
    for s in all_sub_results:
        result.extend(s)
    return result

def convert_label_db(dataset, split, idx, with_bb):
    result = None
    label_mapper = dataset._sourcelabel_to_targetlabels
    queried = []
    for row_with_idx in tqdm(dataset.iter_gt(split, idx=idx)):
        i = row_with_idx[0]
        queried.append(i)
        row = list(row_with_idx[1:])
        rects = row[1]
        def eval_with_bb(r):
            if not r['class'].startswith('-'):
                return 'rect' in r and any(x != 0 for x in r['rect'])
            else:
                return 'rect' not in r or all(x == 0 for x in r['rect'])
        if with_bb:
            # in the case with -, we will not add rect
                                # with all zeros, thus, no need to check if it is all zeros
                                # when it is negative samples
            rects = [r for r in rects if eval_with_bb(r)]
        # all annotations if eval_with_bb(r) is valid for no_bb. Thus, disable
        # the following
        #else:
            #rects = [r for r in rects if not eval_with_bb(r)]
        if result is None:
            result = [None] * dataset.num_rows(split)
            for _ in range(len(result)):
                result[_] = ['d'] * len(row)
        rects2 = []
        # the following code should use convert_one_label
        for rect in rects:
            if rect['class'] in label_mapper:
                for t in label_mapper[rect['class']]:
                    r2 = copy.deepcopy(rect)
                    r2['class'] = t
                    if rect['class'] != t:
                        # keep this for logging
                        r2['class_from'] = rect['class']
                    rects2.append(r2)
        row[1] = rects2
        result[i] = row
    not_coverred = set(idx).difference(queried)
    assert len(not_coverred) == 0
    return result

def create_trainX_db(train_ldtsi, extra_dtsi, tax, out_dataset,
        lift_train=False):
    t_to_ldsi = list_to_dict(train_ldtsi, 2)
    extra_t_to_dsi = list_to_dict(extra_dtsi, 1)
    train_ldtsik = []
    extra_dtsik = []
    for label_type in t_to_ldsi:
        ldsi = t_to_ldsi[label_type]
        extra_dsi = extra_t_to_dsi.get(label_type, [])
        d_to_lsi = list_to_dict(ldsi, 1)
        extra_d_to_si = list_to_dict(extra_dsi, 0)
        k = 0
        sources = []
        sources_label = []
        with_bb = label_type == 'with_bb'
        for dataset in d_to_lsi:
            lsi = d_to_lsi[dataset]
            extra_si = extra_d_to_si.get(dataset, [])
            s_li = list_to_dict(lsi, 1)
            extra_s_to_i = list_to_dict(extra_si, 0)
            for split in s_li:
                li = s_li[split]
                idx_to_l = list_to_dict(li, 1)
                idx = idx_to_l.keys()
                extra_i = extra_s_to_i.get(split, [])
                # link the data tsv
                source = dataset.get_data(split)
                out_split = 'train{}'.format(k)
                train_ldtsik.extend([(l, dataset, label_type, split, i,
                    k) for l, i in li])
                extra_dtsik.extend([(dataset, label_type, split, i, k)
                    for i in extra_i])
                k = k + 1
                sources.append(source)
                logging.info('converting labels: {}-{}'.format(
                    dataset.name, split))

                converted_label = convert_label_db(dataset,
                        split, idx, with_bb=with_bb)
                # convert the file name
                logging.info('delifting the labels')
                for i in tqdm(idx):
                    l = converted_label[i]
                    if lift_train:
                        l[1] = json.dumps(lift_one_image(l[1], tax))
                    else:
                        l[1] = json.dumps(delift_one_image(l[1], tax))
                    l[0] = '{}_{}_{}'.format(dataset.name, split, l[0])
                label_file = out_dataset[label_type].get_data(out_split, 'label')
                logging.info('writing the label file {}'.format(label_file))
                tsv_writer(converted_label, label_file)
                sources_label.append(label_file)
        write_to_file('\n'.join(sources),
                out_dataset[label_type].get_data('trainX'))
        write_to_file('\n'.join(sources_label),
                out_dataset[label_type].get_data('trainX', 'label'))

    logging.info('saving the shuffle file')
    type_to_ldsik = list_to_dict(train_ldtsik, 2)
    extra_type_to_dsik = list_to_dict(extra_dtsik, 1)
    for label_type in type_to_ldsik:
        ldsik = type_to_ldsik[label_type]
        shuffle_info = [(str(k), str(i)) for l, d, s, i, k in ldsik]
        shuffle_info = list(set(shuffle_info))
        if label_type in extra_type_to_dsik:
            dsik = extra_type_to_dsik[label_type]
            # we should not de-duplicate it because it comes from the duplicate
            # policy
            extra_shuffle_info = [(str(k), str(i) ) for d, s, i, k in dsik]
            shuffle_info.extend(extra_shuffle_info)
        random.shuffle(shuffle_info)
        tsv_writer(shuffle_info,
                out_dataset[label_type].get_shuffle_file('train'))

    populate_output_num_images(train_ldtsik, 'toTrain', tax.root)

def create_trainX(train_ldtsi, extra_dtsi, tax, out_dataset,
        lift_train=False, use_original_label=False):
    t_to_ldsi = list_to_dict(train_ldtsi, 2)
    extra_t_to_dsi = list_to_dict(extra_dtsi, 1)
    train_ldtsik = []
    extra_dtsik = []
    for label_type in t_to_ldsi:
        ldsi = t_to_ldsi[label_type]
        extra_dsi = extra_t_to_dsi.get(label_type, [])
        d_to_lsi = list_to_dict(ldsi, 1)
        extra_d_to_si = list_to_dict(extra_dsi, 0)
        k = 0
        sources = []
        sources_origin_label = []
        sources_label = []
        with_bb = label_type == 'with_bb'
        for dataset in d_to_lsi:
            lsi = d_to_lsi[dataset]
            extra_si = extra_d_to_si.get(dataset, [])
            s_li = list_to_dict(lsi, 1)
            extra_s_to_i = list_to_dict(extra_si, 0)
            for split in s_li:
                li = s_li[split]
                idx_to_l = list_to_dict(li, 1)
                idx = idx_to_l.keys()
                extra_i = extra_s_to_i.get(split, [])
                # link the data tsv
                source = dataset.get_data(split)
                out_split = 'train{}'.format(k)
                train_ldtsik.extend([(l, dataset, label_type, split, i,
                    k) for l, i in li])
                extra_dtsik.extend([(dataset, label_type, split, i, k)
                    for i in extra_i])
                k = k + 1
                sources.append(source)
                logging.info('converting labels: {}-{}'.format(
                    dataset.name, split))
                source_origin_label = dataset.get_label_tsv(split)
                if not use_original_label:
                    #converted_label = convert_label(source_origin_label,
                            #idx, dataset._sourcelabel_to_targetlabels,
                            #with_bb=with_bb)
                    converted_label = convert_label_by_dataset(
                            dataset, split,
                            dataset.get_version_by_split(split),
                            idx, dataset._sourcelabel_to_targetlabels,
                            with_bb=with_bb)
                    sources_origin_label.append(source_origin_label)
                    # convert the file name
                    logging.info('delifting the labels')
                    for i in tqdm(idx):
                        l = converted_label[i]
                        if lift_train:
                            l[1] = json.dumps(lift_one_image(l[1], tax))
                        else:
                            l[1] = json.dumps(delift_one_image(l[1], tax))
                        l[0] = '{}_{}_{}'.format(dataset.name, split, l[0])
                    label_file = out_dataset[label_type].get_data(out_split, 'label')
                    logging.info('writing the label file {}'.format(label_file))
                    tsv_writer(converted_label, label_file)
                else:
                    label_file = source_origin_label
                sources_label.append(label_file)
        write_to_file('\n'.join(sources),
                out_dataset[label_type].get_data('trainX'))
        write_to_file('\n'.join(sources_label),
                out_dataset[label_type].get_data('trainX', 'label'))
        write_to_file('\n'.join(sources_origin_label),
                out_dataset[label_type].get_data('trainX', 'origin.label'))
    logging.info('saving the shuffle file')
    type_to_ldsik = list_to_dict(train_ldtsik, 2)
    extra_type_to_dsik = list_to_dict(extra_dtsik, 1)
    for label_type in type_to_ldsik:
        ldsik = type_to_ldsik[label_type]
        shuffle_info = [(str(k), str(i)) for l, d, s, i, k in ldsik]
        shuffle_info = list(set(shuffle_info))
        if label_type in extra_type_to_dsik:
            dsik = extra_type_to_dsik[label_type]
            # we should not de-duplicate it because it comes from the duplicate
            # policy
            extra_shuffle_info = [(str(k), str(i) ) for d, s, i, k in dsik]
            shuffle_info.extend(extra_shuffle_info)
        random.shuffle(shuffle_info)
        tsv_writer(shuffle_info,
                out_dataset[label_type].get_shuffle_file('train'))

        # expand nested trainX if needed
        expand_nested_splitX(out_dataset[label_type].name, 'train')

    populate_output_num_images(train_ldtsik, 'toTrain', tax.root)

def expand_nested_splitX(data, split):
    dataset = TSVDataset(data)
    if not dataset.has(split):
        logging.info('{}-{} does not exist'.format(data, split))
        return
    if op.isfile(dataset.get_data(split)):
        logging.info('{}/{}.tsv already exists'.format(data, split))
        return
    assert op.isfile(dataset.get_data(split + 'X'))

    need_to_expand = False
    src_data_splits = dataset.load_composite_source_data_split(split)
    for src_data, src_split in src_data_splits:
        src_dataset = TSVDataset(src_data)
        if not op.isfile(src_dataset.get_data(src_split)):
            assert op.isfile(src_dataset.get_data(src_split + 'X'))
            need_to_expand = True
            break
    if not need_to_expand:
        logging.info('no need to expand')
        return

    if not op.isfile(dataset.get_data(split, 'label')):
        convertcomposite_to_standard(data, split, ignore_image=True)

    cache_shuffle = {}
    def get_shuffle(d, s):
        if (d, s) in cache_shuffle:
            return cache_shuffle[(d, s)]
        else:
            shuffle = [(int(i), int(j)) for i, j in
                tsv_reader(TSVDataset(d).get_shuffle_file(s))]
            cache_shuffle[(d, s)] = shuffle
            return shuffle

    cache_src_data_splits = {}
    def get_src_data_splits(d, s):
        if (d, s) in cache_src_data_splits:
            return cache_src_data_splits[(d, s)]
        else:
            ssrc_data_splits = TSVDataset(d).load_composite_source_data_split(s)
            cache_src_data_splits[(d, s)] = ssrc_data_splits
            return ssrc_data_splits

    cache_single_tsv = {}
    def get_single(d, s):
        if (d, s) in cache_single_tsv:
            return cache_single_tsv[(d, s)]
        else:
            single = TSVDataset(d).get_data(s)
            cache_single_tsv[(d, s)] = single
            return single

    cache_is_single = {}
    def get_is_single(d, s):
        if (d, s) in cache_is_single:
            return cache_is_single[(d, s)]
        else:
            is_single = op.isfile(TSVDataset(d).get_data(s))
            cache_is_single[(d, s)] = is_single
            return is_single

    def get_tsv_line(d, s, i):
        single_tsv = get_is_single(d, s)
        if single_tsv:
            return get_single(d, s), i
        else:
            ssrc_data_splits = get_src_data_splits(d, s)
            shuffle = get_shuffle(d, s)
            idx_src, idx_row = shuffle[i]
            src_d, src_s = ssrc_data_splits[idx_src]
            return get_tsv_line(src_d, src_s, idx_row)

    shuffle = get_shuffle(data, split)
    all_tsv_idx = []
    for idx_src, idx_row in shuffle:
        ss_data, ss_split = src_data_splits[idx_src]
        tsv, i = get_tsv_line(ss_data, ss_split, idx_row)
        all_tsv_idx.append((tsv, i))
    unique_tsvs = list(list_to_dict(all_tsv_idx, 0).keys())
    tsv_to_idx = {t: i for i, t in enumerate(unique_tsvs)}
    shuffle_result = [(tsv_to_idx[t], i) for t, i in all_tsv_idx]
    tsv_writer(shuffle_result, dataset.get_shuffle_file(split))
    write_to_file('\n'.join(unique_tsvs), dataset.get_data(split + 'X'))

def create_testX_db(test_ldtsi, tax, out_dataset):
    t_to_ldsi = list_to_dict(test_ldtsi, 2)
    for label_type in t_to_ldsi:
        sources = []
        sources_label = []
        ldsi = t_to_ldsi[label_type]
        d_to_lsi = list_to_dict(ldsi, 1)
        k = 0
        all_ki = []
        with_bb = label_type == 'with_bb'
        for dataset in d_to_lsi:
            lsi = d_to_lsi[dataset]
            s_to_li = list_to_dict(lsi, 1)
            for split in s_to_li:
                li = s_to_li[split]
                idx = list_to_dict(li, 1).keys()
                out_split = 'test{}'.format(k)
                s_file = dataset.get_data(split)
                sources.append(s_file)
                converted_label = convert_label_db(dataset,
                        split, idx, with_bb=with_bb)
                for i in tqdm(idx):
                    l = converted_label[i]
                    l[1] = json.dumps(lift_one_image(l[1], tax))
                    l[0] = '{}_{}_{}'.format(dataset.name, split, l[0])
                all_ki.extend([(str(k), str(i)) for i in idx])
                label_file = out_dataset[label_type].get_data(out_split, 'label')
                tsv_writer(converted_label, label_file)
                sources_label.append(label_file)
                k = k + 1
        write_to_file('\n'.join(sources),
                out_dataset[label_type].get_data('testX'))
        write_to_file('\n'.join(sources_label),
                out_dataset[label_type].get_data('testX', 'label'))
        tsv_writer(all_ki, out_dataset[label_type].get_shuffle_file('test'))

def create_testX(test_ldtsi, tax, out_dataset):
    t_to_ldsi = list_to_dict(test_ldtsi, 2)
    for label_type in t_to_ldsi:
        sources = []
        sources_origin_label = []
        sources_label = []
        ldsi = t_to_ldsi[label_type]
        d_to_lsi = list_to_dict(ldsi, 1)
        k = 0
        all_ki = []
        with_bb = label_type == 'with_bb'
        for dataset in d_to_lsi:
            lsi = d_to_lsi[dataset]
            s_to_li = list_to_dict(lsi, 1)
            for split in s_to_li:
                li = s_to_li[split]
                idx = list_to_dict(li, 1).keys()
                out_split = 'test{}'.format(k)
                s_file = dataset.get_data(split)
                sources.append(s_file)
                source_origin_label = dataset.get_label_tsv(split)
                sources_origin_label.append(source_origin_label)
                #converted_label = convert_label(source_origin_label,
                        #idx, dataset._sourcelabel_to_targetlabels,
                        #with_bb=with_bb)
                converted_label = convert_label_by_dataset(
                        dataset, split, dataset.get_version_by_split(split),
                        idx, dataset._sourcelabel_to_targetlabels,
                        with_bb=with_bb)
                for i in tqdm(idx):
                    l = converted_label[i]
                    l[1] = json.dumps(lift_one_image(l[1], tax))
                    l[0] = '{}_{}_{}'.format(dataset.name, split, l[0])
                all_ki.extend([(str(k), str(i)) for i in idx])
                label_file = out_dataset[label_type].get_data(out_split, 'label')
                tsv_writer(converted_label, label_file)
                sources_label.append(label_file)
                k = k + 1
        write_to_file('\n'.join(sources),
                out_dataset[label_type].get_data('testX'))
        write_to_file('\n'.join(sources_label),
                out_dataset[label_type].get_data('testX', 'label'))
        write_to_file('\n'.join(sources_origin_label),
                out_dataset[label_type].get_data('testX', 'origin.label'))
        tsv_writer(all_ki, out_dataset[label_type].get_shuffle_file('test'))
        expand_nested_splitX(out_dataset[label_type].name, 'test')

def remove_or_duplicate_each_type(train_ldtsi, label_to_min_image, label_to_max_image):
    label_to_dtsi = list_to_dict(train_ldtsi, 0)
    extra_dtsi = []
    for label in label_to_dtsi:
        if label is None:
            continue
        dtsi = label_to_dtsi[label]
        max_image = label_to_max_image[label]
        t_to_dsi = list_to_dict(dtsi, 1)
        if isinstance(label_to_min_image, dict):
            min_image = label_to_min_image[label]
        else:
            # label_to_min_image is an integer
            min_image = label_to_min_image
        for t in t_to_dsi:
            dsi = t_to_dsi[t]
            if len(dsi) > max_image:
                # first remove the images with no bounding box
                num_remove = len(dsi) - max_image
                random.seed(9999)
                random.shuffle(dsi)
                dsi = sorted(dsi, key=lambda x: -x[0].cleaness)
                assert len(dsi) > num_remove
                dsi = dsi[: len(dsi) - num_remove]
                t_to_dsi[t] = dsi
            elif len(dsi) < min_image:
                num_duplicate = int(np.ceil(float(min_image) / len(dsi)))
                logging.info('duplicate images for {}/{}: {}->{}, {}'.format(
                    t, label, len(dsi), min_image, num_duplicate))
                # we already has 1 copy of dsi, thus we should subtract 1
                extra_dsi = (num_duplicate - 1) * dsi
                extra_dtsi.extend([[d, t, s, i] for d, s, i in extra_dsi])
        dtsi = dict_to_list(t_to_dsi, 1)
        label_to_dtsi[label] = dtsi
    logging.info('# train instances before duplication: {}'.format(len(train_ldtsi)))
    train_ldtsi = dict_to_list(label_to_dtsi, 0)
    logging.info('# train instances after duplication: {}'.format(len(train_ldtsi)))
    return train_ldtsi, extra_dtsi

def remove_or_duplicate(train_ldtsi, min_image, label_to_max_image):
    # min_image is for the sum of with_bb and no_bb
    # prefer to use _each_type version, which interpret min_image as the value
    # for each type
    label_to_dtsi = list_to_dict(train_ldtsi, 0)
    extra_dtsi = []
    for label in label_to_dtsi:
        dtsi = label_to_dtsi[label]
        max_image = label_to_max_image[label]
        if len(dtsi) > max_image:
            # first remove the images with no bounding box
            num_remove = len(dtsi) - max_image
            type_to_dsi = list_to_dict(dtsi, 1)
            if 'no_bb' in type_to_dsi:
                dsi = type_to_dsi['no_bb']
                if num_remove >= len(dsi):
                    # remove all this images
                    del type_to_dsi['no_bb']
                    num_remove = num_remove - len(dsi)
                else:
                    random.seed(9999)
                    random.shuffle(dsi)
                    type_to_dsi['no_bb'] = dsi[: len(dsi) - num_remove]
                    num_remove = 0
            if num_remove > 0:
                assert 'with_bb' in type_to_dsi
                dsi = type_to_dsi['with_bb']
                random.seed(9999)
                random.shuffle(dsi)
                dsi = sorted(dsi, key=lambda x: -x[0].cleaness)
                assert len(dsi) > num_remove
                type_to_dsi['with_bb'] = dsi[: len(dsi) - num_remove]
                num_remove = 0
            dtsi = dict_to_list(type_to_dsi, 1)
        elif len(dtsi) < min_image:
            num_duplicate = int(np.ceil(float(min_image) / len(dtsi)))
            logging.info('duplicate images for label of {}: {}->{}, {}'.format(
                label, len(dtsi), min_image, num_duplicate))
            extra_dtsi.extend((num_duplicate - 1) * dtsi)
        label_to_dtsi[label] = dtsi
    logging.info('# train instances before duplication: {}'.format(len(train_ldtsi)))
    train_ldtsi = dict_to_list(label_to_dtsi, 0)
    logging.info('# train instances after duplication: {}'.format(len(train_ldtsi)))
    return train_ldtsi, extra_dtsi

def remove_test_in_train(train_ldtsi, test_ldtsi):
    logging.info('before len(train_ldtsi) = {}'.format(len(train_ldtsi)))
    set_train_dtsi = set((d, t, s, i) for l, d, t, s, i in train_ldtsi)
    if len(train_ldtsi) > 0 and type(train_ldtsi[0]) is list:
        # we need to convert it to immutable tuple since list is not hashable
        train_ldtsi = [(l, d, t, s, i) for l, d, t, s, i in train_ldtsi]
    set_train_ldtsi = set(train_ldtsi)
    #assert len(set_train_ldtsi) == len(train_ldtsi)
    set_test_dtsi = set((d, t, s, i) for l, d, t, s, i in test_ldtsi)
    result = [(l, d, t, s, i) for l, d, t, s, i in train_ldtsi
            if (d, t, s, i) not in set_test_dtsi]
    logging.info('after len(train_ldtsi) = {}'.format(len(result)))
    return result

def split_train_test(ldtsi, num_test):
    # group by label_type
    t_to_ldsi = list_to_dict(ldtsi, 2)
    train_ldtsi = []
    test_ldtsi = []
    for label_type in sorted(t_to_ldsi.keys()):
        ldsi= t_to_ldsi[label_type]
        l_to_dsi = list_to_dict(ldsi, 0)
        for rootlabel in sorted(l_to_dsi.keys()):
            dsi = l_to_dsi[rootlabel]
            if len(dsi) < num_test:
                logging.info('rootlabel={}; label_type={}->less than {} images'.format(
                    rootlabel, label_type, len(dsi)))
            curr_num_test = min(num_test, int(len(dsi) / 2))
            random.shuffle(dsi)
            test_ldtsi.extend([(rootlabel, d, label_type, s, i) for d, s, i
                in dsi[:curr_num_test]])
            train_ldtsi.extend([(rootlabel, d, label_type, s, i) for d, s, i
                in dsi[curr_num_test:]])
    return train_ldtsi, test_ldtsi

def regularize_data_sources(data_infos):
    result = []
    default_splits = get_default_splits()
    for data_info in data_infos:
        if type(data_info) is str:
            r = {'name': data_info,
                    'cleaness': 10,
                    'valid_splits': default_splits,
                    'use_all': False}
            result.append(r)
        elif type(data_info) is tuple or type(data_info) is list:
            assert len(data_info) > 0
            r = {'name': data_info[0],
                    'cleaness': data_info[1] if len(data_info) > 1 else 10,
                    'valid_splits': data_info[2] if len(data_info) > 2 else
                                default_splits,
                    'use_all': data_info[3] if len(data_info) > 3 else False,
                    }
            assert len(data_info) < 5
            result.append(r)
        elif type(data_info) is dict:
            r = data_info
            result.append(r)
        else:
            raise Exception('unkwown data_info = {}'.format(data_info))
    for r in result:
        if 'data' in r:
            assert 'name' not in r
            r['name'] = r['data']
            del r['data']
    return result

def parse_data_clean_splits(data_infos):
    '''
    use regularize_data_sources
    '''
    datas, cleaness, all_valid_splits = [], [], []
    default_splits = get_default_splits()
    for data_info in data_infos:
        if type(data_info) is str:
            datas.append(data_info)
            cleaness.append(10)
            all_valid_splits.append(default_splits)
        elif type(data_info) is tuple or type(data_info) is list:
            assert len(data_info) > 0
            datas.append(data_info[0])
            cleaness.append(data_info[1] if len(data_info) > 1 else 10)
            all_valid_splits.append(data_info[2] if len(data_info) > 2 else default_splits)
        elif type(data_info) is dict:
            datas.append(data_info['data'])
            cleaness.append(data_info.get('cleaness', 10))
            all_valid_splits.append(data_info.get('valid_splits', default_splits))
        else:
            raise Exception('unkwown data_info = {}'.format(data_info))
    return datas, cleaness, all_valid_splits

def attach_properties(src_nodes, dst_tree):
    name_to_dst_node = {n.name: n for n in dst_tree.iter_search_nodes() if
            n != dst_tree}
    confusings = []
    for src_node in src_nodes:
        if src_node.name not in name_to_dst_node:
            continue
        dst_node = name_to_dst_node[src_node.name]
        for f in src_node.features:
            if f in ['support', 'name', 'dist', 'sub_group']:
                continue
            if f in dst_node.features:
                if dst_node.__getattribute__(f) == src_node.__getattribute__(f):
                    continue
                else:
                    confusings.append({'name': src_node.name,
                        'feature': f,
                        'value in tree': dst_node.__getattribute__(f),
                        'value in property list': src_node.__getattribute__(f)})
            else:
                dst_node.add_feature(f, src_node.__getattribute__(f))
    assert len(confusings) == 0, pformat(confusings)

def update_taxonomy_by_latest(ref_data, target_data):
    lift_train = False
    from .common import ensure_copy_folder
    # copy everything from ref_data to target_data and _no_bb
    ensure_copy_folder(op.join('data', ref_data),
            op.join('data', target_data))
    ensure_copy_folder(op.join('data', ref_data + '_no_bb'),
            op.join('data', target_data + '_no_bb'))

    # _with_bb
    ref_data = ref_data + '_with_bb'
    target_data = target_data + '_with_bb'
    update_bb_taxonomy_by_latest(ref_data, target_data)

def update_bb_taxonomy_by_latest(ref_data, target_data):
    ref_dataset = TSVDataset(ref_data)
    out_dataset = TSVDataset(target_data)

    split = 'train'
    splitX = '{}X'.format(split)
    all_idxsource_idxrow = [(int(s_idx_source), int(s_idx_row)) for s_idx_source, s_idx_row in
            tsv_reader(ref_dataset.get_shuffle_file(split))]
    pattern = 'data/(.*)/(train|trainval|test)\.label.*\.tsv'
    # e.g. source_origin_label = 'data/SeeingAISplit/train.label.tsv'
    source_data_splits = [re.match(pattern, source_origin_label).groups()
            for source_origin_label, in ref_dataset.iter_data(splitX, 'origin.label')]
    source_data_split_versions = [(d, s, -1) for d, s in source_data_splits]

    lift_train = False
    dump_to_taxonomy_dataset(ref_dataset, all_idxsource_idxrow,
            source_data_split_versions, lift_train, split, out_dataset)

def dump_to_taxonomy_dataset(ref_dataset, all_idxsource_idxrow,
        source_data_split_versions, lift_train, split, out_dataset):
    splitX = split + 'X'
    tax = Taxonomy(load_from_yaml_file(op.join(ref_dataset._data_root, 'root.yaml')))
    sources_label = []
    sources_origin_label = []
    for idxsource, (source_data, source_split, source_version) in enumerate(source_data_split_versions):
        idx = [idx_r for idx_s, idx_r in all_idxsource_idxrow if idxsource == idx_s]
        for n in tax.root.iter_search_nodes():
            if source_data in n.features:
                n.add_feature(source_data,
                        n.__getattribute__(source_data) + ',{}'.format(n.name))
        source_dataset = TSVDatasetSource(source_data, root=tax.root,
                split_infos=[{'split': source_split, 'version': -1}])
        source_dataset._ensure_initialized()
        source_origin_label = source_dataset.get_data(source_split, 'label',
                source_version)
        sources_origin_label.append(source_origin_label)
        converted_label = convert_label(source_origin_label,
                idx, source_dataset._sourcelabel_to_targetlabels,
                with_bb=True)
        for i in tqdm(idx):
            l = converted_label[i]
            if lift_train:
                l[1] = json.dumps(lift_one_image(l[1], tax))
            else:
                l[1] = json.dumps(delift_one_image(l[1], tax))
            l[0] = '{}_{}_{}'.format(source_dataset.name, source_split, l[0])
        out_split = '{}{}'.format(split, idxsource)
        label_file = out_dataset.get_data(out_split, 'label')
        tsv_writer(converted_label, label_file)
        sources_label.append(label_file)

    # the label version might be updated. Thus, the sources_label could be
    # different from the reference dataset
    write_to_file('\n'.join(sources_label),
            out_dataset.get_data(splitX, 'label'))

    write_to_file('\n'.join(sources_origin_label),
            out_dataset.get_data(splitX, 'origin.label'))

    # copy the image source file
    ensure_copy_file(ref_dataset.get_data(splitX),
            out_dataset.get_data(splitX))

    # copy the labelmap
    ensure_copy_file(ref_dataset.get_labelmap_file(),
            out_dataset.get_labelmap_file())

    # copy the shuffle file
    # the idx could be different since we might add more images here
    tsv_writer(all_idxsource_idxrow,
            out_dataset.get_shuffle_file(split))

def test():
    data = 'LogosInTheWild-v2Clean'
    idx = 1237
    dataset = TSVDataset(data)
    tsv = TSVFile(dataset.get_data('train', 'label', 3))
    key, str_rects = tsv.seek(idx)
    key, _, str_im = dataset.seek_by_key(key, 'train')

    #im = img_from_base64(str_im)
    jpgbytestring = base64.b64decode(str_im)
    nparr = np.frombuffer(jpgbytestring, np.uint8)
    im = cv2.imdecode(nparr, cv2.IMREAD_IGNORE_ORIENTATION);

    rects = json.loads(str_rects)
    draw_rects(im, rects)
    save_image(im, '/mnt/jianfw_desk/a.png')

def trim_rects_from_db(rects):
    keys = ['_id', 'create_time', 'idx_in_split',
            'key', 'split', 'data']
    for rect in rects:
        for k in keys:
            if k in rect:
                del rect[k]

class TSVDatasetDB(object):
    def __init__(self, name):
        self.name = name
        self._db = create_mongodb_client()
        self._gt = self._db['qd']['ground_truth']
        self._image = self._db['qd']['image']

    def iter_gt(self, split, version=None, version_by_time=None,
            idx=None, bb_type='with_bb'):
        # query the image db first and then attach the gt information
        match = {'data': self.name,
                 'split': split}
        if idx:
            match['idx_in_split'] = {'$in': list(idx)}
        lookup_pipeline = self._get_gt_rect_pipeline(
                split, version, version_by_time, bb_type,
                extra_filter={'$eq': ['$key', '$$key']},
                in_lookup=True)
        pipeline = [{'$match': match},
                    {'$lookup': {'from': 'ground_truth',
                                 'let': {'key': '$key'},
                                 'pipeline': lookup_pipeline,
                                 'as': 'rects'
                                 }}]
        for row in self._image.aggregate(pipeline, allowDiskUse=True):
            trim_rects_from_db(row['rects'])
            yield row['idx_in_split'], row['key'], row['rects']

    @deprecated('use iter_gt since this function will not return the image with no bb')
    def iter_gt_image(self, split, version=None, version_by_time=None,
            idx=None, bb_type='with_bb'):
        # deprecated. use iter_gt since this function will not return anything
        # if the image in idx has no box
        # if idx is not none, here we do not guarrentee the order is kept.
        # Thus, we also return the index of the image
        if idx:
            extra_filter = {'idx_in_split': {'$in': list(idx)}}
        else:
            extra_filter = None
        pipeline = self._get_gt_rect_pipeline(split,
                version, version_by_time, bb_type,
                extra_filter)
        pipeline.append({'$group': {'_id': '$idx_in_split',
                                    'key': {'$first': '$key'},
                                    'rects': {'$push': '$$ROOT'}}})
        logging.info(pformat(pipeline))
        for result in self._gt.aggregate(pipeline, allowDiskUse=True):
            for rect in result['rects']:
                if '_id' in rect:
                    del rect['_id']
                if 'create_time' in rect:
                    del rect['create_time']
            yield result['_id'], result['key'], result['rects']

    def _get_gt_rect_pipeline(self, split, version, version_by_time,
            bb_type, extra_filter, in_lookup=False):
        if not in_lookup:
            match = {'data': self.name,
                     'split': split}
        else:
            match_condition = []
            match_condition.append({'$eq': ['$data', self.name]})
            match_condition.append({'$eq': ['$split', split]})
        if extra_filter:
            if not in_lookup:
                match.update(extra_filter)
            else:
                match_condition.append(extra_filter)
        if version_by_time:
            if not in_lookup:
                match['create_time'] = {'$lte': version_by_time}
            else:
                match_condition.append({'$lte': ['$create_time', version_by_time]})
            sort_by = 'create_time'
        else:
            if version is None:
                version = 0
            if version != -1:
                if not in_lookup:
                    match['version'] = {'$lte': version}
                else:
                    match_condition.append({'$lte': ['$version', version]})
            sort_by = 'version'
        if in_lookup:
            match = {'$expr': {'$and': match_condition}}
        sort_value = OrderedDict() # use ordered since dict is non-ordered
        sort_value[sort_by] = -1
        sort_value['contribution'] = -1
        pipeline = [{'$match': match},
                    # we may change some properties at the same version or
                    # time. In this case, we prefer contribution=1
                    {'$sort': sort_value},
                    {'$group': {'_id':      '$action_target_id',
                                'rect_info': {'$first': '$$ROOT'}}},
                    {'$replaceRoot': {'newRoot': '$rect_info'}},
                    {'$match': {'contribution': 1}},
                    ]
        if bb_type == 'with_bb':
            cond1 = {'rect': {'$ne': None}}
            cond2_or = []
            for i in range(4):
                cond2_or.append({'rect.{}'.format(i): {'$ne': 0}})
            cond2 = {'$or': cond2_or}
            match_with_bb = {'$and': [cond1, cond2]}
            pipeline.append({'$match': match_with_bb})
        else:
            assert bb_type == 'no_bb'
        return pipeline

    def iter_gt_rect(self, split, version=None, version_by_time=None,
            bb_type='with_bb',
            extra_filter=None):
            # we do not add any filter here. Thus it is no_bb + with_bb
        pipeline = self._get_gt_rect_pipeline(split, version, version_by_time,
                bb_type, extra_filter)
        return self._gt.aggregate(pipeline, allowDiskUse=True)

    def num_rows(self, split):
        pipeline = [{'$match': {'data': self.name, 'split': split}},
                {'$group': {'_id': 1, 'm': {'$max': '$idx_in_split'}}}]
        result = next(self._image.aggregate(pipeline))
        return result['m'] + 1

    def get_data(self, split):
        return op.join('data', self.name, '{}.tsv'.format(split))

    def _get_unique_labels(self):
        # we ignore the version to make it simplier. it should cover all labels
        splits = [split_info['split'] for split_info in self._split_infos]
        pipeline = [{'$match': {'data': self.name,
                                'split': {'$in': splits}}},
                    {'$group': {'_id': '$class'}}]
        labelmap = sorted([r['_id'] for r in self._gt.aggregate(pipeline)])
        return labelmap

class TSVDatasetSourceDB(TSVDatasetDB):
    def __init__(self, name, root=None,
            split_infos=None,
            cleaness=10,
            use_all=False,
            use_negative_label=False,
            select_by_verified=False):
        super(TSVDatasetSourceDB, self).__init__(name)
        self._noffset_count = {}
        self._type = None
        self._root = root
        # the list of <datasetlabel, rootlabel>
        self._sourcelabel_to_targetlabels = None
        self._targetlabel_to_sourcelabels = None
        self._initialized = False
        self._type_to_datasetlabel_to_split_idx = None
        self._type_to_datasetlabel_to_count = None
        self._type_to_split_label_idx = None
        if split_infos is not None:
            self._split_infos = split_infos
        else:
            self._split_infos = [{'split': s, 'version': -1}
                    for s in get_default_splits()]
        self._split_to_info = {s['split']: s for s in self._split_infos}
        assert len(set([split_info['split'] for split_info in
            self._split_infos])) == len(self._split_infos)
        self._use_all = use_all
        self._select_by_verified = select_by_verified
        self.cleaness = cleaness
        self.use_negative_label = use_negative_label

    def populate_info(self, root):
        self._ensure_initialized()
        for node in root.iter_search_nodes():
            if root == node:
                continue
            if node.name in self._targetlabel_to_sourcelabels:
                sourcelabels = self._targetlabel_to_sourcelabels[node.name]
                node.add_feature(self.name, ','.join(sourcelabels))
                if any(is_noffset(l) for l in sourcelabels):
                    node.add_feature(self.name + '_readable',
                            ','.join(get_nick_name(noffset_to_synset(l)) if
                                is_noffset(l) else l for l in sourcelabels))
                total = 0
                for sourcelabel in sourcelabels:
                    for t in self._type_to_datasetlabel_to_count:
                        datasetlabel_to_count = self._type_to_datasetlabel_to_count[t]
                        c = datasetlabel_to_count.get(sourcelabel, 0)
                        total = total + c
                        key = 'images_{}'.format(t)
                        node.add_feature(key,
                                node.__getattribute__(key) + c)
                node.add_feature('{}_total'.format(self.name), total)

    def _ensure_initialized(self):
        if self._initialized:
            return

        self.update_label_mapper()

        self._load_inverted()

        self._initialized = True

    def _load_inverted(self):
        # make sure self.update_label_mapper() is called
        types = ['with_bb', 'no_bb']
        self._type_split_label_idx = []

        usefull_dataset_labels = self._sourcelabel_to_targetlabels.keys()
        usefull_dataset_labels = [l for l in usefull_dataset_labels if not l.startswith('-')]
        for split_info in self._split_infos:
            for bb_type in types:
                iter_merged = self.iter_gt_rect(split=split_info['split'],
                        version=split_info.get('version'),
                        version_by_time=split_info.get('version_by_time'),
                        bb_type=bb_type,
                        extra_filter={'class': {'$in': usefull_dataset_labels}})
                type_split_label_idx = ((bb_type, rect_info['split'],
                    rect_info['class'], rect_info['idx_in_split'])
                    for rect_info in iter_merged)
                self._type_split_label_idx.extend(type_split_label_idx)

        assert not self.use_negative_label, 'not supported'

        self._type_split_label_idx = list(set(self._type_split_label_idx))
        self._type_to_split_label_idx = list_to_dict(
                self._type_split_label_idx, 0)
        self._type_to_datasetlabel_to_split_idx = {}
        for t in self._type_to_split_label_idx:
            split_label_idx = self._type_to_split_label_idx[t]
            label_to_split_idx = list_to_dict(split_label_idx, 1)
            self._type_to_datasetlabel_to_split_idx[t] = label_to_split_idx
        self._type_to_datasetlabel_to_count = {}
        for t in self._type_to_datasetlabel_to_split_idx:
            datasetlabel_to_split_idx = self._type_to_datasetlabel_to_split_idx[t]
            datasetlabel_to_count = {l: len(datasetlabel_to_split_idx[l]) for l in
                    datasetlabel_to_split_idx}
            self._type_to_datasetlabel_to_count[t] = datasetlabel_to_count

        self._split_to_num_image = {split_info['split']: self.num_rows(split_info['split']) for split_info in
                self._split_infos}

    def update_label_mapper(self):
        root = self._root

        labelmap = self._get_unique_labels()
        hash_labelmap = set(labelmap)

        tree_noffsets = {}
        for node in root.iter_search_nodes():
            if node == root or not node.noffset:
                continue
            for s in node.noffset.split(','):
                tree_noffsets[s] = node.name
        name_to_targetlabels = {}
        targetlabel_has_whitelist = set()
        invalid_list = []
        any_source_key = 'label_names_in_all_dataset_source'
        for node in root.iter_search_nodes():
            if node == root:
                continue
            if hasattr(node, self.name) or hasattr(node, any_source_key):
                if hasattr(node, self.name):
                    # this is like a white-list
                    values = node.__getattribute__(self.name)
                else:
                    values = node.__getattribute__(any_source_key)
                if values is not None:
                    source_terms = values.split(',')
                    for t in source_terms:
                        t = t.strip()
                        if t not in name_to_targetlabels:
                            name_to_targetlabels[t] = set()
                        if t not in hash_labelmap:
                            invalid_list.append((t, self.name, node.name))
                            continue
                        name_to_targetlabels[t].add(node.name)
                # even if it is None, we will also add it to white-list so that
                # we will not automatically match the term.
                targetlabel_has_whitelist.add(node.name)
            else:
                # we will keep the lower case always for case-insensitive
                # comparison
                all_candidate_src_names = [node.name.lower()]
                if hasattr(node, 'alias_names'):
                    all_candidate_src_names.extend([s.strip() for s in
                        node.alias_names.split(',')])
                for t in set(all_candidate_src_names):
                    if t not in name_to_targetlabels:
                        name_to_targetlabels[t] = set()
                    name_to_targetlabels[t].add(node.name)

        sourcelabel_targetlabel = []
        if len(invalid_list) != 0:
            logging.warn('invalid white list information: {}'.format(pformat(invalid_list)))

        #result = {}
        label_to_synset = LabelToSynset()
        for l in labelmap:
            matched = False
            if l.lower() in name_to_targetlabels:
                matched = True
                for t in name_to_targetlabels[l.lower()]:
                    sourcelabel_targetlabel.append((l, t))
            if l in name_to_targetlabels:
                for t in name_to_targetlabels[l]:
                    sourcelabel_targetlabel.append((l, t))
                matched = True
            if not matched:
                succeed, ns = label_to_synset.convert(l)
                if not succeed:
                    continue
                for n in ns:
                    n = synset_to_noffset(n)
                    if n in tree_noffsets:
                        t = tree_noffsets[n]
                        if t in targetlabel_has_whitelist:
                            # if it has white list, we will not respect the
                            # noffset to do the autmatic matching
                            continue
                        sourcelabel_targetlabel.append((l, t))
                        #result[l] = t
        if self.use_negative_label:
            # we just add the mapping here. no need to check if -s is in the
            # source label list
            sourcelabel_targetlabel.extend([('-' + s, '-' + t) for s, t in
                sourcelabel_targetlabel])

        self._sourcelabel_to_targetlabels = list_to_dict_unique(sourcelabel_targetlabel,
                0)
        self._targetlabel_to_sourcelabels = list_to_dict_unique(sourcelabel_targetlabel,
                1)

        return self._sourcelabel_to_targetlabels

    def iter_gt_image(self, split, idx=None, bb_type='with_bb'):
        split_info = self._split_to_info[split]

        return super(TSVDatasetSourceDB, self).iter_gt_image(split,
                version=split_info.get('version'),
                version_by_time=split_info.get('version_by_time'),
                idx=idx, bb_type=bb_type)

    def select_tsv_rows(self, label_type):
        self._ensure_initialized()
        result = []
        if label_type in self._type_to_split_label_idx:
            split_label_idx = self._type_to_split_label_idx[label_type]
            datasetlabel_to_splitidx = list_to_dict(split_label_idx, 1)
            for datasetlabel in datasetlabel_to_splitidx:
                if datasetlabel in self._sourcelabel_to_targetlabels:
                    split_idxes = datasetlabel_to_splitidx[datasetlabel]
                    targetlabels = self._sourcelabel_to_targetlabels[datasetlabel]
                    for targetlabel in targetlabels:
                        result.extend([(targetlabel, split, idx) for split, idx in
                            split_idxes])
        # must_have_indices
        for split_info in self._split_infos:
            split = split_info['split']
            must_have_indices = split_info.get('must_have_indices', [])
            # we set the target label here as None so that the post-processing
            # will not ignore it. The real labels will also be converted
            # corrected since we do not depend on this target label only.
            result.extend((None, split, i) for i in must_have_indices)
        if self._use_all:
            split_to_targetlabel_idx = list_to_dict(result, 1)
            for s in split_to_targetlabel_idx:
                rootlabel_idxes = split_to_targetlabel_idx[s]
                idx_to_rootlabel = list_to_dict(rootlabel_idxes, 1)
                num_image = self._split_to_num_image[s]
                idxes = set(range(num_image)).difference(set(idx_to_rootlabel.keys()))
                for i in idxes:
                    # for these images, the root label is hard-coded as None
                    result.append((None, s, i))
            for split_info in self._split_infos:
                s = split_info['split']
                if s in split_to_targetlabel_idx:
                    continue
                if s not in self._split_to_num_image:
                    continue
                result.extend([(None, s, i) for i in
                    range(self._split_to_num_image[s])])
        return result

def build_tax_dataset_from_db(taxonomy_folder, **kwargs):
    random.seed(777)
    dataset_name = kwargs.get('data',
            op.basename(taxonomy_folder))
    overall_dataset = TSVDataset(dataset_name)
    if op.isfile(overall_dataset.get_labelmap_file()):
        logging.info('ignore to build taxonomy since {} exists'.format(
            overall_dataset.get_labelmap_file()))
        return
    init_logging()
    logging.info('building {}'.format(dataset_name))
    all_tax = load_all_tax(taxonomy_folder)
    tax = merge_all_tax(all_tax)
    tax.update()
    initialize_images_count(tax.root)
    mapper = LabelToSynset()
    mapper.populate_noffset(tax.root)
    imagenet22k = TSVDatasetSource('imagenet22k_448', tax.root)
    if op.isfile(imagenet22k.get_labelmap_file()):
        disambibuity_noffsets(tax.root, imagenet22k.load_noffsets())
    else:
        logging.info('there is no imagenet22k_448 dataset to help identify the noffset')
    populate_url_for_offset(tax.root)

    ambigous_noffset_file = op.join(overall_dataset._data_root,
            'ambigous_noffsets.yaml')
    output_ambigous_noffsets(tax.root, ambigous_noffset_file)

    data_infos = regularize_data_sources(kwargs['datas'])

    data_sources = [TSVDatasetSourceDB(root=tax.root, **d)
            for d in data_infos]

    for s in data_sources:
        s.populate_info(tax.root)

    populate_cum_images(tax.root)

    labels, child_parent_sgs = child_parent_print_tree2(tax.root, 'name')

    label_map_file = overall_dataset.get_labelmap_file()
    write_to_file('\n'.join(labels), label_map_file)
    # save the parameter
    write_to_yaml_file((taxonomy_folder, kwargs), op.join(overall_dataset._data_root,
            'generate_parameters.yaml'))
    dest_taxonomy_folder = op.join(overall_dataset._data_root,
            'taxonomy_folder')
    if op.isdir(dest_taxonomy_folder):
        shutil.rmtree(dest_taxonomy_folder)
    shutil.copytree(taxonomy_folder, dest_taxonomy_folder)

    out_dataset = {'with_bb': TSVDataset(dataset_name + '_with_bb'),
            'no_bb': TSVDataset(dataset_name + '_no_bb')}

    for label_type in out_dataset:
        target_file = out_dataset[label_type].get_labelmap_file()
        ensure_directory(op.dirname(target_file))
        shutil.copy(label_map_file, target_file)

    logging.info('cum_images_with_bb: {}'.format(tax.root.cum_images_with_bb))
    logging.info('cum_images_no_bb: {}'.format(tax.root.cum_images_no_bb))

    # write the simplified version of the tree
    dest = op.join(overall_dataset._data_root, 'root.simple.yaml')
    write_to_yaml_file(tax.dump(['images_with_bb']), dest)

    tree_file = overall_dataset.get_tree_file()
    write_to_file('\n'.join(['{} {}{}'.format(c, p, '' if sg < 0 else ' {}'.format(sg))
                             for c, p, sg in child_parent_sgs]),
            tree_file)
    for label_type in out_dataset:
        target_file = out_dataset[label_type].get_tree_file()
        ensure_directory(op.dirname(target_file))
        shutil.copy(tree_file, target_file)

    node_should_have_images(tax.root, 200,
            op.join(overall_dataset._data_root, 'labels_with_few_images.yaml'))

    # get the information of all train val
    ldtsi = []
    logging.info('collecting all candidate images')
    for label_type in out_dataset:
        for dataset in data_sources:
            targetlabel_split_idxes = dataset.select_tsv_rows(label_type)
            for rootlabel, split, idx in targetlabel_split_idxes:
                ldtsi.append((rootlabel, dataset, label_type, split, idx))
    # we need to remove the duplicates. the duplicates could come from such
    # cases: for example, we have Laptop and laptop in the image. Both of the
    # labels are mapped to laptop, which is in the target domain. In this case,
    # the image could be in the list twice
    ldtsi = list(set(ldtsi))

    num_test = kwargs.get('num_test', 50)

    # for each label, let's duplicate the image or remove the image
    default_max_image = kwargs.get('max_image_per_label', 1000)
    label_to_max_image = {n.name: n.__getattribute__('max_image_extract_for_train')
            if 'max_image_extract_for_train' in n.features and n.__getattribute__('max_image_extract_for_train') > default_max_image
            else default_max_image for n in tax.root.iter_search_nodes() if n != tax.root}
    label_to_max_image = {l: max(label_to_max_image[l], num_test) for l in label_to_max_image}
    # negative images constraint
    labels = list(label_to_max_image.keys())
    for l in labels:
        label_to_max_image['-' + l] = label_to_max_image[l]
    label_to_max_image[None] = 10000000000
    min_image = kwargs.get('min_image_per_label', 200)

    logging.info('keep a small image pool to split')
    label_to_max_augmented_images = {l: label_to_max_image[l] * 3 for l in label_to_max_image}
    # reduce the computing cost
    ldtsi, extra_dtsi = remove_or_duplicate_each_type(ldtsi, 0,
            label_to_max_augmented_images)
    assert len(extra_dtsi) == 0

    logging.info('select the best test image')
    if num_test == 0:
        test_ldtsi = []
    else:
        # generate the test set from the best data source
        label_to_max_images_for_test = {l: num_test for l in
            label_to_max_image}
        test_ldtsi, extra_dtsi = remove_or_duplicate_each_type(ldtsi, 0,
                label_to_max_images_for_test)
        assert len(extra_dtsi) == 0

    logging.info('removing test images from image pool')
    train_ldtsi = remove_test_in_train(ldtsi, test_ldtsi)

    logging.info('select the final training images')
    train_ldtsi, extra_dtsi = remove_or_duplicate_each_type(train_ldtsi, min_image,
            label_to_max_image)

    logging.info('creating the train data')
    create_trainX_db(train_ldtsi, extra_dtsi, tax, out_dataset,
            lift_train=kwargs.get('lift_train', False))

    populate_output_num_images(test_ldtsi, 'toTest', tax.root)

    # dump the tree to yaml format
    dest = op.join(overall_dataset._data_root, 'root.yaml')
    d = tax.dump()
    write_to_yaml_file(d, dest)
    for label_type in out_dataset:
        target_file = op.join(out_dataset[label_type]._data_root, 'root.yaml')
        ensure_directory(op.dirname(target_file))
        shutil.copy(dest, target_file)

    create_testX_db(test_ldtsi, tax, out_dataset)

    logging.info('done')

def build_taxonomy_from_single_composite_source(source_data,
        source_split, source_version,
        min_image_per_label,
        out_data):
    info = get_frame_info()
    out_dataset = TSVDataset(out_data)
    if op.isdir(out_dataset._data_root):
        logging.info('ignore to build since exists')
        return
    populate_dataset_details(source_data)
    dataset = TSVDataset(source_data)
    label_to_idx = dataset.load_inverted_label(source_split,
            version=source_version)
    if min_image_per_label < 1:
        # this is the ratio beteen the real value and the average
        mean_count = calc_mean([len(idx) for _, idx in label_to_idx.items()])
        min_image_per_label = int(mean_count * min_image_per_label)
    all_idx = list(range(dataset.num_rows(source_split)))
    idx_to_labels = list_to_dict(dict_to_list(label_to_idx, 0), 1)

    while len(label_to_idx) > 0:
        # remove the labels if the len(idx) >= min_image_per_label
        to_remove = [l for l in label_to_idx if len(label_to_idx[l]) >= min_image_per_label]
        for l in to_remove:
            del label_to_idx[l]
        if len(label_to_idx) == 0:
            break
        min_label = min(label_to_idx, key=lambda x: len(label_to_idx[x]))
        min_idx = copy.deepcopy(label_to_idx[min_label])
        min_count = len(min_idx)
        copies = (min_image_per_label + min_count - 1)  // min_count - 1
        info['duplicate_info${}'.format(min_label)] = copies
        assert copies > 0
        # add these extra images
        all_idx.extend(copies * min_idx)
        for i in min_idx:
            for l in idx_to_labels[i]:
                if l in label_to_idx:
                    # otherwise, it means that label is enough
                    for _ in range(copies):
                        label_to_idx[l].append(i)
    random.seed(6)
    random.shuffle(all_idx)
    ensure_copy_file(dataset.get_labelmap_file(),
            out_dataset.get_labelmap_file())

    shuffle = TSVFile(dataset.get_shuffle_file(source_split))
    tsv_writer((shuffle[i] for i in all_idx), out_dataset.get_shuffle_file('train'))
    tsv_copy(dataset.get_data(source_split + 'X'),
            out_dataset.get_data('trainX'))

    out_dataset.write_data(
            dataset.iter_data('train', 'hw', filter_idx=all_idx),
            'train', 'hw')
    out_dataset.write_data(
            dataset.iter_data('train', 'label', version=source_version,
                filter_idx=all_idx),
            'train', 'label')
    out_dataset.write_data(list(info.items()), 'train', 'generate_info')

def build_taxonomy_from_single_source(source_data,
        source_split, source_version,
        min_image_per_label,
        out_data):
    populate_dataset_details(source_data)
    populate_dataset_hw(source_data, ['train'])
    dataset = TSVDataset(source_data)
    label_to_idx = dataset.load_inverted_label(source_split,
            version=source_version)
    all_idx = list(range(dataset.num_rows(source_split)))
    idx_to_labels = list_to_dict(dict_to_list(label_to_idx, 0), 1)

    while len(label_to_idx) > 0:
        # remove the labels if the len(idx) >= min_image_per_label
        to_remove = [l for l in label_to_idx if len(label_to_idx[l]) >= min_image_per_label]
        for l in to_remove:
            del label_to_idx[l]
        if len(label_to_idx) == 0:
            break
        min_label = min(label_to_idx, key=lambda x: len(label_to_idx[x]))
        min_idx = copy.deepcopy(label_to_idx[min_label])
        min_count = len(min_idx)
        copies = (min_image_per_label + min_count - 1)  // min_count - 1
        assert copies > 0
        # add these extra images
        all_idx.extend(copies * min_idx)
        for i in min_idx:
            for l in idx_to_labels[i]:
                if l in label_to_idx:
                    # otherwise, it means that label is enough
                    for _ in range(copies):
                        label_to_idx[l].append(i)
    random.seed(6)
    random.shuffle(all_idx)
    out_dataset = TSVDataset(out_data)
    shuffle = [(0, r) for r in all_idx]
    tsv_writer(shuffle, out_dataset.get_train_shuffle_file())
    ensure_copy_file(dataset.get_labelmap_file(),
            out_dataset.get_labelmap_file())

    write_to_file(dataset.get_data('train'),
            out_dataset.get_data('trainX'))
    write_to_file(dataset.get_data('train', 'hw'),
            out_dataset.get_data('trainX', 'hw'))
    write_to_file(dataset.get_data('train', 'label', version=source_version),
            out_dataset.get_data('trainX', 'label'))

def build_taxonomy_impl(taxonomy_folder,
        data, datas,
        taxonomy=None,
        num_test=50, max_image_per_label=1000,
        min_image_per_label=200,
        lift_train=False,
        use_original_label=False,
        **kwargs):
    random.seed(777)
    dataset_name = data
    overall_dataset = TSVDataset(dataset_name)
    if op.isfile(overall_dataset.get_labelmap_file()):
        logging.info('ignore to build taxonomy since {} exists'.format(
            overall_dataset.get_labelmap_file()))
        return
    init_logging()
    logging.info('building {}'.format(dataset_name))
    if taxonomy_folder is not None:
        all_tax = load_all_tax(taxonomy_folder)
        tax = merge_all_tax(all_tax)
    else:
        tax = Taxonomy(taxonomy)
    tax.update()
    initialize_images_count(tax.root)
    mapper = LabelToSynset()
    mapper.populate_noffset(tax.root)
    imagenet22k = TSVDatasetSource('imagenet22k_448', tax.root)
    if op.isfile(imagenet22k.get_labelmap_file()):
        disambibuity_noffsets(tax.root, imagenet22k.load_noffsets())
    else:
        logging.info('there is no imagenet22k_448 dataset to help identify the noffset')
    populate_url_for_offset(tax.root)

    ambigous_noffset_file = op.join(overall_dataset._data_root,
            'ambigous_noffsets.yaml')
    output_ambigous_noffsets(tax.root, ambigous_noffset_file)

    if isinstance(datas, str):
        datas = load_from_yaml_file(datas)
    data_infos = regularize_data_sources(datas)

    data_sources = [TSVDatasetSource(root=tax.root, **d)
            for d in data_infos]

    for s in data_sources:
        s.populate_info(tax.root)

    populate_cum_images(tax.root)

    labels, child_parent_sgs = child_parent_print_tree2(tax.root, 'name')

    label_map_file = overall_dataset.get_labelmap_file()
    write_to_file('\n'.join(labels), label_map_file)
    # save the parameter
    write_to_yaml_file(get_frame_info(),
            op.join(overall_dataset._data_root,
            'generate_parameters.yaml'))
    dest_taxonomy_folder = op.join(overall_dataset._data_root,
            'taxonomy_folder')
    if op.isdir(dest_taxonomy_folder):
        shutil.rmtree(dest_taxonomy_folder)
    if taxonomy_folder is not None:
        shutil.copytree(taxonomy_folder, dest_taxonomy_folder)
    else:
        write_to_yaml_file(taxonomy, op.join(dest_taxonomy_folder, 'root.yaml'))

    out_dataset = {'with_bb': TSVDataset(dataset_name + '_with_bb'),
            'no_bb': TSVDataset(dataset_name + '_no_bb')}

    for label_type in out_dataset:
        target_file = out_dataset[label_type].get_labelmap_file()
        ensure_directory(op.dirname(target_file))
        shutil.copy(label_map_file, target_file)

    logging.info('cum_images_with_bb: {}'.format(tax.root.cum_images_with_bb))
    logging.info('cum_images_no_bb: {}'.format(tax.root.cum_images_no_bb))

    # write the simplified version of the tree
    dest = op.join(overall_dataset._data_root, 'root.simple.yaml')
    write_to_yaml_file(tax.dump(['images_with_bb']), dest)

    tree_file = overall_dataset.get_tree_file()
    write_to_file('\n'.join(['{} {}{}'.format(c, p, '' if sg < 0 else ' {}'.format(sg))
                             for c, p, sg in child_parent_sgs]),
            tree_file)
    for label_type in out_dataset:
        target_file = out_dataset[label_type].get_tree_file()
        ensure_directory(op.dirname(target_file))
        shutil.copy(tree_file, target_file)

    node_should_have_images(tax.root, 200,
            op.join(overall_dataset._data_root, 'labels_with_few_images.yaml'))

    # get the information of all train val
    ldtsi = []
    logging.info('collecting all candidate images')
    for label_type in out_dataset:
        for dataset in data_sources:
            targetlabel_split_idxes = dataset.select_tsv_rows(label_type)
            for rootlabel, split, idx in targetlabel_split_idxes:
                ldtsi.append((rootlabel, dataset, label_type, split, idx))
    # we need to remove the duplicates. the duplicates could come from such
    # cases: for example, we have Laptop and laptop in the image. Both of the
    # labels are mapped to laptop, which is in the target domain. In this case,
    # the image could be in the list twice
    ldtsi = list(set(ldtsi))

    # for each label, let's duplicate the image or remove the image
    default_max_image = max_image_per_label
    label_to_max_image = {n.name: n.__getattribute__('max_image_extract_for_train')
            if 'max_image_extract_for_train' in n.features and n.__getattribute__('max_image_extract_for_train') > default_max_image
            else default_max_image for n in tax.root.iter_search_nodes() if n != tax.root}
    label_to_max_image = {l: max(label_to_max_image[l], num_test) for l in label_to_max_image}
    # negative images constraint
    labels = list(label_to_max_image.keys())
    for l in labels:
        label_to_max_image['-' + l] = label_to_max_image[l]
    label_to_max_image[None] = 10000000000

    label_to_min_image = {n.name: n.__getattribute__('min_image_extract_for_train')
            if 'min_image_extract_for_train' in n.features
            else min_image_per_label for n in tax.root.iter_search_nodes() if n != tax.root}

    logging.info('keep a small image pool to split')
    label_to_max_augmented_images = {l: label_to_max_image[l] * 3 for l in label_to_max_image}
    # reduce the computing cost
    ldtsi, extra_dtsi = remove_or_duplicate_each_type(ldtsi, 0,
            label_to_max_augmented_images)
    assert len(extra_dtsi) == 0

    logging.info('select the best test image')
    if num_test == 0:
        test_ldtsi = []
    else:
        # generate the test set from the best data source
        label_to_max_images_for_test = {l: num_test for l in
            label_to_max_image}
        test_ldtsi, extra_dtsi = remove_or_duplicate_each_type(ldtsi, 0,
                label_to_max_images_for_test)
        assert len(extra_dtsi) == 0

    logging.info('removing test images from image pool')
    train_ldtsi = remove_test_in_train(ldtsi, test_ldtsi)

    logging.info('select the final training images')
    train_ldtsi, extra_dtsi = remove_or_duplicate_each_type(
        train_ldtsi,
        label_to_min_image,
        label_to_max_image)

    logging.info('creating the train data')
    create_trainX(train_ldtsi, extra_dtsi, tax, out_dataset,
            lift_train=lift_train,
            use_original_label=use_original_label)

    populate_output_num_images(test_ldtsi, 'toTest', tax.root)

    # dump the tree to yaml format
    dest = op.join(overall_dataset._data_root, 'root.yaml')
    d = tax.dump()
    write_to_yaml_file(d, dest)
    for label_type in out_dataset:
        target_file = op.join(out_dataset[label_type]._data_root, 'root.yaml')
        ensure_directory(op.dirname(target_file))
        shutil.copy(dest, target_file)

    create_testX(test_ldtsi, tax, out_dataset)

    logging.info('done')

def delift_one_image(rects, tax):
    # currently, for the training, we need to delift the label. That is, if it
    # is man, we should remove the person label
    rects2 = []
    for curr_r in rects:
        if curr_r['class'].startswith('-'):
            # if it is a background, we do nothing here. In the future, we
            # might change this logic
            rects2.append(curr_r)
            continue
        curr_label = curr_r['class']
        if 'rect' not in curr_r:
            same_place_rects = rects2
        else:
            ious = [calculate_iou(r['rect'], curr_r['rect']) for r in rects2]
            same_place_rects = [r for i, r in zip(ious, rects2) if i > 0.9]
        # if current label is one of parent of the same_place rects, ignore it
        ignore = False
        for same_place_r in same_place_rects:
            ancestors = tax.name_to_ancestors[same_place_r['class']]
            if curr_label in ancestors or same_place_r['class'] == curr_label:
                ignore = True
                break
        if ignore:
            continue
        ancestors = tax.name_to_ancestors[curr_label]
        to_removed = []
        for same_place_r in same_place_rects:
            if same_place_r['class'] in ancestors:
                to_removed.append(same_place_r)
        for t in to_removed:
            rects2.remove(t)
        rects2.append(curr_r)
    return rects2

def lift_one_image(rects, tax):
    rects2 = []
    for curr_r in rects:
        if curr_r['class'].startswith('-'):
            rects2.append(curr_r)
            continue
        label = curr_r['class']
        all_label = tax.name_to_ancestors[label]
        all_label.add(label)
        for l in all_label:
            same_label_rects = [r for r in rects2 if r['class'] == l]
            if 'rect' in curr_r:
                ious = [calculate_iou(r['rect'], curr_r['rect']) for r in
                    same_label_rects if 'rect' in r]
                if len(ious) > 0 and max(ious) > 0.9:
                    continue
                else:
                    r = copy.deepcopy(curr_r)
                    r['class'] = l
                    rects2.append(r)
            else:
                if len(same_label_rects) == 0:
                    r = copy.deepcopy(curr_r)
                    r['class'] = l
                    rects2.append(r)
    return rects2

def populate_output_num_images(ldtX, suffix, root):
    label_to_node = {n.name: n for n in root.iter_search_nodes() if n != root}
    targetlabel_to_dX = list_to_dict(ldtX, 0)
    for targetlabel in targetlabel_to_dX:
        if not targetlabel or targetlabel.startswith('-'):
            # currently, we ignore this background case. In the future, we
            # might change this logic
            continue
        if not targetlabel:
            # it means background images
            continue
        dtX = targetlabel_to_dX[targetlabel]
        dataset_to_X = list_to_dict(dtX, 0)
        for dataset in dataset_to_X:
            X = dataset_to_X[dataset]
            if len(X) == 0:
                continue
            key = '{}_{}'.format(dataset.name, suffix)
            value = len(X)
            label_to_node[targetlabel].add_feature(key, value)
        labeltype_to_dX = list_to_dict(dtX, 1)
        for labeltype in labeltype_to_dX:
            dX = labeltype_to_dX[labeltype]
            key = '{}_{}'.format(labeltype, suffix)
            value = len(dX)
            label_to_node[targetlabel].add_feature(key, value)

def output_ambigous_noffsets(root, ambigous_noffset_file):
    ambigous = []
    for node in root.iter_search_nodes():
        if hasattr(node, 'noffsets') and node.noffset is None:
            noffsets = node.noffsets.split(',')
            d = create_info_for_ambigous_noffset(node.name, noffsets)
            d['parent_name'] = ','.join([n.name for n in
                        node.get_ancestors()[:-1]])
            ambigous.append(d)
    if len(ambigous) > 0:
        logging.info('output ambigous terms to {}'.format(ambigous_noffset_file))
        write_to_yaml_file(ambigous, ambigous_noffset_file)
    else:
        logging.info('Congratulations on no ambigous terms.')

def output_ambigous_noffsets_main(tax_input_folder, ambigous_file_out):
    all_tax = load_all_tax(tax_input_folder)
    tax = merge_all_tax(all_tax)
    mapper = LabelToSynset()
    mapper.populate_noffset(tax.root)
    imagenet22k = TSVDatasetSource('imagenet22k')
    if op.isfile(imagenet22k.get_labelmap_file()):
        logging.info('remove the noffset if it is not in imagenet22k')
        disambibuity_noffsets(tax.root, imagenet22k.load_noffsets())
    else:
        logging.info('no imagenet22k data used to help remove noffset ambiguities')

    populate_url_for_offset(tax.root)

    output_ambigous_noffsets(tax.root, ambigous_file_out)

def convert_inverted_file(name):
    d = TSVDataset(name)
    splits = get_default_splits()
    for split in splits:
        logging.info('loading {}-{}'.format(name, split))
        x = d.load_inverted_label(split)
        def gen_rows():
            for label in x:
                idx = x[label]
                yield label, ' '.join(map(str, idx))

        inverted_file = d.get_data(split, 'inverted.label')
        target_file = op.splitext(inverted_file)[0] + '.tsv'
        if not op.isfile(inverted_file):
            if op.isfile(target_file):
                os.remove(target_file)
            continue
        if op.isfile(target_file):
            continue
        tsv_writer(gen_rows(), target_file)

def standarize_crawled(tsv_input, tsv_output):
    rows = tsv_reader(tsv_input)
    def gen_rows():
        for i, row in enumerate(rows):
            if (i % 1000) == 0:
                logging.info(i)
            image_str = row[-1]
            image_label = row[0]
            rects = [{'rect': [0, 0, 0, 0], 'class': image_label}]
            image_name = '{}_{}'.format(op.basename(tsv_input), i)
            yield image_name, json.dumps(rects), image_str
    tsv_writer(gen_rows(), tsv_output)

def get_data_sources(version):
    return load_from_yaml_file('./aux_data/data_sources/{}.yaml'.format(version))

def get_img_url2(img_key):
    # don't use get_image_url
    clean_name = _map_img_key_to_name2(img_key)
    url = _get_url_from_name(clean_name)
    return url

def get_img_url(img_key):
    # use version 2
    clean_name = _map_img_key_to_name(img_key)
    url = _get_url_from_name(clean_name)
    return url

def map_image_key_to_url_key(data, split, key):
    return hash_sha1(str((data, split, key)))

@deprecated(reason='need to incoroprate the data split info')
def _map_img_key_to_name2(key):
    assert len(key) > 0
    ext = '.jpg'
    pattern = '^([0-9]|-|[a-z]|[A-Z]|\.|_)*$'
    if not re.match(pattern, key):
        key = hash_sha1(key)
    key = key.lower()
    if key.endswith(ext):
        return key
    else:
        return key + ext

def _map_img_key_to_name(key):
    # use version 2
    _EXT = ".jpg"
    if key.startswith("brand"):
        return "brand" + str(hash(key)) + _EXT
    else:
        key = key.lower()
        if key.endswith(_EXT):
            return key
        else:
            return key + _EXT

def _get_url_from_name(name):
    _SITE = 'https://cogsimagestorage.blob.core.windows.net/'
    _CONTAINER_NAME = "detectortrain"
    return _SITE + _CONTAINER_NAME + "/" + name

def parse_combine(key):
    pattern = '(.*?)_(train|trainval|test)_(.*)'
    result = re.match(pattern, key)
    if result is None:
        return None, None, key
    else:
        return result.groups()

def convert_to_uhrs_with_url(data):
    dataset = TSVDataset(data)
    for split in get_default_splits():
        if not dataset.has(split, 'label'):
            continue
        v = dataset.get_latest_version(split, 'label')
        def gen_rows():
            for row in dataset.iter_data(split, 'label', version=v):
                key = row[0]
                _, _, key = parse_combine(key)
                row.append(get_img_url(key))
                yield row
        dataset.write_data(gen_rows(), split,
                'url', version=v)

def find_same_location_rects(target, rects, iou=0.95):
    return [r for r in rects if
        calculate_iou(target['rect'], r['rect']) > iou]

def find_same_rects(target, rects, iou=0.95):
    same_class_rects = [r for r in rects if r['class'] == target['class']]
    return [r for r in same_class_rects if
        calculate_iou(target['rect'], r['rect']) > iou]

def rects_diff(lefts, rights, iou=0.95):
    result = []
    for l in lefts:
        if len(find_same_rects(l, rights, iou)) == 0:
            result.append(l)
    return result

def rects_inter(lefts, rights, iou=0.95):
    result = []
    for l in lefts:
        if len(find_same_rects(l, rights, iou)) > 0:
            result.append(l)
    return result


def rect_in_rects(target, rects, iou=0.95):
    same_class_rects = [r for r in rects if r['class'] == target['class']]
    if 'rect' not in target:
        return len(same_class_rects) > 0
    else:
        return any(r for r in same_class_rects if 'rect' in r and
            calculate_iou(target['rect'], r['rect']) > iou)

def strict_rect_in_rects(target, rects):
    return any(float_tolorance_equal(target, r) for r in rects)

def load_key_rects(iter_data):
    assert type(iter_data) is not str
    result = []
    logging.info('loading key rects')
    for row in tqdm(iter_data):
        assert len(row) == 2
        rects = json.loads(row[1])
        if isinstance(rects, int):
            # in this case, it is imagenet2012 dataset, which is the class
            # index.
            rects = [{'class': str(rects)}]
        result.append([row[0], rects])
    return result

def convert_uhrs_result_back_to_sources(in_tsv, debug=True, tree_file=None):
    rows = tsv_reader(in_tsv)
    key_rects3 = []
    num_yes, num_no, num_un = 0, 0, 0
    for row in rows:
        # key, yes, no, uncertain
        assert len(row) == 4
        rects_yes = json.loads(row[1])
        rects_no = json.loads(row[2])
        rects_un = json.loads(row[3])
        num_yes = num_yes + len(rects_yes)
        num_no = num_no + len(rects_no)
        num_un = num_un + len(rects_un)
        key_rects3.append([row[0], [rects_yes, rects_no, rects_un]])

    logging.info('#yes={}; #no={}; #un={}; #yes/(#yes+#no+#un)={}'.format(
        num_yes, num_no, num_un, 1.*num_yes/(num_yes+num_no+num_un)))

    if tree_file:
        tax = Taxonomy(load_from_yaml_file(tree_file))
        mapper = LabelToSynset()
        mapper.populate_noffset(tax.root)

    datas = get_data_sources()
    datas = [data for data, _ in datas]
    datasplitkey_rects3 = [[parse_combine(key), rects3]
            for key, rects3 in key_rects3]
    data_split_key_rects3 = [(data, split, key, rects3)
            for (data, split, key), rects3 in datasplitkey_rects3]

    data_to_split_key_rects3 = list_to_dict(data_split_key_rects3, 0)

    for data in data_to_split_key_rects3:
        logging.info(data)
        if tree_file:
            source_dataset = TSVDatasetSource(data, tax.root)
            source_dataset._ensure_initialized()
        else:
            source_dataset = TSVDataset(data)
        split_key_rects3 = data_to_split_key_rects3[data]
        split_to_key_rects3 = list_to_dict(split_key_rects3, 0)
        for split in split_to_key_rects3:
            logging.info(split)
            key_rects3 = split_to_key_rects3[split]
            key_to_rects3 = list_to_dict(key_rects3, 0)
            v = source_dataset.get_latest_version(split, 'label')
            logging.info('{} - {}'.format(data, split))
            source_key_rects = load_key_rects(source_dataset.iter_data(split, 'label', version=v))
            is_equal = True
            num_added, num_removed = 0, 0
            meta = {'in_tsv': in_tsv}
            for i, (key, origin_rects) in tqdm(enumerate(source_key_rects)):
                if debug:
                    old_origin_rects = copy.deepcopy(origin_rects)
                yes_rects, no_rects, un_rects = key_to_rects3.get(key, [[[], [], []]])[0]
                # if no_rects are in original, remove it. remove first
                for r in no_rects:
                    delete_rects = find_same_rects(r, origin_rects)
                    if tree_file:
                        if r['class'] in source_dataset._targetlabel_to_sourcelabels:
                            for reverse_class in source_dataset._targetlabel_to_sourcelabels[r['class']]:
                                r2 = copy.deepcopy(r)
                                r2['class'] = reverse_class
                                delete_rects2 = find_same_rects(r2, origin_rects)
                                delete_rects.extend(delete_rects2)
                    if 'class_from' in r:
                        r2 = copy.deepcopy(r)
                        r2['class'] = r2['class_from']
                        delete_rects2 = find_same_rects(r2, origin_rects)
                        delete_rects.extend(delete_rects2)
                    for d in delete_rects:
                        # delete rects may include duplicate terms
                        if d in origin_rects:
                            origin_rects.remove(d)
                            num_removed = num_removed + 1
                            is_equal = False
                # if yes_rects are not in original, add it
                for r in yes_rects:
                    same_rects = find_same_rects(r, origin_rects)
                    if len(same_rects) > 0:
                        for s in same_rects:
                            if s.get('uhrs_confirm', 0) == 0:
                                is_equal = False
                            s['uhrs_confirm'] = s.get('uhrs_confirm', 0) + 1
                    else:
                        r['uhrs_confirm'] = r.get('uhrs_confirm', 0) + 1
                        origin_rects.append(copy.deepcopy(r))
                        is_equal = False
                        num_added = num_added + 1
                for r in un_rects:
                    same_rects = find_same_rects(r, origin_rects)
                    for s in same_rects:
                        if s.get('uhrs_uncertain', 0) == 0:
                            is_equal = True
                        s['uhrs_uncertain'] = s.get('uhrs_uncertain', 0) + 1
                if debug:
                    if len(origin_rects) != len(old_origin_rects):
                        for _, _, im_str in source_dataset.iter_data(split, filter_idx=[i]):
                            im = img_from_base64(im_str)
                            old_im = im.copy()
                            draw_bb(old_im, [r['rect'] for r in old_origin_rects],
                                    [r['class'] for r in old_origin_rects])
                            new_im = im.copy()
                            draw_bb(new_im, [r['rect'] for r in origin_rects],
                                    [r['class'] for r in origin_rects])
                            yes_im = im.copy()
                            draw_bb(yes_im, [r['rect'] for r in yes_rects],
                                    [r['class'] for r in yes_rects])
                            no_im = im.copy()
                            draw_bb(no_im, [r['rect'] for r in no_rects],
                                    [r['class'] for r in no_rects])

                            logging.info(pformat(old_origin_rects))
                            logging.info(pformat(origin_rects))
                            logging.info(pformat(yes_rects))
                            logging.info(pformat(no_rects))
                            show_images([old_im, new_im, yes_im, no_im], 2, 2)

            assert not source_dataset.has(split, 'label', v + 1)
            if not is_equal:
                source_dataset.write_data(((key, json.dumps(rects)) for key, rects in source_key_rects),
                        split, 'label', version=v+1)
                meta_file = source_dataset.get_data(split, 'label.metadata', version=v+1) + '.yaml'
                meta['num_added_rects'] = num_added
                meta['num_removed_rects'] = num_removed
                meta['total_number_images'] = len(source_key_rects)

                meta['avg_added_rects'] = 1. * num_added / meta['total_number_images']
                meta['avg_removed_rects'] = 1. * num_removed / meta['total_number_images']
                write_to_yaml_file(meta, meta_file)
                logging.info(pformat(meta))
            else:
                logging.info('equal - {} - {}'.format(data, split))
        populate_dataset_details(data)

def merge_multi_by_key(tsv_file1, all_tsv_file2, out_tsv,
        from_flag1, all_from_flag2):
    files = [tsv_file1]
    files.extend(all_tsv_file2)
    all_key_rects = [load_key_rects(tsv_reader(f)) for f in files]
    all_key_to_rects = [{key: rects for key, rects in key_rects}
        for key_rects in all_key_rects]
    keys = [key for key, _ in all_key_rects[0]]
    flags = [from_flag1]
    flags.extend(all_from_flag2)
    def gen_rows():
        for key in keys:
            all_rects = [key_to_rects[key] for key_to_rects in all_key_to_rects if key in key_to_rects]
            for rects, f in zip(all_rects, flags):
                for r in rects:
                    r['from'] = f
            rects = all_rects[0]
            for r in all_rects[1:]:
                rects.extend(r)
            yield key, json_dump(rects)
    tsv_writer(gen_rows(), out_tsv)

def merge_by_key(tsv_file1, tsv_file2, out_tsv,
        from_flag1='', from_flag2=''):
    files = [tsv_file1, tsv_file2]
    all_key_rects = [load_key_rects(tsv_reader(f)) for f in files]
    all_key_to_rects = [{key: rects for key, rects in key_rects}
        for key_rects in all_key_rects]
    keys = [key for key, _ in all_key_rects[0]]
    flags = [from_flag1, from_flag2]
    def gen_rows():
        for key in keys:
            all_rects = [key_to_rects[key] for key_to_rects in all_key_to_rects if key in key_to_rects]
            for rects, f in zip(all_rects, flags):
                for r in rects:
                    r['from'] = f
            rects = all_rects[0]
            for r in all_rects[1:]:
                rects.extend(r)
            yield key, json_dump(rects)
    tsv_writer(gen_rows(), out_tsv)

def threshold_merged_prediction(pred_tsv_file, from_to_class_to_threshold,
        output_file):
    def gen_rows():
        for key, str_rects in tsv_reader(pred_tsv_file):
            rects = json.loads(str_rects)
            all_rect = []
            for r in rects:
                if type(from_to_class_to_threshold[r['from']]) is dict:
                    th = from_to_class_to_threshold[r['from']][r['class']]
                else:
                    th = from_to_class_to_threshold[r['from']]
                assert type(th) is float or type(th) is int
                if r['conf'] > th:
                    all_rect.append(r)
            yield key, json_dump(all_rect)
    tsv_writer(gen_rows(), output_file)

def remove_has_confirmed_colocation_labels(all_data, iou_threshold=0.75):
    all_split = get_default_splits()
    debug = False
    for data in all_data:
        dataset = TSVDataset(data)
        for split in all_split:
            if not dataset.has(split):
                continue
            logging.info('{} -> {}'.format(data, split))
            info = {'total': 0, 'removed': 0}
            def gen_rows():
                for i, (key, en_rects) in tqdm(enumerate(dataset.iter_data(split,
                    'label', version=-1))):
                    rects = json.loads(en_rects)
                    info['total'] = info['total'] + len(rects)
                    if debug:
                        _, _, im = next(dataset.iter_data(split, filter_idx=[i]))
                        im = img_from_base64(im)
                        im_origin = np.copy(im)
                        draw_bb(im_origin, [r['rect'] for r in rects], [r['class'] for r in rects])
                    all_removed = []
                    # some labels has no rect
                    good_rects = [r for r in rects if 'rect' in r and
                            ('conf' not in r or ('conf' in r and 'uhrs_confirm' in r))]
                    pro_no_verify = [r for r in rects if 'conf' in r and
                            'uhrs_confirm' not in r and 'rect' in r]
                    no_rects = [r for r in rects if 'rect' not in r]
                    assert len(good_rects) + len(pro_no_verify) + len(no_rects)==len(rects)
                    to_remove = []
                    for r in pro_no_verify:
                        if any((s for s in good_rects if
                                calculate_iou(r['rect'], s['rect']) >
                                iou_threshold)):
                            to_remove.append(r)
                    for t in to_remove:
                        rects.remove(t)
                    info['removed'] = info['removed'] + len(to_remove)
                    all_removed.extend(to_remove)
                    if debug and len(all_removed) > 0:
                        logging.info(len(all_removed))
                        im_removed = np.copy(im)
                        logging.info(pformat(all_removed))
                        draw_bb(im_removed, [r['rect'] for r in all_removed],
                                [r['class'] for r in all_removed])
                        show_images([im_origin, im_removed], 1, 2)
                    yield key, json_dump(rects)
            def gen_info():
                yield 'remove auto-pro and has good labels in same locatoin', iou_threshold
                yield 'total', info['total']
                yield 'removed', info['removed']
                ratio = 1. * info['removed'] / info['total']
                yield 'ratio', ratio
                logging.info(pformat(info))
                logging.info(ratio)

            dataset.update_data(gen_rows(), split, 'label', generate_info=gen_info())

def get_taxonomy_path(data):
    pattern = 'Tax(.*)V([0-9]*)_(.*)'
    result = re.match(pattern, data)
    assert result is not None
    major, minor, revision = result.groups()
    return './aux_data/taxonomy10k/Tax{0}/Tax{0}V{1}'.format(major, minor)

def uhrs_verify_db_merge_to_tsv(collection_name='uhrs_logo_verification',
        extra_match=None):
    set_interpretation_result_for_uhrs_result(collection_name)
    c = create_bbverification_db(collection_name=collection_name)
    data_split_to_key_rects, all_id = c.get_completed_uhrs_result(
            extra_match=extra_match)
    merge_uhrs_result_to_dataset(data_split_to_key_rects)
    size = 1000
    i = 0
    while True:
        start = i * size
        end = i * size + size
        if start >= len(all_id):
            break
        if end >= len(all_id):
            end = len(all_id)
        c.set_status_as_merged(all_id[start:end])
        i += 1
    y = [c.set_status_as_merged([i]) for i in tqdm(all_id)]
    y = list(map(lambda i: c.set_status_as_merged([i]), all_id))

def uhrs_merge_one(uhrs_rect, target_rects, tag_only=False):
    info = {'num_added': 0,
            'num_removed': 0,
            'verified_confirmed': 0,
            'verified_removed': 0,
            'non_verified_confirmed': 0,
            'non_verified_removed': 0}
    same_rect, iou = find_best_matched_rect(uhrs_rect, target_rects, check_iou=(not tag_only))
    if iou < 0.8:
        if is_positive_uhrs_verified(uhrs_rect):
            target_rects.append(uhrs_rect)
            info['num_added'] = 1
        return info

    if is_verified_rect(same_rect):
        if is_positive_uhrs_verified(uhrs_rect):
            info['verified_confirmed'] = 1
        elif is_negative_uhrs_verified(uhrs_rect):
            info['verified_removed'] = 1
            target_rects.remove(same_rect)
    else:
        if is_positive_uhrs_verified(uhrs_rect):
            info['non_verified_confirmed'] = 1
        elif is_negative_uhrs_verified(uhrs_rect):
            info['non_verified_removed'] = 1
            target_rects.remove(same_rect)

    same_rect['uhrs'] = {}
    for t, v in viewitems(uhrs_rect['uhrs']):
        same_rect['uhrs'][t] = v

    return info

def merge_uhrs_result_to_dataset(data_split_to_key_rects, tag_only=False):
    from .common import list_to_dict
    from .common import json_dump
    for (data, split), uhrs_key_rects in viewitems(data_split_to_key_rects):
        logging.info((data, split))
        dataset = TSVDataset(data)
        uhrs_key_to_rects = list_to_dict(uhrs_key_rects, 0)
        logging.info('number of image will be affected: {}'.format(len(uhrs_key_rects)))
        info = {}
        def gen_rows():
            for key, str_rects in dataset.iter_data(split, 'label', -1,
                    progress=True):
                rects = json.loads(str_rects)
                if key in uhrs_key_to_rects:
                    uhrs_rects = uhrs_key_to_rects[key]
                    del uhrs_key_to_rects[key]
                else:
                    uhrs_rects = []
                for uhrs_rect in uhrs_rects:
                    sub_info = uhrs_merge_one(uhrs_rect, rects, tag_only=tag_only)
                    for k, v in viewitems(sub_info):
                        info[k] = v + info.get(k, 0)
                yield key, json_dump(rects)
            assert len(uhrs_key_to_rects) == 0
        def generate_info():
            for k, v in viewitems(info):
                yield k, v
            for key, rects in viewitems(uhrs_key_to_rects):
                yield key, json_dump(rects)
        dataset.update_data(gen_rows(), split, 'label',
                generate_info=generate_info())

def set_interpretation_result_for_uhrs_result(collection_name='uhrs_logo_verification'):
    c = create_bbverification_db(collection_name=collection_name)
    query = {'status': {'$in': [c.status_merged, c.status_completed]},
            'interpretation_result': None}
    positive_ids, negative_ids, uncertain_ids = [], [], []
    pos, neg, un = 0, 0, 0
    for rect_info in tqdm(c.collection.find(query)):
        rect = rect_info['rect']
        rect.update({'uhrs': rect_info['uhrs_completed_result']})
        if is_positive_uhrs_verified(rect):
            positive_ids.append(rect_info['_id'])
            if len(positive_ids) > 1000:
                logging.info('positive = {}'.format(pos))
                q = {'_id': {'$in': positive_ids}}
                c.collection.update_many(filter=q,
                        update={'$set': {'interpretation_result': 1}})
                pos += len(positive_ids)
                positive_ids.clear()
        elif is_negative_uhrs_verified(rect):
            negative_ids.append(rect_info['_id'])
            if len(negative_ids) > 1000:
                logging.info('neg = {}'.format(neg))
                q = {'_id': {'$in': negative_ids}}
                c.collection.update_many(filter=q,
                        update={'$set': {'interpretation_result': -1}})
                neg += len(negative_ids)
                negative_ids.clear()
        else:
            uncertain_ids.append(rect_info['_id'])
            if len(uncertain_ids) > 1000:
                logging.info('un = {}'.format(un))
                query = {'_id': {'$in': uncertain_ids}}
                c.collection.update_many(filter=query,
                        update={'$set': {'interpretation_result': 0}})
                un += len(uncertain_ids)
                uncertain_ids.clear()


    if len(positive_ids) > 0:
        query = {'_id': {'$in': positive_ids}}
        c.collection.update_many(filter=query,
                update={'$set': {'interpretation_result': 1}})
    if len(negative_ids) > 0:
        query = {'_id': {'$in': negative_ids}}
        c.collection.update_many(filter=query,
                update={'$set': {'interpretation_result': -1}})
    if len(uncertain_ids) > 0:
        query = {'_id': {'$in': uncertain_ids}}
        c.collection.update_many(filter=query,
                update={'$set': {'interpretation_result': 0}})

def uhrs_verify_db_closest_rect(collection, test_data, test_split, gt_key, p):
    rect_infos = list(collection.find({'data': test_data,
        'split': test_split,
        'key': gt_key}))
    rects = [rect_info['rect'] for rect_info in rect_infos]
    best_idx, best_iou = find_best_matched_rect_idx(p, rects, check_class=True)

    if best_idx is not None:
        rect_info = rect_infos[best_idx]
    else:
        rect_info = None

    return rect_info, best_iou

def verify_prediction_by_db(pred_file, test_data, test_split, conf_th=0.3,
        priority_tier=1, collection_name='uhrs_bounding_box_verification'):
    from qd.process_tsv import ensure_upload_image_to_blob
    from qd.process_tsv import parse_combine
    ensure_upload_image_to_blob(test_data, test_split)

    dataset = TSVDataset(test_data)
    gt_iter = dataset.iter_data(test_split, 'label', version=-1)
    pred_iter = tsv_reader(pred_file)
    key_url_iter = dataset.iter_data(test_split, 'key.url')

    db_task = []
    num_task, num_exists, num_matched_gt, num_change_pri = 0, 0, 0, 0
    c = create_bbverification_db(collection_name=collection_name)
    for gt_row, pred_row, url_row in tqdm(zip(gt_iter, pred_iter,
        key_url_iter)):
        gt_key, gt_str_rects = gt_row
        pred_key, pred_str_rects = pred_row
        assert gt_key == pred_key == url_row[0]
        source_data, source_split, source_key = parse_combine(gt_key)
        if source_data is None and source_split is None:
            source_data = test_data
            source_split = test_split
        gt_rects = json.loads(gt_str_rects)
        pred_rects = json.loads(pred_str_rects)
        for p in pred_rects:
            if p['conf'] < conf_th:
                continue
            # check with the gt
            _, best_iou = find_best_matched_rect(p, gt_rects)
            if best_iou > 0.7:
                num_matched_gt = 0
                continue
            best_rect_info, best_iou = uhrs_verify_db_closest_rect(c.collection, source_data,
                    source_split, source_key, p)
            if best_iou > 0.95:
                num_exists = num_exists + 1
                if best_rect_info['priority_tier'] != c.urgent_priority_tier and \
                        best_rect_info['status'] == c.status_requested:
                    c.collection.update_one(
                            {'_id': best_rect_info['_id']},
                            update={'$set': {'priority_tier': c.urgent_priority_tier}})
                    num_change_pri = num_change_pri + 1
                continue
            p['from'] = pred_file
            url = url_row[1]
            task = {'url': url,
                'data': source_data,
                'split': source_split,
                'key': source_key,
                'rect': p,
                'priority_tier': priority_tier,
                'priority': 0.5}
            num_task = num_task + 1
            db_task.append(task)
            if len(db_task) > 1000:
                c.request_by_insert(db_task)
                db_task = []
    if len(db_task) > 0:
        c.request_by_insert(db_task)
        db_task = []
    logging.info('#task = {}; #exists in db = {}; #matched gt = {}; #num pri change = {}'.format(
        num_task, num_exists, num_matched_gt, num_change_pri))

def convertcomposite_to_standard(data,
        split='train', ignore_image=False, **kwargs):
    dataset = TSVDataset(data)
    if op.isfile(dataset.get_data(split, t='label')) and \
            op.isfile(dataset.get_data(split)):
        logging.info('file exists')
        return
    rows_image = dataset.iter_composite(split, t=None, version=None)
    num_image = dataset.num_rows(split)
    out_tsv = dataset.get_data(split)
    if not ignore_image:
        logging.info('writing image tsv: {}'.format(out_tsv))
        rows_label = dataset.iter_data(split, t='label', version=None)
        tsv_writer(((row_label[0], row_label[1], row_image[2]) for row_image, row_label
                in tqdm(zip(rows_image, rows_label), total=num_image)),
                out_tsv)
    if not op.isfile(dataset.get_data(split, t='label')):
        logging.info('writing image label tsv')
        rows = dataset.iter_composite(split, t='label', version=None)
        tsv_writer(tqdm(rows), dataset.get_data(split, t='label'))

def gen_merged_candidate(prediction_file,
        gts, srclabel_to_destlabel=None, **kwargs):
    # we will only consider the bounding box if the prob is larger than 0.7
    prob_th = kwargs['merge_min_prob']
    prob_th_max = kwargs['merge_max_prob']
    # we will add the bounding box only if the iou is lower than 0.7 to avoid
    # duplicate addition
    iou_th = kwargs['merge_max_iou']
    aug_labels = kwargs['aug_labels']
    prediction = tsv_reader(prediction_file)
    is_aligned = kwargs.get('is_aligned', False)
    logging.info('loading prediction: {}'.format(prediction_file))
    if not is_aligned:
        key_to_pred = {}
        for p in tqdm(prediction):
            assert len(p) == 2
            key = p[0]
            rects = json.loads(p[1])
            key_to_pred[key] = rects
    num_image = 0
    num_added = 0
    for gt in tqdm(gts):
        assert len(gt) == 2
        num_image = num_image + 1
        key = gt[0]
        g_rects = json.loads(gt[1])
        origin_g_rects = copy.deepcopy(g_rects)
        add_rects = []
        if not is_aligned:
            if key not in key_to_pred:
                yield key, origin_g_rects, add_rects, g_rects
                continue
            p_rects = key_to_pred[key]
        else:
            p_key, p_rects = next(prediction)
            p_rects = json.loads(p_rects)
            assert key == p_key
        p_rects = [r for r in p_rects if r['conf'] > prob_th and
                r['conf'] <= prob_th_max]
        if aug_labels is not None:
            p_rects = [r for r in p_rects if r['class'] in aug_labels]
        if srclabel_to_destlabel:
            p_rects = [r for r in p_rects if r['class'] in srclabel_to_destlabel]
            for r in p_rects:
                r['class'] = srclabel_to_destlabel[r['class']]
        p_rects = sorted(p_rects, key=lambda x: -x['conf'])
        for p_rect in p_rects:
            is_add = True
            ious = [calculate_iou(p_rect['rect'], g_rect['rect'])
                    for g_rect in g_rects]
            if len(ious) > 0 and max(ious) > iou_th:
                is_add = False
            if is_add:
                num_added = num_added + 1
                p_rect['from'] = prediction_file
                # add p_rect
                add_rects.append(p_rect)
                g_rects.append(p_rect)
        yield key, origin_g_rects, add_rects, g_rects
    logging.info('num_image = {}'.format(num_image))
    logging.info('num_added = {}'.format(num_added))


def merge_prediction_to_gt(prediction_file, gts, im_rows, dataset, split,
        **param):
    dataset.iter_data(split, 'label')
    gts2 = gen_merged_candidate(prediction_file, gts, **param)
    if im_rows is not None:
        if False:
            # show image
            for gt2, im_row in zip(gts2, im_rows):
                assert gt2[0] == im_row[0]
                im = img_from_base64(im_row[-1])
                origin_rects = gt2[1]
                add_rects = gt2[2]
                merged_rects = gt2[3]
                if len(add_rects) == 0:
                    continue
                im_origin_gt = np.copy(im)
                draw_bb(im_origin_gt, [r['rect'] for r in origin_rects],
                        [r['class'] for r in origin_rects])
                im_add = np.copy(im)
                draw_bb(im_add, [r['rect'] for r in add_rects],
                        [r['class'] for r in add_rects],
                        [r['conf'] for r in add_rects])
                im_merged = np.copy(im)
                draw_bb(im_merged, [r['rect'] for r in merged_rects],
                        [r['class'] for r in merged_rects])
                logging.info(pformat(add_rects))
                show_images([im_origin_gt, im_add, im_merged], 1, 3)
        else:
            # save image
            for i, (gt2, im_row) in enumerate(zip(gts2, im_rows)):
                assert gt2[0] == im_row[0]
                im = img_from_base64(im_row[-1])
                origin_rects = gt2[1]
                add_rects = gt2[2]
                merged_rects = gt2[3]
                if len(add_rects) == 0:
                    continue
                add_class = list(set([r['class'] for r in add_rects]))
                for c in add_class:
                    c_rects = [r for r in add_rects if r['class'] == c]
                    im_add = np.copy(im)
                    draw_rects(c_rects, im_add, add_label=False)
                    im_origin = np.copy(im)
                    draw_rects([x for x in origin_rects if x['class'] == c], im_origin)
                    im_merged = np.copy(im)
                    draw_rects([x for x in merged_rects if x['class'] == c], im_merged)
                    top = np.concatenate((im, im_add), 1)
                    bottom = np.concatenate((im_origin, im_merged), 1)
                    x = np.concatenate((top, bottom), 0)
                    save_image(x, './output/add_image/{}_{}.jpg'.format(
                        c, im_row[0]))
    else:
        meta = {'num_image': 0,
                'num_added_total': 0,
                'from': prediction_file}
        all_added = []
        def gen_rows():
            for key, origin_rects, add_rects, merged_rects in gts2:
                all_added.append((key, add_rects))
                meta['num_added_total'] = meta['num_added_total']+len(add_rects)
                meta['num_image'] = meta['num_image'] + 1
                yield key, json_dump(merged_rects)
        # we will not change the files if they are the same
        def gen_info():
            meta['num_added_each'] = 1. * meta['num_added_total'] / meta['num_image']
            for k, v in viewitems(meta):
                yield k, v
            for k, rects in all_added:
                yield k, json_dump(rects)
        dataset.update_data(gen_rows(), split, 'label',
                generate_info=gen_info())

def generate_labels_to_verify(predict_file, data, split, th,
        pred_to_gt, output_file):
    dataset = TSVDataset(data)
    if op.isfile(op.join(dataset._data_root, 'root.yaml')):
        tax = Taxonomy(load_from_yaml_file(op.join(dataset._data_root, 'root.yaml')))
    else:
        tax = None
    gt_key_rects = [(row[0], json.loads(row[1])) for row in tqdm(dataset.iter_data(split,
        'label', version=-1))]
    logging.info('loading pred')
    pred_key_to_rects = {}
    for row in tqdm(tsv_reader(predict_file)):
        key = row[0]
        rects = json.loads(row[1])
        pred_key_to_rects[key] = [r for r in rects if r['conf'] > th]

    def gen_rows(pred_to_gt=True):
        logging.info('start to writting')
        for key, gt_rects in tqdm(gt_key_rects):
            if tax is not None:
                rects2 = lift_one_image(gt_rects, tax)
                del gt_rects[:]
                gt_rects.extend(rects2)
            pred_rects = pred_key_to_rects.get(key)
            if pred_rects is None:
                continue
            need_confirm = []
            if pred_to_gt:
                for pr in pred_rects:
                    if not onebb_in_list_of_bb(pr, gt_rects):
                        need_confirm.append(pr)
            else:
                for g in gt_rects:
                    if len(g) == 2:
                        assert 'class' in g and 'rect' in g
                        continue
                    if not onebb_in_list_of_bb(g, pred_rects):
                        need_confirm.append(g)
            yield key, json.dumps(need_confirm)

    tsv_writer(gen_rows(pred_to_gt=pred_to_gt), output_file)

def onebb_in_list_of_bb(bb, bbs):
    bbs = [b for b in bbs if b['class'] == bb['class']]
    return any(calculate_iou(b['rect'], bb['rect']) > 0.5 for b in bbs)

def inject_accuracy():
    all_full_expid = os.listdir('./output')
    for full_expid in tqdm(all_full_expid):
        inject_accuracy_one(full_expid)

def inject_accuracy_one(full_expid):
    from qd.db import create_annotation_db
    c = create_annotation_db()
    all_predict = glob.glob(op.join('output', full_expid, 'snapshot',
        '*.predict'))
    all_predict.extend(glob.glob(op.join('output', full_expid, 'snapshot',
        '*.predict.tsv')))

    all_report = glob.glob(op.join('output', full_expid, 'snapshot', '*.report'))
    for report_file in all_report:
        from .common import parse_test_data_with_version
        try:
            test_data, test_split, test_version = parse_test_data_with_version(op.basename(report_file))
        except:
            continue
        info = {'full_expid': full_expid,
                'report_file': op.basename(report_file),
                'test_data': test_data,
                'test_split': test_split,
                'test_version': test_version,
                }
        if 'coco_box' in report_file or '.randomness.' in report_file:
            acc = load_from_yaml_file(report_file)
        elif '.neg_aware_gmap.' in report_file:
            acc = load_from_yaml_file(report_file)
            if 'neg_aware_gmap.noNMSGt.noNMSDet.noExpandDet.report' in report_file:
                key = 'nngmAP'
            elif '.neg_aware_gmap.noNMSGt.report' in report_file:
                key = 'ngmAP'
            else:
                key = 'gmAP'
            acc = {key: acc['map']}
        elif '.top1.' in report_file:
            acc = load_from_yaml_file(report_file)
        elif report_file.endswith('predict.tsv.speed.yaml'):
            all_meter = load_from_yaml_file(report_file)['meters']
            all_meter = [m for m in all_meter if m['name'] == '']
            assert len(all_meter) == 1
            acc = all_meter[0]
            del acc['name']
        elif report_file.endswith('caption.report'):
            acc = json.loads(read_to_buffer(report_file))
        elif any(report_file.endswith(k) for k in [
            'ir_acc.report',
            'cap_acc.report',
            'cap_acc.nospace.report',
            'vqa_acc.report',
            'vqa2.report',
            'stvqa_acc.report',
            'stvqa_nopro_acc',
            'stvqa_anls.report',
            'nlvr2.report',
            'acc.report',
            'vizwiz_vqa.report',
            'textvqa_acc.report',
        ]):
            acc = load_from_yaml_file(report_file)
            from .common import get_all_path, dict_get_path_value
            all_path = get_all_path(acc)
            acc = {p: dict_get_path_value(acc, p) for p in all_path}
        elif report_file.endswith('attr.report'):
            x = json.loads(read_to_buffer(report_file))
            acc = {'attr': x['overall']['0.5']['map']}
        else:
            map_json_file = report_file + '.map.json'
            if op.isfile(map_json_file):
                x = json.loads(read_to_buffer(map_json_file))
                acc = {}
                for k1 in x:
                    for k2 in x[k1]:
                        for k3 in x[k1][k2]:
                            k = '{}${}${}'.format(k1, k2, k3)
                            acc[k] = x[k1][k2][k3]
            else:
                continue
        for k in list(acc.keys()):
            curr = copy.deepcopy(info)
            curr['metric_name'] = k
            exist = False
            for found in c.iter_acc(**curr):
                exist = True
                if found['metric_value'] == acc[k]:
                    continue
                else:
                    # the newer one will overwrite the old one. Sometimes, the
                    # code has issues and the experiment results are not
                    # correct
                    c.update_one_acc(query=curr,
                            update={'$set': {'metric_value': acc[k]}})
            if not exist:
                curr['metric_value'] = acc[k]
                c.insert_acc(**curr)

def find_predict_file(report_file, all_predict):
    for k in ['.ir_acc.report',
              '.vqa_acc.report',
              '.stvqa_acc.report',
              '.stvqa_anls.report',
              '.caption.report',
              '.attr.report'
              ]:
        if report_file.endswith(k):
            pred = report_file[:-len(k)] + '.tsv'
            if pred in all_predict:
                return pred
    #model_iter_0200182.pt.TaxImageNet2012CapDef.test.max_token100.vphoto_def.predict.top1.vphoto_def.report
    #model_iter_0200182.pt.TaxImageNet2012CapDef.test.max_token100.vphoto_def.predict.tsv
    r_pattern = '.*\.(v[^\.]*)\..*(v[^\.]*)\..*'
    result = re.match(r_pattern, op.basename(report_file))
    if result is not None:
        v1, v2 = result.groups()
        if v1 == v2:
           pattern = '(.*)\.top1\.{}\.report'.format(v1)
           result = re.match(pattern, op.basename(report_file))
           if result is not None:
               prefix, = result.groups()
               p = prefix + '.tsv'
               p = op.join(op.dirname(report_file), p)
               for p1 in all_predict:
                   if p1 == p:
                       return p


    found = False
    for p in all_predict:
        if p + '.report' == report_file:
            assert not found
            found = True
            result = p
            return result
    for p in all_predict:
        if p.endswith('.predict.tsv'):
            ps = p[: -len('.predict.tsv')]
        elif p.endswith('.predict'):
            ps = p[: -len('.predict')]
        else:
            continue
        if report_file.startswith(ps):
            rs = report_file[len(ps):]
            logging.info(rs)
            eval_keys = ['predict', 'tsv', 'coco_box', 'neg_aware_gmap', 'top1',
                    'noNMSGt', 'noNMSDet', 'noExpandDet', 'speed', 'MaxDet.*']
            p0 = ''.join(['(\.{})?'.format(k) for k in eval_keys])
            pattern = '{}(\.v[0-9]*)?(\.0)?\.(report|yaml)'.format(p0)
            if re.match(pattern, rs):
                assert not found
                found = True
                result = p
    if not found:
        matched = [p for p in all_predict if report_file.startswith(p)]
        if len(matched) > 0:
            result = sorted(matched, key=lambda x: -len(x))[0]
            found = True
    assert found
    return result

def softnms_row_process(row, sigma=0.5, method=2, Nt=0.5):
    from .common import softnms_c
    key, str_rects = row
    rects = json.loads(str_rects)
    all_class_rect = [(r['class'], r) for r in rects]
    class_to_rects = list_to_dict(all_class_rect, 0)
    rects2 = []
    for c, rs in class_to_rects.items():
        rs = softnms_c(rs, sigma=sigma, method=method, Nt=Nt)
        for r in rs:
            r['class'] = c
        rects2.extend(rs)
    return key, json_dump(rects2)

def tsv_subset_process_NtoN(info):
    row_processor = info['row_processor']
    idx_process = info['idx_process']
    tmp_out = info['tmp_out']
    idx_range_start = info['idx_range_start']
    idx_range_end = info['idx_range_end']
    head = info['head']
    sep = info['out_sep']
    if all(op.isfile(t) and all(op.isfile(s) for s in get_tsv_associates(t)) for t in tmp_out):
        logging.info('skip since exist: {}'.format(tmp_out))
        return
    if 'in_tsvs' in info:
        tsvs = info['in_tsvs']
    else:
        tsvs = [TSVFile(t) for t in info['in_tsv_files']]
    def gen_rows():
        if idx_process == 0 and head is not None:
            yield head
        for i in tqdm(range(idx_range_start, idx_range_end)):
            r = row_processor([t[i] for t in tsvs])
            if r is None:
                continue
            yield r
    tsv_writers(gen_rows(), tmp_out, sep=sep)
    logging.info('done to generate {}'.format(tmp_out))

def tsv_subset_process_1toN(info, with_row_idx):
    row_processor = info['row_processor']
    idx_process = info['idx_process']
    tmp_out = info['tmp_out']
    idx_range_start = info['idx_range_start']
    idx_range_end = info['idx_range_end']
    head = info['head']
    sep = info['out_sep']
    if all(op.isfile(t) for t in tmp_out):
        logging.info('skip since exist: {}'.format(tmp_out))
        return
    if 'in_tsv' in info:
        tsv = info['in_tsv']
    else:
        in_tsv_file = info['in_tsv_file']
        tsv = TSVFile(in_tsv_file)
    def gen_rows():
        if idx_process == 0 and head is not None:
            yield head
        for i in tqdm(range(idx_range_start, idx_range_end)):
            try:
                if with_row_idx:
                    r = row_processor(tsv[i], i)
                else:
                    r = row_processor(tsv[i])
                if r is None:
                    continue
                if isinstance(r, (list, tuple)):
                    yield r
                else:
                    while True:
                        try:
                            x = next(r)
                            yield x
                        except StopIteration:
                            break
            except:
                logging.info(f'{tsv}-{i}')
                raise
    tsv_writers(gen_rows(), tmp_out, sep=sep)

def tsv_subset_process(info, with_row_idx=False):
    row_processor = info['row_processor']
    idx_process = info['idx_process']
    tmp_out = info['tmp_out']
    idx_range_start = info['idx_range_start']
    idx_range_end = info['idx_range_end']
    head = info['head']
    sep = info['out_sep']
    if op.isfile(tmp_out):
        logging.info('skip since exist: {}'.format(tmp_out))
        return
    if 'in_tsv' in info:
        tsv = info['in_tsv']
    else:
        in_tsv_file = info['in_tsv_file']
        tsv = TSVFile(in_tsv_file)
    def gen_rows():
        if idx_process == 0 and head is not None:
            yield head
        for i in tqdm(range(idx_range_start, idx_range_end)):
            # this is a breaking change to add i as option, but we have to do
            # this as row_processor has no way to figure it out which row it is
            try:
                if with_row_idx:
                    r = row_processor(tsv[i], i)
                else:
                    r = row_processor(tsv[i])
                if r is None:
                    continue
                if isinstance(r, (list, tuple)):
                    yield r
                else:
                    while True:
                        try:
                            x = next(r)
                            yield x
                        except StopIteration:
                            break
            except:
                logging.info(f'{tsv}/{i}')
                raise
    tsv_writer(gen_rows(), tmp_out, sep=sep)

def tsv_subset_aggregator(info, with_row_idx=False):
    row_processor = info['row_processor']
    tmp_out = info['tmp_out']
    idx_range_start = info['idx_range_start']
    idx_range_end = info['idx_range_end']
    if File.isfile(tmp_out):
        return load_from_yaml_file(tmp_out)
    if 'in_tsv' in info:
        tsv = info['in_tsv']
    else:
        in_tsv_file = info['in_tsv_file']
        tsv = TSVFile(in_tsv_file)
    ret = {}
    for i in tqdm(range(idx_range_start, idx_range_end)):
        # this is a breaking change to add i as option, but we have to do
        # this as row_processor has no way to figure it out which row it is
        try:
            if with_row_idx:
                row_processor(ret, tsv[i], i)
            else:
                row_processor(ret, tsv[i])
        except:
            logging.info(f'{tsv}/{i}')
            raise
    write_to_yaml_file(ret, tmp_out)
    return ret

def multi_tsv_subset_process(info):
    row_processor = info['row_processor']
    idx_process = info['idx_process']
    in_tsv_files = info['in_tsv_files']
    tmp_out = info['tmp_out']
    idx_range = info['idx_range']
    head = info['head']
    out_sep = info['out_sep']
    if op.isfile(tmp_out):
        logging.info('skip to create {}'.format(tmp_out))
        return
    def gen_rows():
        if idx_process == 0 and head is not None:
            yield head
        tsvs = [TSVFile(f) for f in in_tsv_files]
        for i in tqdm(idx_range):
            row_result = row_processor([tsv[i] for tsv in tsvs])
            if row_result is not None:
                # we can return None to remove rows
                yield row_result
    tsv_writer(gen_rows(), tmp_out, sep=out_sep)

def multi_tsv_row_merger(rows):
    all_key = [row[0] for row in rows]
    assert all(all_key[0] == key for key in all_key[1:])
    all_rect = [json.loads(row[1]) for row in rows]
    str_rect = json_dump([r for rs in all_rect for r in rs])
    return all_key[0], str_rect

def parallel_tsv_process_NtoN(row_processor, in_tsv_files,
        out_tsv_files, num_process, num_jobs=None, head=None, out_sep='\t'):
    in_tsvs = [TSVFile(in_tsv_file) if isinstance(in_tsv_file, str) else
               in_tsv_file for in_tsv_file in in_tsv_files]
    total = len(in_tsvs[0])
    assert all(len(t) == total for t in in_tsvs[1:])
    for t in in_tsvs:
        t.close()
    if num_jobs is None:
        num_jobs = max(1, num_process)
    rows_each_rank = (total + num_jobs - 1) // num_jobs
    all_task = []
    folder = get_tmp_folder()
    for i in range(num_jobs):
        start = i * rows_each_rank
        end = start + rows_each_rank
        end = min(end, total)
        tmp_out = ['{}/{}'.format(folder, out_tsv_file) + '.{}.{}.tsv'.format(i, num_jobs)
            for out_tsv_file in out_tsv_files]
        info = {
            'row_processor': row_processor,
            'idx_process': i,
            #'in_tsv_files': in_tsv_files,
            'in_tsvs': in_tsvs,
            'tmp_out': tmp_out,
            'idx_range_start': start,
            'idx_range_end': end,
            'head': head,
            'out_sep': out_sep,
        }
        all_task.append(info)
    from .common import parallel_map
    parallel_map(tsv_subset_process_NtoN, all_task,
                 num_worker=num_process)
    all_out = [task['tmp_out'] for task in all_task]
    for i, out_tsv_file in enumerate(out_tsv_files):
        tmp_out = [out[i] for out in all_out]
        concat_tsv_files(tmp_out, out_tsv_file, gen_lineidx=False)
        delete_tsv_files(tmp_out)

def parallel_multi_tsv_process(row_processor, in_tsv_files,
        out_tsv_file, num_process, num_jobs=None, head=None, out_sep='\t'):
    in_tsvs = [TSVFile(in_tsv_file) for in_tsv_file in in_tsv_files]
    total = len(in_tsvs[0])
    assert all(len(t) == total for t in in_tsvs[1:])
    if num_jobs is None:
        num_jobs = max(1, num_process)
    rows_each_rank = (total + num_jobs - 1) // num_jobs
    all_task = []
    for i in range(num_jobs):
        start = i * rows_each_rank
        end = start + rows_each_rank
        end = min(end, total)
        if num_jobs == 1:
            tmp_out = out_tsv_file
        else:
            tmp_out = out_tsv_file + '.{}.{}.tsv'.format(i, num_jobs)
        info = {'row_processor': row_processor,
                'idx_process': i,
                'in_tsv_files': in_tsv_files,
                'tmp_out': tmp_out,
                'idx_range': list(range(start, end)),
                'head': head,
                'out_sep': out_sep
                }
        all_task.append(info)
    from .common import parallel_map
    parallel_map(multi_tsv_subset_process, all_task,
            num_worker=num_process)
    if num_jobs > 1:
        all_out = [task['tmp_out'] for task in all_task]
        concat_tsv_files(all_out, out_tsv_file, gen_lineidx=False)
        delete_tsv_files(all_out)

def parallel_tsv_process_1toN(row_processor, in_tsv_file,
                              out_tsv_files, num_process, num_jobs=None, head=None, out_sep='\t',
                              with_row_idx=False,
                              ):
    # in_tsv_file can be a string, interpreted as a tsv file
    # or a list
    if isinstance(in_tsv_file, str):
        in_tsv = TSVFile(in_tsv_file)
    else:
        in_tsv = in_tsv_file
    total = len(in_tsv)
    if num_jobs is None:
        if num_process == 0:
            num_jobs = 1
        else:
            num_jobs = num_process
    rows_each_rank = (total + num_jobs - 1) // num_jobs
    all_task = []
    if isinstance(in_tsv, (TSVFile, CompositeTSVFile)):
        # we need to clear all the cache in TSVFile. otherwise, the process
        # might need a lot of time to copy the cache in in_tsv, e.g. lineidx
        # when the number of data are huge.
        in_tsv.close()
    folder = get_tmp_folder()
    for i in range(num_jobs):
        start = i * rows_each_rank
        end = start + rows_each_rank
        end = min(end, total)
        tmp_out = ['{}/{}'.format(folder, out_tsv_file) + '.{}.{}.tsv'.format(i, num_jobs)
            for out_tsv_file in out_tsv_files]
        info = {'row_processor': row_processor,
                'idx_process': i,
                #'in_tsv_file': in_tsv_file,
                'in_tsv': in_tsv,
                'tmp_out': tmp_out,
                'idx_range_start': start,
                'idx_range_end': end,
                'head': head,
                'out_sep': out_sep
                }
        all_task.append(info)
    from .common import parallel_map
    parallel_map(lambda x: tsv_subset_process_1toN(x, with_row_idx), all_task,
            num_worker=num_process)
    all_out = [task['tmp_out'] for task in all_task]
    if isinstance(in_tsv, TSVFile):
        # explicitly close the file
        in_tsv.close()
    for i, out_tsv_file in enumerate(out_tsv_files):
        tmp_out = [out[i] for out in all_out]
        concat_tsv_files(tmp_out, out_tsv_file, gen_lineidx=False)
        delete_tsv_files(tmp_out)

def parallel_tsv_process(row_processor, in_tsv_file,
                         out_tsv_file,
                         num_process=64,
                         num_jobs=None,
                         head=None,
                         out_sep='\t',
                         with_row_idx=False,
                         ):
    if isinstance(in_tsv_file, str):
        in_tsv = TSVFile(in_tsv_file)
    else:
        in_tsv = in_tsv_file
    total = len(in_tsv)
    if num_jobs is None:
        if num_process == 0:
            num_jobs = 1
        else:
            num_jobs = num_process
    rows_each_rank = (total + num_jobs - 1) // num_jobs
    all_task = []
    if isinstance(in_tsv, TSVFile):
        # we need to clear all the cache in TSVFile. otherwise, the process
        # might need a lot of time to copy the cache in in_tsv, e.g. lineidx
        # when the number of data are huge.
        in_tsv.close()
    folder = get_tmp_folder()
    for i in range(num_jobs):
        start = i * rows_each_rank
        end = start + rows_each_rank
        end = min(end, total)
        #tmp_out = out_tsv_file + '.{}.{}.tsv'.format(i, num_jobs)
        tmp_out = '{}/{}'.format(folder, out_tsv_file) + '.{}.{}.tsv'.format(i, num_jobs)
        info = {'row_processor': row_processor,
                'idx_process': i,
                #'in_tsv_file': in_tsv_file,
                'in_tsv': in_tsv,
                'tmp_out': tmp_out,
                'idx_range_start': start,
                'idx_range_end': end,
                'head': head,
                'out_sep': out_sep
                }
        all_task.append(info)
    from .common import parallel_map
    parallel_map(lambda x: tsv_subset_process(x, with_row_idx), all_task,
            num_worker=num_process)
    all_out = [task['tmp_out'] for task in all_task]
    concat_tsv_files(all_out, out_tsv_file, gen_lineidx=False)
    delete_tsv_files(all_out)

def parallel_tsv_aggregator(
    mapper,
    reducer,
    in_tsv_file,
    num_process=64,
    num_jobs=None,
    head=None,
    out_sep='\t',
    with_row_idx=False,
):
    if isinstance(in_tsv_file, str):
        in_tsv = TSVFile(in_tsv_file)
    else:
        in_tsv = in_tsv_file
    total = len(in_tsv)
    if num_jobs is None:
        if num_process == 0:
            num_jobs = 1
        else:
            num_jobs = num_process
    rows_each_rank = (total + num_jobs - 1) // num_jobs
    all_task = []
    if isinstance(in_tsv, TSVFile):
        # we need to clear all the cache in TSVFile. otherwise, the process
        # might need a lot of time to copy the cache in in_tsv, e.g. lineidx
        # when the number of data are huge.
        in_tsv.close()
    folder = get_tmp_folder()
    basename = datetime.now().strftime('%Y_%m_%d_%H_%M_%S') + '_' + str(hash(in_tsv_file))
    for i in range(num_jobs):
        start = i * rows_each_rank
        end = start + rows_each_rank
        end = min(end, total)
        #tmp_out = out_tsv_file + '.{}.{}.tsv'.format(i, num_jobs)
        tmp_out = '{}/{}'.format(folder, basename) + '.{}.{}.yaml'.format(i, num_jobs)
        info = {'row_processor': mapper,
                'idx_process': i,
                #'in_tsv_file': in_tsv_file,
                'in_tsv': in_tsv,
                'tmp_out': tmp_out,
                'idx_range_start': start,
                'idx_range_end': end,
                'head': head,
                'out_sep': out_sep
                }
        all_task.append(info)
    from .common import parallel_map
    rets = parallel_map(lambda x: tsv_subset_aggregator(x, with_row_idx), all_task,
            num_worker=num_process)
    rets = reducer(rets)
    all_out = [task['tmp_out'] for task in all_task]
    for f in all_out:
        ensure_remove_file(f)
    return rets

def mpi_tsv_process(row_processor, in_tsv_file, out_tsv_file):
    mpi_size = get_mpi_size()
    mpi_rank = get_mpi_rank()
    in_tsv = TSVFile(in_tsv_file)
    total = len(in_tsv)
    rows_each_rank = (total + mpi_size - 1) // mpi_size
    logging.info('size for each rank = {}'.format(rows_each_rank))
    start = mpi_rank * rows_each_rank
    end = min(start + rows_each_rank, total)
    logging.info('rank = {}; start = {}; end = {}'.format(mpi_rank,
        start, end))
    out_tsv_rank = out_tsv_file + '.{}.{}.tsv'.format(mpi_rank, mpi_size)
    def gen_rows():
        for i in tqdm(range(start, end)):
            row = in_tsv[i]
            row = row_processor(row)
            yield row
    tsv_writer(gen_rows(), out_tsv_rank)

    all_out = [out_tsv_file + '.{}.{}.tsv'.format(i, mpi_size) for i in
            range(mpi_size)]

    while True:
        if all(op.isfile(f) for f in all_out):
            break
        logging.info('waiting {}'.format('; '.join(f for f in all_out if not
            op.isfile(f))))
        time.sleep(5)

    if mpi_rank == 0:
        concat_tsv_files(all_out, out_tsv_file)
        delete_tsv_files(all_out)
    else:
        while not op.isfile(out_tsv_file):
            logging.info('waiting {}'.format(out_tsv_file))
            time.sleep(5)

def create_focus_dataset(data, source_version, target_labels, out_data):
    out_dataset = TSVDataset(out_data)
    write_to_file('\n'.join(target_labels),
            out_dataset.get_labelmap_file())

    # train
    dataset = TSVDataset(data)
    write_to_file(dataset.get_data('train'), out_dataset.get_data('trainX'))
    write_to_file(dataset.get_data('train', t='hw'),
            out_dataset.get_data('trainX', t='hw'))

    def gen_rows():
        target_label_set = set(target_labels)
        for key, str_rects in dataset.iter_data('train', t='label',
                version=source_version, progress=True):
            rects = json.loads(str_rects)
            rects = [r for r in rects if r['class'] in target_label_set]
            yield '_'.join([data, 'train', key]), json_dump(rects)
    out_dataset.write_data(gen_rows(), 'train', t='label')
    num_rows = dataset.num_rows('train')
    tsv_writer(((0, i) for i in range(num_rows)),
            op.join(out_dataset._data_root, 'train.shuffle.txt'))

class CogAPI(object):
    def __init__(self):
        self.remote_image_features = ['adult']
        self.run_ocr = False
        self.computervision_client = None

    def ensure_init(self):
        from msrest.authentication import CognitiveServicesCredentials
        from azure.cognitiveservices.vision.computervision import ComputerVisionClient
        if self.computervision_client is None:
            x = load_from_yaml_file('aux_data/configs/cognitive_credential.yaml')
            self.subscription_key = x['subscription_key']
            self.endpoint = x['endpoint']
            self.computervision_client = ComputerVisionClient(self.endpoint,
                    CognitiveServicesCredentials(self.subscription_key))

    def call(self, image_url):
        self.ensure_init()
        result = {}
        if self.remote_image_features:
            remote_image_analysis = self.computervision_client.analyze_image(image_url,
                    self.remote_image_features)
            from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
            if 'adult' in self.remote_image_features:
                result.update(self.parse_adult(remote_image_analysis))
            if 'description' in self.remote_image_features:
                result.update(self.parse_description(remote_image_analysis))
            if VisualFeatureTypes.image_type in self.remote_image_features:
                result.update(self.parse_type(remote_image_analysis))
        if self.run_ocr:
            recognize_printed_results = self.computervision_client.batch_read_file(image_url,  raw=True)
            result.update(self.parse_ocr(recognize_printed_results))

        return result

    def parse_ocr(self, recognize_printed_results):
        from azure.cognitiveservices.vision.computervision.models import TextOperationStatusCodes
        # Get the operation location (URL with an ID at the end) from the response
        operation_location_remote = recognize_printed_results.headers["Operation-Location"]
        # Grab the ID from the URL
        operation_id = operation_location_remote.split("/")[-1]

        # Call the "GET" API and wait for it to retrieve the results 
        while True:
            get_printed_text_results = self.computervision_client.get_read_operation_result(operation_id)
            if get_printed_text_results.status not in ['NotStarted', 'Running']:
                break
            time.sleep(1)

        lines = []
        if get_printed_text_results.status == TextOperationStatusCodes.succeeded:
            for text_result in get_printed_text_results.recognition_results:
                for line in text_result.lines:
                    lines.append({'text': line.text, 'bounding_box':
                        line.bounding_box})
        return {'ocr': lines}

    def parse_description(self, result):
        return {'captions': [{'text': cap.text,
                              'conf': cap.confidence}
                              for cap in result.description.captions]}

    def parse_type(self, remote_image_analysis):
        return {'clip_art_type': remote_image_analysis.image_type.clip_art_type,
                'line_drawing_type': remote_image_analysis.image_type.line_drawing_type,
                }

    def parse_adult(self, remote_image_analysis):
        result = {'is_adult': remote_image_analysis.adult.is_adult_content,
                'adult_score': remote_image_analysis.adult.adult_score,
                'is_racy': remote_image_analysis.adult.is_racy_content,
                'racy_score': remote_image_analysis.adult.racy_score}
        return result

def adult_filtering_by_cogapi(data, split):
    dataset = TSVDataset(data)
    api = CogAPI()
    in_tsv = dataset.get_data(split, 'key.url')
    out_tsv = dataset.get_data(split, 'key.adult')
    def row_processor(row):
        key, url = row
        try:
            result = api.call(url)
            return key, json_dump(result)
        except:
            from .common import print_trace
            print_trace()
            return key, json_dump(-1)
    if op.isfile(out_tsv):
        return
    parallel_tsv_process(row_processor, in_tsv,
            out_tsv, num_process=16, num_jobs=1024)

def scale_ocr_result(result, h_scale, w_scale):
    for r in result:
        r['rect'][::2] = [x * w_scale for x in r['rect'][::2]]
        r['rect'][1::2] = [x * h_scale for x in r['rect'][1::2]]
        r['lines'][::2] = [x * w_scale for x in r['lines'][::2]]
        r['lines'][1::2] = [x * h_scale for x in r['lines'][1::2]]

def ocr_engine_result_to_rects(lines):
    rects = []
    for info in lines:
        r = {}
        xs = info['boundingBox'][::2]
        ys = info['boundingBox'][1::2]
        r['rect'] = [min(xs), min(ys), max(xs), max(ys)]
        r['class'] = 'text'
        r['lines'] = info['boundingBox']
        r['text'] = info['text']
        rects.append(r)
    return rects

def run_ocr_on_content(input_file, urls):
    headers = {
        'Content-Type':'image/jpg'
    }
    url = urls[int(random.random() * 9999) % len(urls)]
    import requests
    r = requests.post(url, headers=headers, data=input_file)

    if (r.ok):
        x = json.loads(r.text)
        if 'analyzeResult' not in x or 'readResults' not in x['analyzeResult']:
            return []
        rects = ocr_engine_result_to_rects(x['analyzeResult']['readResults'][0]['lines'])
        return rects

class OCRRowProcessor(object):
    def __init__(self, urls):
        self.urls = urls

    def __call__(self, row):
        if len(row) == 3:
            key, str_rects, str_im = row
        else:
            key, str_im = row
        im = img_from_base64(str_im)
        def proper_image_scale(im):
            h, w = im.shape[:2]
            if h <= w:
                ratio = 800. / h
                w = ratio * w
                h = 800
                if w > 8000:
                    w = 8000
            else:
                ratio = 800. / w
                w = 800
                h = ratio * h
                if h > 8000:
                    h = 8000
            im = cv2.resize(im, (int(w), int(h)))
            return im

        if max(im.shape[:2]) <= 200 or max(im.shape[:2]) > 8000:        
            im_scale = proper_image_scale(im)
            h_scale, w_scale = (1. * im_scale.shape[0] / im.shape[0], 1. *
                    im_scale.shape[1] / im.shape[1])
        else:
            h_scale, w_scale = 1, 1
            im_scale = im

        content = cv2.imencode('.jpg', im_scale)[1]
        try:
            result = run_ocr_on_content(content.tobytes(), self.urls)
        except:
            logging.info(pformat(im_scale.shape))
            raise
        if result is None:
            logging.info('unable to process {}'.format(key))
            return key, -1
        else:
            scale_ocr_result(result, 1./h_scale, 1./w_scale)
            return key, json_dump(result)

def ocr_tsv(in_tsv, out_tsv, urls):
    processor = OCRRowProcessor(urls)
    parallel_tsv_process(processor, in_tsv, out_tsv,
            num_process=5, num_jobs=10240)

def create_dataset_from_pred(pred_tsv, conf_th, src_data, out_data):
    # create label
    def gen_rows():
        for key, str_rects in tqdm(tsv_reader(pred_tsv)):
            rects = json.loads(str_rects)
            high_rects = [r for r in rects if r['conf'] > conf_th]
            if len(high_rects) > 0:
                yield '_'.join([src_data, 'train', key]), json_dump(high_rects)
    tsv_writer(gen_rows(), './data/{}/train.label.tsv'.format(out_data))

    create_image_X_by_key(out_data, 'train', 'label')
    ensure_copy_file(TSVDataset('coco2017Full').get_labelmap_file(),
        TSVDataset(out_data).get_labelmap_file())
    populate_dataset_details(out_data)
    populate_dataset_hw(out_data, ['train'])

def create_image_X_by_key(target_data, target_split, t):
    # given train.label.tsv where key is a composed key, we derive the
    # trainX.tsv and the shuffle file
    dataset = TSVDataset(target_data)
    data_split_keys = [parse_combine(key) for key, _ in
            dataset.iter_data(target_split, t)]
    data_to_split_keys = list_to_dict(data_split_keys, 0)
    data_splits = []
    for data, split_keys in data_to_split_keys.items():
        split_to_keys = list_to_dict(split_keys, 0)
        for split in split_to_keys:
            data_splits.append((data, split))
    data_to_dataset = {}
    def get_dataset(src_data):
        if src_data not in data_to_dataset:
            data_to_dataset[src_data] = TSVDataset(src_data)
        return data_to_dataset[src_data]
    src_tsv = [get_dataset(data).get_data(split) for data, split in data_splits]
    data_split_to_idx = {data_split: i for i, data_split in
            enumerate(data_splits)}
    def gen_shuffle():
        for src_data, src_split, src_key in data_split_keys:
            idx_source = data_split_to_idx[(src_data, src_split)]
            idx_key = get_dataset(src_data).get_idx_by_key(src_key,
                    src_split)
            yield idx_source, idx_key

    tsv_writer(gen_shuffle(),
            get_dataset(target_data).get_shuffle_file(target_split))
    write_to_file('\n'.join(src_tsv),
            get_dataset(target_data).get_data(target_split + 'X'))

def create_tiny_set(data, out_data):
    dataset = TSVDataset(data)
    out_dataset = TSVDataset(out_data)

    num_image = dataset.num_rows('train')
    all_idx = list(range(num_image))
    random.shuffle(all_idx)

    out_num_train = int(0.1 * len(all_idx))
    out_num_test = int(0.05 * len(all_idx))
    train_idx = all_idx[:out_num_train]
    test_idx = all_idx[out_num_train: (out_num_train + out_num_test)]

    ensure_copy_file(dataset.get_labelmap_file(),
            out_dataset.get_labelmap_file())

    for t in [None, 'label']:
        out_dataset.write_data(dataset.iter_data('train',
                                                 t=t,
                                                 filter_idx=train_idx,
                                                 progress=True),
                               split='train',
                               t=t)
        out_dataset.write_data(dataset.iter_data('train',
                                                 t=t,
                                                 filter_idx=test_idx,
                                                 progress=True),
                               split='test',
                               t=t)

def create_tsv_by_image_folder(folder, tsv_file,
        max_short=1200, key_func=None):
    from .common import is_valid_image
    from .common import insensitive_glob
    from .process_image import load_image
    if key_func is None:
        key_func = lambda f: op.basename(f)
    def gen_rows():
        for ext in ['png', 'jpg', 'jpeg', 'JPG']:
            pattern = '*.{}'.format(ext)
            pattern = op.join(folder, pattern)
            im_files = insensitive_glob(pattern)
            for f in tqdm(im_files):
                im = load_image(f)
                if not is_valid_image(im):
                    continue
                if min(im.shape[:2]) > max_short:
                    ratio = 1. * max_short / min(im.shape[:2])
                    h = int(im.shape[0] * ratio)
                    w = int(im.shape[1] * ratio)
                    im = cv2.resize(im, (w, h))
                    yield key_func(f), encoded_from_img(im)
                else:
                    yield key_func(f), base64.b64encode(read_to_buffer(f))
    tsv_writer(gen_rows(), tsv_file)

def create_versions_from_uncertain_label(data, split):
    dataset = TSVDataset(data)
    info = defaultdict(int)
    assert not dataset.has(split, 'label', version=1)
    assert not dataset.has(split, 'label', version=2)
    def gen_rows():
        iter_label = dataset.iter_data(split, 'label')
        for key, str_rects in tqdm(iter_label):
            rects = json.loads(str_rects)
            for r in rects:
                if r['class'].endswith('_?'):
                    r['class'] = r['class'][:-2]
                    info[r['class']] += 1
            yield key, json_dump(rects)
    def gen_info():
        yield 'remove _? in class name', 'keep the boxes'
        for k, v in info.items():
            yield k, v
    dataset.update_data(gen_rows(), split, 'label',
            generate_info=gen_info())

    info = defaultdict(int)
    def gen_rows2():
        iter_label = dataset.iter_data(split, 'label')
        for key, str_rects in iter_label:
            rects = json.loads(str_rects)
            origin_num = len(rects)
            info['original box'] += origin_num
            if split == 'train':
                rects = [r for r in rects if not r['class'].endswith('_?')]
                info['removed'] += origin_num - len(rects)
            else:
                assert split == 'test'
                for r in rects:
                    if r['class'].endswith('_?'):
                        r['class'] = r['class'][:-2]
                        r['diff'] = 1
                        info['new diff set'] += 1
            yield key, json_dump(rects)
    def gen_info2():
        if split == 'train':
            yield 'remove the box whose label ends with _?',
        else:
            assert split == 'test'
            yield 'remove _? if the class ends with _?.', \
                    'keep the box', \
                    'add diff = 1'
        for k, v in info.items():
            yield k, v
    dataset.update_data(gen_rows2(), split, 'label',
            generate_info=gen_info2())

def remove_size_in_label(data, split, version):
    dataset = TSVDataset(data)
    def gen_rows():
        for key, str_rects in tqdm(dataset.iter_data(split, 'label',
            version=version)):
            rects = json.loads(str_rects)
            assert len(rects) == 1
            c = rects[0]['class']
            assert not all(s in c for s in ['_s_', '_S_'])
            for s in ['_s_', '_S_']:
                parts = c.split('_s_')
                assert len(parts) in [1, 2]
                rects[0]['class'] = parts[0]
                if len(parts) == 2:
                    break
            yield key, json_dump(rects)
    dataset.update_data(gen_rows(), split, 'label',
        generate_info=[('remove size in the label', )])

def crop_detbox(data, split, version, out_data=None, save_type=None):
    dataset = TSVDataset(data)
    iter_image = dataset.iter_data(split)
    iter_label = dataset.iter_data(split, 'label', version=version)
    labels = []
    def gen_rows():
        for (image_key, _, image_str), (label_key, str_rects) in tqdm(
                zip(iter_image, iter_label)):
            assert image_key == label_key
            im = img_from_base64(image_str)
            h, w = im.shape[:2]
            rects = json.loads(str_rects)
            for i, r in enumerate(rects):
                x0, y0, x1, y1 = r['rect']
                x0, y0 = int(x0), int(y0)
                x1, y1 = math.ceil(x1), math.ceil(y1)
                x0, y0 = max(0, x0), max(0, y0)
                x1, y1 = min(x1, w), min(y1, h)
                if x1 <= x0 or y1 <= y0:
                    continue
                sub = im[y0:y1, x0:x1, :]
                del r['rect']
                res_key = image_key + '${}'.format(i)
                str_label = json_dump([r])
                labels.append((res_key, str_label))
                yield res_key, str_label, encoded_from_img(sub,
                        save_type=save_type)
    if out_data is None:
        out_data = '{}_crop_{}{}'.format(data, split, version)
    out_dataset = TSVDataset(out_data)
    if out_dataset.has(split):
        logging.info('{} - {} exists'.format(out_dataset.name,
            split))
        return out_data
    #assert not out_dataset.has(split)
    out_dataset.write_data(gen_rows(), split)
    out_dataset.write_data(labels, split, 'label')
    populate_dataset_details(out_data)
    return out_data

def create_single_class_detection_taxonomy(data, split, version, root_name,
        excludes=None):
    populate_dataset_details(data)
    dataset = TSVDataset(data)
    labels = [l for l, in dataset.iter_data(split, 'labelmap', version=version)]
    if excludes is not None:
        lower_label = [(l.lower(), l) for l in labels]
        lower_to_labels = list_to_dict(lower_label, 0)
        lower_excludes = [e.lower() for e in excludes]
        for e in lower_excludes:
            if e in lower_to_labels:
                del lower_to_labels[e]
        labels = [l for _, ls in lower_to_labels.items() for l in ls]
    info = [{'name': root_name,
            'alias_names': ','.join(labels)}]
    return info

def unflatten_to_detection(data, split, pred, output,
        threshold=0.01):
    dataset = TSVDataset(data)
    iter_label = dataset.iter_data(split, 'label')
    iter_pred = tsv_reader(pred)
    def gen_rows():
        pre_key = None
        pre_rects = []
        for (key, str_rects), (pred_key, pred_rects) in tqdm(zip(
                iter_label, iter_pred)):
            assert key == pred_key
            idx = key.rfind('$')
            real_key = key[:idx]
            if real_key != pre_key:
                if pre_key is not None:
                    yield pre_key, json_dump(pre_rects)
                pre_key = real_key
                pre_rects = []
            gt = json.loads(str_rects)
            assert len(gt) == 1
            gt = gt[0]
            pred = json.loads(pred_rects)
            for p in pred:
                sub_gt = copy.deepcopy(gt)
                sub_gt['class'] = p['class']
                sub_gt['conf'] *= p['conf']
                if sub_gt['conf'] > threshold:
                    pre_rects.append(sub_gt)
        yield pre_key, json_dump(pre_rects)
    tsv_writer(gen_rows(), output)

def flatten_labels_for_classification(image_data, image_split, label_tsv,
        out_data, out_split):
    input_argument = get_frame_info()
    input_argument['func'] = 'flatten_labels_for_classification'

    dataset = TSVDataset(image_data)

    populate_dataset_hw(image_data, [image_split])

    iter_hw = dataset.iter_data(image_split, 'hw')
    if label_tsv is not None:
        iter_label = tsv_reader(label_tsv)
    else:
        iter_label = dataset.iter_data(image_split, 'label')

    all_idx = []
    def gen_rows():
        for idx_image, ((label_key, str_rects), (hw_key, str_hw)) in enumerate(
                zip(iter_label, iter_hw)):
            assert label_key == hw_key
            h, w = map(int, str_hw.split(' '))
            rects = json.loads(str_rects)
            for i, r in enumerate(rects):
                x1, y1, x2, y2 = r['rect']

                # since it is for classification, it has to be a valid box.
                # Here we do the fewest pre-processing/filtering to have a
                # valid box coordinates
                x1 = int(x1)
                y1 = int(y1)
                x2 = math.ceil(x2)
                y2 = math.ceil(y2)

                x1 = min(w, max(0, x1))
                x2 = min(w, max(0, x2))
                y1 = min(h, max(0, y1))
                y2 = min(h, max(0, y2))

                if y2 <= y1 or x2 <= x1:
                    continue
                r['rect'] = [x1, y1, x2, y2]
                all_idx.append(idx_image)
                yield label_key + '${}'.format(i), json_dump([r])
    if out_data == None:
        out_data = '{}_{}'.format(image_data,
                hash_sha1(label_tsv)[-5:])
    out_dataset = TSVDataset(out_data)
    if op.isdir(out_dataset._data_root):
        logging.info('{} exists'.format(out_dataset._data_root))
        return out_data
    out_dataset.write_data(gen_rows(), out_split, 'label')
    tsv_writer(((0, i) for i in all_idx),
            out_dataset.get_shuffle_file(out_split))
    write_to_file(dataset.get_data(image_split), out_dataset.get_data(out_split
        + 'X'))
    readme_file = op.join(out_dataset._data_root, 'readme.yaml')

    write_to_yaml_file(input_argument, readme_file)

    expand_nested_splitX(out_data, out_split)

    return out_data

def download_images_to_tsv(info, tsv_file, key='key'):
    from .common import url_to_bytes
    from .common import bytes_to_image
    def processor(i):
        bs = url_to_bytes(i['url'])
        if bs is None:
            return None
        try:
            im = bytes_to_image(bs)
        except:
            return None
        if im is None:
            return None
        return i[key], json_dump([]), base64.b64encode(bs)
    parallel_tsv_process(processor, info, tsv_file, num_process=16,
            num_jobs=1024)

def smart_resize_dataset(data, input_size, out_data, resize_type='cv2',
        save_type=None):
    if resize_type is None:
        resize_type = 'cv2'
    assert resize_type in ['cv2', 'pil']
    dataset = TSVDataset(data)
    out_dataset = TSVDataset(out_data)

    for split in ['train', 'test']:
        if not dataset.has(split):
            continue
        infos = []
        def gen_rows():
            if resize_type == 'cv2':
                for key, str_rects, str_im in dataset.iter_data(split,
                        progress=True):
                    im = img_from_base64(str_im)
                    im2, left, top, scale = smart_resize(im, input_size)
                    im2 = im2.astype(np.uint8)
                    infos.append((left, top, scale))
                    yield key, json_dump([]), encoded_from_img(im2,
                            save_type=save_type)
            else:
                assert resize_type == 'pil'
                for key, str_rects, str_im in dataset.iter_data(split,
                        progress=True):
                    im = pilimg_from_base64(str_im)
                    im2, left, top, scale = smart_resize(im, input_size)
                    infos.append((left, top, scale))
                    yield key, json_dump([]), encoded_from_img(im2,
                            save_type=save_type)
        if not out_dataset.has(split):
            out_dataset.write_data(gen_rows(), split)
        v = 0
        while dataset.has(split, 'label', version=v):
            def gen_label_rows():
                for i, (key, str_rects) in enumerate(dataset.iter_data(split, 'label',
                        version=v, progress=True)):
                    rects = json.loads(str_rects)
                    def transform_box(r, info):
                        left, top, scale = info
                        x1, y1, x2, y2 = r['rect']
                        out_x1 = x1 * scale + left
                        out_y1 = y1 * scale + top
                        out_x2 = x2 * scale + left
                        out_y2 = y2 * scale + top
                        r['rect'] = [out_x1, out_y1, out_x2, out_y2]
                        return r
                    rects = [transform_box(r, infos[i]) for r in rects]
                    yield key, json_dump(rects)
            if not out_dataset.has(split, 'label', version=v):
                out_dataset.write_data(gen_label_rows(), split, 'label', version=v)
            v += 1
    ensure_copy_file(dataset.get_labelmap_file(),
            out_dataset.get_labelmap_file())
    populate_dataset_hw(out_data)

def smart_resize_lefttop(im, target_size, lower=True):
    from .process_image import is_pil_image
    if is_pil_image(im):
        w, h = im.size
    else:
        h, w, c = im.shape
    alpha = target_size / np.sqrt(h * w)
    if lower:
        height2 = int(alpha * h)
        width2 = int(alpha * w)
    else:
        height2 = int(np.round(alpha * h))
        width2 = int(np.round(alpha * w))
    if h > w:
        if lower:
            input_height = height2 // 32 * 32
            input_width = (input_height * w) // h // 32 * 32
            im_scale = input_width / w
        else:
            input_height = (height2 + 31) // 32 * 32
            input_width = ((input_height * w + h - 1) // h +
                                        31) // 32 * 32
    else:
        if lower:
            input_width = (width2) // 32 * 32
            input_height = (input_width * h) // w // 32 * 32
            im_scale = input_height / h
        else:
            input_width = (width2 + 31) // 32 * 32
            input_height = ((input_width * h + w - 1) // w +
                                        31) // 32 * 32

    im_resized = cv2.resize(im, None, None, fx=im_scale, fy=im_scale,
                            interpolation=cv2.INTER_LINEAR)
    if is_pil_image(im_resized):
        new_w, new_h = im_resized.size
    else:
        new_h, new_w = im_resized.shape[0:2]
    left = 0
    right = input_width - new_w - left
    top = 0
    bottom = input_height - new_h - top
    from .process_image import copy_make_border
    im_squared = copy_make_border(im_resized, top=top,
            bottom=bottom, left=left, right=right)
    return im_squared, im_scale

def smart_resize(im, target_size):
    from .process_image import is_pil_image
    if is_pil_image(im):
        w, h = im.size
    else:
        h, w, c = im.shape
    alpha = target_size / np.sqrt(h * w)
    height2 = int(np.round(alpha * h))
    width2 = int(np.round(alpha * w))
    if h > w:
        input_height = (height2 + 31) // 32 * 32
        input_width = ((input_height * w + h - 1) // h +
                                    31) // 32 * 32
    else:
        input_width = (width2 + 31) // 32 * 32
        input_height = ((input_width * h + w - 1) // w +
                                    31) // 32 * 32

    if float(h) / input_height > float(w) / input_width:
        rescale_size = input_height
    else:
        rescale_size = input_width
    from .process_image import im_rescale
    im_resized, im_scale = im_rescale(im, rescale_size)
    if is_pil_image(im_resized):
        new_w, new_h = im_resized.size
    else:
        new_h, new_w = im_resized.shape[0:2]
    left = (input_width - new_w) // 2
    right = input_width - new_w - left
    top = (input_height - new_h) // 2
    bottom = input_height - new_h - top
    from .process_image import copy_make_border
    im_squared = copy_make_border(im_resized, top=top,
            bottom=bottom, left=left, right=right)
    return im_squared, left, top, im_scale

def kmeans(np_feature, k, seed=6):
    from sklearn.preprocessing import normalize
    from libKMCUDA import kmeans_cuda
    np_feature = normalize(np_feature, axis=1)
    init = 'kmeans++'
    while True:
        centroid, assignments = kmeans_cuda(np_feature,
                                            k,
                                            verbosity=1,
                                            seed=seed,
                                            device=1,
                                            init=init,
                                            metric='cos',
                                            )
        unique_assignment, cluster_count = np.unique(assignments, return_counts=True)
        if len(unique_assignment) == k:
            logging.info('reached non-empty')
            break
        num_new = k - len(unique_assignment)
        logging.info('adding {}'.format(num_new))
        new_from_cluster_idx = np.argsort(cluster_count)[-num_new:]
        cluster_idx = unique_assignment[new_from_cluster_idx]
        feat_idx = [np.where(assignments == i)[0] for i in cluster_idx]
        feat_idx = np.concatenate(feat_idx)
        np.random.seed(6)
        feat_idx = np.random.permutation(feat_idx)
        new_cluster = np_feature[feat_idx[:num_new]]
        new_cluster_idx = 0
        for c in range(len(centroid)):
            if np.isnan(centroid[c][0]):
                centroid[c] = new_cluster[new_cluster_idx]
                new_cluster_idx += 1
        assert new_cluster_idx == len(new_cluster)
        init = centroid
    return centroid, assignments

def kmeans_pred_to_dataX(data, split, k, feature_fname, out_data,
                         num_kmeans=1):
    out_dataset = TSVDataset(out_data)
    if op.isfile(out_dataset.get_labelmap_file()) and \
            op.isfile(out_dataset.get_data(split + 'X')):
        return out_data
    dataset = TSVDataset(data)
    iter_label = dataset.iter_data(split, 'label')
    iter_pred = tsv_reader(feature_fname)
    np_feature = None
    num_rows = dataset.num_rows(split)
    for i, (label_row, pred_row) in tqdm(enumerate(zip(iter_label, iter_pred))):
        assert label_row[0] == pred_row[0]
        f = json.loads(pred_row[-1])[0]['feature']
        if np_feature is None:
            np_feature = np.zeros((num_rows, len(f)), dtype=np.float32)
        np_feature[i] = f
    # there are some issues with multi-gpu. Thus setting device=1 which means to use
    # gpu 0
    all_centroid, all_assignment = [], []
    for i in range(num_kmeans):
        centroid, assignments = kmeans(np_feature, k, seed=i + 9)
        all_centroid.append(centroid)
        all_assignment.append(assignments)
    disk_assign = [int(i) for _, i in out_dataset.iter_data(split, 'label')]
    for a, da in zip(assignments, disk_assign):
        assert a == da
    for i, centroid in zip(range(num_kmeans), all_centroid):
        out_dataset.write_data(centroid, split, 'kmeans_center', version=i)
    write_to_file(dataset.get_data(split),
                  out_dataset.get_data(split + 'X'))
    tsv_writer(((0, i) for i in range(num_rows)),
               out_dataset.get_shuffle_file(split))
    if op.isfile(out_dataset.get_labelmap_file()):
        labelmap = out_dataset.load_labelmap()
        assert len(labelmap) == k
        for i, l in enumerate(labelmap):
            assert l == str(i)
    else:
        tsv_writer(((i,) for i in range(k)), out_dataset.get_labelmap_file())
    iter_label = dataset.iter_data(split, 'label')
    def gen_label():
        for row in zip(iter_label, *all_assignment):
            key = row[0][0]
            label = '_'.join(map(str, [int(a) for a in row[1:]]))
            yield key, json_dump([{'class': label}])
    tsv_writer(gen_label(), out_dataset.get_data(split, 'label'))
    tsv_writer([(feature_fname,)], out_dataset.get_data(split, 'feature_ptr'))
    return out_data

def create_compare_dataset(pred1, pred2, suffix1, suffix2, data, split, out_data):
    out_dataset = TSVDataset(out_data)
    dataset = TSVDataset(data)
    tsv_writer([(dataset.get_data(split),)], out_dataset.get_data(split + 'X'))
    tsv_writer(((0, i)  for i in range(dataset.num_rows(split)) for _ in
                range(2)),
               out_dataset.get_shuffle_file(split))
    iter_pred1 = tsv_reader(pred1)
    iter_pred2 = tsv_reader(pred2)
    iter_gt = dataset.iter_data(split, 'label')
    def gen_label_rows():
        for row_gt, row_pred1, row_pred2 in zip(iter_gt, iter_pred1, iter_pred2):
            assert row_gt[0] == row_pred1[0] == row_pred2[0]
            rects_pred1 = json.loads(row_pred1[1])
            rects_pred1 = [r for r in rects_pred1 if r['conf'] > 0.2]
            rects_pred2 = json.loads(row_pred2[1])
            rects_pred2 = [r for r in rects_pred2 if r['conf'] > 0.2]
            yield row_gt[0] + suffix1, json_dump(rects_pred1)
            yield row_gt[0] + suffix2, json_dump(rects_pred2)
    out_dataset.write_data(gen_label_rows(), split, 'label')
    expand_nested_splitX(out_data, split)
    populate_dataset_details(out_data)

def duplicate_balance_fg_classes(
    data, split, version,
    bkg_class, fg_ratio, fg_rel_class_ratio, out_data):
    # some recommendation: fg_ratio = 0.3
    # fg_rel_class_ratio: 0.1
    info = get_frame_info()
    populate_dataset_details(data)
    out_dataset = TSVDataset(out_data)
    if op.isfile(out_dataset.get_labelmap_file()):
        logging.info('{} exists'.format(out_dataset))
        return
    dataset = TSVDataset(data)

    # {label: list_of_idx}
    label_to_idx = dataset.load_inverted_label(split, version)
    # the number of images with unknown categories
    unknown_count = len(label_to_idx[bkg_class])
    # total number of images with product categories
    product_count = sum([len(idx) for l, idx in label_to_idx.items()
         if l != bkg_class])
    # current ratio
    curr_ratio = 1. * product_count / (product_count + unknown_count)
    # we need to duplicate product images by dup_factor times
    dup_factor = int(math.ceil(fg_ratio / curr_ratio))
    # duplicate operations by duplicating the index
    dup_label_to_idx = {l: idx * dup_factor for l, idx in label_to_idx.items()
                        if l != bkg_class}
    info['dup_factor'] = dup_factor
    # calculate the average number of images for each category
    avg_count = calc_mean([len(idx) for l, idx in dup_label_to_idx.items()])
    # based on the parameters, each category should have at_least_count images.
    at_least_count = int(avg_count * fg_rel_class_ratio)
    for l, idx in dup_label_to_idx.items():
        if len(idx) < at_least_count:
            info[l] = (len(idx), at_least_count)
    # duplicate the images by duplicating the index
    dup_label_to_idx = {l: idx
                        if len(idx) >= at_least_count else idx * int(math.ceil(1. * at_least_count / len(idx)))
                        for l, idx in dup_label_to_idx.items()
                        }
    # make the index as a list
    final_idx = [i for _, idxs in dup_label_to_idx.items() for i in idxs]
    final_idx.extend(label_to_idx[bkg_class])
    # random shuffling
    random.shuffle(final_idx)
    # save it
    tsv_writer(((0, i) for i in final_idx), out_dataset.get_shuffle_file(split))
    assert op.isfile(dataset.get_data(split))
    assert op.isfile(dataset.get_data(split, t='label', version=version))
    tsv_writer([(dataset.get_data(split),)], out_dataset.get_data(split + 'X'))
    tsv_writer([(dataset.get_data(split, t='label', version=version),)],
               out_dataset.get_data(split + 'X', t='label'))
    ensure_copy_file(dataset.get_labelmap_file(),
                     out_dataset.get_labelmap_file())
    out_dataset.write_data(info.items(), split, t='generate_info')

    populate_dataset_details(out_data)

def split_dataset_one(
    data, num_split, out_data,
    split_type, split, version
):
    dataset = TSVDataset(data)
    out_dataset = TSVDataset(out_data)
    num_rows = dataset.num_rows(split, t=split_type, version=version)
    each_size = (num_rows + num_split - 1) //  num_split
    shuffle = []
    t_to_tsvs = defaultdict(list)
    for i in range(num_split):
        start = i * each_size
        end = start + each_size
        end = min(end, num_rows)
        t = split_type
        logging.info('split = {}; i = {}; t = {}'.format(
            split, i, t))
        def gen_rows():
            for j, row in enumerate(dataset.iter_data(
                split, t=t,
                version=version,
                filter_idx=range(start,
                                 end),
                    progress=True)):
                if t is None:
                    shuffle.append((i, j))
                yield row
        ssplit = '{}_{}_{}'.format(split, i, num_split)
        if out_dataset.has(ssplit, t, version=version):
            iter_in_file = out_dataset.iter_data(ssplit, t, version=version)
            for in_file, curr in zip(iter_in_file, gen_rows()):
                assert in_file == curr
        else:
            out_dataset.safe_write_data(
                gen_rows(),
                split=ssplit, t=t,
                version=version)
        t_to_tsvs[str(t)].append(out_dataset.get_data(ssplit, t, version))
    if op.isfile(out_dataset.get_shuffle_file(split)):
        iter_in_file = tsv_reader(out_dataset.get_shuffle_file(split))
        for x, y in zip(iter_in_file, shuffle):
            assert x == y
    else:
        tsv_writer(shuffle, out_dataset.get_shuffle_file(split))
    t = split_type
    split_x = '{}X'.format(split)
    target_file = out_dataset.get_data(split_x, t=t, version=version)
    if op.isfile(target_file):
        assert '\n'.join(t_to_tsvs[str(t)]) == read_to_buffer(target_file).decode()
    else:
        write_to_file('\n'.join(t_to_tsvs[str(t)]), target_file)
    ensure_copy_file(dataset.get_labelmap_file(), out_dataset.get_labelmap_file())

def split_dataset(
    data, num_split, out_data,
    split_types=(None, 'label', 'hw', 'caption'),
    splits=('train', 'trainval', 'test'),
):
    dataset = TSVDataset(data)
    out_dataset = TSVDataset(out_data)
    for split in splits:
        num_rows = dataset.num_rows(split)
        each_size = (num_rows + num_split - 1) //  num_split
        shuffle = []
        t_to_tsvs = defaultdict(list)
        for i in range(num_split):
            start = i * each_size
            end = start + each_size
            end = min(end, num_rows)
            for t in split_types:
                logging.info('split = {}; i = {}; t = {}'.format(
                    split, i, t))
                def gen_rows():
                    for j, row in enumerate(dataset.iter_data(
                        split, t=t,
                        filter_idx=range(start,
                                         end),
                            progress=True)):
                        if t is None:
                            shuffle.append((i, j))
                        yield row
                ssplit = '{}_{}_{}'.format(split, i, num_split)
                if out_dataset.has(ssplit, t):
                    iter_in_file = out_dataset.iter_data(ssplit, t)
                    for in_file, curr in zip(iter_in_file, gen_rows()):
                        assert in_file == curr
                else:
                    out_dataset.write_data(gen_rows(), split=ssplit, t=t)
                t_to_tsvs[str(t)].append(out_dataset.get_data(ssplit, t))
        if op.isfile(out_dataset.get_shuffle_file(split)):
            iter_in_file = tsv_reader(out_dataset.get_shuffle_file(split))
            for x, y in zip(iter_in_file, shuffle):
                assert x == y
        else:
            tsv_writer(shuffle, out_dataset.get_shuffle_file(split))
        for t in split_types:
            split_x = '{}X'.format(split)
            target_file = out_dataset.get_data(split_x, t=t)
            if op.isfile(target_file):
                assert '\n'.join(t_to_tsvs[str(t)]) == read_to_buffer(target_file).decode()
            else:
                write_to_file('\n'.join(t_to_tsvs[str(t)]), target_file)
    ensure_copy_file(dataset.get_labelmap_file(), out_dataset.get_labelmap_file())

def merge_dataset_parallel(*args, **kwargs):
    merge_dataset(*args, **kwargs)

#@deprecated('use merge_dataset_parallel')
#def merge_dataset(source_infos, target_info, ps):
    #target_dataset = TSVDataset(target_info['data'])
    #for p in ps:
        #tsv_list = []
        #for source in source_infos:
            #dataset = TSVDataset(source['data'])
            #if 'version_info' in source and p['type'] in source['version_info']:
                #version = source['version_info'][p['type']]
            #elif 'version' in p:
                #version = p['version']
            #else:
                #version = None
            #tsv_file = dataset.get_data(source['split'], t=p['type'],
                                #version=version)
            #if op.isfile(tsv_file):
                #tsv_list.append(tsv_file)
            #else:
                #tsv_x = dataset.get_data(source['split'] + 'X', t=p['type'],
                                         #version=version)
                #curr_list = load_list_file(tsv_x)
                #curr_shuffle = dataset.get_shuffle_file(source['split'])
                #logging.info('loading {}'.format(curr_shuffle))
                #tsv_list.extend(curr_list)
        #out_tsvx = target_dataset.get_data(
            #target_info['split'] + 'X', p['type'],
            #version=target_info.get('version'))
        #if not op.isfile(out_tsvx):
            #write_to_file('\n'.join(tsv_list), out_tsvx)
        #else:
            #es = load_list_file(out_tsvx)
            #assert len(tsv_list) == len(es)
            #for e, c in zip(es, tsv_list):
                #assert e == c, (e, c)

    #for p in ps:
        #if p.get('type') is not None:
            #continue
        #tsv_list = []
        #def gen_shuffle():
            #for source in source_infos:
                #dataset = TSVDataset(source['data'])
                #if 'version_info' in source and p['type'] in source['version_info']:
                    #version = source['version_info'][p['type']]
                #elif 'version' in p:
                    #version = p['version']
                #else:
                    #version = None
                #tsv_file = dataset.get_data(source['split'], t=p['type'],
                                    #version=version)
                #tsv_offset = len(tsv_list)
                #if op.isfile(tsv_file):
                    #num = dataset.num_rows(source['split'])
                    #for i in range(num):
                        #yield tsv_offset, i
                    #tsv_list.append(tsv_file)
                #else:
                    #tsv_x = dataset.get_data(source['split'] + 'X', t=p['type'],
                                             #version=version)
                    #curr_list = load_list_file(tsv_x)
                    #curr_shuffle = dataset.get_shuffle_file(source['split'])
                    #logging.info('loading {}'.format(curr_shuffle))
                    #for i, j in tqdm(tsv_reader(curr_shuffle)):
                        #yield int(i) + tsv_offset, j
                    #tsv_list.extend(curr_list)
        #shuffle_f = target_dataset.get_shuffle_file(target_info['split'])
        #if not op.isfile(shuffle_f):
            #tsv_writer(gen_shuffle(), shuffle_f)
        #else:
            #for in_file, curr in zip(tsv_reader(shuffle_f), gen_shuffle()):
                #assert in_file[0] == str(curr[0])
                #assert in_file[1] == str(curr[1])



#@deprecated('try to switch to merge_dataset2, tested once')
#def merge_dataset(source_infos, target_info, ps):
    #target_dataset = TSVDataset(target_info['data'])
    #for p in ps:
        #tsv_list = []
        #shuffle = []
        #for source in source_infos:
            #dataset = TSVDataset(source['data'])
            #if 'version_info' in source and p['type'] in source['version_info']:
                #version = source['version_info'][p['type']]
            #elif 'version' in p:
                #version = p['version']
            #else:
                #version = None
            #tsv_file = dataset.get_data(source['split'], t=p['type'],
                                #version=version)
            #tsv_offset = len(tsv_list)
            #if op.isfile(tsv_file):
                #num = dataset.num_rows(source['split'])
                #shuffle.extend([(tsv_offset, i) for i in range(num)])
                #tsv_list.append(tsv_file)
            #else:
                #tsv_x = dataset.get_data(source['split'] + 'X', t=p['type'],
                                         #version=version)
                #curr_list = load_list_file(tsv_x)
                #curr_shuffle = dataset.get_shuffle_file(source['split'])
                #logging.info('loading {}'.format(curr_shuffle))
                #shuffle.extend([(int(i) + tsv_offset, j) for i, j in
                                #tqdm(tsv_reader(curr_shuffle))])
                #tsv_list.extend(curr_list)
        #out_tsvx = target_dataset.get_data(
            #target_info['split'] + 'X', p['type'],
            #version=target_info.get('version'))
        #if not op.isfile(out_tsvx):
            #write_to_file('\n'.join(tsv_list), out_tsvx)
        #else:
            #es = load_list_file(out_tsvx)
            #assert len(tsv_list) == len(es)
            #for e, c in zip(es, tsv_list):
                #assert e == c, (e, c)
        #if p.get('type') is None:
            #shuffle_f = target_dataset.get_shuffle_file(target_info['split'])
            #if not op.isfile(shuffle_f):
                #tsv_writer(shuffle, shuffle_f)
            #else:
                #for in_file, curr in zip(tsv_reader(shuffle_f), shuffle):
                    #assert in_file[0] == str(curr[0])
                    #assert in_file[1] == str(curr[1])

    ## labelmap
    ##target_labelmap = target_dataset.get_labelmap_file()
    ##lss = [TSVDataset(source['data']).load_labelmap() for source in source_infos]
    ##labelmap = list(set([l for ls in lss for l in ls]))
    ##labelmap = sorted(labelmap)
    ##if not op.isfile(target_labelmap):
        ##write_to_file('\n'.join(labelmap), target_labelmap)
    ##else:
        ##existing = load_list_file(target_labelmap)
        ##assert len(existing) == len(labelmap)
        ##for e, l in zip(existing, labelmap):
            ##assert e == l

def merge_dataset_row_processor(row, offset):
    i, j = row
    return int(i) + offset, j

def merge_dataset(source_infos, target_info, ps):
    target_dataset = TSVDataset(target_info['data'])
    for p in ps:
        tsv_list = []
        for source in source_infos:
            dataset = TSVDataset(source['data'])
            if 'version_info' in source and p['type'] in source['version_info']:
                version = source['version_info'][p['type']]
            elif 'version' in p:
                version = p['version']
            else:
                version = None
            tsv_file = dataset.get_data(source['split'], t=p['type'],
                                version=version)
            if File.isfile(tsv_file):
                tsv_list.append(tsv_file)
            else:
                tsv_x = dataset.get_data(source['split'] + 'X', t=p['type'],
                                         version=version)
                curr_list = load_list_file(tsv_x)
                curr_shuffle = dataset.get_shuffle_file(source['split'])
                logging.info('loading {}'.format(curr_shuffle))
                tsv_list.extend(curr_list)
        out_tsvx = target_dataset.get_data(
            target_info['split'] + 'X', p['type'],
            version=target_info.get('version'))
        if not File.isfile(out_tsvx):
            tsv_writer(((_t, ) for _t in tsv_list), out_tsvx)
            #write_to_file('\n'.join(tsv_list), out_tsvx)
        else:
            es = load_list_file(out_tsvx)
            assert len(tsv_list) == len(es)
            for e, c in zip(es, tsv_list):
                assert e == c, (e, c)

    all_num_tsv = []
    for source in source_infos:
        dataset = TSVDataset(source['data'])
        curr_shuffle = dataset.get_shuffle_file(source['split'])
        if File.isfile(curr_shuffle):
            tsv_x = dataset.get_data(source['split'] + 'X', t=p['type'],
                                     version=version)
            curr_list = load_list_file(tsv_x)
            all_num_tsv.append(len(curr_list))
        else:
            assert File.isfile(dataset.get_data(source['split']))
            all_num_tsv.append(1)

    s = 0
    tsv_offset = [0]
    for n in all_num_tsv[:-1]:
        s += n
        tsv_offset.append(s)

    all_shuffle = []
    target_shuffle = target_dataset.get_shuffle_file(target_info['split'])
    folder = get_tmp_folder()
    for idxsource, source in enumerate(source_infos):
        dataset = TSVDataset(source['data'])
        out_shuffle = op.join(folder,
                              '{}'.format(target_shuffle) +
                              '.{}.{}.tsv'.format(idxsource, len(source_infos)),
                              )
        src_shuffle_tsv = dataset.get_shuffle_file(source['split'])
        parallel_tsv_process(lambda row: merge_dataset_row_processor(row, tsv_offset[idxsource]),
                             src_shuffle_tsv,
                             out_shuffle,
                             64,
                             )
        all_shuffle.append(out_shuffle)
    concat_tsv_files(all_shuffle,
                     target_dataset.get_shuffle_file(target_info['split']),
                     gen_lineidx=False
                     )
    delete_tsv_files(all_shuffle)

def tuple_merge_pred_label_to_gt(x):
    pred, data, split, in_version, out_version = x
    merge_pred_label_to_gt(pred, data, split, in_version, out_version)

def merge_pred_label_to_gt(pred, data, split, in_version, out_version):
    dataset = TSVDataset(data)
    def gen_rows():
        iter_pred = tsv_reader(pred)
        iter_gt = dataset.iter_data(split, 'label', version=in_version)
        for row_pred, row_gt in tqdm(zip(iter_pred, iter_gt)):
            assert row_pred[0] == row_gt[0]
            pred_rects = json.loads(row_pred[1])
            gt = json.loads(row_gt[1])
            for p in pred_rects:
                if 'zlib_feature' in p:
                    del p['zlib_feature']
                if 'attr_labels' in p:
                    del p['attr_labels']
                if 'attr_scores' in p:
                    del p['attr_scores']
            gt.extend(pred_rects)
            yield row_pred[0], json_dump(gt)
    #assert not dataset.has(split, 'label', version=out_version)
    dataset.write_data(gen_rows(), split, 'label', version=out_version)

def generate_caption_linelist(data, split, version=None):
    dataset = TSVDataset(data)
    if not dataset.has(split, 'caption', version=None):
        logging.info('caption does not exist')
        return
    iter_caption = dataset.iter_data(split, 'caption', version=version)
    iter_shuffle = tsv_reader(dataset.get_shuffle_file(split))

    def gen_rows():
        for (idx_source, idx_row), (key, s) in tqdm(zip(iter_shuffle,
                                                        iter_caption)):
            cs = json.loads(s)
            for idx_caption in range(len(cs)):
                yield idx_source, idx_row, idx_caption
    if not dataset.has(split, 'caption_linelist', version=version):
        dataset.safe_write_data(gen_rows(), split, 'caption_linelist',
                           version=version)

def generate_key_idximage_idxcaption_from_num(data, split, version):
    dataset = TSVDataset(data)
    if not dataset.has(split, 'caption', version):
        logging.info('ignore to generate as caption file does not exist')
        return
    iter_caption = dataset.iter_data(split, 'num_caption', version=version)

    def gen_rows():
        for idx_img, (key, n) in tqdm(enumerate(iter_caption)):
            n = int(n)
            for i in range(n):
                yield key, idx_img, i

    t = 'key_idximage_idxcaption'
    if not dataset.has(split, t, version=version):
        dataset.write_data(gen_rows(), split, t, version=version)

def derive_key_idximage_idxcaption_from_merge_dataset(source_infos, target_info):
    for s in source_infos:
        assert s.get('version_info') is None
        generate_key_idximage_idxcaption(s['data'], s['split'])

    split = target_info['split']
    def gen_rows():
        offset = 0
        for s in source_infos:
            dataset = TSVDataset(s['data'])
            for key, idximage, idxcaption in dataset.iter_data(s['split'],
                                                               t='key_idximage_idxcaption'):
                yield key, int(idximage) + offset, idxcaption
            added = False
            for t in [None, 'caption', 'hw']:
                if dataset.has(s['split'], t):
                    offset += dataset.num_rows(s['split'])
                    added = True
                    break
            assert added
    data = target_info['data']
    out = TSVDataset(data)
    if not out.has(split, 'key_idximage_idxcaption'):
        out.write_data(gen_rows(), split, t='key_idximage_idxcaption')

def derive_key_idximage_idxcaption_from_merge_dataset_parallel_multi_source(source_infos, target_info):
    for s in source_infos:
        assert s.get('version_info') is None
        generate_key_idximage_idxcaption(s['data'], s['split'])

    out = TSVDataset(target_info['data'])
    if out.has(target_info['split'], 'key_idximage_idxcaption'):
        return

    all_size = [len(TSVSplitProperty(s['data'], s['split'], 'caption')) for s in
                source_infos]
    cum_size = 0
    all_offset = [cum_size]
    for s in all_size[:-1]:
        cum_size += s
        all_offset.append(cum_size)

    in_tsvs = list(zip(all_offset, source_infos))
    from .cloud_storage import create_cloud_fuse
    c = create_cloud_fuse()
    tsvs = [TSVDataset(s['data']).get_data(s['split'], 'key_idximage_idxcaption') for s
            in source_infos]
    c.ensure_cache(tsvs)
    c.ensure_cache([get_tsv_associates(t)[0] for t in tsvs])

    def row_processor(row):
        offset, s = row
        for key, idximage, idxcaption in TSVSplitProperty(s['data'],
                                                          s['split'],
                                                          'key_idximage_idxcaption'):
            yield key, int(idximage) + offset, idxcaption
    out_tsv = out.get_data(target_info['split'], 'key_idximage_idxcaption')
    parallel_tsv_process(
        row_processor,
        in_tsvs,
        out_tsv,
        64
    )

def derive_key_idximage_idxcaption_from_merge_dataset_parallel_each(source_infos, target_info):
    for s in source_infos:
        assert s.get('version_info') is None
        generate_key_idximage_idxcaption(s['data'], s['split'])

    out = TSVDataset(target_info['data'])
    if out.has(target_info['split'], 'key_idximage_idxcaption'):
        return

    all_size = [len(TSVSplitProperty(s['data'], s['split'], 'caption')) for s in
                source_infos]
    cum_size = 0
    all_offset = [cum_size]
    for s in all_size[:-1]:
        cum_size += s
        all_offset.append(cum_size)

    from .cloud_storage import create_cloud_fuse
    c = create_cloud_fuse()
    tsvs = [TSVDataset(s['data']).get_data(s['split'], 'key_idximage_idxcaption') for s
            in source_infos]
    c.ensure_cache(tsvs)
    c.ensure_cache([get_tsv_associates(t)[0] for t in tsvs])

    tmp_folder = get_tmp_folder()
    out_tsv = out.get_data(target_info['split'], 'key_idximage_idxcaption')
    all_tmp = []
    for offset, s in zip(all_offset, source_infos):
        curr_in_tsv = TSVDataset(s['data']).get_data(s['split'], 'key_idximage_idxcaption')
        curr_out_tsv = op.join(tmp_folder, out_tsv + f'.{offset}.tsv')

        def row_processor(row):
            key, idximage, idxcaption = row
            return key, int(idximage) + offset, idxcaption
        parallel_tsv_process(
            row_processor,
            curr_in_tsv,
            curr_out_tsv,
            64
        )
        all_tmp.append(curr_out_tsv)
    concat_tsv_files(all_tmp, out_tsv, gen_lineidx=False)
    delete_tsv_files(all_tmp)

def derive_key_idximage_idxcaption_idxsource_from_merge_dataset_parallel_multi_source(source_infos, target_info):
    for s in source_infos:
        assert s.get('version_info') is None
        generate_key_idximage_idxcaption_idxsource(s['data'], s['split'])

    out = TSVDataset(target_info['data'])
    if out.has(target_info['split'], 'key_idximage_idxcaption_idxsource'):
        return

    all_size = [len(TSVSplitProperty(s['data'], s['split'], 'caption')) for s in
                source_infos]
    cum_size = 0
    all_img_offset = [cum_size]
    for s in all_size[:-1]:
        cum_size += s
        all_img_offset.append(cum_size)

    all_source_offset = [0]
    cum = 0
    for s in source_infos[:-1]:
        curr_dataset = TSVDataset(s['data'])
        if File.isfile(curr_dataset.get_data(s['split'])):
            num = 1
        else:
            num = len(load_list_file(curr_dataset.get_data(s['split'] + 'X')))
        cum += num
        all_source_offset.append(cum)

    in_tsvs = list(zip(all_img_offset, all_source_offset, source_infos))
    from .cloud_storage import create_cloud_fuse
    c = create_cloud_fuse()
    tsvs = [TSVDataset(s['data']).get_data(s['split'],
                                           'key_idximage_idxcaption_idxsource') for s
            in source_infos]
    c.ensure_cache(tsvs)
    c.ensure_cache([get_tsv_associates(t)[0] for t in tsvs])

    def row_processor(row):
        img_offset, source_offset, s = row
        for idxsource, in TSVSplitProperty(s['data'], s['split'], 'key_idximage_idxcaption_idxsource'):
            yield int(idxsource) + source_offset,
    out_tsv = out.get_data(target_info['split'], 'key_idximage_idxcaption_idxsource')
    parallel_tsv_process(row_processor, in_tsvs, out_tsv, 64)

def derive_key_idximage_idxcaption_idxsource_from_merge_dataset_parallel_each_source(source_infos, target_info):
    for s in source_infos:
        assert s.get('version_info') is None
        generate_key_idximage_idxcaption_idxsource(s['data'], s['split'])

    out = TSVDataset(target_info['data'])
    if out.has(target_info['split'], 'key_idximage_idxcaption_idxsource'):
        return

    all_size = [len(TSVSplitProperty(s['data'], s['split'], 'caption')) for s in
                source_infos]
    cum_size = 0
    all_img_offset = [cum_size]
    for s in all_size[:-1]:
        cum_size += s
        all_img_offset.append(cum_size)

    all_source_offset = [0]
    cum = 0
    for s in source_infos[:-1]:
        curr_dataset = TSVDataset(s['data'])
        if File.isfile(curr_dataset.get_data(s['split'])):
            num = 1
        else:
            num = len(load_list_file(curr_dataset.get_data(s['split'] + 'X')))
        cum += num
        all_source_offset.append(cum)

    c = create_cloud_fuse()
    tsvs = [TSVDataset(s['data']).get_data(s['split'],
                                           'key_idximage_idxcaption_idxsource') for s
            in source_infos]
    c.ensure_cache(tsvs)
    c.ensure_cache([get_tsv_associates(t)[0] for t in tsvs])

    out_tsv = out.get_data(target_info['split'], 'key_idximage_idxcaption_idxsource')

    tmp_folder = get_tmp_folder()
    all_tmp = []
    for img_offset, source_offset, s in zip(all_img_offset, all_source_offset, source_infos):
        def row_processor(row):
            idxsource, = row
            return int(idxsource) + source_offset,
        in_tsv = TSVDataset(s['data']).get_data(s['split'], 'key_idximage_idxcaption_idxsource')
        curr_out = '{}/{}'.format(tmp_folder, out_tsv + '.{}.tsv'.format(img_offset))
        parallel_tsv_process(row_processor, in_tsv, curr_out, 64)
        all_tmp.append(curr_out)
    concat_tsv_files(all_tmp, out_tsv, gen_lineidx=False)
    delete_tsv_files(all_tmp)

def assert_dataset_each_length_same(data):
    dataset = TSVDataset(data)
    img_tsvs = load_list_file(dataset.get_data('trainX'))
    cap_tsvs = load_list_file(dataset.get_data('trainX', 'caption'))
    assert len(img_tsvs) == len(cap_tsvs)
    #c = create_cloud_fuse()
    #c.ensure_cache(fs)
    for i, c in tqdm(zip(img_tsvs, cap_tsvs)):
        assert len(TSVFile(i)) == len(TSVFile(c)), (i, c)


def generate_key_idximage_idxcaption(data, split, version=None):
    dataset = TSVDataset(data)
    if not dataset.has(split, 'caption', version):
        logging.info('ignore to generate as caption file does not exist')
        return
    #from qd.data_layer.loader import iter_data_loader
    #iter_caption = iter_data_loader(
        #data, split, 'caption', version=version,
        #transform=lambda x: (x[0], json.loads(x[1])),
        #num_workers=8,
    #)
    info = defaultdict(int)
    def row_processor(row, idx_img):
        key, s = row
        try:
            cs = json.loads(s)
        except:
            logging.info('{}/{}'.format(s, idx_img))
            raise
        for i in range(len(cs)):
            if ('caption' in cs[i] and isinstance(cs[i]['caption'], str)) or ('caption' not in cs[i]):
                yield (key, idx_img, i)
                info['valid'] += 1
            else:
                info['non_str_caption'] += 1

    t = 'key_idximage_idxcaption'
    if not dataset.has(split, t, version=version):
        tsv = TSVSplitProperty(data=data, split=split, t='caption', version=version)
        fs = dataset.get_data(split + 'X', 'caption', version=version)
        if File.isfile(fs):
            tsvs = load_list_file(fs)
            File.prepare(tsvs)
            File.prepare([get_tsv_associates(t)[0] for t in tsvs])
        parallel_tsv_process(
            row_processor,
            tsv,
            dataset.get_data(split, t, version=version),
            64,
            with_row_idx=True
        )
    logging.info(info)

def generate_idximage_idxcaption(
        data, split, version=None):
    dataset = TSVDataset(data)
    if not dataset.has(split, 'caption', version):
        logging.info('ignore to generate as caption file does not exist')
        return
    info = defaultdict(int)
    def row_processor(row, idx_img):
        key, s = row
        cs = json.loads(s)
        for i in range(len(cs)):
            if ('caption' in cs[i] and isinstance(cs[i]['caption'], str)) or ('caption' not in cs[i]):
                yield idx_img, i
                info['valid'] += 1
            else:
                info['non_str_caption'] += 1

    t = 'idximage_idxcaption'
    if not dataset.has(split, t, version=version):
        tsv = TSVSplitProperty(data=data, split=split, t='caption', version=version)
        c = create_cloud_fuse()
        fs = dataset.get_data(split + 'X', 'caption', version=version)
        if File.isfile(fs):
            tsvs = load_list_file(fs)
            c.ensure_cache(tsvs)
            c.ensure_cache([get_tsv_associates(t)[0] for t in tsvs])
        parallel_tsv_process(
            row_processor,
            tsv,
            dataset.get_data(split, t, version=version),
            64,
            with_row_idx=True
        )
    logging.info(info)

@deprecated('use generate_idximage_idxcaption')
def generate_idximage_idxcaption_parallel(*args, **kwargs):
    generate_idximage_idxcaption(*args, **kwargs)

#def generate_key_idximage_idxcaption(data, split, version=None):
    #dataset = TSVDataset(data)
    #if not dataset.has(split, 'caption', version):
        #logging.info('ignore to generate as caption file does not exist')
        #return
    ##from qd.data_layer.loader import iter_data_loader
    ##iter_caption = iter_data_loader(
        ##data, split, 'caption', version=version,
        ##transform=lambda x: (x[0], json.loads(x[1])),
        ##num_workers=8,
    ##)
    #info = defaultdict(int)
    #def gen_rows():
        ##iter_caption = dataset.iter_data(split, 'caption', version=version)
        ##total = dataset.num_rows(split, 'caption')
        #iter_caption = TSVSplitProperty(data=data, split=split, t='caption', version=version)
        ##for idx_img, (key, s) in tqdm(enumerate(iter_caption), total=total):
        #for idx_img, (key, s) in tqdm(enumerate(iter_caption)):
            #cs = json.loads(s)
            #for i in range(len(cs)):
                #if ('caption' in cs[i] and isinstance(cs[i]['caption'], str)) or ('caption' not in cs[i]):
                    #yield key, idx_img, i
                    #info['valid'] += 1
                #else:
                    #info['non_str_caption'] += 1

    #t = 'key_idximage_idxcaption'
    #if not dataset.has(split, t, version=version):
        #dataset.write_data(gen_rows(), split, t, version=version)
    #logging.info(info)

def generate_idximage_idxcaption_from_with_key(data, split, version=None):
    dataset = TSVDataset(data)
    def gen_rows():
        #iter_caption = dataset.iter_data(split, 'caption', version=version)
        #total = dataset.num_rows(split, 'caption')
        iter_old = TSVSplitProperty(data=data, split=split, t='key_idximage_idxcaption', version=version)
        #for idx_img, (key, s) in tqdm(enumerate(iter_caption), total=total):
        for key, idximage, idxcaption in tqdm(iter_old):
            yield idximage, idxcaption

    t = 'idximage_idxcaption'
    if not dataset.has(split, t, version=version):
        dataset.write_data(gen_rows(), split, t, version=version)


#def generate_idximage_idxcaption(data, split, version=None):
    #dataset = TSVDataset(data)
    #if not dataset.has(split, 'caption', version):
        #logging.info('ignore to generate as caption file does not exist')
        #return
    ##from qd.data_layer.loader import iter_data_loader
    ##iter_caption = iter_data_loader(
        ##data, split, 'caption', version=version,
        ##transform=lambda x: (x[0], json.loads(x[1])),
        ##num_workers=8,
    ##)
    #info = defaultdict(int)
    #def gen_rows():
        ##iter_caption = dataset.iter_data(split, 'caption', version=version)
        ##total = dataset.num_rows(split, 'caption')
        #iter_caption = TSVSplitProperty(data=data, split=split, t='caption', version=version)
        ##for idx_img, (key, s) in tqdm(enumerate(iter_caption), total=total):
        #for idx_img, (key, s) in tqdm(enumerate(iter_caption)):
            #cs = json.loads(s)
            #for i in range(len(cs)):
                #if ('caption' in cs[i] and isinstance(cs[i]['caption'], str)) or ('caption' not in cs[i]):
                    #yield idx_img, i
                    #info['valid'] += 1
                #else:
                    #info['non_str_caption'] += 1

    #t = 'idximage_idxcaption'
    #if not dataset.has(split, t, version=version):
        #dataset.write_data(gen_rows(), split, t, version=version)
    #logging.info(info)

def generate_num_caption(data, split, version=None):
    dataset = TSVDataset(data)
    if not dataset.has(split, 'caption', version):
        logging.info('ignore to generate as caption file does not exist')
        return
    iter_caption = dataset.iter_data(split, 'caption', version=version)

    def gen_rows():
        for (key, s) in tqdm(iter_caption):
            cs = json.loads(s)
            yield key, len(cs)

    if not dataset.has(split, 'num_caption', version=version):
        dataset.write_data(gen_rows(), split, 'num_caption', version=version)

def create_n_class_label(data_sources, out_data, out_split):
    frame_info = get_frame_info()
    out_dataset = TSVDataset(out_data)
    if out_dataset.has(out_split, 'label'):
        logging.info('skip for {}'.format(pformat(frame_info)))
        return
    all_ds = []
    for ds in data_sources:
        expand = ds.get('expand')
        if expand:
            curr_dataset = TSVDataset(ds['data'])
            data_splits = curr_dataset.load_composite_source_data_split(
                ds['split'])
            for d, s in data_splits:
                all_ds.append({'data': d, 'split': s, 'version': ds['version']})
        else:
            # in expand, we only support data/split/version. If we add other property, we should also update this code
            assert len(ds) == 3
            assert all(k in ds for k in ['data', 'split', 'version'])
            all_ds.append(ds)
    data_sources = all_ds
    # with expand, data_sources will be different. We save this information
    frame_info['data_sources'] = data_sources

    def gen_rows():
        for idx_source, data_source in enumerate(data_sources):
            curr_dataset = TSVDataset(data_source['data'])
            iter_label = curr_dataset.iter_data(
                data_source['split'],
                'label',
                version=data_source['version'],
            )
            total = curr_dataset.num_rows(data_source['split'])
            for i, (key, str_rects) in tqdm(enumerate(iter_label), total=total):
                rects = json.loads(str_rects)
                if isinstance(rects, int):
                    rects = [{'class': str(rects)}]
                if len(rects) == 0:
                    continue
                for r in rects:
                    if '$' not in r['class']:
                        if not r['class'].startswith('-'):
                            r['class'] = '$'.join([data_source['data'], r['class']])
                        else:
                            r['class'] = '$'.join([data_source['data'], r['class'][1:]])
                            r['class'] = '-{}'.format(r['class'])
                yield (key, json_dump(rects)), (idx_source, i)
    def gen_info():
        yield 'create_n_classs_label',
        ps = dict_get_all_path(frame_info)
        for p in ps:
            v = dict_get_path_value(frame_info, p)
            yield p, v

    label_file = out_dataset.get_data(out_split, 'label')
    shuffle_file = out_dataset.get_shuffle_file(out_split)
    if not op.isfile(label_file):
        tsv_writers(gen_rows(), (label_file, shuffle_file))
        gen_tsv = out_dataset.get_gen_info_data(out_split, 'label')
        tsv_writer(gen_info(), gen_tsv)
    labelmap = []
    #domain_info = []
    for ds_data in set(d['data'] for d in data_sources):
        curr_labelmap = TSVDataset(ds_data).load_labelmap()
        labelmap.extend(['{}${}'.format(
            ds_data, l) for l in curr_labelmap])
        #domain_info.append({'data': ds_data, 'labelmap': curr_labelmap})
    write_to_file('\n'.join(set(labelmap)), out_dataset.get_labelmap_file())
    #write_to_yaml_file(domain_info, out_dataset.get_file('domainmap.yaml'))
    image_tsvs = [TSVDataset(source['data']).get_data(source['split']) for source in
                  data_sources]
    write_to_file('\n'.join(image_tsvs), out_dataset.get_data(out_split + 'X'))
    expand_nested_splitX(out_data, out_split)

def merge_prediction_to_data(
    data, split, in_version, predict_file, conf_th, out_version,
    merge_colocated_iou=0.8,
):
    frame_info = get_frame_info()
    dataset = TSVDataset(data)
    assert dataset.has(split, 'label', version=in_version), (
        dataset, split, in_version
    )
    if dataset.has(split, 'label', version=out_version):
        logging.info('out version esists {}'.format(out_version))
        return
    iter_gt = dataset.iter_data(split, 'label', version=in_version)
    info = defaultdict(int)
    def gen_rows():
        debug = False
        iter_pred = tsv_reader(predict_file)
        for i, (row_gt, row_pred) in tqdm(enumerate(
            zip(iter_gt, iter_pred))):
            key = row_gt[0]
            assert row_pred[0] == key
            gt = json.loads(row_gt[1])
            pred = json.loads(row_pred[1])
            pred = [p for p in pred if p['conf'] >= conf_th]
            if debug:
                origin_gt = copy.deepcopy(gt)
                extra = []
            info['num_label_pre'] += len(gt)
            for g in gt:
                if 'location_id' not in g:
                    g['location_id'] = hash_sha1(g['rect'])
            for p in pred:
                if 'location_id' not in p:
                    p['location_id'] = hash_sha1(p['rect'])
            for p in pred:
                idx, iou = find_best_matched_rect_idx(p, gt, check_class=False)
                if iou >= merge_colocated_iou:
                    p['rect'] = gt[idx]['rect']
                    p['location_id'] = gt[idx]['location_id']
                    info['merge_colocated'] += 1
                p['predict_file'] = predict_file
                info['added'] += 1
                if debug:
                    extra.append(p)
                gt.append(p)
            if debug and len(extra) > 0:
                logging.info('debugging')
                im = img_from_base64(dataset.seek_by_idx(i, split='train')[-1])
                im_gt = im.copy()
                draw_rects(origin_gt, im_gt)
                im_extra = im.copy()
                draw_rects(extra, im_extra)
                save_image(np.concatenate((im_gt, im_extra)),
                           '/mnt/gpu02_raid/jianfw/work/tmp9/{}.jpg'.format(key))
            info['num_label_after'] += len(gt)
            yield key, json_dump(gt)
    def gen_info():
        for k, v in frame_info.items():
            yield k, v
        for k, v in info.items():
            yield k, v
    dataset.write_data(gen_rows(), split, 'label', version=out_version,
                       generate_info=gen_info())
    logging.info(pformat(info))

def create_1_class_label(data_sources, out_data, out_split,
                         cls_name):
    frame_info = get_frame_info()
    out_dataset = TSVDataset(out_data)
    if out_dataset.has(out_split, 'label'):
        logging.info('skip for {}'.format(pformat(frame_info)))
        return
    def gen_rows():
        for idx_source, data_source in enumerate(data_sources):
            curr_dataset = TSVDataset(data_source['data'])
            iter_label = curr_dataset.iter_data(
                data_source['split'],
                'label',
                version=data_source['version'],
            )
            for i, (key, str_rects) in tqdm(enumerate(iter_label)):
                rects = json.loads(str_rects)
                if len(rects) == 0:
                    continue
                for r in rects:
                    assert 'from_class' not in r
                    r['from_class'] = r['class']
                    r['class'] = 'object'
                yield (key, json_dump(rects)), (idx_source, i)
    def gen_info():
        yield 'create_1_classs_label',
        for k, v in frame_info.items():
            yield k, v

    label_file = out_dataset.get_data(out_split, 'label')
    shuffle_file = out_dataset.get_shuffle_file(out_split)
    assert not op.isfile(label_file)
    tsv_writers(gen_rows(), (label_file, shuffle_file))
    gen_tsv = out_dataset.get_gen_info_data(out_split, 'label')
    tsv_writer(gen_info(), gen_tsv)
    write_to_file(cls_name, out_dataset.get_labelmap_file())

    image_tsvs = [TSVDataset(source['data']).get_data(source['split']) for source in
                  data_sources]
    write_to_file('\n'.join(image_tsvs), out_dataset.get_data(out_split + 'X'))
    expand_nested_splitX(out_data, out_split)

def open_each_tsv(data, split, hold=False):
    dataset = TSVDataset(data)
    files = load_list_file(dataset.get_data(split + 'X'))
    all_tsv = []
    for f in files:
        logging.info('open and read {}'.format(f))
        tsv = TSVFile(f)
        tsv[0]
        if hold:
            all_tsv.append(tsv)
        else:
            tsv.close()
        from .common import print_opened_files
        print_opened_files()

def iter_caption_to_json(iter_caption, json_file):
    # save gt caption to json format so thet we can call the api
    key_captions = [(key, json.loads(p)) for key, p in iter_caption]

    info = {
        'info': 'dummy',
        'licenses': 'dummy',
        'type': 'captions',
    }
    info['images'] = [{'file_name': k, 'id': k} for k, _ in key_captions]
    n = 0
    annotations = []
    for k, cs in key_captions:
        for c in cs:
            annotations.append({
                'image_id': k,
                'caption': c['caption'],
                'id': n
            })
            n += 1
    info['annotations'] = annotations
    from .common import write_to_file
    write_to_file(json.dumps(info), json_file)

def compare_2_way_save(iter_pred1, iter_pred2, iter_image, out_data,
                       diff_iou=0.95, inter_iou=0.95, force=False, hint1='1', hint2='2'):
    labels = []
    def gen_rows():
        for _, ((key1, rects1), (key2, rects2), (im_key, _, str_im)) in tqdm(enumerate(zip(
                iter_pred1, iter_pred2, iter_image))):
            assert key1 == key2 == im_key
            unique_rects1 = rects_diff(rects1, rects2, iou=diff_iou)
            unique_rects2 = rects_diff(rects2, rects1, iou=diff_iou)
            k, l = key1 + '_{}'.format(hint1), json_dump(rects1)
            labels.append((k, l))
            yield k, l, str_im
            k, l = key2 + '_{}'.format(hint2), json_dump(rects2)
            labels.append((k, l))
            yield k, l, str_im
            k, l = key1 + '_{}_unique'.format(hint1), json_dump(unique_rects1)
            labels.append((k, l))
            yield k, l, str_im
            k, l = key2 + '_{}_unique'.format(hint2), json_dump(unique_rects2)
            labels.append((k, l))
            yield k, l, str_im
    if force:
        from .common import ensure_remove_dir
        ensure_remove_dir(op.join('data', out_data))
    out_dataset = TSVDataset(out_data)
    assert not op.isdir(out_dataset._data_root) and \
            not op.islink(out_dataset._data_root)
    out_dataset.write_data(gen_rows(), 'test')
    out_dataset.write_data(labels, 'test', 'label')
    populate_dataset_details(out_data)

def create_caption_map(data, split, version):
    dataset = TSVDataset(data)
    all_cap = []
    for k, ss in tqdm(dataset.iter_data(
        split,
        'caption',
        version=version,
    )):
        rects = json.loads(ss)
        for r in rects:
            all_cap.append(r['caption'])
    all_cap = list(set(all_cap))
    dataset.write_data(((c,)for c in all_cap), split, 'captionmap',
                       version=version)

def create_filter_dataset(src_data, excludes, dst_data):
    filters = []
    data, split = src_data, 'train'
    for e in excludes:
        filter_file = 'data/remove_duplicate/target_{}_{}_exclude_{}_{}_0.995_filter_by_pixel.tsv'.format(
            data, split, e['data'], e['split']
        )
        filters.append(filter_file)

    ensure_directory(op.join('data', dst_data))
    for f in glob.glob(op.join('data', src_data, 'trainX*')):
        dest_f = op.join('data', dst_data, op.basename(f))
        ensure_copy_file(f, dest_f)

    shuffle = [(int(i), int(j)) for i, j in tsv_reader(op.join('data', src_data, 'train.shuffle.txt'))]

    idx = []
    for f in filters:
        idx.extend([int(idx_train) for idx_test, idx_train in tsv_reader(f)])
    all_idx = set(range(len(shuffle)))
    logging.info('before {}'.format(len(all_idx)))
    all_idx = all_idx.difference(idx)
    logging.info('after {}'.format(len(all_idx)))
    if len(idx) > 0:
        assert max(idx) < len(shuffle)
    dest_shuffle = [shuffle[i] for i in all_idx]

    tsv_writer(dest_shuffle, op.join('data', dst_data, 'train.shuffle.txt'))

    #generate_key_idximage_idxcaption(dst_data, 'train', None)

def remove_duplicate_loader(exclude, train_data, train_split):
    #dataset = TSVDataset('TaxFlickr30K')
    #dataset = TSVDataset('TaxVQA')
    num_workers = 64
    batch_size = 32
    use_hist = False
    threshold = 0.995
    out_file = 'data/remove_duplicate/target_{}_{}_exclude_{}_{}_{}_filter_by_hist.tsv'.format(
        train_data,
        train_split,
        exclude['data'],
        exclude['split'],
        threshold)
    out_file2 = 'data/remove_duplicate/target_{}_{}_exclude_{}_{}_{}_filter_by_pixel.tsv'.format(
        train_data,
        train_split,
        exclude['data'],
        exclude['split'],
        threshold)
    if File.isfile(out_file2):
        return
    def get_hist(data):
        image = data['image']
        all_hist = []
        h, w = image.shape[:2]
        if use_hist:
            for i in range(3):
                hist = cv2.calcHist([image], [i], None, [256], [0, 256])
                hist = torch.tensor(hist)
                all_hist.append(hist)

                hist = cv2.calcHist([image[:h//2]], [i], None, [256], [0, 256])
                hist = torch.tensor(hist)
                all_hist.append(hist)

                hist = cv2.calcHist([image[h//2:]], [i], None, [256], [0, 256])
                hist = torch.tensor(hist)
                all_hist.append(hist)

                hist = cv2.calcHist([image[:, :w//2, :]], [i], None, [256], [0, 256])
                hist = torch.tensor(hist)
                all_hist.append(hist)

                hist = cv2.calcHist([image[:, w//2:, :]], [i], None, [256], [0, 256])
                hist = torch.tensor(hist)
                all_hist.append(hist)

            hist = torch.cat(all_hist, dim=0)
            hist = torch.nn.functional.normalize(hist, dim=0)
            #ipdb> pp hist.shape
            #torch.Size([256, 1])
            data['hist'] = hist.squeeze()
        else:
            image = torch.tensor(cv2.resize(image, (64, 64)))
            image = image.view(1, -1)
            image = torch.nn.functional.normalize(image.float(), dim=1)
            image = image.squeeze()
            data['hist'] = image
        del data['image']
        return data

    hists = []
    info = exclude
    loader = create_tsv_dataset_loader(
        info['data'], info['split'],
        batch_size=batch_size,
        num_workers=num_workers,
        extra_transform=get_hist,
    )
    for info in tqdm(loader, total=len(loader)):
        hist = info['hist']
        hists.append(hist)
    hists = torch.cat(hists, dim=0)

    data_loader = create_tsv_dataset_loader(
        train_data, train_split,
        extra_transform=get_hist,
        num_workers=num_workers,
        batch_size=batch_size,
    )

    #device = 'cuda'
    device = 'cpu'
    hists = hists.to(device)
    def gen_rows():
        for i, info in tqdm(enumerate(data_loader), total=len(data_loader)):
            hist = info['hist']
            hist = hist.squeeze(-1)

            hist = hist.to(device)

            sim = torch.matmul(hists, hist.t())
            idx = sim > threshold
            del sim

            idx_test, idx_train = idx.nonzero(as_tuple=True)
            idx_train = idx_train + i * batch_size
            for nz_test, nz_train  in zip(idx_test.to('cpu'), idx_train.to('cpu')):
                yield int(nz_test), int(nz_train)
    tsv_writer(gen_rows(), out_file)

    remove_duplicate_double_check_im(
        exclude['data'], exclude['split'], train_data, out_file, out_file2,
        1-threshold,
    )

def remove_duplicate_double_check_im(
        exclude_data, exclude_split, train_data, tsv_fn, out_tsv, threshold):
    row_processor = CheckDuplicate(exclude_data, exclude_split, train_data,
                                   threshold)
    parallel_tsv_process(row_processor, tsv_fn, out_tsv, 64)

class CheckDuplicate(object):
    def __init__(self, exclude_data, exclude_split, train_data, threshold):
        from .tsv_io import TSVSplitProperty
        self.tsv_exclude = TSVSplitProperty(exclude_data, exclude_split)
        self.tsv_train = TSVSplitProperty(train_data, 'train')
        self.threshold = threshold

    def __call__(self, row):
        idx_exclude, idx_train = map(int, row)
        im_exclude = self.tsv_exclude[idx_exclude][-1]
        im_train = self.tsv_train[idx_train][-1]
        im_exclude = img_from_base64(im_exclude)
        im_train = img_from_base64(im_train)
        im2 = im_exclude
        im1 = im_train
        if im2.shape != im1.shape:
            im2 = cv2.resize(im2, (im1.shape[1], im1.shape[0]))
        if np.absolute(im1 - im2).mean() < self.threshold * im1.mean():
            return row
        return None


def create_tsv_dataset_loader(
        data, split, extra_transform=None, batch_size=1, num_workers=1):
    from qd.data_layer.loader import create_data_loader
    from qd.data_layer.dataset import (
        CaptionIdxTSVDataset,
        ImageIdxTSVDataset,
        DatasetPlusTransform,
    )
    from qd.data_layer.transform import (
        LoadLabel,
        LoadHW,
        LoadFeature,
        LoadImage,
        LoadCaption,
        IdentifyTextAB,
        RandomPairNegative,
        CaptionTensorizer,
        TokenizeTransform,
        NoChange,
        PrepareLabel,
        RemoveUselessKeys,
        RenameKey,
        AppendDummyFeature,
    )
    from torchvision.transforms import transforms
    tsv = ImageIdxTSVDataset(data, split)
    loader = LoadImage(data, split)
    hw = LoadHW(data, split)
    useless = RemoveUselessKeys(['dataset'])
    ts = [
        loader,
        hw,
        useless,
    ]
    if extra_transform is not None:
        ts.append(extra_transform)
    transform = transforms.Compose(ts)
    return create_data_loader(tsv, transform, batch_size, num_workers=num_workers)

def parse_hw_column(s_hw):
    try:
        h, w = map(int, s_hw.split(' '))
        return h, w
    except:
        info = json.loads(s_hw)
        if isinstance(info, list):
            assert len(info) == 1
            info = info[0]
        return info['height'], info['width']

def limit_image_size_with_filter(in_rows, min_size, max_size, resave=True, quality=None):
    assert len(in_rows) == 2
    result = limit_image_size(in_rows[:1], min_size, max_size, resave=True, quality=None)
    if result is None:
        return None
    else:
        assert result[0][0] == in_rows[1][0]
        return result[0], in_rows[1]

def limit_image_size(in_rows, min_size, max_size, resave=True, quality=None):
    #debug = True
    debug = False
    if len(in_rows) == 2:
        in_img_row, in_hw_row = in_rows
        h, w = parse_hw_column(in_hw_row[1])
    else:
        in_img_row = in_rows[0]
        h, w = None, None
    img = img_from_base64(in_img_row[-1])
    if img is None:
        img = pilimg_from_base64(in_img_row[-1])
        if img is None:
            return None
        img = np.array(img)
        if img.size == 0:
            return None
        # Convert RGB to BGR
        img = img[:, :, ::-1].copy()
    if h is None:
        h, w = img.shape[:2]
    if w != img.shape[1] or h != img.shape[0]:
        logging.info('{}: w = {}; h = {}; img.shape = {}'.format(
            in_img_row[0], w, h, img.shape))
        w = img.shape[1]
        h = img.shape[0]
    image_min = min(h, w)
    image_max = max(h, w)
    if image_min < min_size and image_max < max_size:
        if resave or quality:
            im = img_from_base64(in_img_row[-1])
            in_img_row[-1] = encoded_from_img(im, quality=quality)
        return in_img_row,
    scale = min(min_size * 1. / image_min, max_size * 1. / image_max)
    w2 = int(round(scale * w))
    h2 = int(round(scale * h))
    w2 = max(1, w2)
    h2 = max(1, h2)
    #img = pilimg_from_base64(in_img_row[-1])
    #img = ImageOps.exif_transpose(img)
    if debug:
        img.show()
    img = cv2.resize(img, (w2, h2),
                     interpolation=cv2.INTER_AREA,
                     )
    #save_image(img, '/tmp/b.jpg')
    #img = img.resize((w2, h2))
    in_img_row[-1] = encoded_from_img(img, quality=quality)
    if debug:
        img.show()
    return in_img_row,
    # we need to do resizing

def parallel_limit_image_size(im_tsv, hw_tsv, min_size, max_size,
                              quality,
                              out_tsv):
    process_func = lambda x: limit_image_size(x, min_size, max_size,
                                              quality=quality)
    num_process = 64
    if hw_tsv is not None:
        parallel_tsv_process_NtoN(process_func, (im_tsv, hw_tsv), (out_tsv, ),
                                  num_process)
    else:
        parallel_tsv_process_NtoN(process_func, (im_tsv,), (out_tsv, ),
                                  num_process)

def parallel_limit_image_size_with_filter(im_tsv, other_tsv, min_size, max_size,
                              quality,
                              out_tsv, out_other_tsv):
    process_func = lambda x: limit_image_size_with_filter(
        x, min_size, max_size, quality=quality)
    num_process = 64
    parallel_tsv_process_NtoN(process_func, (im_tsv, other_tsv), (out_tsv, out_other_tsv), num_process)

def filter_dataset_by_size(data, out_data):
    dataset = TSVDataset(data)
    out_dataset = TSVDataset(out_data)
    iter_hw = dataset.iter_data('train', 'hw')

    if op.isfile(dataset.get_shuffle_file('train')):
        iter_shuffle = tsv_reader(dataset.get_shuffle_file('train'))
        info = defaultdict(int)
        def gen_rows():
            for (key, str_hw), s in tqdm(zip(iter_hw, iter_shuffle)):
                try:
                    h, w = map(int, str_hw.split(' '))
                except:
                    x = json.loads(str_hw)
                    if isinstance(x, list):
                        x = x[0]
                    h, w = x['height'], x['width']
                r = 1. * h / w
                if r > 4 or r < 1. / 4:
                    info['ratio_too_large'] += 1
                    continue
                if min(h, w) < 64:
                    info['size_too_small'] += 1
                    continue
                yield s
        tsv_writer(gen_rows(), out_dataset.get_shuffle_file('train'))
        for t in [None, 'hw', 'caption']:
            ensure_copy_file(
                dataset.get_data('trainX', t),
                out_dataset.get_data('trainX', t),
            )
    else:
        info = defaultdict(int)
        def gen_rows():
            for i, (key, str_hw) in tqdm(enumerate(iter_hw)):
                try:
                    h, w = map(int, str_hw.split(' '))
                except:
                    x = json.loads(str_hw)
                    if isinstance(x, list):
                        x = x[0]
                    h, w = x['height'], x['width']
                r = 1. * h / w
                if r > 4 or r < 1. / 4:
                    info['ratio_too_large'] += 1
                    continue
                if min(h, w) < 64:
                    info['size_too_small'] += 1
                    continue
                yield (0, i)
        tsv_writer(gen_rows(), out_dataset.get_shuffle_file('train'))
        for t in [None, 'hw', 'caption']:
            write_to_file(
                dataset.get_data('train', t),
                out_dataset.get_data('trainX', t),
            )

def limit_dataset_image_size_to_single(
        data, split, image_t, min_size, max_size,
        quality, out_data):
    dataset = TSVDataset(data)
    out_dataset = TSVDataset(out_data)

    process_func = lambda x: limit_image_size(
        x, min_size, max_size, quality=quality)

    i = TSVSplitProperty(data, split)
    h = TSVSplitProperty(data, split, 'hw')
    o = out_dataset.get_data(split, image_t)
    if not op.isfile(o):
        parallel_tsv_process_NtoN(process_func, (i, h), (o, ), 64)

    for t in ['label', 'caption']:
        if dataset.has(split, t):
            tsv_writer(
                TSVSplitProperty(data, split, t),
                out_dataset.get_data(split, t)
            )
    populate_dataset_hw(out_data, splits=[split], img_t=image_t)
    if File.isfile(dataset.get_labelmap_file()):
        ensure_copy_file(
            dataset.get_labelmap_file(),
            out_dataset.get_labelmap_file()
        )

def limit_dataset_image_size(
        data, split, image_t, min_size, max_size,
        quality, out_data):
    dataset = TSVDataset(data)
    out_dataset = TSVDataset(out_data)

    if op.isfile(dataset.get_data(split, image_t)):
        in_img_tsv = [dataset.get_data(split, image_t)]
        caption_tsvs = [dataset.get_data(split, 'caption')]
        in_hw_tsv = [dataset.get_data(split, 'hw')]
        out_tsv = [out_dataset.get_data(split, image_t)]
        single_tsv = True
    else:
        in_img_tsv = load_list_file(dataset.get_data(split + 'X', image_t))
        hw_file = dataset.get_data(split + 'X', 'hw')
        if op.isfile(hw_file):
            in_hw_tsv = load_list_file(dataset.get_data(split + 'X', 'hw'))
        else:
            hw_file = dataset.get_data(split, 'hw')
            assert op.isfile(hw_file)
            in_hw_tsv = [hw_file]
            iter_shuffle = tsv_reader(dataset.get_shuffle_file(split))
            for i, (idx_source, idx_row) in enumerate(iter_shuffle):
                assert i == int(idx_row)
                assert int(idx_source) == 0
            assert len(in_img_tsv) == 1
            assert len(TSVFile(hw_file)) == len(TSVFile(in_img_tsv[0]))
        num_split = len(in_img_tsv)
        out_tsv = [out_dataset.get_data(split + '_{}_{}'.format(i, num_split))
                   for i in range(num_split)]
        caption_tsvs = load_list_file(
            dataset.get_data(split + 'X', 'caption')
        )
        label_tsvs = load_list_file(
            dataset.get_data(split + 'X', 'label')
        )
        single_tsv = False

    process_func = lambda x: limit_image_size(
        x, min_size, max_size, quality=quality)

    for i, h, o in zip(in_img_tsv, in_hw_tsv, out_tsv):
        from qd.process_tsv import parallel_tsv_process_NtoN
        if not op.isfile(o):
            parallel_tsv_process_NtoN(process_func, (i, h), (o, ), 64)

    if not single_tsv:
        write_to_file(
            '\n'.join(out_tsv),
            out_dataset.get_data(split + 'X', image_t),
        )
        write_to_file(
            '\n'.join(caption_tsvs),
            out_dataset.get_data(split + 'X', 'caption')
        )
        write_to_file(
            '\n'.join(label_tsvs),
            out_dataset.get_data(split + 'X', 'label')
        )
        tsv_copy(
            dataset.get_shuffle_file(split),
            out_dataset.get_shuffle_file(split),
        )
        splits = ['train_{}_{}'.format(i, num_split) for i in range(num_split)]
        populate_dataset_hw(out_data, splits=splits)
        write_to_file(
            '\n'.join((out_dataset.get_data(
                split + '_{}_{}'.format(i, num_split), 'hw',
            ) for i in range(num_split))),
            out_dataset.get_data(split + 'X', 'hw'),
        )
    else:
        for t in ['label', 'caption']:
            from_f = dataset.get_data(split, t)
            if File.isfile(from_f):
                tsv_copy(
                    dataset.get_data(split, t),
                    out_dataset.get_data(split, t)
                )
        populate_dataset_hw(out_data, splits=[split], img_t=image_t)
    if File.isfile(dataset.get_labelmap_file()):
        ensure_copy_file(
            dataset.get_labelmap_file(),
            out_dataset.get_labelmap_file()
        )

def video_convert_tsv_one(old_tsv, new_tsv):
    def gen_rows():
        for row in tsv_reader(old_tsv):
            #assert len(row) == 34
            idx = (len(row) - 2) // 2 + 2
            yield row[0], row[idx]
    if not op.isfile(new_tsv):
        tsv_writer(gen_rows(), new_tsv)

def get_max_iter_for_vl_plus_l_data(
    data, vl_data_rate, num_epoch_vl, effective_batch_size,
    effective_batch_size_text_only,
):

    iter_cache = TSVDataset(data).iter_data(
        'train',
        'key_idximage_idxcaption_num_valid_image',
    )
    info = {k: int(v) for k, v in iter_cache}
    num_vl_pair = info['num_valid_vl_pair']
    num_l_pair = info['num_l_only_pair']
    max_iter = int(num_vl_pair * num_epoch_vl / effective_batch_size / vl_data_rate + 0.5)
    num_epoch_l = max_iter * (1 - vl_data_rate) * effective_batch_size_text_only / num_l_pair
    logging.info(f'num_epoch_l = {num_epoch_l}; max_iter={max_iter}')
    return max_iter

def generate_num_vl_data(data, split, out_type='key_idximage_idxcaption_num_valid_image'):
    # this is mainly used by VisionLanguagePlusLanguageBatchSampler
    dataset = TSVDataset(data)
    source_list = load_list_file(dataset.get_data(split + 'X'))
    is_valid = tuple([s != 'd' for s in source_list])
    source = TSVSplitProperty(data, split,
                               'key_idximage_idxcaption_idxsource')
    ranges = split_to_chunk_to_range(len(source), num_chunk=64)
    def process(x):
        start, end = x
        info = defaultdict(int)
        with_image = True
        for i in tqdm(range(start, end)):
            s = int(source[i][0])
            if is_valid[s]:
                assert with_image
                info['with_valid_image'] += 1
            elif with_image:
                with_image = False
            info['total'] += 1
        info['with_image'] = with_image
        return info
    infos = parallel_map(process, ranges)
    ret = defaultdict(int)
    for i in infos:
        for k, v in i.items():
            ret[k] += v
    assert ret['total'] == len(source)
    for i, info in enumerate(infos):
        if not info['with_image']:
            for j in range(i + 1, len(infos)):
                assert not info['with_image']

    out_file = dataset.get_data(split, out_type)
    tsv_writer((
        ('num_valid_vl_pair', ret['with_valid_image']),
        ('num_l_only_pair', ret['total'] - ret['with_valid_image']),
        ('num_total', ret['total']),
    ), out_file)

def generate_num_vl_data_old(data, split, out_type='key_idximage_idxcaption_num_valid_image'):
    # this is mainly used by VisionLanguagePlusLanguageBatchSampler
    dataset = TSVDataset(data)
    source_list = load_list_file(dataset.get_data(split + 'X'))
    num_valid_image = 0
    num_total = 0
    with_image = True
    for s, in tqdm(TSVSplitProperty(data, split,
                               'key_idximage_idxcaption_idxsource')):
        if source_list[int(s)] != 'd':
            assert with_image
            num_valid_image += 1
        elif with_image:
            with_image = False
        num_total += 1
    out_file = dataset.get_data(split, out_type)
    tsv_writer((
        ('num_valid_vl_pair', num_valid_image),
        ('num_l_only_pair', num_total - num_valid_image),
        ('num_total', num_total),
    ), out_file)

def generate_num_each_tsv(data, split,
                          idxsource_type='key_idximage_idxcaption_idxsource',
                          out_type='key_idximage_idxcaption_num_each_tsv',
                          ):
    result = []
    dataset = TSVDataset(data)
    #for s, in tqdm(TSVSplitProperty(data, split, 'key_idximage_idxcaption_idxsource')):
            # TSVSplitProperty here is twice slower
    total = dataset.num_rows(split, t=idxsource_type)
    ranges = split_to_chunk_to_range(total, num_chunk=64)

    def chunk_processor(x):
        start, end = x
        tsv = TSVSplitProperty(data, split, idxsource_type)
        info = defaultdict(int)
        prev_s = -1
        for i in tqdm(range(start, end)):
            s = int(tsv[i][0])
            info[s] += 1
            if prev_s == -1:
                prev_s = s
            elif prev_s < s:
                prev_s = s
            else:
                assert prev_s == s, (i, prev_s, s)
        return info

    infos = parallel_map(chunk_processor, ranges, num_worker=64)
    for i in range(len(infos) - 1):
        assert max(infos[i].keys()) <= min(infos[i + 1].keys())
    result = defaultdict(int)
    for i in infos:
        for k, v in i.items():
            result[k] += v

    # we assumed the last tsv is not empty. If it is, it will crash and we need
    # to append more zeros here.
    num_tsv = len(load_list_file(dataset.get_data(split + 'X')))
    assert min(result.keys()) >= 0
    assert max(result.keys()) < num_tsv

    dataset.write_data(((result.get(i, 0),) for i in range(num_tsv)), split, out_type,)

def generate_num_each_tsv_old(data, split, idxsource_type='key_idximage_idxcaption_idxsource'):
    result = []
    curr_num = 0
    curr_idx = -1
    dataset = TSVDataset(data)
    #for s, in tqdm(TSVSplitProperty(data, split, 'key_idximage_idxcaption_idxsource')):
            # TSVSplitProperty here is twice slower
    total = dataset.num_rows(split, t=idxsource_type)
    with File.open(dataset.get_data(split, idxsource_type), 'r') as fp:
        for line in tqdm(fp, total=total):
            s = int(line)
            if s != curr_idx:
                if curr_idx != -1:
                    # the order should not be random
                    assert len(result) == curr_idx
                    result.append(curr_num)
                while curr_idx + 1 < s:
                    result.append(0)
                    curr_idx += 1
                assert s == curr_idx + 1, (curr_idx, s)
                curr_idx = s
                curr_num = 0
            curr_num += 1

    assert len(result) == curr_idx, (len(result), curr_idx)
    result.append(curr_num)

    # we assumed the last tsv is not empty. If it is, it will crash and we need
    # to append more zeros here.
    num_tsv = len(load_list_file(dataset.get_data(split + 'X')))
    assert num_tsv == len(result), (num_tsv, len(result))
    dataset.write_data(((r,) for r in result),
                       split,
                       'key_idximage_idxcaption_num_each_tsv',
                       )

#@deprecated('use generate_idximage_idxcaption_idxsource_parallel')
#def generate_key_idximage_idxcaption_idxsource(data, split):
    #logging.info(f'{data}/{split}')
    #generate_key_idximage_idxcaption(data, split)
    #k_img_cap = TSVSplitProperty(data, split, 'key_idximage_idxcaption')
    #dataset = TSVDataset(data)
    #if dataset.has(split, 'key_idximage_idxcaption_idxsource'):
        #return
    #x_tsv = dataset.get_data(split + 'X', 'caption')
    #if File.isfile(x_tsv):
        #all_idx_source = [int(idx_source) for idx_source, _ in
                          #tqdm(TSVFile(dataset.get_shuffle_file(split)))]
        #logging.info('loading composite source idx')
        #result = ((all_idx_source[int(idx_img)],) for _, idx_img, _ in
                  #tqdm(k_img_cap))
        #dataset.write_data(result, split, 'key_idximage_idxcaption_idxsource')
    #else:
        #dataset.write_data([(0,)] * len(k_img_cap), split, 'key_idximage_idxcaption_idxsource')

def generate_idximage_idxcaption_idxsource(data, split):
    logging.info(f'{data}/{split}')
    generate_idximage_idxcaption_parallel(data, split)
    k_img_cap = TSVSplitProperty(data, split, 'idximage_idxcaption')
    dataset = TSVDataset(data)
    if dataset.has(split, 'idximage_idxcaption_idxsource'):
        return
    x_tsv = dataset.get_data(split + 'X', 'caption')
    if File.isfile(x_tsv):
        shuffle_tsv = TSVFile(dataset.get_shuffle_file(split))
        def row_processor(row):
            key, idximage, _ = row
            ret = int(shuffle_tsv[int(idximage)][0])
            return (ret, )
        parallel_tsv_process(
            row_processor,
            k_img_cap,
            dataset.get_data(split, 'idximage_idxcaption_idxsource'),
            64,
        )
    else:
        dataset.write_data([(0,)] * len(k_img_cap), split, 'idximage_idxcaption_idxsource')

@deprecated('use generate_idxsource_of_idximage_idxcaption')
def generate_idxsource_of_idximage_idxcaption_parallel(*args, **kwargs):
    generate_idxsource_of_idximage_idxcaption(*args, **kwargs)

def generate_idxsource_of_idximage_idxcaption(
    data, split, in_type='key_idximage_idxcaption', out_type='key_idximage_idxcaption_idxsource'):
    logging.info(f'{data}/{split}')
    if in_type == 'key_idximage_idxcaption':
        generate_key_idximage_idxcaption(data, split)
    elif in_type == 'idximage_idxcaption':
        generate_idximage_idxcaption(data, split)
    k_img_cap = TSVSplitProperty(data, split, in_type)
    dataset = TSVDataset(data)
    if dataset.has(split, out_type):
        return
    x_tsv = dataset.get_data(split + 'X', 'caption')
    if File.isfile(x_tsv):
        shuffle_tsv = TSVFile(dataset.get_shuffle_file(split))
        def row_processor(row):
            idximage = row[-2]
            ret = int(shuffle_tsv[int(idximage)][0])
            return (ret, )
        parallel_tsv_process(
            row_processor,
            k_img_cap,
            dataset.get_data(split, out_type),
            64,
        )
    else:
        dataset.write_data([(0,)] * len(k_img_cap), split, out_type)


@deprecated('use generate_idxsource_of_idximage_idxcaption_parallel')
def generate_key_idximage_idxcaption_idxsource_parallel(*args, **kwargs):
    generate_idxsource_of_idximage_idxcaption_parallel(*args, **kwargs)

@deprecated('use generate_idxsource_of_idximage_idxcaption_parallel')
def generate_key_idximage_idxcaption_idxsource(*args, **kwargs):
    generate_idxsource_of_idximage_idxcaption_parallel(*args, **kwargs)


def generate_cache_tsv(data):
    dataset = TSVDataset(data)
    all_file = []
    for t in [None, 'caption']:
        files = load_list_file(dataset.get_data('trainX', t=t))
        all_file.extend(files)
    all_file = [f for f in all_file if f != 'd']
    all_file.extend([get_tsv_associates(f)[0] for f in all_file])
    ret = ','.join(all_file)
    return ret

def get_precache_tsv_files(data):
    dataset = TSVDataset(data)
    all_fs = []
    for t in [None, 'caption']:
        if File.isfile(dataset.get_data('trainX', t=t)):
            fs = load_list_file(dataset.get_data('trainX', t=t))
            fs = [f for f in fs if f != 'd']
            fs.append(dataset.get_shuffle_file('train'))
        else:
            fs = [dataset.get_data('train', t=t)]
        fs.extend([x for f in fs for x in get_tsv_associates(f)])
        all_fs.extend(fs)
    return ','.join(set(all_fs))

def shuffle_dataset_by_file(in_data, out_data):
    in_dataset = TSVDataset(in_data)
    out_dataset = TSVDataset(out_data)
    ts = [None, 'caption']
    old2new = None
    random.seed(666)
    for t in ts:
        from_file = in_dataset.get_data('trainX', t=t)
        out_file = out_dataset.get_data('trainX', t=t)
        fnames = load_list_file(from_file)
        if old2new is None:
            old2new = list(range(len(fnames)))
            random.shuffle(old2new)
        new_out = [None] * len(fnames)
        for i, j in enumerate(old2new):
            new_out[j] = fnames[i]
        write_to_file('\n'.join(new_out), out_file)

    def mapper(ret, row, idx):
        i = int(row[0])
        if 'x' not in ret:
            ret['x'] = []
        ret = ret['x']
        if len(ret) == 0 or ret[-1]['img_tsv_idx'] != i:
            ret.append({
                'start': idx,
                'end': idx,
                'img_tsv_idx': i,
            })
        else:
            assert ret[-1]['img_tsv_idx'] == i
            ret[-1]['end'] = idx

    def reducer(rets):
        result = []
        for ret in rets:
            r = ret['x']
            for _i, curr_r in enumerate(r):
                if len(result) == 0:
                    result.append(curr_r)
                elif result[-1]['img_tsv_idx'] == curr_r['img_tsv_idx']:
                    assert result[-1]['end'] == curr_r['start'] - 1
                    result[-1]['end'] = curr_r['end']
                else:
                    result.append(curr_r)
        return result

    results = parallel_tsv_aggregator(
        mapper,
        reducer,
        in_tsv_file=in_dataset.get_shuffle_file('train'),
        with_row_idx=True,
        num_process=64,
    )

    for i in range(len(results) - 1):
        assert results[i]['img_tsv_idx'] < results[i + 1]['img_tsv_idx']
    random.shuffle(results)
    def process(r):
        folder = get_tmp_folder()
        new_idx_img = old2new[r['img_tsv_idx']]
        tmp_file = '{}/{}.tsv'.format(folder, new_idx_img)
        def gen_rows():
            in_dataset = TSVDataset(in_data)
            tsv = TSVFile(in_dataset.get_shuffle_file('train'))
            for i in range(r['start'], r['end'] + 1):
                old_idx_img, idx_caption = tsv[i]
                assert old_idx_img == str(r['img_tsv_idx'])
                yield new_idx_img, idx_caption
        tsv_writer(gen_rows(), tmp_file)

    parallel_map(process, results, num_worker=64)
    folder = get_tmp_folder()
    tmp_files = ['{}/{}.tsv'.format(folder, i) for i in range(len(results))]
    logging.info(len(tmp_files))
    tmp_files = [t for t in tmp_files if op.isfile(t)]
    logging.info(len(tmp_files))
    concat_tsv_files(tmp_files, out_dataset.get_shuffle_file('train'))
    delete_tsv_files(tmp_files)

def populate_vl_dataset(data, split='train'):
    generate_idximage_idxcaption(data, split)
    from qd.process_tsv import generate_idxsource_of_idximage_idxcaption
    generate_idxsource_of_idximage_idxcaption(
        data, split,
        in_type='idximage_idxcaption',
    )
    from qd.process_tsv import generate_num_each_tsv
    generate_num_each_tsv(data, split)
    from qd.process_tsv import generate_num_vl_data
    generate_num_vl_data(data, split)

def create_simple_tax(data):
    out_data = 'Tax' + data
    dataset = TSVDataset(data)
    out_dataset = TSVDataset(out_data)
    for split in ['train', 'val', 'test', 'trainval']:
        if not dataset.has(split):
            continue
        for t in [None, 'caption']:
            src_file = dataset.get_data(split, t)
            write_to_file(
                src_file,
                out_dataset.get_data(split + 'X', t)
            )
        num = dataset.num_rows(split)
        tsv_writer(((0, i) for i in range(num)),
                   out_dataset.get_shuffle_file(split))

def convert_cap_to_tag(predict_file, tag_predict):
    def gen_rows():
        if isinstance(predict_file, str):
            iter_pred = tsv_reader(predict_file)
        else:
            iter_pred = predict_file
        for key, s in iter_pred:
            s = json.loads(s)
            assert len(s) == 1
            tags = map(lambda x: x.strip(), s[0]['caption'].split(','))
            ret = [{'class': t, 'conf': 1.} for t in tags]
            yield key, json_dump(ret)
    tsv_writer(gen_rows(), tag_predict)

def create_question_annotation_json(data, split):
    gt = TSVSplitProperty(data, split, 'caption')
    questions = []
    annotations = []
    for image_id, s in gt:
        s = json.loads(s)
        for i in s:
            q = {
                'image_id': image_id,
                'question_id': i.get('question_id'),
                'question': i['question'],
            }
            questions.append(q)
            a = copy.deepcopy(q)
            answers = [{
                'answer': ans,
            } for ans in i['answers']]
            a['answers'] = answers
            a['question_type'] = 'null'
            a['answer_type'] = 'null'
            annotations.append(a)

    # list of dict (image_id, question, question_id), image_id and question_id
    # are integer
    question_info = {
        'info': None,
        'task_type': 'Open-Ended',
        'data_type': 'mscoco',
        'license': None,
        'data_subtype': None,
        'questions': questions,
    }
    annotation_info = {
        'info': None,
        'task_type': 'Open-Ended',
        'data_type': 'mscoco',
        'license': None,
        'data_subtype': None,
        'annotations': annotations,
    }
    out_f = f'data/{data}/{split}.caption.question.json'
    write_to_file(json_dump(question_info), out_f)
    out_f = f'data/{data}/{split}.caption.annotation.json'
    write_to_file(json_dump(annotation_info), out_f)

if __name__ == '__main__':
    from .common import parse_general_args
    init_logging()
    kwargs = parse_general_args()
    logging.info('param:\n{}'.format(pformat(kwargs)))
    function_name = kwargs['type']
    del kwargs['type']
    locals()[function_name](**kwargs)