summaryrefslogtreecommitdiff
path: root/python-cykhash.spec
blob: cedec8d3e29a9e140c045013d3b0cc62e7f36050 (plain)
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
%global _empty_manifest_terminate_build 0
Name:		python-cykhash
Version:	2.0.1
Release:	1
Summary:	cython wrapper for khash-sets/maps, efficient implementation of isin and unique
License:	MIT
URL:		https://github.com/realead/cykhash
Source0:	https://mirrors.nju.edu.cn/pypi/web/packages/a5/24/bc570e0f3bb769ebdc178dce52b5be3575ced0c8135a32f0299dff2c138e/cykhash-2.0.1.tar.gz
BuildArch:	noarch


%description
# cykhash

cython wrapper for khash-sets/maps, efficient implementation of `isin` and `unique`

## About:

  * Brings functionality of khash (https://github.com/attractivechaos/klib/blob/master/khash.h) to Python and Cython and can be used seamlessly in numpy or pandas.

  * Numpy's world is lacking the concept of a (hash-)set. This shortcoming is fixed and efficient (memory- and speedwise compared to pandas') `unique` and `isin` are implemented.

  * Python-set/dict have big memory-footprint. For some datatypes the overhead can be reduced by using khash by factor 4-8.

## Installation:

The recommended way to install the library is via `conda` package manager using the `conda-forge` channel:

    conda install -c conda-forge cykhash

You can also install the library using `pip`. To install the latest release:

    pip install cykhash

To install the most recent version of the module:

    pip install https://github.com/realead/cykhash/zipball/master

