reflexive 1.2.2__py3-none-any.whl → 1.2.4__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.
reflexive/__init__.py CHANGED
@@ -12,4 +12,8 @@ from reflexive.visualise import (
12
12
  Display,
13
13
  RES_graph)
14
14
 
15
- __all__ = ["Config","AWS","S3","Comprehend","Nlp","Display","RES_graph"]
15
+ from reflexive.res import (
16
+ Res_analyse,
17
+ Res_display)
18
+
19
+ __all__ = ["Config","AWS","S3","Comprehend","Nlp","Display","RES_graph","Res_analyse","Res_display"]
reflexive/analyse.py CHANGED
@@ -8,7 +8,7 @@ from reflexive import cfg
8
8
  from reflexive import session
9
9
 
10
10
  import logging
11
- logging.basicConfig(level=logging.DEBUG)
11
+ #logging.basicConfig(level=logging.DEBUG)
12
12
  logger = logging.getLogger(__name__)
13
13
 
14
14
 
reflexive/res.py ADDED
@@ -0,0 +1,198 @@
1
+ import json
2
+ from pandas import DataFrame
3
+ from spacy import displacy
4
+ from time import sleep
5
+ from reflexive import Config
6
+ from reflexive import AWS
7
+ from reflexive import S3
8
+ from reflexive import Comprehend
9
+ from reflexive import Nlp
10
+ from reflexive import Display
11
+ from reflexive import RES_graph
12
+
13
+ class Res_analyse:
14
+
15
+ config:Config
16
+ aws:AWS
17
+ s3:S3
18
+ comprehend:Comprehend
19
+ nlp:Nlp
20
+
21
+ def __init__(self,aws_profile="default") -> None:
22
+ return self._setup(aws_profile)
23
+
24
+ def _setup(self,aws_profile) -> None:
25
+ self.config = Config(aws_profile)
26
+ self.config.set_parameters(name_prefix="RES")
27
+ self.aws = AWS(self.config)
28
+ return None
29
+
30
+ def set_parameters(self,
31
+ s3_access_point:str,
32
+ s3_bucket_name:str,
33
+ comprehend_service_role_name:str,
34
+ reflexive_entity_name:str,
35
+ reflexive_entity_version:str) -> str:
36
+ self.config.set_s3_parameters(s3_access_point,s3_bucket_name)
37
+ self.config.set_comprehend_parameters(comprehend_service_role_name)
38
+ self.config.set_comprehend_custom_entity_parameters(reflexive_entity_name,reflexive_entity_version)
39
+ params = self.config.get_parameters()
40
+ return json.dumps(params, indent=2)
41
+
42
+ def setup_aws(self) -> None:
43
+ # Create a new S3 client
44
+ self.s3 = S3(self.aws)
45
+ # Create a new Comprehend client
46
+ self.comprehend = Comprehend(self.aws)
47
+ # Create an Nlp object to perform analysis on the text
48
+ self.nlp = Nlp(self.aws)
49
+ return None
50
+
51
+ def get_basic_analytics(self,df:DataFrame) -> DataFrame:
52
+
53
+ # Text length - this is needed for comprehend analytics
54
+ df = self.nlp.text_length(df)
55
+ #df = nlp.remove_IQR_outliers(df)
56
+ # Comprehend analysis
57
+ results = self.nlp.comprehend_analysis(self.comprehend,df)
58
+ #print(results)
59
+ errors = self.nlp.check_results(results)
60
+ #print(errors)
61
+ if errors=={}:
62
+ print("No errors, so adding results to dataframe")
63
+ df = self.nlp.add_results_to_df(results,df)
64
+ df = self.nlp.comprehend_analytics(df)
65
+ return df
66
+
67
+ def get_reflexive_analytics(self,df:DataFrame) -> DataFrame:
68
+ # Reflexive expression analysis
69
+ response = self.nlp.analyse_reflexive_expressions(df,self.s3,self.comprehend)
70
+ #print(response)
71
+ job_id = self.comprehend.get_current_job_id()
72
+ print("Job ID:",job_id)
73
+ status = self.comprehend.check_job_status()
74
+ print("Status:",status)
75
+
76
+ # Get the details of the job
77
+ # details = comp.get_job_details()
78
+ # print("Job details:",details)
79
+
80
+ inc = 0
81
+ while status=="SUBMITTED" or status=="IN_PROGRESS":
82
+ print("Waiting 10 seconds...")
83
+ sleep(10)
84
+ status = self.comprehend.check_job_status()
85
+ print(f"Job status {inc}:",status)
86
+ inc += 1
87
+
88
+ # Download from S3 and extract results
89
+ print("Downloading and extracting results...")
90
+ results = self.comprehend.download_and_extract(self.s3)
91
+ print("RESULTS:")
92
+ print(results)
93
+
94
+ # Extract output of analysis and add to df
95
+ return self.nlp.add_to_dataframe(df,results)
96
+
97
+ class Res_display:
98
+
99
+ res_analyse:Res_analyse
100
+ vis:Display
101
+
102
+ def __init__(self,res:Res_analyse) -> None:
103
+ return self._setup(res)
104
+
105
+ def _setup(self,res:Res_analyse) -> None:
106
+ self.res_analyse = res
107
+ self.vis = Display(res.aws)
108
+ return None
109
+
110
+ def show_text(self,df:DataFrame,inline=True) -> str:
111
+ df = self.vis.add_offsets(df)
112
+ disp_data = self.vis.create_displacy(df)
113
+ if inline:
114
+ displacy.render(disp_data,manual=True,style="ent", options=self.res_analyse.config.display_options)
115
+ html_out = "Set inline to false to produce HTML"
116
+ else:
117
+ html_out = displacy.render(disp_data,manual=True,style="ent", options=self.res_analyse.config.display_options,page=True,jupyter=False)
118
+ return html_out
119
+
120
+ def get_interactions(self,df:DataFrame) -> DataFrame:
121
+ #Get RE sequence
122
+ df = self._add_res_sequence(df)
123
+ df = self._add_res_interactions(df)
124
+ df = self._add_res_adj_matrix(df)
125
+ return df
126
+
127
+ def show_graph(self,df:DataFrame,inline=True) -> str:
128
+ for am in df.res_adj_matrix:
129
+ g = RES_graph(am)
130
+ g.show()
131
+ return("nothing yet")
132
+
133
+ def _add_res_sequence(self,df):
134
+ temp_df = df.copy()
135
+ temp_df['res_sequence'] = temp_df.reflexive_expressions.apply(self._get_res_sequence)
136
+ return temp_df
137
+
138
+ def _add_res_interactions(self,df):
139
+ temp_df = df.copy()
140
+ temp_df['res_interactions'] = temp_df.res_sequence.apply(self._count_res_interactions)
141
+ return temp_df
142
+
143
+ def _add_res_adj_matrix(self,df):
144
+ temp_df = df.copy()
145
+ temp_df['res_adj_matrix'] = temp_df.res_interactions.apply(self._create_adj_matrix)
146
+ return temp_df
147
+
148
+ def _get_res_sequence(self,reflexive_expressions):
149
+ re_seq = [label for re,label in reflexive_expressions]
150
+ res_seq = []
151
+ # Need to substitute new RES labels for old RE labels
152
+ for re in re_seq:
153
+ if re=='ER' or re=='VR':
154
+ res_seq.append('NR')
155
+ elif re=='EV':
156
+ res_seq.append('EP')
157
+ elif re=='CN':
158
+ res_seq.append('AF')
159
+ else:
160
+ res_seq.append(re)
161
+ return res_seq
162
+
163
+ def _empty_res_interactions(self) -> dict[tuple,int]:
164
+ RE_types = ['RR','NR','AR','AF','EP']
165
+ RE_interactions:dict[tuple,int] = dict()
166
+ for t1 in RE_types:
167
+ for t2 in RE_types:
168
+ entry = tuple(sorted((t1,t2)))
169
+ if entry not in RE_interactions.keys():
170
+ RE_interactions[entry] = 0
171
+ return RE_interactions
172
+
173
+ def _count_res_interactions(self,re_sequence:list[str]) -> dict[tuple,int]:
174
+ re_ints = self._empty_res_interactions()
175
+ limit = len(re_sequence)-1
176
+ for i,s in enumerate(re_sequence):
177
+ if i < limit:
178
+ rei = tuple(sorted((s,re_sequence[i+1])))
179
+ #print(i,rei)
180
+ re_ints[rei] += 1
181
+ return re_ints
182
+
183
+ def _create_adj_matrix(self,interactions:dict[tuple,int])->list[list[int]]:
184
+ re_types = ["RR","NR","AR","AF","EP"]
185
+ matrix = []
186
+ for r in re_types:
187
+ row = []
188
+ for c in re_types:
189
+ key = tuple(sorted((r,c)))
190
+ #print(key)
191
+ weight = interactions.get(key,0)
192
+ row.append(weight)
193
+ matrix.append(row)
194
+ return matrix
195
+
196
+
197
+
198
+
reflexive/session.py CHANGED
@@ -7,7 +7,7 @@ import json
7
7
  from reflexive import cfg
