spatial_stats 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3d63578b1952570f2de9a3ab045c8a977988960c2942c73416b86de6b228a091
4
- data.tar.gz: 591927bf3c2fd3d7723b7e155bbae6a2c73313b45c749c6ab552e445e5f3b609
3
+ metadata.gz: c45774fe6829810899c98f75430ac47a135f839762663381e9621f983e7a0baa
4
+ data.tar.gz: 719b2421748188426d1c44b73c00744d66a65b8ac5e23034a22d875da5fbfd61
5
5
  SHA512:
6
- metadata.gz: dfe901b03685c5d66539793d7446738c49b0cdaae3022559544620962aa9d63e4d668d0177b8dc3c44cff797ce5c8ebed8fdeba87a52ec28ebdf0d02b69625b3
7
- data.tar.gz: 3cfde56bb984bfbf3ab83bb89e02797a24eb47b1a8387e695890127ee5711f185a114f300906f96728c0e51996f223091f0a9455713f8fb3c3ff34b8285615da
6
+ metadata.gz: 8ab6221b26cd81ab2af42d195349d188ec482d6cb346677a28d3a69c16e8472060645938f806df6f14ee76eabef11d0e7e12904513b47b005ebf75d6e898caac
7
+ data.tar.gz: 2d0a3df4f22008bef642dcff5c74b2ae8c8c4590314ea110a58fedf8a4c949649ba649f8bca77016fb1a0cf49d0571eee7fecd007d610d873693f98a8fbab6b6
@@ -28,60 +28,79 @@ VALUE csr_matrix_alloc(VALUE self)
28
28
  return TypedData_Wrap_Struct(self, &csr_matrix_type, csr);
29
29
  }
30
30
 
