reflexive 2.0.0__py3-none-any.whl → 2.2.0__py3-none-any.whl

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.
@@ -7,7 +7,10 @@ from functools import partial
7
7
  import tarfile
8
8
  import json
9
9
  import os
10
- import numpy as np
10
+ from numpy import (
11
+ asarray,
12
+ dot
13
+ )
11
14
  from numpy.linalg import norm
12
15
  from itertools import chain
13
16
  from graph_tool.all import (
@@ -15,6 +18,7 @@ from graph_tool.all import (
15
18
  similarity,
16
19
  adjacency)
17
20
 
21
+
18
22
  ### PIPELINE FUNCTIONS
19
23
 
20
24
  # Clean text using supplied function and calculate text length
@@ -99,9 +103,15 @@ def _add_res_weights(df):
99
103
  temp_df['res_weights'] = temp_df.res_interactions.apply(_calc_res_weights)
100
104
  return temp_df
101
105
 
106
+ def _add_semantic_weights(df,ranking_factors={}):
107
+ temp_df = df.copy()
108
+ ranks = partial(_calc_semantic_weights,factors=ranking_factors)
109
+ temp_df['semantic_weights'] = temp_df.res_weights.apply(ranks)
110
+ return temp_df
111
+
102
112
  def _add_res_adj_matrix(df):
103
113
  temp_df = df.copy()
104
- temp_df['res_adj_matrix'] = temp_df.res_weights.apply(_create_adj_matrix)
114
+ temp_df['res_adj_matrix'] = temp_df.semantic_weights.apply(_create_adj_matrix)
105
115
  return temp_df
106
116
 
107
117
  def _get_res_sequence(offsets_clean):
@@ -134,6 +144,16 @@ def _calc_res_weights(interactions:dict[tuple,int])->dict[tuple,float]:
134
144
  for edge,count in interactions.items():
135
145
  weights[edge] = round(count/(max_count),2)
136
146
  return weights
147
+
148
+
149
+
150
+ def _calc_semantic_weights(weights:dict[tuple,float], factors:dict[tuple,float]={})->dict[tuple,float]:
151
+ if not factors:
152
+ return weights
153
+ else:
154
+ for edge,w in weights.items():
155
+ weights[edge] = factors[edge] * w
156
+ return weights
137
157
 
138
158
 
139
159
  def _create_adj_matrix(weights:dict[tuple,float])->list[list[float]]:
@@ -149,7 +169,7 @@ def _create_adj_matrix(weights:dict[tuple,float])->list[list[float]]:
149
169
  matrix.append(row)
150
170
  return matrix
151
171
 
152
- ### GRAPH ANALYSIS
172
+ ### SIMILARITY ANALYSIS
153
173
 
154
174
  def _jaccard_similarity(g1:Graph,g2:Graph)->float:
155
175
  return similarity(g1, g2,
@@ -157,10 +177,67 @@ def _jaccard_similarity(g1:Graph,g2:Graph)->float:
157
177
  #label1=g1.vp['v_labels'], label2=g2.vp['v_labels'],
158
178
  norm=True, p=1.0, distance=False, asymmetric=False)
159
179
 
160
- def _cosine_similarity(m1,m2)->float:
161
- v1 = list(chain.from_iterable(m1))
162
- v2 = list(chain.from_iterable(m2))
163
- return np.dot(v1,v2)/(norm(v1)*norm(v2))
180
+ # def _cosine_similarity(m1,m2)->float:
181
+ # v1 = list(chain.from_iterable(m1))
182
+ # v2 = list(chain.from_iterable(m2))
183
+ # return np.dot(v1,v2)/(norm(v1)*norm(v2))
184
+
185
+ def _vectorise_adj(matrix):
186
+ return list(chain.from_iterable((matrix[i][j] for j in range(i,5)) for i in range(5)))
187
+
188
+ def zero_pos(am):
189
+ nm = []
190
+ for r,row in enumerate(am):
191
+ nr = []
192
+ for c,weight in enumerate(row):
193
+ if r < 3 or c < 3:
194
+ nr.append(weight)
195
+ else:
196
+ nr.append(0)
197
+ nm.append(nr)
198
+ return nm
199
+
200
+ def zero_mod(am):
201
+ nm = []
202
+ for r,row in enumerate(am):
203
+ nr = []
204
+ for c,weight in enumerate(row):
205
+ if r >= 3 or c >= 3:
206
+ nr.append(weight)
207
+ else:
208
+ nr.append(0)
209
+ nm.append(nr)
210
+ return nm
211
+
212
+ def _adj_vector(adj_matrix):
213
+ return _vectorise_adj(adj_matrix)
214
+
215
+ def _positional_vector(adj_matrix):
216
+ return _vectorise_adj(zero_pos(adj_matrix))
217
+
218
+ def _modal_vector(adj_matrix):
219
+ return _vectorise_adj(zero_mod(adj_matrix))
220
+
221
+ def _cosine(A,B):
222
+ return dot(A,B)/(norm(A)*norm(B))
223
+
224
+ def _am4idx(df,idx:int):
225
+ return df.res_adj_matrix[idx]
226
+
227
+ def _similarity(m1,m2,vector_func):
228
+ return float(_cosine(vector_func(m1),vector_func(m2)))
229
+
230
+ def _res_similarity(df,idx1,idx2,vector_func):
231
+ return _similarity(_am4idx(df,idx1),_am4idx(df,idx2),vector_func)
232
+
233
+ _interaction_similarity = partial(_res_similarity,vector_func=_adj_vector)
234
+ _positional_similarity = partial(_res_similarity,vector_func=_positional_vector)
235
+ _modal_similarity = partial(_res_similarity,vector_func=_modal_vector)
236
+
237
+ def _similarities(df,idx1,idx2):
238
+ return {"interaction": _interaction_similarity(df,idx1,idx2),
239
+ "positional": _positional_similarity(df,idx1,idx2),
240
+ "modal":_modal_similarity(df,idx1,idx2)}
164
241
 
165
242
 
166
243
 
reflexive/res_analysis.py CHANGED
@@ -20,9 +20,10 @@ from reflexive.analysis_functions import (
20
20
  _add_res_sequence,
21
21
  _add_res_interactions,
22
22
  _add_res_weights,
23
+ _add_semantic_weights,
23
24
  _add_res_adj_matrix,
24
25
  _jaccard_similarity,
25
- _cosine_similarity,
26
+ _similarities,
26
27
  _date_string,
27
28
  _create_local_dir
28
29
  )
@@ -39,6 +40,12 @@ class RES_analyser:
39
40
  config:dict
40
41
 
41
42
  logger = logging.getLogger(__name__)
43
+
44
+ ranking_factors = {('RR','RR'):0.5, ('NR','RR'):0.9, ('AR','RR'):0.9, ('AF','RR'):0.8, ('EP','RR'):0.7, #Alpha sorted tuples
45
+ ('NR','NR'):0.6, ('AR','NR'):1.0, ('AF','NR'):0.8, ('EP','NR'):0.7,
46
+ ('AR','AR'):0.6, ('AF','AR'):0.8, ('AR','EP'):0.7,
47
+ ('AF','AF'):0.5, ('AF','EP'):0.6,
48
+ ('EP','EP'):0.5}
42
49
 
43
50
 
44
51
  def __init__(self,parameters,prefix="res",postfix=None,dir="/data/")->None:
@@ -113,15 +120,20 @@ class RES_analyser:
113
120
  df = _add_res_sequence(df)
114
121
  df = _add_res_interactions(df)
115
122
  df = _add_res_weights(df)
123
+ df = _add_semantic_weights(df,self.ranking_factors)
116
124
  df = _add_res_adj_matrix(df)
117
125
  return df
126
+
127
+ # def add_semantic_weights(self,df):
128
+ # return _add_semantic_weights(df,self.ranking_factors)
118
129
 
119
130
  # Graph Jaccard Similarity
120
131
  def get_jaccard_similarity(self,g1,g2):
121
132
  return _jaccard_similarity(g1,g2)
122
133
 
123
- def get_cosine_similarity(self,m1,m2):
124
- return _cosine_similarity(m1,m2)
134
+ def get_cosine_similarities(self,df,idx1,idx2):
135
+ return _similarities(df,idx1,idx2)
136
+
125
137
 
126
138
  ############################################################
127
139
  # UTILITY METHODS
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: reflexive
3
- Version: 2.0.0
3
+ Version: 2.2.0
4
4
  Summary: Supports AWS Reflexive Expressions Systems (RES) Analysis
5
5
  Project-URL: Repository, https://github.com/nlytx/reflexive.git
6
6
  Author-email: Andrew Gibson <andrew@nlytx.io>
@@ -0,0 +1,9 @@
1
+ reflexive/__init__.py,sha256=UkA6xvSu40CGYguUuiBqBMnPBkld0h0GR9UnZrptdZI,167
2
+ reflexive/analysis_functions.py,sha256=ufB2AGaJyFZKYHppa7T30YYdONEotHVvLkrxMHFfKJE,16191
3
+ reflexive/display_functions.py,sha256=2LdkINOgmZfiV7nkW0x_IeimxW3J80YIOOnjX21-RJA,5699
4
+ reflexive/res_analysis.py,sha256=ZeXMA8ckRo1UCct4F4tjCwLbU3iBwShiIvnVGwP-x7E,7453
5
+ reflexive/service.py,sha256=O0MX2BCHTSNG_eW6LHBN1FOjaNTCGgYgh7vsk58NNAk,1927
6
+ reflexive-2.2.0.dist-info/METADATA,sha256=FNVFaVGcF8Oi7KuvM18RenUG5f4wNeVZSRY-ae7P_5s,574
7
+ reflexive-2.2.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
8
+ reflexive-2.2.0.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
9
+ reflexive-2.2.0.dist-info/RECORD,,
@@ -1,9 +0,0 @@
1
- reflexive/__init__.py,sha256=UkA6xvSu40CGYguUuiBqBMnPBkld0h0GR9UnZrptdZI,167
2
- reflexive/analysis_functions.py,sha256=cVTQtQnQigMJSCoQOtXI0_tyX49Re96Uz-ubx4UToUw,13966
3
- reflexive/display_functions.py,sha256=2LdkINOgmZfiV7nkW0x_IeimxW3J80YIOOnjX21-RJA,5699
4
- reflexive/res_analysis.py,sha256=jpb2Fh_jLEZmi4t6I6Q9nVE9LYRc_YJMGx12_co_69Y,6841
5
- reflexive/service.py,sha256=O0MX2BCHTSNG_eW6LHBN1FOjaNTCGgYgh7vsk58NNAk,1927
6
- reflexive-2.0.0.dist-info/METADATA,sha256=60ZmW9iHzBxs3BZeUm6KrsRq3LFwDQB6xGS_zuRaYoo,574
7
- reflexive-2.0.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
8
- reflexive-2.0.0.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
9
- reflexive-2.0.0.dist-info/RECORD,,