8
8
 
9
9
  import logging
10
- logging.basicConfig(level=logging.DEBUG)
10
+ #logging.basicConfig(level=logging.DEBUG)
11
11
  logger = logging.getLogger(__name__)
12
12
 
13
13
  class AWS:
reflexive/util.py CHANGED
@@ -3,7 +3,7 @@ import json
3
3
  import pandas as pd
4
4
  from sklearn.preprocessing import MinMaxScaler
5
5
  import logging
6
- logging.basicConfig(level=logging.DEBUG)
6
+ #logging.basicConfig(level=logging.DEBUG)
7
7
  logger = logging.getLogger(__name__)
8
8
 
9
9
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: reflexive
3
- Version: 1.2.2
3
+ Version: 1.2.4
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,11 @@
1
+ reflexive/__init__.py,sha256=x5l8GV_9yC8yJyjTLqiM_WkfuPMLuNgQxF6PIz5MfFU,422
2
+ reflexive/analyse.py,sha256=CU6mQ8PxQrkOvuSe4mmnuI18jGMkuxql3zLZ1vEeMEY,17466
3
+ reflexive/cfg.py,sha256=tDPiC9NwpEVoT05_831sMsMfHhJt8pVrSOsgbtzTNDM,4267
4
+ reflexive/res.py,sha256=eXqdd9g24AoPcXLdddM9iE85Fx8iucZN6ODHnRsuRa4,6937
5
+ reflexive/session.py,sha256=8STWo8sqmWn-1PHcloFbgmLEvd7lsiFTU4t7ESRTsvw,10046
6
+ reflexive/util.py,sha256=uU7-lqUmiCiCaR85AdNFKXrdrBspjb1Rmd-yThA3fRU,3699
7
+ reflexive/visualise.py,sha256=pVnsPJOnHiLAIvDDm9P0u6-sGQINC4GVOY9b0GrzTK4,11725
8
+ reflexive-1.2.4.dist-info/METADATA,sha256=mibCUwBnfR03eOBn4vbNMbyAY2wDhfVU6RPeg0D5qc4,574
9
+ reflexive-1.2.4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
10
+ reflexive-1.2.4.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
11
+ reflexive-1.2.4.dist-info/RECORD,,
@@ -1,10 +0,0 @@
1
- reflexive/__init__.py,sha256=i503RuB3poQF-9otbkxPBD2GnqEBpFSn1TnIyx5DxME,331
2
- reflexive/analyse.py,sha256=LsDgFI_UUlK88ecQc19iPdcweAvT11CZCqtWtCsevC8,17465
3
- reflexive/cfg.py,sha256=tDPiC9NwpEVoT05_831sMsMfHhJt8pVrSOsgbtzTNDM,4267
4
- reflexive/session.py,sha256=ttyUAowaKgkwAgejRb_af5nZruKeH_kn3NB1NtitYUk,10045
5
- reflexive/util.py,sha256=zwlFncsWrRSuK8rsQaZKFNVLTkRySb1WPeY6Hz6qHhc,3698
6
- reflexive/visualise.py,sha256=pVnsPJOnHiLAIvDDm9P0u6-sGQINC4GVOY9b0GrzTK4,11725
7
- reflexive-1.2.2.dist-info/METADATA,sha256=stD6fGt19L8kTosYXpahYcCJroFCdeep6IhYCODbt7I,574
8
- reflexive-1.2.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
9
- reflexive-1.2.2.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
10
- reflexive-1.2.2.dist-info/RECORD,,