31
- void mat_to_sparse(csr_matrix *csr, VALUE data, VALUE num_rows, VALUE num_cols)
31
+ void mat_to_sparse(csr_matrix *csr, VALUE data, VALUE keys, VALUE num_rows)
32
32
  {
33
+
33
34
  int nnz = 0;
34
- int m = NUM2INT(num_rows);
35
- int n = NUM2INT(num_cols);
35
+ int n = NUM2INT(num_rows);
36
+ int m;
37
+
38
+ VALUE key;
39
+ VALUE row;
40
+ VALUE entry;
41
+ VALUE key_lookup = rb_hash_new();
42
+ VALUE weight_sym = ID2SYM(rb_intern("weight"));
43
+ VALUE id_sym = ID2SYM(rb_intern("id"));
36
44
 
37
45
  double *values;
38
46
  int *col_index;
39
47
  int *row_index;
40
48
 
41
49
  int nz_idx;
42
- double entry;
50
+ double weight;
43
51
 
44
52
  int i;
45
53
  int j;
46
- int index;
47
54
 
48
55
  // first get number non zero count so we can alloc values and col_index
49
- for (i = 0; i < m; i++)
56
+ for (i = 0; i < n; i++)
50
57
  {
51
- for (j = 0; j < n; j++)
52
- {
53
- index = i * n + j;
54
- if (NUM2DBL(rb_ary_entry(data, index)) != 0)
55
- {
56
- nnz++;
57
- }
58
- }
58
+ key = rb_ary_entry(keys, i);
59
+
60
+ // set lookup index for this key
61
+ rb_hash_aset(key_lookup, key, INT2NUM(i));
62
+
63
+ // check the value of this row is actually an array
64
+ // if it is, add array len to nnz
65
+ row = rb_hash_aref(data, key);
66
+ Check_Type(row, T_ARRAY);
67
+ nnz += rb_array_len(row);
59
68
  }
60
69
 
61
70
  values = malloc(sizeof(double) * nnz);
62
71
  col_index = malloc(sizeof(int) * nnz);
63
- row_index = malloc(sizeof(int) * (m + 1));
72
+ row_index = malloc(sizeof(int) * (n + 1));
64
73
 
65
- // for every non-zero, record value, column and then get values per row
74
+ // for every row, work through each hash
75
+ // in each hash, add the weight to values and get col_index
76
+ // by looking at the key_lookup of id.
77
+ // Row index will be computed by adding len of each row and updating array.
66
78
  nz_idx = 0;
67
- for (i = 0; i < m; i++)
79
+ for (i = 0; i < n; i++)
68
80
  {
69
81
  row_index[i] = nz_idx;
70
- for (j = 0; j < n; j++)
82
+
83
+ key = rb_ary_entry(keys, i);
84
+ row = rb_hash_aref(data, key);
85
+ m = rb_array_len(row);
86
+
87
+ for (j = 0; j < m; j++)
71
88
  {
72
- index = i * n + j;
73
- entry = NUM2DBL(rb_ary_entry(data, index));
74
- if (entry != 0)
75
- {
76
- values[nz_idx] = entry;
77
- col_index[nz_idx] = j;
78
- nz_idx++;
79
- }
89
+ entry = rb_ary_entry(row, j);
90
+ Check_Type(entry, T_HASH);
91
+
92
+ key = rb_hash_aref(entry, id_sym);
93
+ weight = NUM2DBL(rb_hash_aref(entry, weight_sym));
94
+
95
+ // assign the nnz the weight
96
+ // get index in the keys array of key from lookup table
97
+ values[nz_idx] = weight;
98
+ col_index[nz_idx] = NUM2INT(rb_hash_aref(key_lookup, key));
99
+ nz_idx++;
80
100
  }
81
101
  }
82
- row_index[m] = nnz;
102
+ row_index[n] = nnz;
83
103
 
84
- csr->m = m;
85
104
  csr->n = n;
86
105
  csr->nnz = nnz;
87
106
  csr->values = values;
@@ -112,27 +131,27 @@ void mat_to_sparse(csr_matrix *csr, VALUE data, VALUE num_rows, VALUE num_cols)
112
131
  *
113
132
  * @return [CSRMatrix]
114
133
  */
115
- VALUE csr_matrix_initialize(VALUE self, VALUE data, VALUE num_rows, VALUE num_cols)
134
+ VALUE csr_matrix_initialize(VALUE self, VALUE data, VALUE num_rows)
116
135
  {
117
-
136
+ VALUE keys;
118
137
  csr_matrix *csr;
119
138
  TypedData_Get_Struct(self, csr_matrix, &csr_matrix_type, csr);
120
139
  csr->init = 0;
121
140
 
122
- Check_Type(data, T_ARRAY);
141
+ Check_Type(data, T_HASH);
123
142
  Check_Type(num_rows, T_FIXNUM);
124
- Check_Type(num_cols, T_FIXNUM);
143
+
144
+ keys = rb_funcall(data, rb_intern("keys"), 0);
125
145
 
126
146
  // check dimensions are correct
127
- if (NUM2INT(num_rows) * NUM2INT(num_cols) != rb_array_len(data))
147
+ if (NUM2INT(num_rows) != rb_array_len(keys))
128
148
  {
129
- rb_raise(rb_eArgError, "n_rows * n_cols != data.size, check your dimensions");
149
+ rb_raise(rb_eArgError, "n_rows != keys.size, check your dimensions");
130
150
  }
131
151
 
132
- mat_to_sparse(csr, data, num_rows, num_cols);
152
+ mat_to_sparse(csr, data, keys, num_rows);
133
153
 
134
- rb_iv_set(self, "@m", num_rows);
135
- rb_iv_set(self, "@n", num_cols);
154
+ rb_iv_set(self, "@n", num_rows);
136
155
  rb_iv_set(self, "@nnz", INT2NUM(csr->nnz));
137
156
 
138
157
  return self;
@@ -203,8 +222,8 @@ VALUE csr_matrix_row_index(VALUE self)
203
222
 
204
223
  TypedData_Get_Struct(self, csr_matrix, &csr_matrix_type, csr);
205
224
 
206
- result = rb_ary_new_capa(csr->m + 1);
207
- for (i = 0; i <= csr->m; i++)
225
+ result = rb_ary_new_capa(csr->n + 1);
226
+ for (i = 0; i <= csr->n; i++)
208
227
  {
209
228
  rb_ary_store(result, i, INT2NUM(csr->row_index[i]));
210
229
  }
@@ -239,11 +258,11 @@ VALUE csr_matrix_mulvec(VALUE self, VALUE vec)
239
258
  rb_raise(rb_eArgError, "Dimension Mismatch CSRMatrix.n != vec.size");
240
259
  }
241
260
 
242
- result = rb_ary_new_capa(csr->m);
261
+ result = rb_ary_new_capa(csr->n);
243
262
 
244
263
  // float *vals = (float *)DATA_PTR(result);
245
264
 
246
- for (i = 0; i < csr->m; i++)
265
+ for (i = 0; i < csr->n; i++)
247
266
  {
248
267
  tmp = 0;
249
268
  for (jj = csr->row_index[i]; jj < csr->row_index[i + 1]; jj++)
@@ -285,7 +304,7 @@ VALUE csr_matrix_dot_row(VALUE self, VALUE vec, VALUE row)
285
304
  }
286
305
 
287
306
  i = NUM2INT(row);
288
- if (!(i >= 0 && i < csr->m))
307
+ if (!(i >= 0 && i < csr->n))
289
308
  {
290
309
  rb_raise(rb_eArgError, "Index Error row_idx >= m or idx < 0");
291
310
  }
@@ -4,7 +4,6 @@
4
4
  typedef struct csr_matrix
5
5
  {
6
6
  char init;
7
- int m;
8
7
  int n;
9
8
  int nnz;
10
9
  double *values;
@@ -23,9 +22,9 @@ static const rb_data_type_t csr_matrix_type = {
23
22
  0,
24
23
  RUBY_TYPED_FREE_IMMEDIATELY};
25
24
 
26
- void mat_to_sparse(csr_matrix *csr, VALUE data, VALUE num_rows, VALUE num_cols);
25
+ void mat_to_sparse(csr_matrix *csr, VALUE data, VALUE keys, VALUE num_rows);
27
26
  VALUE csr_matrix_alloc(VALUE self);
28
- VALUE csr_matrix_initialize(VALUE self, VALUE data, VALUE num_rows, VALUE num_cols);
27
+ VALUE csr_matrix_initialize(VALUE self, VALUE data, VALUE num_rows);
29
28
  VALUE csr_matrix_values(VALUE self);
30
29
  VALUE csr_matrix_col_index(VALUE self);
31
30
  VALUE csr_matrix_row_index(VALUE self);
@@ -18,7 +18,7 @@ void Init_spatial_stats()
18
18
  VALUE csr_matrix_class = rb_define_class_under(weights_mod, "CSRMatrix", rb_cData);
19
19
 
20
20
  rb_define_alloc_func(csr_matrix_class, csr_matrix_alloc);
21
- rb_define_method(csr_matrix_class, "initialize", csr_matrix_initialize, 3);
21
+ rb_define_method(csr_matrix_class, "initialize", csr_matrix_initialize, 2);
22
22
  rb_define_method(csr_matrix_class, "values", csr_matrix_values, 0);
23
23
  rb_define_method(csr_matrix_class, "col_index", csr_matrix_col_index, 0);
24
24
  rb_define_method(csr_matrix_class, "row_index", csr_matrix_row_index, 0);
Binary file
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SpatialStats
4
- VERSION = '1.0.1'
4
+ VERSION = '1.0.2'
5
5
  end
@@ -55,7 +55,7 @@ module SpatialStats
55
55
  #
56
56
  # @return [CSRMatrix]
57
57
  def sparse
58
- @sparse ||= CSRMatrix.new(dense.to_a.flatten, n, n)
58
+ @sparse ||= CSRMatrix.new(weights, n)
59
59
  end
60
60
 
61
61
  ##
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spatial_stats
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keith Doggett