Skip to content

Commit 6dee1b2

Browse files
authored
Merge pull request #222 from deeptools/hotfix_to_string
fix toString for numpy arrays
2 parents d15a9f5 + 708ae8e commit 6dee1b2

File tree

5 files changed

+15
-14
lines changed

5 files changed

+15
-14
lines changed

hicexplorer/HiCMatrix.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def load_cool(self, pMatrixFile, pChrnameList=None, pMatrixOnly=None, pIntraChro
199199
cut_intervals = []
200200

201201
for values in cut_intervals_data_frame.values:
202-
cut_intervals.append(tuple([toBytes(values[0]), values[1], values[2], 1.0]))
202+
cut_intervals.append(tuple([toString(values[0]), values[1], values[2], 1.0]))
203203

204204
# try to restore nan_bins.
205205
try:
@@ -284,8 +284,7 @@ def load_npz(matrixFile):
284284
else:
285285
distance_counts = _ma['dist_counts'].tolist()
286286

287-
map(toString, _ma['chrNameList'])
288-
cut_intervals = zip(_ma['chrNameList'], _ma['startList'],
287+
cut_intervals = zip(toString(_ma['chrNameList']), _ma['startList'],
289288
_ma['endList'], _ma['extraList'])
290289

291290
assert len(cut_intervals) == matrix.shape[0], \
@@ -880,13 +879,13 @@ def convert_to_obs_exp_matrix(self, maxdepth=None, zscore=False, perchr=False):
880879
# if zscore is needed, compute standard deviation: std = sqrt(mean(abs(x - x.mean())**2))
881880
if zscore:
882881
values_sqrt_diff = \
883-
np.abs((submatrix.data[dist_list == bin_dist_plus_one] - mu[bin_dist_plus_one])**2)
882+
np.abs((submatrix.data[dist_list == bin_dist_plus_one] - mu[bin_dist_plus_one]) ** 2)
884883
# the standard deviation is the sum of the differences with mu squared (value variable)
885884
# plus all zeros that are not included in the sparse matrix
886885
# for which the standard deviation is
887886
# (0 - mu)**2 = (mu)**2
888887
# The number of zeros is the diagonal length - the length of the non zero values
889-
zero_values_sqrt_diff_sum = (diagonal_length - len(values_sqrt_diff)) * mu[bin_dist_plus_one]**2
888+
zero_values_sqrt_diff_sum = (diagonal_length - len(values_sqrt_diff)) * mu[bin_dist_plus_one] ** 2
890889

891890
_std = np.sqrt((values_sqrt_diff.sum() + zero_values_sqrt_diff_sum) / diagonal_length)
892891
std[bin_dist_plus_one] = _std

hicexplorer/hicAggregateContacts.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import matplotlib.cm as cm
1313
import hicexplorer.HiCMatrix as hm
1414
import hicexplorer.utilities
15-
from .utilities import toBytes
15+
from .utilities import toString
1616
from .utilities import check_chrom_str_bytes
1717

1818
import logging
@@ -557,7 +557,7 @@ def main(args=None):
557557
seen[chrom] = set()
558558
over_1_5 = 0
559559
empty_mat = 0
560-
chrom_bin_range = ma.getChrBinRange(toBytes(chrom))
560+
chrom_bin_range = ma.getChrBinRange(toString(chrom))
561561

562562
log.info("processing {}".format(chrom))
563563

@@ -566,7 +566,7 @@ def main(args=None):
566566
# check all other regions that may interact with the
567567
# current interval at the given depth range
568568

569-
bin_id = ma.getRegionBinRange(toBytes(chrom), start, end)
569+
bin_id = ma.getRegionBinRange(toString(chrom), start, end)
570570
if bin_id is None:
571571
continue
572572
else:
@@ -577,7 +577,7 @@ def main(args=None):
577577
if counter % 50000 == 0:
578578
log.info("Number of contacts considered: {:,}".format(counter))
579579

580-
bin_id2 = ma.getRegionBinRange(toBytes(chrom), start2, end2)
580+
bin_id2 = ma.getRegionBinRange(toString(chrom), start2, end2)
581581
if bin_id2 is None:
582582
continue
583583
else:

hicexplorer/hicPlotMatrix.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ def main(args=None):
499499
log.debug("ma.chrBinBoundaries {}".format(ma.chrBinBoundaries))
500500
if sys.version_info[0] == 3:
501501
args.chromosomeOrder = toBytes(args.chromosomeOrder)
502-
for chrom in args.chromosomeOrder:
502+
for chrom in toString(args.chromosomeOrder):
503503
if chrom in ma.chrBinBoundaries:
504504
valid_chromosomes.append(chrom)
505505
else:

hicexplorer/test/test_hicmatrix.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
def test_save_load():
2323
outfile = '/tmp/matrix.h5'
24-
cut_intervals = [(b'a', 0, 10, 1), (b'a', 10, 20, 1),
25-
(b'a', 20, 30, 1), (b'a', 30, 40, 1), (b'b', 40, 50, 1)]
24+
cut_intervals = [('a', 0, 10, 1), ('a', 10, 20, 1),
25+
('a', 20, 30, 1), ('a', 30, 40, 1), ('b', 40, 50, 1)]
2626
hic = hm.hiCMatrix()
2727
hic.nan_bins = []
2828
matrix = np.array([[1, 8, 5, 3, 0],
@@ -139,8 +139,8 @@ def test_convert_to_zscore_matrix_2():
139139

140140
def test_save_load_cooler_format():
141141
outfile = '/tmp/matrix2.cool'
142-
cut_intervals = [(b'a', 0, 10, 1), (b'a', 10, 20, 1),
143-
(b'a', 20, 30, 1), (b'a', 30, 40, 1), (b'b', 40, 50, 1)]
142+
cut_intervals = [('a', 0, 10, 1), ('a', 10, 20, 1),
143+
('a', 20, 30, 1), ('a', 30, 40, 1), ('b', 40, 50, 1)]
144144
hic = hm.hiCMatrix()
145145
hic.nan_bins = []
146146
matrix = np.array([[1, 8, 5, 3, 0],

hicexplorer/utilities.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ def toString(s):
250250
return s.decode('ascii')
251251
if isinstance(s, list):
252252
return [toString(x) for x in s]
253+
if isinstance(s, np.ndarray):
254+
return s.astype(str)
253255
return s
254256

255257

0 commit comments

Comments
 (0)