Skip to content

Commit

Permalink
merge error fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kylessmith committed Nov 21, 2023
1 parent 4f147da commit e782286
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 16 deletions.
19 changes: 15 additions & 4 deletions ailist/AIList_core.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ cdef extern from "src/ailist/ailist_simulate.c":
# C is include here so that it doesn't need to be compiled externally
pass

cdef extern from "src/ailist/ailist_closest.c":
# C is include here so that it doesn't need to be compiled externally
pass

cdef extern from "src/ailist/overlap_index.c":
# C is include here so that it doesn't need to be compiled externally
pass
Expand Down Expand Up @@ -93,10 +97,10 @@ cdef extern from "src/ailist/augmented_interval_list.h":
int n # Current position

ctypedef struct overlap_index_t:
int size; # Current size
int max_size; # Maximum size
ailist_t *ail; # Store ailist
long *indices; # Store indices
int size # Current size
int max_size # Maximum size
ailist_t *ail # Store ailist
long *indices # Store indices


#-------------------------------------------------------------------------------------
Expand Down Expand Up @@ -356,6 +360,13 @@ cdef extern from "src/ailist/augmented_interval_list.h":
void ailist_simulate(ailist_t *ail, ailist_t *simulation, int n) nogil


#-------------------------------------------------------------------------------------
# ailist_closest.c
#=====================================================================================

ailist_t *ailist_closest(int start, int end, ailist_t *ail, int k) nogil


cpdef object rebuild_AIList(bytes data, bytes b_length)


Expand Down
58 changes: 53 additions & 5 deletions ailist/AIList_core.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ cdef class AIList(object):

# Check if object is constructed
if self.is_constructed:
return self.c_ailist.lenC[self.c_ailist.nc]
lenC = np.zeros(self.c_ailist.nc, dtype=np.intc)
for i in range(self.c_ailist.nc):
lenC[i] = self.c_ailist.lenC[i]
return lenC
else:
return None

Expand All @@ -211,7 +214,10 @@ cdef class AIList(object):

# Check if object is constructed
if self.is_constructed:
return self.c_ailist.idxC[self.c_ailist.nc]
idxC = np.zeros(self.c_ailist.nc, dtype=np.intc)
for i in range(self.c_ailist.nc):
idxC[i] = self.c_ailist.idxC[i]
return idxC
else:
return None

Expand Down Expand Up @@ -408,11 +414,17 @@ cdef class AIList(object):
if isinstance(key, slice):
raise IndexError("Cannot use slice as key")

# Find id
#cindexed_ailist = self._interval_id(key)
# Must be integer
# Check if key is greater than length
if key >= self.c_ailist.nr:
raise IndexError("Value larger than AIList length")

# Check if negative
if key < 0:
key = self.c_ailist.nr + key

cinterval = self.c_ailist.interval_list[key]
interval = Interval(cinterval.start, cinterval.end)
#interval.set_i(cinterval)

return interval

Expand Down Expand Up @@ -1756,6 +1768,42 @@ cdef class AIList(object):
return filtered_ail


def closest(self,
start,
end,
k = 5):
"""
Find k closest intervals to given interval
Parameters
----------
start : int
Start position of interval
end : int
End position of interval
k : int
Number of closest intervals to find [default = 5]
Returns
-------
closest_ail : AIList
AIList of closest intervals
"""

# Check if object is still open
if self.is_closed:
raise NameError("AIList object has been closed.")

# Initialize closest list
cdef AIList closest_ail = AIList()

# Call C function
cdef ailist_t *c_closest_ail = ailist_closest(start, end, self.c_ailist, k)
closest_ail.set_list(c_closest_ail)

return closest_ail


def copy(self):
"""
Make a copy of the AIList
Expand Down
29 changes: 27 additions & 2 deletions ailist/LabeledIntervalArray_core.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ cdef class LabeledIntervalArray(object):

# Check if negative
if key < 0:
key = self.laia.total_nr - key
key = self.laia.total_nr + key

# Create LabeledInterval wrapper
output_interval = LabeledInterval()
Expand Down Expand Up @@ -1848,6 +1848,31 @@ cdef class LabeledIntervalArray(object):
nhits : numpy.ndarray{int}
Number of hits per position
.. warning::
This requires :func:`~aiarray.LabeledIntervalArray.construct` and will run it if not already run.
See Also
--------
LabeledIntervalArray.construct: Construct LabeledIntervalArray
LabeledIntervalArray.add: Add interval to LabeledIntervalArray
LabeledIntervalArray.intersect: Find intervals overlapping given range
Examples
--------
>>> from aiarray import LabeledIntervalArray
>>> ail = LabeledIntervalArray()
>>> ail.add(1, 2, 'a')
>>> ail.add(3, 4, 'a')
>>> ail.add(2, 6, 'a')
>>> ail
LabeledIntervalArray
(1-2, 'a')
(3-4, 'a')
(2-6, 'a')
>>> q = ail.nhits(2, 10, 'a')
>>> q
2
"""

