
    tfL                     B    d dl mZ d dlZd dlZd ZddZddZd Z	d Z
y)    Nc                    | d   }d|j                   v r|j                   d   }nd}d|j                   v r|j                   d   }n|j                   d   }|||j                   d   |j                   d   |j                   d   d	S )
zt
    Get information about the tileset.

    :param hdf_file: A file handle for an HDF5 file (h5py.File('...'))
    metazmin-posr   zmax-posz
max-lengthz	max-widthmax-zoom	tile-size)max_posmin_pos	max_widthmax_zoom	tile_size)attrs)hdf_filedr   r   s       [/var/www/html/software/conda/envs/higlass/lib/python3.12/site-packages/clodius/hdf_tiles.pyget_tileset_infor      s     	AAGG'')$AGG'')$'',' WW[)GGJ'WW[)     c                     |dk  rt        d      |t        |       }||k  r%||z   dz  } || |   |      dk  r|dz   }n|}||k  r%|S )a  Bisect_left with with an additional comparator.

    Based on the bisect_left function from the python bisect module.

    Return the index where to insert item x in list a, assuming a is sorted.

    The return value i is such that all e in a[:i] have e < x, and all e in
    a[i:] have e >= x.  So if x already appears in the list, a.insert(x) will
    insert just before the leftmost x already there.

    Optional args lo (default 0) and hi (default len(a)) bound the
    slice of a to be searched.

    Args:
        a (array): The array to bisect
        x (object): The object to find the insertion point of
        lo (int): The starting index of items to search in a
        hi (int): The end index of items to search in a
        comparator (function(a,b)): A way to compare objects
    r   lo must be non-negative      
ValueErrorlenaxlohi
comparatormids         r   bisect_leftr    !   sj    , 
Av233	zV
r'Bw1nafa 1$qBB r' Ir   c                     |dk  rt        d      |t        |       }||k  r%||z   dz  } ||| |         dk  r|}n|dz   }||k  r%|S )a   Bisect_right with with an additional comparator.

    Based on the bisect_right function from the python bisect module.

    Return the index where to insert item x in list a, assuming a is sorted.

    The return value i is such that all e in a[:i] have e <= x, and all e in
    a[i:] have e > x.  So if x already appears in the list, a.insert(x) will
    insert just after the rightmost x already there.

    Optional args lo (default 0) and hi (default len(a)) bound the
    slice of a to be searched.


    Args:
        a (array): The array to bisect
        x (object): The object to find the insertion point of
        lo (int): The starting index of items to search in a
        hi (int): The end index of items to search in a
        comparator (function(a,b)): A way to compare objects
    r   r   r   r   r   r   s         r   bisect_rightr"   D   sj    , 
Av233	zV
r'Bw1na3 1$BqB r' Ir   c                 R   |d|z  kD  rt        d       g S |dk  rt        d       g S | d   }t        |j                  d         }t        |j                  d         }| t        |         }|d||z
  z  z  }||z  }||z   }	d }
d	 }t	        ||g|

      }t        ||	g|

      }||| S )ak  
    Get a discrete set of data from an hdf_tile file.

    Args:
        hdf_file (h5py.File): File handle for the file containing the information
        z (int): The zoom level of this tile
        x (int): The x position of this tile

    Returns:
        A 2D array of entries at that position. It is assumed that names of the
        columns are known.
    r   OUT OF RIGHT RANGEr   OUT OF LEFT RANGEr   r   r   c                 <    t        | d         t        |d         z
  S )Nr   intr   bs     r   comparator_startz+get_discrete_data.<locals>.comparator_start       1Q4y3qt9$$r   c                 <    t        | d         t        |d         z
  S )Nr   r'   r)   s     r   comparator_endz)get_discrete_data.<locals>.comparator_end   r,   r   )r   )printr(   r   strr    r"   )r   zr   r   r   r
   f
tile_width
tile_starttile_endr+   r.   tile_data_starttile_data_ends                 r   get_discrete_datar8   g   s     	16z"#	1u!"	AAGGK()I177:&'H
 	QA Q8a<00JZJJ&H%% "!j\>NOO XJ;KLM_]++r   c                    |d|z  kD  rt        d       g S |dk  rt        d       g S | d   }t        |j                  d         }t        |j                  d         }t        |j                  d         }|d|z  z  }d	|j                  v rt        |j                  d	         }n|}||z
  }	|t        j                  |	|z        z  }
|	|
z
  }d|z  }||z  }t        |d|z  z  |z        }t        ||z         }t        |d|
z  z        }	 | d
t        t        |
            z      }||kD  rWt        j                  ||z
        }|j                  t        j                         t        j                  |t        |            }nj||k  rC||k  r>||| dd }t        j                  ||dz   | t        j                  |t        |            }n"t        j                  ||| t        |            }	 d}dt        t        |
            z   | v r| dt        t        |
            z      }t        j                  ||| t        |            }d||z
  z  }t        j                  t        |            }|j                  |       ||z
  }||z  }|S |S )z
    Return a tile from an hdf_file.

    :param hdf_file: A file handle for an HDF5 file (h5py.File('...'))
    :param z: The zoom level
    :param x: The x position of the tile
    r   r$   r   r%   r   r   z	zoom-stepr   zmax-positionvalues_Nr   nan_values_)r/   r(   r   mathfloorr0   npzerosfillnanct	aggregater   )r   r1   r   r   r   	zoom_stepr
   r	   max_positionrznext_stored_zoomzoom_offset
num_to_aggtotal_in_length	start_posend_posr2   r   	ret_arrayf_nan	nan_arraynum_aggregatednum_vals_arraynum_summed_arrayaverages_arrays                            r   get_datarT      s    	16z"#	1u!"	AAGGK()IAGGK()I177:&'HAM)I 177>23 	AB !4::b9n#==''K k!J*,O Qk))I57I)o-.G |a+;&;;<L 	S%5!6778A<HHWy()	rvvLLC
O4		\	!lW&<i #(*,
W%LLC
O4	LL9W!5s:G	 Es3/011X=S1A-B)CCDLLy!93z?K	x!|,#i.1N+)I5"%55r   )r   NN)clodius.arrayarrayrB   r<   numpyr>   r   r    r"   r8   rT    r   r   <module>rY      s+      6 F F0,f\r   