Skip to content

Commit

Permalink
feat(MET4FoF redundancy): method init_parameters1 removed. removed un…
Browse files Browse the repository at this point in the history
…necessary comments
  • Loading branch information
anupam-prasad committed Mar 17, 2021
1 parent 2db93c2 commit afe47c3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 39 deletions.
2 changes: 1 addition & 1 deletion agentMET4FOF_redundancy/redundancy1.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def calc_best_estimate(y_arr, vy_arr2d, problim):
chi2obs: float
observed value of chi-squared, used for consistency evaluation
"""
# print('y_arr.shape = ', y_arr.shape)

print(f'cbe y_arr = {y_arr}')
n_estims = len(y_arr)

Expand Down
64 changes: 35 additions & 29 deletions agentMET4FOF_redundancy/redundancyAgents1.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def agent_loop(self):
streams content and push it via invoking :py:func:`AgentMET4FOF.send_output`.
"""
if self.current_state == "Running":
self.set_output_data(channel="default", data=self._data_stream._next_sample_generator(batch_size=10))
self.set_output_data(channel="default", data=self._data_stream.next_sample(batch_size=10))
super().agent_loop()

@property
Expand All @@ -73,32 +73,26 @@ class RedundancyAgent(MetrologicalAgent):
a_arr: np.ndarray
a_arr2d: np.ndarray

def init_parameters(self, input_data_maxlen=25, output_data_maxlen=25):
def init_parameters(
self,
input_data_maxlen: int = 25,
output_data_maxlen: int = 25,
sensor_key_list: list = None,
n_pr: int = 1,
problim: float = .9,
calc_type: str = 'lcs'
):
"""
Initialize the redundancy agent as an instance of the :py:mod:`MetrologicalAgent` class.
Parameters
Parent class parameters
----------
input_data_maxlen: int
output_data_maxlen: int
"""
self.metadata = MetaData(
device_id="RedAgent01",
time_name="time",
time_unit="s",
quantity_names="m",
quantity_units="kg",
misc="nothing")
super().init_parameters(input_data_maxlen=25, output_data_maxlen=25)
self.set_output_data(channel="default", metadata=self.metadata)
def init_parameters1(self, calc_type, sensor_key_list, n_pr, problim):
"""
Parameters used for both methods :func:`lcs` and :func:`lcss`.
Parameters
----------
calc_type: str
calculation type: 'lcs' or 'lcss'
Expand All @@ -107,13 +101,27 @@ def init_parameters1(self, calc_type, sensor_key_list, n_pr, problim):
n_pr: integer
size of the batch of data that is handled at once by the Redundancy Agent
problim: float
limit probability used for conistency evaluation
limit probability used for consistency evaluation
"""

if sensor_key_list is None:
sensor_key_list = []
super().init_parameters(input_data_maxlen=25, output_data_maxlen=25)
self.metadata = MetaData(
device_id="RedAgent01",
time_name="time",
time_unit="s",
quantity_names="m",
quantity_units="kg",
misc="nothing")

self.calc_type = calc_type
self.sensor_key_list = sensor_key_list
self.n_pr = n_pr
self.problim = problim

self.set_output_data(channel="default", metadata=self.metadata)

def init_parameters2(self, fsam, f1, f2, ampl_ratio, phi1, phi2):
"""
Additional parameters used for this particular example in combination with the :py:func:`lcss` method.
Expand All @@ -136,18 +144,19 @@ def init_parameters2(self, fsam, f1, f2, ampl_ratio, phi1, phi2):
initial phase of second frequency component
"""
# set-up vector a_arr and matrix a_arr2d for redundancy method
a = np.identity(self.n_pr)
id_mat = np.identity(self.n_pr)
id_fft = np.fft.fft(id_mat)

n_pr2 = int(self.n_pr / 2)
afft = np.fft.fft(a)
bfft = np.real(afft[:n_pr2, :]) / n_pr2
cfft = -np.imag(afft[:n_pr2, :]) / n_pr2
bfft = id_fft[:n_pr2, :].real / n_pr2
cfft = -id_fft[:n_pr2, :].imag / n_pr2

c1 = 1 / np.cos(phi1)
c2 = 1 / np.sin(-phi1)
c3 = ampl_ratio / np.cos(phi2)
c4 = ampl_ratio / np.sin(-phi2)

t_max = self.n_pr / fsam
df = 1 / t_max
df = fsam / self.n_pr

ind_freq1 = int(f1/df)
ind_freq2 = int(f2/df)
Expand All @@ -161,6 +170,7 @@ def init_parameters2(self, fsam, f1, f2, ampl_ratio, phi1, phi2):
a_row2 = a_row2.reshape((1, len(a_row2)))
a_row3 = a_row3.reshape((1, len(a_row3)))
a_row4 = a_row4.reshape((1, len(a_row4)))

self.a_arr2d = np.concatenate((a_row1, a_row2, a_row3, a_row4), axis=0)
self.a_arr = np.zeros(shape=(4, 1))

Expand Down Expand Up @@ -191,7 +201,6 @@ def agent_loop(self):
ut_data_arr2d = np.full(shape=(self.n_pr, n_sensors), fill_value=np.nan)
x_data_arr2d = np.full(shape=(self.n_pr, n_sensors), fill_value=np.nan)
ux_data_arr2d = np.full(shape=(self.n_pr, n_sensors), fill_value=np.nan)
# print('buff = ', buff)
i_sensor = 0
# for key in buff.keys(): # arbitrary order