# Check if object is still open
Expand Down Expand Up @@ -3143,7 +3168,7 @@ cdef class LabeledIntervalArray(object):
return False
else:
return True


def copy(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion ailist/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .LabeledIntervalArray_core import LabeledIntervalArray, LabeledInterval, get_include

# This is extracted automatically by the top-level setup.py.
__version__ = '2.0.1'
__version__ = '2.1.1'
__author__ = "Kyle S. Smith"


Expand Down
83 changes: 83 additions & 0 deletions ailist/src/ailist/ailist_closest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//=============================================================================
// Quick and efficient storing/querying of intervals
// by Kyle S. Smith and Jianglin Feng
//
//-----------------------------------------------------------------------------

#include "augmented_interval_list.h"

//-----------------------------------------------------------------------------

int distance(int start, int end, interval_t interval)
{
int startDistance = abs(interval.start - start);
int endDistance = abs(interval.end - end);
return startDistance < endDistance ? startDistance : endDistance;
}


int sum_distances(float *distances, int k)
{
float sum = 0;
int i;
for (i = 0; i < k; i++)
{
sum += distances[i];
}
return sum;
}

int find_max_distance(float *distances, int k)
{
int max_index = 0;
int i;
for (i = 0; i < k; i++)
{
if (distances[i] > distances[max_index])
{
max_index = i;
}
}

return max_index;
}

ailist_t *ailist_closest(int start, int end, ailist_t *ail, int k)
{ /* Find closest intervals to given interval */

float max_distance = MAXFLOAT;
int max_index = 0;
//float sum_distance = 0;
float *distances = (float*) malloc (sizeof(float) * k);
int *index = (int*) malloc (sizeof(int) * k);

// Initialize distances
int i;
for (i = 0; i < k; i++)
{
distances[i] = MAXFLOAT;
index[i] = -1;
}

// Iterate over intervals
for (i = 0; i < ail->nr; i++)
{
float current_distance = distance(start, end, ail->interval_list[i]);
if (current_distance < max_distance)
{
distances[max_index] = current_distance;
index[max_index] = i;
max_index = find_max_distance(distances, k);
max_distance = distances[max_index];
}
}

// Create new list
ailist_t *closest_list = ailist_init();
for (i = 0; i < k; i++)
{
ailist_add(closest_list, ail->interval_list[index[i]].start, ail->interval_list[index[i]].end, ail->interval_list[index[i]].id_value);
}

return closest_list;
}
5 changes: 3 additions & 2 deletions ailist/src/ailist/ailist_iter.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ int ailist_sorted_iter_next(ailist_sorted_iter_t *iter)
selected_comp = j;
}

// Increment label_comp_counter for selected comp
iter->comp_used[selected_comp] = iter->comp_used[selected_comp] + 1;
}

// Increment label_comp_counter for selected comp
iter->comp_used[selected_comp] = iter->comp_used[selected_comp] + 1;

return 1;
}

Expand Down
8 changes: 8 additions & 0 deletions ailist/src/ailist/augmented_interval_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "../array_query/array_query_utilities.h"
#include "interval.h"

//-------------------------------------------------------------------------------------
#define MAXFLOAT 3.4028234664e+38
//-------------------------------------------------------------------------------------

typedef struct {
Expand Down Expand Up @@ -299,4 +301,10 @@ ailist_t *ailist_downsample(ailist_t *ail, double proportion);
void ailist_simulate(ailist_t *ail, ailist_t *simulation, int n);


//-------------------------------------------------------------------------------------
// ailist_closest.c
//=====================================================================================

ailist_t *ailist_closest(int start, int end, ailist_t *ail, int k);

#endif
2 changes: 1 addition & 1 deletion ailist/src/labeled_aiarray/labeled_augmented_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
//-------------------------------------------------------------------------------------

static const int khStrInt = 32;
KHASH_MAP_INIT_STR(khStrInt, uint32_t)
KHASH_MAP_INIT_STR(khStrInt, uint32_t);
typedef khash_t(khStrInt) strhash_t;

#define kh_name_set(kname, hash, key, val) ({int ret; k = kh_put(kname, hash,key,&ret); kh_value(hash,k) = val, kh_key(hash,k) = strdup(key); ret;})
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ailist"
version = "2.0.2"
version = "2.1.1"
description = "Python package for Augmented Interval List"
authors = ["Kyle S. Smith <kyle.smith@stjude.org>"]
maintainers = ["Kyle S. Smith <kyle.smith@stjude.org>"]
Expand Down

0 comments on commit e782286

Please sign in to comment.