Attention: On Linux/Mac `python-dev` should be installed for that (see also https://stackoverflow.com/questions/21530577/fatal-error-python-h-no-such-file-or-directory) and MSVC on Windows.

## Dependencies:

To build the library from source, Cython>=0.28 is required as well as a c-build tool chain.

See (https://github.com/realead/cykhash/blob/master/doc/README4DEVELOPER.md) for dependencies needed for development.

## Quick start

#### Hash set and isin

Creating a hashset and using it in `isin`:

    # prepare data:
    >>> import numpy as np 
    >>> a = np.arange(42, dtype=np.int64)
    >>> b = np.arange(84, dtype=np.int64)
    >>> result = np.empty(b.size, dtype=np.bool_)

    # actually usage
    >>> from cykhash import Int64Set_from_buffer, isin_int64

    >>> lookup = Int64Set_from_buffer(a) # create a hashset
    >>> isin_int64(b, lookup, result)    # running time O(b.size)
    >>> isin_int64(b, lookup, result)    # lookup is reused and not recreated


### `unique`

Finding `unique` in `O(n)` (compared to numpy's  `np.unique` - `O(n*logn)`) and smaller memory-footprint than pandas' `pd.unique`:

    # prepare input
    >>> import numpy as np
    >>> a = np.array([1,2,3,3,2,1], dtype=np.int64)
    
    # actual usage:
    >>> from cykhash import unique_int64
    >>> unique_buffer = unique_int64(a) # unique element are exposed via buffer-protocol

    # can be converted to a numpy-array without copying via
    >>> unique_array = np.ctypeslib.as_array(unique_buffer)
    >>> unique_array.shape
    (3,)


### Hash map

Maps and sets handle `nan`-correctly (try it out with Python's dict/set):

    >>> from cykhash import Float64toInt64Map
    >>> my_map = Float64toInt64Map() # values are 64bit integers
    >>> my_map[float("nan")] = 1
    >>> my_map[float("nan")]
    1



## Functionality overview

### Hash sets

`Int64Set`, `Int32Set`, `Float64Set`, `Float32Set` ( and `PyObjectSet`) are implemented. They are more or less drop-in replacements for Python's `set`. Furthermore, given the Cython-interface, efficient extensions of functionality are easily done.


The biggest advantage of these sets is that they need about 4-8 times less memory than the usual Python-sets and are somewhat faster for integers or floats. 

As `PyObjectSet` is somewhat slower than the usual `set` and needs about the same amount of memory, it should be used only if all `nan`s should be treated as equivalent.

The most efficient way to create such sets is to use `XXXXSet_from_buffer(...)`, e.g. `Int64Set_from_buffer`, if the data container at hand supports buffer protocol (e.g. numpy-arrays, `array.array` or `ctypes`-arrays). Or `XXXXSet_from(...)` for any iterator.


### Hash maps

`Int64toInt64Map`, `Int32toInt32Map`, `Float64toInt64Map`, `Float32toInt32Map` ( and `PyObjectMap`) are implemented. They are more or less drop-in replacements for Python's `dict` (however, not every piece of `dict`'s functionality makes sense, for example `setdefault(x, default)` without `default`-argument, because `None` cannot be inserted, also the khash-maps don't preserve the insertion order, so there is also no `reversed`). Furthermore, given the Cython-interface, efficient extensions of functionality are easily done.

Biggest advantage of these sets is that they need about 4-8 times less memory than the usual Python-dictionaries and are somewhat faster for integers or floats.


As `PyObjectMap` is somewhat slower than the usual `dict` and needs about the same amount of memory, it should be used only if all `nan`s should be treated as equivalent.

### isin

  * implemented are `isin_int64`, `isin_int32`, `isin_float64`, `isin_float32`
  * using hash set instead of arrays in `isin` function has the advantage, that the look-up data structure doesn't have to be reconstructed for every call, thus reducing the running time from `O(n+m)`to `O(n)`, where `n` is the number of queries and `m`-number of elements in the look up array.
  * Thus cykash's `isin` can be order of magnitude faster than the numpy's or pandas' versions.

#### all, none, any, and count_if

  * siblings functions of `isin_XXX` are:
      * `all_XXX`/`all_XXX_from_iterator` which return `True` if all elements of the query array can be found in the set.
      * `any_XXX`/`any_XXX_from_iterator` which return `True` if at least one element of the query array can be found in the set.
      * `none_XXX`/`none_XXX_from_iterator` which return `True` if none of  elements from the query array can be found in the set.
      * `count_if_XXX`/`count_if_XXX_from_iterator` which return the number of elements from the query array can be found in the set.
  * `all_XXX`, `any_XXX`, `none_XXX` and `count_if_XXX` are faster than using `isin_XXX` and applying numpy's versions of these function on the resulting array.
  * `from_iterator` version works with any iterable, but the version for buffers are more efficient.

### unique

  * implemented are `unique_int64`, `unique_int32`, `unique_float64`, `unique_float32`
  * returns an object which implements the buffer protocol, so `np.ctypeslib.as_array` (recommended) or `np.frombuffer` (less safe, as memory can get reinterpreted) can be used to create numpy arrays.
  * differently as pandas, the returned uniques aren't in the order of the appearance. If order of appearence is important use `unique_stable_xxx`-versions, which needs somewhat more memory.
  * the signature is `unique_xxx(buffer, size_hint=0.0)` the initial memory-consumption of the hash-set will be `len(buffer)*size_hint` unless `size_hint<=0.0`, in this case it will be ensured, that no rehashing is needed even if all elements are unique in the buffer.

As pandas uses maps instead of sets internally for `unique`, it needs about 4 times more peak memory and is 1.6-3 times slower.


### Floating-point numbers as keys

There is a problem with floating-point sets or maps, i.e. `Float64Set`, `Float32Set`, `Float64toInt64Map` and `Float32toInt32Map`: The standard definition of "equal" and hash-function based on the bit representation don't define a meaningful or desired behavior for the hash set:

   * `NAN != NAN` and thus it is not equivalence relation
   * `-0.0 == 0.0` but `hash(-0.0)!=hash(0.0)`, but `x==y => hash(x)==hash(y)` is neccessary for set to work properly.

This problem is resolved through following special case handling:

   * `hash(-0.0):=hash(0.0)`
   * `hash(x):=hash(NAN)` for any not a number `x`.
   * `x is equal y <=> x==y || (x!=x && y!=y)`

A consequence of the above rule, that the equivalence classes of `{0.0, -0.0}` and `e{x | x is not a number}` have more than one element. In the set these classes are represented by the first seen element from the class.

The above holds also for `PyObjectSet` (this behavior is not the same as fro Python-`set` which shows a different behavior for nans).

### Examples:

#### Hash sets

Python: Creates a set from a numpy-array and looks up whether an element is in the resulting set:

    >>> import numpy as np
    >>> from cykhash import Int64Set_from_buffer
    >>> a =  np.arange(42, dtype=np.int64)
    >>> my_set = Int64Set_from_buffer(a) # no reallocation will be needed
    >>> 41 in my_set 
    True
    >>> 42 not in my_set
    True

Python: Create a set from an iterable and looks up whether an element is in the resulting set:

    >>> from cykhash import Int64Set_from
    >>> my_set = Int64Set_from(range(42)) # no reallocation will be needed
    >>> assert 41 in my_set and 42 not in my_set

Cython: Create a set and put some values into it:

    from cykhash.khashsets cimport Int64Set
    my_set = Int64Set(number_of_elements_hint=12)  # reserve place for at least 12 integers
    cdef Py_ssize_t i
    for i in range(12):
       my_set.add(i)
    assert 11 in my_set and 12 not in my_set

#### Hash maps

Python: Creating `int64->float64` map using `Int64toFloat64Map_from_buffers`:

    >>> import numpy as np
    >>> from cykhash import Int64toFloat64Map_from_buffers
    >>> keys = np.array([1, 2, 3, 4], dtype=np.int64)
    >>> vals = np.array([5, 6, 7, 8], dtype=np.float64)
    >>> my_map = Int64toFloat64Map_from_buffers(keys, vals) # there will be no reallocation
    >>> assert my_map[4] == 8.0

Python: Creating `int64->int64` map from scratch:

    >>> import numpy as np
    >>> from cykhash import Int64toInt64Map

    # my_map will not need reallocation for at least 12 elements
    >>> my_map = Int64toInt64Map(number_of_elements_hint=12)
    >>> for i in range(12):  my_map[i] = i+1
    >>> assert my_map[5] == 6


#### isin

Python: Creating look-up data structure from a numpy-array, performing `isin`-query

    >>> import numpy as np
    >>> from cykhash import Int64Set_from_buffer, isin_int64
    >>> a = np.arange(42, dtype=np.int64)
    >>> lookup = Int64Set_from_buffer(a)

    >>> b = np.arange(84, dtype=np.int64)
    >>> result = np.empty(b.size, dtype=np.bool_)

    >>> isin_int64(b, lookup, result)    # running time O(b.size)
    >>> assert np.sum(result.astype(np.int_)) == 42


#### unique

Python: using `unique_int64`:

    >>> import numpy as np
    >>> from cykhash import unique_int64
    >>> a = np.array([1,2,3,3,2,1], dtype=np.int64)
    >>> u = np.ctypeslib.as_array(unique_int64(a)) # there will be no reallocation
    >>> assert set(u) == {1,2,3}

Python: using `unique_stable_int64`: 

    >>> import numpy as np
    >>> from cykhash import unique_stable_int64
    >>> a = np.array([3,2,1,1,2,3], dtype=np.int64)
    >>> u = np.ctypeslib.as_array(unique_stable_int64(a)) # there will be no reallocation
    >>> assert list(u) == [3,2,1] 



## API

See (https://github.com/realead/cykhash/blob/master/doc/README_API.md) for a more detailed API description.

## Performance

See (https://github.com/realead/cykhash/blob/master/doc/README_PERFORMANCE.md) for results of performance tests.

## Trivia

* This project was inspired by the following stackoverflow question: https://stackoverflow.com/questions/50779617/pandas-pd-series-isin-performance-with-set-versus-array.

* pandas also uses `khash` (and thus was a source of inspiration), but wraps only maps and doesn't wrap sets. Thus, pandas' `unique` needs more memory as it should. Those maps are also never exposed, so there is no way to reuse the look-up structure for multiple calls to `isin`.

* `khash` is a good choice, but there are other alternatives, e.g. https://github.com/sparsehash/sparsehash. See also https://stackoverflow.com/questions/48129713/fastest-way-to-find-all-unique-elements-in-an-array-with-cython/48142655#48142655 for a comparison for different `unique` implementations.

* A similar approach for sets/maps in pure Cython: https://github.com/realead/tighthash, which is quite slower than khash.

* There is no dependency on `numpy`: this library uses buffer protocol, thus it works for `array.array`, `numpy.ndarray`, `ctypes`-arrays and anything else. However, some interfaces are somewhat cumbersome (which type should be created as answer?) and for convenient usage it might be a good idea to wrap the functionality so objects of right types are created.

## Compatibility between cykhash-versions:

There are different levels of compatibility: 

 * for code using only pure python interface
 * for code using cython/cdef-interface and built against a particular cykash version

Ther rules are as follows:
 
 * there is no warranty for major versions mismatch: i.e. code written with cykhash `1.x.y` might not run with cykhash `2.z.w` and vice versa.
 * if only pure python interface is used, code for the same major version will ran for version with higher minor version, i.e. code for cykhash `2.0.x` will run with cykhash `2.1.y` (but not the other way around: that means new functions could be added to pure python interface)
 * if cython's `cdef` interface is used, i.e. a cython-extension was build using pxi-files from cykhash, then versions are compartible only if the the minor versions are the same, e.g. `2.0.x` could be replaced by `2.0.y` in the installation, but when replacing with `2.1.z` the dependent cython-extension must be rebuilt.

## History:
### Release 2.0.1 (05.02.2022):

  * Tests work for Python 3.11
  * Tests work for numpy 1.24
  * Drops support for Python 3.6 and Python 3.7

#### Release 2.0.0 (09.11.2021):

  * Implementation of `any`, `all`, `none` and `count_if`
  * Hash-sets are now (almost) drop-in replacements of Python's sets
  * Breaking change: iterator from maps doesn't no longer returns items but only keys. However there are following new methods `keys()`, `values()` and `items()`which return so called mapvies, which correspond more or less to dictviews (but for mapsview doesn't hold that "Dictionary order is guaranteed to be insertion order.").
  * Hash-Maps are now (almost) drop-in replacements of Python's dicts. Differences: insertion order isn't preserved, thus there is also no `reversed()`-method, `setdefault(key, default)` isn't possible without `default` because `None` cannot be inserted in the map
  * Better hash-functions for float64, float32, int64 and int32 (gh-issue #4).
  * Breaking change: different names/signatures for maps
  * supports tracemalloc for Py3.6+
  * supports Python 3.10

#### Release 1.0.2 (30.05.2020):

  * can be installed via conda-forge to all operating systems
  * can be installed via pip in a clean environment (Cython>=0.28 is now fetched automatically)

#### Release 1.0.1 (27.05.2020):

  * released on PyPi

#### Older:

  * 0.4.0: uniques_stable, preparing for release
  * 0.3.0: PyObjectSet, Maps for Int64/32 and also Float64/32, unique-versions
  * 0.2.0: Int32Set, Float64Set, Float32Set
  * 0.1.0: Int64Set





%package -n python3-cykhash
Summary:	cython wrapper for khash-sets/maps, efficient implementation of isin and unique
Provides:	python-cykhash
BuildRequires:	python3-devel
BuildRequires:	python3-setuptools
BuildRequires:	python3-pip
%description -n python3-cykhash
# cykhash

cython wrapper for khash-sets/maps, efficient implementation of `isin` and `unique`

## About:

  * Brings functionality of khash (https://github.com/attractivechaos/klib/blob/master/khash.h) to Python and Cython and can be used seamlessly in numpy or pandas.

  * Numpy's world is lacking the concept of a (hash-)set. This shortcoming is fixed and efficient (memory- and speedwise compared to pandas') `unique` and `isin` are implemented.

  * Python-set/dict have big memory-footprint. For some datatypes the overhead can be reduced by using khash by factor 4-8.

## Installation:

The recommended way to install the library is via `conda` package manager using the `conda-forge` channel:

    conda install -c conda-forge cykhash

You can also install the library using `pip`. To install the latest release:

    pip install cykhash

To install the most recent version of the module:

    pip install https://github.com/realead/cykhash/zipball/master

Attention: On Linux/Mac `python-dev` should be installed for that (see also https://stackoverflow.com/questions/21530577/fatal-error-python-h-no-such-file-or-directory) and MSVC on Windows.

## Dependencies:

To build the library from source, Cython>=0.28 is required as well as a c-build tool chain.

See (https://github.com/realead/cykhash/blob/master/doc/README4DEVELOPER.md) for dependencies needed for development.

## Quick start

#### Hash set and isin

Creating a hashset and using it in `isin`:

    # prepare data:
    >>> import numpy as np 
    >>> a = np.arange(42, dtype=np.int64)
    >>> b = np.arange(84, dtype=np.int64)
    >>> result = np.empty(b.size, dtype=np.bool_)

    # actually usage
    >>> from cykhash import Int64Set_from_buffer, isin_int64

    >>> lookup = Int64Set_from_buffer(a) # create a hashset
    >>> isin_int64(b, lookup, result)    # running time O(b.size)
    >>> isin_int64(b, lookup, result)    # lookup is reused and not recreated


### `unique`

Finding `unique` in `O(n)` (compared to numpy's  `np.unique` - `O(n*logn)`) and smaller memory-footprint than pandas' `pd.unique`:

    # prepare input
    >>> import numpy as np
    >>> a = np.array([1,2,3,3,2,1], dtype=np.int64)
    
    # actual usage:
    >>> from cykhash import unique_int64
    >>> unique_buffer = unique_int64(a) # unique element are exposed via buffer-protocol

    # can be converted to a numpy-array without copying via
    >>> unique_array = np.ctypeslib.as_array(unique_buffer)
    >>> unique_array.shape
    (3,)


### Hash map

Maps and sets handle `nan`-correctly (try it out with Python's dict/set):

    >>> from cykhash import Float64toInt64Map
    >>> my_map = Float64toInt64Map() # values are 64bit integers
    >>> my_map[float("nan")] = 1
    >>> my_map[float("nan")]
    1



## Functionality overview

### Hash sets

`Int64Set`, `Int32Set`, `Float64Set`, `Float32Set` ( and `PyObjectSet`) are implemented. They are more or less drop-in replacements for Python's `set`. Furthermore, given the Cython-interface, efficient extensions of functionality are easily done.


The biggest advantage of these sets is that they need about 4-8 times less memory than the usual Python-sets and are somewhat faster for integers or floats. 

As `PyObjectSet` is somewhat slower than the usual `set` and needs about the same amount of memory, it should be used only if all `nan`s should be treated as equivalent.

The most efficient way to create such sets is to use `XXXXSet_from_buffer(...)`, e.g. `Int64Set_from_buffer`, if the data container at hand supports buffer protocol (e.g. numpy-arrays, `array.array` or `ctypes`-arrays). Or `XXXXSet_from(...)` for any iterator.


### Hash maps

`Int64toInt64Map`, `Int32toInt32Map`, `Float64toInt64Map`, `Float32toInt32Map` ( and `PyObjectMap`) are implemented. They are more or less drop-in replacements for Python's `dict` (however, not every piece of `dict`'s functionality makes sense, for example `setdefault(x, default)` without `default`-argument, because `None` cannot be inserted, also the khash-maps don't preserve the insertion order, so there is also no `reversed`). Furthermore, given the Cython-interface, efficient extensions of functionality are easily done.

Biggest advantage of these sets is that they need about 4-8 times less memory than the usual Python-dictionaries and are somewhat faster for integers or floats.


As `PyObjectMap` is somewhat slower than the usual `dict` and needs about the same amount of memory, it should be used only if all `nan`s should be treated as equivalent.

### isin

  * implemented are `isin_int64`, `isin_int32`, `isin_float64`, `isin_float32`
  * using hash set instead of arrays in `isin` function has the advantage, that the look-up data structure doesn't have to be reconstructed for every call, thus reducing the running time from `O(n+m)`to `O(n)`, where `n` is the number of queries and `m`-number of elements in the look up array.
  * Thus cykash's `isin` can be order of magnitude faster than the numpy's or pandas' versions.

#### all, none, any, and count_if

  * siblings functions of `isin_XXX` are:
      * `all_XXX`/`all_XXX_from_iterator` which return `True` if all elements of the query array can be found in the set.
      * `any_XXX`/`any_XXX_from_iterator` which return `True` if at least one element of the query array can be found in the set.
      * `none_XXX`/`none_XXX_from_iterator` which return `True` if none of  elements from the query array can be found in the set.
      * `count_if_XXX`/`count_if_XXX_from_iterator` which return the number of elements from the query array can be found in the set.
  * `all_XXX`, `any_XXX`, `none_XXX` and `count_if_XXX` are faster than using `isin_XXX` and applying numpy's versions of these function on the resulting array.
  * `from_iterator` version works with any iterable, but the version for buffers are more efficient.

### unique

  * implemented are `unique_int64`, `unique_int32`, `unique_float64`, `unique_float32`
  * returns an object which implements the buffer protocol, so `np.ctypeslib.as_array` (recommended) or `np.frombuffer` (less safe, as memory can get reinterpreted) can be used to create numpy arrays.
  * differently as pandas, the returned uniques aren't in the order of the appearance. If order of appearence is important use `unique_stable_xxx`-versions, which needs somewhat more memory.
  * the signature is `unique_xxx(buffer, size_hint=0.0)` the initial memory-consumption of the hash-set will be `len(buffer)*size_hint` unless `size_hint<=0.0`, in this case it will be ensured, that no rehashing is needed even if all elements are unique in the buffer.

As pandas uses maps instead of sets internally for `unique`, it needs about 4 times more peak memory and is 1.6-3 times slower.


### Floating-point numbers as keys

There is a problem with floating-point sets or maps, i.e. `Float64Set`, `Float32Set`, `Float64toInt64Map` and `Float32toInt32Map`: The standard definition of "equal" and hash-function based on the bit representation don't define a meaningful or desired behavior for the hash set:

   * `NAN != NAN` and thus it is not equivalence relation
   * `-0.0 == 0.0` but `hash(-0.0)!=hash(0.0)`, but `x==y => hash(x)==hash(y)` is neccessary for set to work properly.

This problem is resolved through following special case handling:

   * `hash(-0.0):=hash(0.0)`
   * `hash(x):=hash(NAN)` for any not a number `x`.
   * `x is equal y <=> x==y || (x!=x && y!=y)`

A consequence of the above rule, that the equivalence classes of `{0.0, -0.0}` and `e{x | x is not a number}` have more than one element. In the set these classes are represented by the first seen element from the class.

The above holds also for `PyObjectSet` (this behavior is not the same as fro Python-`set` which shows a different behavior for nans).

### Examples:

#### Hash sets

Python: Creates a set from a numpy-array and looks up whether an element is in the resulting set:

    >>> import numpy as np
    >>> from cykhash import Int64Set_from_buffer
    >>> a =  np.arange(42, dtype=np.int64)
    >>> my_set = Int64Set_from_buffer(a) # no reallocation will be needed
    >>> 41 in my_set 
    True
    >>> 42 not in my_set
    True

Python: Create a set from an iterable and looks up whether an element is in the resulting set:

    >>> from cykhash import Int64Set_from
    >>> my_set = Int64Set_from(range(42)) # no reallocation will be needed
    >>> assert 41 in my_set and 42 not in my_set

Cython: Create a set and put some values into it:

    from cykhash.khashsets cimport Int64Set
    my_set = Int64Set(number_of_elements_hint=12)  # reserve place for at least 12 integers
    cdef Py_ssize_t i
    for i in range(12):
       my_set.add(i)
    assert 11 in my_set and 12 not in my_set

#### Hash maps

Python: Creating `int64->float64` map using `Int64toFloat64Map_from_buffers`:

    >>> import numpy as np
    >>> from cykhash import Int64toFloat64Map_from_buffers
    >>> keys = np.array([1, 2, 3, 4], dtype=np.int64)
    >>> vals = np.array([5, 6, 7, 8], dtype=np.float64)
    >>> my_map = Int64toFloat64Map_from_buffers(keys, vals) # there will be no reallocation
    >>> assert my_map[4] == 8.0

Python: Creating `int64->int64` map from scratch:

    >>> import numpy as np
    >>> from cykhash import Int64toInt64Map

    # my_map will not need reallocation for at least 12 elements
    >>> my_map = Int64toInt64Map(number_of_elements_hint=12)
    >>> for i in range(12):  my_map[i] = i+1
    >>> assert my_map[5] == 6


#### isin

Python: Creating look-up data structure from a numpy-array, performing `isin`-query

    >>> import numpy as np
    >>> from cykhash import Int64Set_from_buffer, isin_int64
    >>> a = np.arange(42, dtype=np.int64)
    >>> lookup = Int64Set_from_buffer(a)

    >>> b = np.arange(84, dtype=np.int64)
    >>> result = np.empty(b.size, dtype=np.bool_)

    >>> isin_int64(b, lookup, result)    # running time O(b.size)
    >>> assert np.sum(result.astype(np.int_)) == 42


#### unique

Python: using `unique_int64`:

    >>> import numpy as np
    >>> from cykhash import unique_int64
    >>> a = np.array([1,2,3,3,2,1], dtype=np.int64)
    >>> u = np.ctypeslib.as_array(unique_int64(a)) # there will be no reallocation
    >>> assert set(u) == {1,2,3}

Python: using `unique_stable_int64`: 

    >>> import numpy as np
    >>> from cykhash import unique_stable_int64
    >>> a = np.array([3,2,1,1,2,3], dtype=np.int64)
    >>> u = np.ctypeslib.as_array(unique_stable_int64(a)) # there will be no reallocation
    >>> assert list(u) == [3,2,1] 



## API

See (https://github.com/realead/cykhash/blob/master/doc/README_API.md) for a more detailed API description.

## Performance

See (https://github.com/realead/cykhash/blob/master/doc/README_PERFORMANCE.md) for results of performance tests.

## Trivia

* This project was inspired by the following stackoverflow question: https://stackoverflow.com/questions/50779617/pandas-pd-series-isin-performance-with-set-versus-array.

* pandas also uses `khash` (and thus was a source of inspiration), but wraps only maps and doesn't wrap sets. Thus, pandas' `unique` needs more memory as it should. Those maps are also never exposed, so there is no way to reuse the look-up structure for multiple calls to `isin`.

* `khash` is a good choice, but there are other alternatives, e.g. https://github.com/sparsehash/sparsehash. See also https://stackoverflow.com/questions/48129713/fastest-way-to-find-all-unique-elements-in-an-array-with-cython/48142655#48142655 for a comparison for different `unique` implementations.

* A similar approach for sets/maps in pure Cython: https://github.com/realead/tighthash, which is quite slower than khash.

* There is no dependency on `numpy`: this library uses buffer protocol, thus it works for `array.array`, `numpy.ndarray`, `ctypes`-arrays and anything else. However, some interfaces are somewhat cumbersome (which type should be created as answer?) and for convenient usage it might be a good idea to wrap the functionality so objects of right types are created.

## Compatibility between cykhash-versions:

There are different levels of compatibility: 

 * for code using only pure python interface
 * for code using cython/cdef-interface and built against a particular cykash version

Ther rules are as follows:
 
 * there is no warranty for major versions mismatch: i.e. code written with cykhash `1.x.y` might not run with cykhash `2.z.w` and vice versa.
 * if only pure python interface is used, code for the same major version will ran for version with higher minor version, i.e. code for cykhash `2.0.x` will run with cykhash `2.1.y` (but not the other way around: that means new functions could be added to pure python interface)
 * if cython's `cdef` interface is used, i.e. a cython-extension was build using pxi-files from cykhash, then versions are compartible only if the the minor versions are the same, e.g. `2.0.x` could be replaced by `2.0.y` in the installation, but when replacing with `2.1.z` the dependent cython-extension must be rebuilt.

## History:
### Release 2.0.1 (05.02.2022):

  * Tests work for Python 3.11
  * Tests work for numpy 1.24
  * Drops support for Python 3.6 and Python 3.7

#### Release 2.0.0 (09.11.2021):

  * Implementation of `any`, `all`, `none` and `count_if`
  * Hash-sets are now (almost) drop-in replacements of Python's sets
  * Breaking change: iterator from maps doesn't no longer returns items but only keys. However there are following new methods `keys()`, `values()` and `items()`which return so called mapvies, which correspond more or less to dictviews (but for mapsview doesn't hold that "Dictionary order is guaranteed to be insertion order.").
  * Hash-Maps are now (almost) drop-in replacements of Python's dicts. Differences: insertion order isn't preserved, thus there is also no `reversed()`-method, `setdefault(key, default)` isn't possible without `default` because `None` cannot be inserted in the map
  * Better hash-functions for float64, float32, int64 and int32 (gh-issue #4).
  * Breaking change: different names/signatures for maps
  * supports tracemalloc for Py3.6+
  * supports Python 3.10

#### Release 1.0.2 (30.05.2020):

  * can be installed via conda-forge to all operating systems
  * can be installed via pip in a clean environment (Cython>=0.28 is now fetched automatically)

#### Release 1.0.1 (27.05.2020):

  * released on PyPi

#### Older:

  * 0.4.0: uniques_stable, preparing for release
  * 0.3.0: PyObjectSet, Maps for Int64/32 and also Float64/32, unique-versions
  * 0.2.0: Int32Set, Float64Set, Float32Set
  * 0.1.0: Int64Set





%package help
Summary:	Development documents and examples for cykhash
Provides:	python3-cykhash-doc
%description help
# cykhash

cython wrapper for khash-sets/maps, efficient implementation of `isin` and `unique`

## About:

  * Brings functionality of khash (https://github.com/attractivechaos/klib/blob/master/khash.h) to Python and Cython and can be used seamlessly in numpy or pandas.

  * Numpy's world is lacking the concept of a (hash-)set. This shortcoming is fixed and efficient (memory- and speedwise compared to pandas') `unique` and `isin` are implemented.

  * Python-set/dict have big memory-footprint. For some datatypes the overhead can be reduced by using khash by factor 4-8.

## Installation:

The recommended way to install the library is via `conda` package manager using the `conda-forge` channel:

    conda install -c conda-forge cykhash

You can also install the library using `pip`. To install the latest release:

    pip install cykhash

To install the most recent version of the module:

    pip install https://github.com/realead/cykhash/zipball/master

Attention: On Linux/Mac `python-dev` should be installed for that (see also https://stackoverflow.com/questions/21530577/fatal-error-python-h-no-such-file-or-directory) and MSVC on Windows.

## Dependencies:

To build the library from source, Cython>=0.28 is required as well as a c-build tool chain.

See (https://github.com/realead/cykhash/blob/master/doc/README4DEVELOPER.md) for dependencies needed for development.

## Quick start

#### Hash set and isin

Creating a hashset and using it in `isin`:

    # prepare data:
    >>> import numpy as np 
    >>> a = np.arange(42, dtype=np.int64)
    >>> b = np.arange(84, dtype=np.int64)
    >>> result = np.empty(b.size, dtype=np.bool_)

    # actually usage
    >>> from cykhash import Int64Set_from_buffer, isin_int64

    >>> lookup = Int64Set_from_buffer(a) # create a hashset
    >>> isin_int64(b, lookup, result)    # running time O(b.size)
    >>> isin_int64(b, lookup, result)    # lookup is reused and not recreated


### `unique`

Finding `unique` in `O(n)` (compared to numpy's  `np.unique` - `O(n*logn)`) and smaller memory-footprint than pandas' `pd.unique`:

    # prepare input
    >>> import numpy as np
    >>> a = np.array([1,2,3,3,2,1], dtype=np.int64)
    
    # actual usage:
    >>> from cykhash import unique_int64
    >>> unique_buffer = unique_int64(a) # unique element are exposed via buffer-protocol

    # can be converted to a numpy-array without copying via
    >>> unique_array = np.ctypeslib.as_array(unique_buffer)
    >>> unique_array.shape
    (3,)


### Hash map

Maps and sets handle `nan`-correctly (try it out with Python's dict/set):

    >>> from cykhash import Float64toInt64Map
    >>> my_map = Float64toInt64Map() # values are 64bit integers
    >>> my_map[float("nan")] = 1
    >>> my_map[float("nan")]
    1



## Functionality overview

### Hash sets

`Int64Set`, `Int32Set`, `Float64Set`, `Float32Set` ( and `PyObjectSet`) are implemented. They are more or less drop-in replacements for Python's `set`. Furthermore, given the Cython-interface, efficient extensions of functionality are easily done.


The biggest advantage of these sets is that they need about 4-8 times less memory than the usual Python-sets and are somewhat faster for integers or floats. 

As `PyObjectSet` is somewhat slower than the usual `set` and needs about the same amount of memory, it should be used only if all `nan`s should be treated as equivalent.

The most efficient way to create such sets is to use `XXXXSet_from_buffer(...)`, e.g. `Int64Set_from_buffer`, if the data container at hand supports buffer protocol (e.g. numpy-arrays, `array.array` or `ctypes`-arrays). Or `XXXXSet_from(...)` for any iterator.


### Hash maps

`Int64toInt64Map`, `Int32toInt32Map`, `Float64toInt64Map`, `Float32toInt32Map` ( and `PyObjectMap`) are implemented. They are more or less drop-in replacements for Python's `dict` (however, not every piece of `dict`'s functionality makes sense, for example `setdefault(x, default)` without `default`-argument, because `None` cannot be inserted, also the khash-maps don't preserve the insertion order, so there is also no `reversed`). Furthermore, given the Cython-interface, efficient extensions of functionality are easily done.

Biggest advantage of these sets is that they need about 4-8 times less memory than the usual Python-dictionaries and are somewhat faster for integers or floats.


As `PyObjectMap` is somewhat slower than the usual `dict` and needs about the same amount of memory, it should be used only if all `nan`s should be treated as equivalent.

### isin

  * implemented are `isin_int64`, `isin_int32`, `isin_float64`, `isin_float32`
  * using hash set instead of arrays in `isin` function has the advantage, that the look-up data structure doesn't have to be reconstructed for every call, thus reducing the running time from `O(n+m)`to `O(n)`, where `n` is the number of queries and `m`-number of elements in the look up array.
  * Thus cykash's `isin` can be order of magnitude faster than the numpy's or pandas' versions.

#### all, none, any, and count_if

  * siblings functions of `isin_XXX` are:
      * `all_XXX`/`all_XXX_from_iterator` which return `True` if all elements of the query array can be found in the set.
      * `any_XXX`/`any_XXX_from_iterator` which return `True` if at least one element of the query array can be found in the set.
      * `none_XXX`/`none_XXX_from_iterator` which return `True` if none of  elements from the query array can be found in the set.
      * `count_if_XXX`/`count_if_XXX_from_iterator` which return the number of elements from the query array can be found in the set.
  * `all_XXX`, `any_XXX`, `none_XXX` and `count_if_XXX` are faster than using `isin_XXX` and applying numpy's versions of these function on the resulting array.
  * `from_iterator` version works with any iterable, but the version for buffers are more efficient.

### unique

  * implemented are `unique_int64`, `unique_int32`, `unique_float64`, `unique_float32`
  * returns an object which implements the buffer protocol, so `np.ctypeslib.as_array` (recommended) or `np.frombuffer` (less safe, as memory can get reinterpreted) can be used to create numpy arrays.
  * differently as pandas, the returned uniques aren't in the order of the appearance. If order of appearence is important use `unique_stable_xxx`-versions, which needs somewhat more memory.
  * the signature is `unique_xxx(buffer, size_hint=0.0)` the initial memory-consumption of the hash-set will be `len(buffer)*size_hint` unless `size_hint<=0.0`, in this case it will be ensured, that no rehashing is needed even if all elements are unique in the buffer.

As pandas uses maps instead of sets internally for `unique`, it needs about 4 times more peak memory and is 1.6-3 times slower.


### Floating-point numbers as keys

There is a problem with floating-point sets or maps, i.e. `Float64Set`, `Float32Set`, `Float64toInt64Map` and `Float32toInt32Map`: The standard definition of "equal" and hash-function based on the bit representation don't define a meaningful or desired behavior for the hash set:

   * `NAN != NAN` and thus it is not equivalence relation
   * `-0.0 == 0.0` but `hash(-0.0)!=hash(0.0)`, but `x==y => hash(x)==hash(y)` is neccessary for set to work properly.

This problem is resolved through following special case handling:

   * `hash(-0.0):=hash(0.0)`
   * `hash(x):=hash(NAN)` for any not a number `x`.
   * `x is equal y <=> x==y || (x!=x && y!=y)`

A consequence of the above rule, that the equivalence classes of `{0.0, -0.0}` and `e{x | x is not a number}` have more than one element. In the set these classes are represented by the first seen element from the class.

The above holds also for `PyObjectSet` (this behavior is not the same as fro Python-`set` which shows a different behavior for nans).

### Examples:

#### Hash sets

Python: Creates a set from a numpy-array and looks up whether an element is in the resulting set:

    >>> import numpy as np
    >>> from cykhash import Int64Set_from_buffer
    >>> a =  np.arange(42, dtype=np.int64)
    >>> my_set = Int64Set_from_buffer(a) # no reallocation will be needed
    >>> 41 in my_set 
    True
    >>> 42 not in my_set
    True

Python: Create a set from an iterable and looks up whether an element is in the resulting set:

    >>> from cykhash import Int64Set_from
    >>> my_set = Int64Set_from(range(42)) # no reallocation will be needed
    >>> assert 41 in my_set and 42 not in my_set

Cython: Create a set and put some values into it:

    from cykhash.khashsets cimport Int64Set
    my_set = Int64Set(number_of_elements_hint=12)  # reserve place for at least 12 integers
    cdef Py_ssize_t i
    for i in range(12):
       my_set.add(i)
    assert 11 in my_set and 12 not in my_set

#### Hash maps

Python: Creating `int64->float64` map using `Int64toFloat64Map_from_buffers`:

    >>> import numpy as np
    >>> from cykhash import Int64toFloat64Map_from_buffers
    >>> keys = np.array([1, 2, 3, 4], dtype=np.int64)
    >>> vals = np.array([5, 6, 7, 8], dtype=np.float64)
    >>> my_map = Int64toFloat64Map_from_buffers(keys, vals) # there will be no reallocation
    >>> assert my_map[4] == 8.0

Python: Creating `int64->int64` map from scratch:

    >>> import numpy as np
    >>> from cykhash import Int64toInt64Map

    # my_map will not need reallocation for at least 12 elements
    >>> my_map = Int64toInt64Map(number_of_elements_hint=12)
    >>> for i in range(12):  my_map[i] = i+1
    >>> assert my_map[5] == 6


#### isin

Python: Creating look-up data structure from a numpy-array, performing `isin`-query

    >>> import numpy as np
    >>> from cykhash import Int64Set_from_buffer, isin_int64
    >>> a = np.arange(42, dtype=np.int64)
    >>> lookup = Int64Set_from_buffer(a)

    >>> b = np.arange(84, dtype=np.int64)
    >>> result = np.empty(b.size, dtype=np.bool_)

    >>> isin_int64(b, lookup, result)    # running time O(b.size)
    >>> assert np.sum(result.astype(np.int_)) == 42


#### unique

Python: using `unique_int64`:

    >>> import numpy as np
    >>> from cykhash import unique_int64
    >>> a = np.array([1,2,3,3,2,1], dtype=np.int64)
    >>> u = np.ctypeslib.as_array(unique_int64(a)) # there will be no reallocation
    >>> assert set(u) == {1,2,3}

Python: using `unique_stable_int64`: 

    >>> import numpy as np
    >>> from cykhash import unique_stable_int64
    >>> a = np.array([3,2,1,1,2,3], dtype=np.int64)
    >>> u = np.ctypeslib.as_array(unique_stable_int64(a)) # there will be no reallocation
    >>> assert list(u) == [3,2,1] 



## API

See (https://github.com/realead/cykhash/blob/master/doc/README_API.md) for a more detailed API description.

## Performance

See (https://github.com/realead/cykhash/blob/master/doc/README_PERFORMANCE.md) for results of performance tests.

## Trivia

* This project was inspired by the following stackoverflow question: https://stackoverflow.com/questions/50779617/pandas-pd-series-isin-performance-with-set-versus-array.

* pandas also uses `khash` (and thus was a source of inspiration), but wraps only maps and doesn't wrap sets. Thus, pandas' `unique` needs more memory as it should. Those maps are also never exposed, so there is no way to reuse the look-up structure for multiple calls to `isin`.

* `khash` is a good choice, but there are other alternatives, e.g. https://github.com/sparsehash/sparsehash. See also https://stackoverflow.com/questions/48129713/fastest-way-to-find-all-unique-elements-in-an-array-with-cython/48142655#48142655 for a comparison for different `unique` implementations.

* A similar approach for sets/maps in pure Cython: https://github.com/realead/tighthash, which is quite slower than khash.

* There is no dependency on `numpy`: this library uses buffer protocol, thus it works for `array.array`, `numpy.ndarray`, `ctypes`-arrays and anything else. However, some interfaces are somewhat cumbersome (which type should be created as answer?) and for convenient usage it might be a good idea to wrap the functionality so objects of right types are created.

## Compatibility between cykhash-versions:

There are different levels of compatibility: 

 * for code using only pure python interface
 * for code using cython/cdef-interface and built against a particular cykash version

Ther rules are as follows:
 
 * there is no warranty for major versions mismatch: i.e. code written with cykhash `1.x.y` might not run with cykhash `2.z.w` and vice versa.
 * if only pure python interface is used, code for the same major version will ran for version with higher minor version, i.e. code for cykhash `2.0.x` will run with cykhash `2.1.y` (but not the other way around: that means new functions could be added to pure python interface)
 * if cython's `cdef` interface is used, i.e. a cython-extension was build using pxi-files from cykhash, then versions are compartible only if the the minor versions are the same, e.g. `2.0.x` could be replaced by `2.0.y` in the installation, but when replacing with `2.1.z` the dependent cython-extension must be rebuilt.

## History:
### Release 2.0.1 (05.02.2022):

  * Tests work for Python 3.11
  * Tests work for numpy 1.24
  * Drops support for Python 3.6 and Python 3.7

#### Release 2.0.0 (09.11.2021):

  * Implementation of `any`, `all`, `none` and `count_if`
  * Hash-sets are now (almost) drop-in replacements of Python's sets
  * Breaking change: iterator from maps doesn't no longer returns items but only keys. However there are following new methods `keys()`, `values()` and `items()`which return so called mapvies, which correspond more or less to dictviews (but for mapsview doesn't hold that "Dictionary order is guaranteed to be insertion order.").
  * Hash-Maps are now (almost) drop-in replacements of Python's dicts. Differences: insertion order isn't preserved, thus there is also no `reversed()`-method, `setdefault(key, default)` isn't possible without `default` because `None` cannot be inserted in the map
  * Better hash-functions for float64, float32, int64 and int32 (gh-issue #4).
  * Breaking change: different names/signatures for maps
  * supports tracemalloc for Py3.6+
  * supports Python 3.10

#### Release 1.0.2 (30.05.2020):

  * can be installed via conda-forge to all operating systems
  * can be installed via pip in a clean environment (Cython>=0.28 is now fetched automatically)

#### Release 1.0.1 (27.05.2020):

  * released on PyPi

#### Older:

  * 0.4.0: uniques_stable, preparing for release
  * 0.3.0: PyObjectSet, Maps for Int64/32 and also Float64/32, unique-versions
  * 0.2.0: Int32Set, Float64Set, Float32Set
  * 0.1.0: Int64Set





%prep
%autosetup -n cykhash-2.0.1

%build
%py3_build

%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
	find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
	find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
	find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
	find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
	find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .

%files -n python3-cykhash -f filelist.lst
%dir %{python3_sitelib}/*

%files help -f doclist.lst
%{_docdir}/*

%changelog
* Fri May 05 2023 Python_Bot <Python_Bot@openeuler.org> - 2.0.1-1
- Package Spec generated