Expand All @@ -203,17 +212,15 @@ def agent_loop(self):
ux_data_arr2d[:, i_sensor] = data_arr[:, 3]
i_sensor = i_sensor + 1

#print('calc_type: ', self.calc_type)
if self.calc_type == "lcs":
#print('case lcs')
data = np.full(shape=(self.n_pr, 4), fill_value=np.nan)
for i_pnt in range(self.n_pr):
y_arr = np.array(x_data_arr2d[i_pnt, :])
y_arr = y_arr.reshape((n_sensors, 1))
vy_arr2d = np.zeros(shape=(n_sensors, n_sensors))
for i_sensor in range(n_sensors):
vy_arr2d[i_sensor, i_sensor] = np.square(ux_data_arr2d[i_pnt, i_sensor])
#data = np.array([1, 2, 3, 4])

n_sols, ybest, uybest, chi2obs, indkeep = calc_lcs(y_arr, vy_arr2d, self.problim)
if n_sols == 1: # time stamp is value of first sensor
if isinstance(ybest, np.ndarray):
Expand Down Expand Up @@ -249,7 +256,6 @@ def agent_loop(self):
if len(data.shape) == 1:
data = data.reshape((1, len(data)))

# print('data = ', data)
self.set_output_data(channel="default", data=data)
super().agent_loop()

Expand Down
11 changes: 5 additions & 6 deletions agentMET4FOF_tutorials/tutorial_7_redundancyAgents.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def demonstrate_redundancy_agent_four_signals():
problim = 0.95

# start agent network server
agent_network: AgentNetwork = AgentNetwork(dashboard_modules=True)
agent_network: AgentNetwork = AgentNetwork(dashboard_modules=True, backend='mesa')

# Initialize signal generating class outside of agent framework.
signal_arr = [MetrologicalMultiWaveGenerator(sfreq=fsam, freq_arr=np.array([freq]), intercept=intercept,
Expand All @@ -42,18 +42,17 @@ def demonstrate_redundancy_agent_four_signals():
sensor_key_list = []
for count, signal in enumerate(signal_arr):
sensor_key_list += ["Sensor" + str(count + 1)]
source_agents += [
agent_network.add_agent(name=sensor_key_list[-1], agentType=MetrologicalMultiWaveGeneratorAgent)]
source_agents += [agent_network.add_agent(name=sensor_key_list[-1], agentType=MetrologicalMultiWaveGeneratorAgent)]
source_agents[-1].init_parameters(signal=signal, batch_size=batch_size)

# Redundant data processing agent
redundancy_name1 = "RedundancyAgent1" # Name cannot contain spaces!!
redundancy_name1 = "RedundancyAgent1"
redundancy_agent1 = agent_network.add_agent(name=redundancy_name1, agentType=RedundancyAgent)
redundancy_agent1.init_parameters1(sensor_key_list=sensor_key_list, calc_type="lcs", n_pr=n_pr, problim=problim)
redundancy_agent1.init_parameters(sensor_key_list=sensor_key_list, n_pr=n_pr, problim=problim, calc_type="lcs")

# Initialize metrologically enabled plotting agent.
monitor_agent1 = agent_network.add_agent(name="MonitorAgent_SensorValues",
agentType=MetrologicalMonitorAgent) # Name cannot contain spaces!!
agentType=MetrologicalMonitorAgent)
monitor_agent2 = agent_network.add_agent(name="MonitorAgent_RedundantEstimate", agentType=MetrologicalMonitorAgent)

# Bind agents.
Expand Down
6 changes: 3 additions & 3 deletions agentMET4FOF_tutorials/tutorial_8_redundancyAgents.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def demonstrate_redundancy_agent_onesignal():
is started. The network and the calculated results can be monitored in a browser at the address http://127.0.0.1:8050/.
"""
# parameters
batch_size = 10
batch_size = 20
n_pr = 20
fsam = 40
f1 = 6
Expand All @@ -32,7 +32,7 @@ def demonstrate_redundancy_agent_onesignal():
problim = 0.95

# start agent network server
agent_network = AgentNetwork(dashboard_modules=True)
agent_network = AgentNetwork(dashboard_modules=True, backend='mesa')

# Initialize signal generating class outside of agent framework.
signal1 = MetrologicalMultiWaveGenerator(sfreq=fsam, freq_arr=np.array([f1, f2]), ampl_arr=np.array([ampl1, ampl2]),
Expand All @@ -47,7 +47,7 @@ def demonstrate_redundancy_agent_onesignal():
sensor_key_list = [source_name1]
redundancy_name1 = "RedundancyAgent1"
redundancy_agent1 = agent_network.add_agent(name=redundancy_name1, agentType=RedundancyAgent)
redundancy_agent1.init_parameters1(sensor_key_list=sensor_key_list, calc_type="lcss", n_pr=n_pr, problim=problim)
redundancy_agent1.init_parameters(sensor_key_list=sensor_key_list, calc_type="lcss", n_pr=n_pr, problim=problim)

# prior knowledge needed for redundant evaluation of the data
redundancy_agent1.init_parameters2(fsam=fsam, f1=f1, f2=f2, ampl_ratio=ampl1/ampl2, phi1=phi1, phi2=phi2)
Expand Down

0 comments on commit afe47c3

Please sign in to comment.