def get_monotonic_indicator(net_params, layer_params_list, weights, data, n_layers=4): input_layer = layers.Input(shape=(data.shape[1],)) enc = layers.Dense(layer_params_list[-1]['n_neuron'], activation=layer_params_list[-1]['act'], kernel_initializer=layer_params_list[-1]['init'])(input_layer) for i in range(n_layers - 1): enc = layers.Dense(layer_params_list[-2 - i]['n_neuron'], activation=layer_params_list[-2 - i]['act'], kernel_initializer=layer_params_list[-2 - i]['init'])(enc) dec = layers.Dense(layer_params_list[1]['n_neuron'], activation=layer_params_list[1]['act'], kernel_initializer=layer_params_list[1]['init'])(enc) for i in range(n_layers - 2): dec = layers.Dense(layer_params_list[i + 2]['n_neuron'], activation=layer_params_list[i + 2]['act'], kernel_initializer=layer_params_list[i + 2]['init'])(dec) output_layer = layers.Dense(len(data.T), activation=layer_params_list[-1]['act'], kernel_initializer=layer_params_list[-1]['init'])(dec) model = models.Model(input_layer, output_layer) model.set_weights(weights) model = models.Model(input_layer, enc) indicators = model.predict(data) rho_mk = np.abs(calc_rho(indicators)) return indicators[:, np.argmax(rho_mk)]
def y(self): """ Define the function in terms of x to return some value """ svol = 1.5 * 0.0172**2 / 370**2 # scattering volume in cm^3 self.update_params() mkey = 'Solvent' sol_density = tuple(np.ones_like(self.__Density__[mkey])) R = self.__R__[mkey] rho, eirho, adensity, rhor, eirhor, adensityr = calc_rho( R=tuple(R), material=tuple(self.__material__[mkey]), relement=self.relement, density=tuple(self.__Density__[mkey]), sol_density=sol_density, Energy=self.Energy, Rmoles=tuple(self.__Rmoles__[mkey]), NrDep=self.NrDep) for mkey in self.__mkeys__: if mkey != 'Solvent': trho, teirho, tadensity, trhor, teirhor, tadensityr = calc_rho( R=tuple(self.__R__[mkey]), material=tuple(self.__material__[mkey]), relement=self.relement, density=tuple(self.__Density__[mkey]), sol_density=sol_density, Energy=self.Energy, Rmoles=tuple(self.__Rmoles__[mkey]), NrDep=self.NrDep) vf = np.array(self.__VolFrac__[mkey]) rho = rho + vf * trho eirho = eirho + vf * teirho adensity = adensity + vf * tadensity major = np.sum(self.__R__[self.__mkeys__[0]]) minor = np.sum( np.array(self.__R__[self.__mkeys__[0]]) * np.array(self.__RzRatio__[self.__mkeys__[0]])) self.output_params['scaler_parameters'][ 'Diameter (Angstroms)'] = 2 * major self.output_params['scaler_parameters'][ 'Height (Angstroms)'] = 2 * minor if self.dist == 'LogNormal': dstd = 2 * np.sqrt((np.exp(self.Rsig**2) - 1) * np.exp(2 * np.log(major) + self.Rsig**2)) hstd = 2 * np.sqrt((np.exp(self.Rsig**2) - 1) * np.exp(2 * np.log(minor) + self.Rsig**2)) self.output_params['scaler_parameters'][ 'Diameter_Std (Angstroms)'] = dstd self.output_params['scaler_parameters'][ 'Height_Std (Angstroms)'] = hstd else: self.output_params['scaler_parameters'][ 'Diameter_Std (Angstroms)'] = 2 * self.Rsig self.output_params['scaler_parameters'][ 'Height_Std (Angstroms)'] = 2 * self.Rsig * minor / major if type(self.x) == dict: sqf = {} key = 'SAXS-term' sqft = self.ellipsoid_dict( tuple(self.x[key]), tuple(self.__R__[self.__mkeys__[0]]), tuple(self.__RzRatio__[self.__mkeys__[0]]), self.Rsig, tuple(rho), tuple(eirho), tuple(adensity), dist=self.dist, Np=self.Np, Nalf=self.Nalf) if self.SF is None: struct = np.ones_like( self.x[key] ) # hard_sphere_sf(self.x[key], D = self.D, phi = 0.0) elif self.SF == 'Hard-Sphere': struct = hard_sphere_sf(self.x[key], D=self.D, phi=self.phi) else: struct = sticky_sphere_sf(self.x[key], D=self.D, phi=self.phi, U=self.U, delta=0.01) for key in self.x.keys(): if key == 'SAXS-term': sqf[key] = self.norm * 6.022e20 * sqft[ key] * struct + self.sbkg # in cm^-1 if key == 'Cross-term': sqf[key] = self.norm * 6.022e20 * sqft[ key] * struct + self.cbkg # in cm^-1 if key == 'Resonant-term': sqf[key] = self.norm * 6.022e20 * sqft[ key] * struct + self.abkg # in cm^-1 key1 = 'Total' total = self.norm * 6.022e20 * sqft[key1] * struct + self.sbkg if not self.__fit__: dr, rdist, totalR = self.calc_Rdist( tuple(self.__R__[self.__mkeys__[0]]), self.Rsig, self.dist, self.Np) self.output_params['Distribution'] = {'x': dr, 'y': rdist} self.output_params['Total'] = {'x': self.x[key], 'y': total} for key in self.x.keys(): self.output_params[key] = {'x': self.x[key], 'y': sqf[key]} xtmp, ytmp = create_steps(self.__R__[self.__mkeys__[0]], rho) self.output_params['rho_r'] = {'x': xtmp, 'y': ytmp} xtmp, ytmp = create_steps(self.__R__[self.__mkeys__[0]], eirho) self.output_params['eirho_r'] = {'x': xtmp, 'y': ytmp} xtmp, ytmp = create_steps(self.__R__[self.__mkeys__[0]], adensity) self.output_params['adensity_r'] = {'x': xtmp, 'y': ytmp} self.output_params['Structure_Factor'] = { 'x': self.x[key], 'y': struct } # xtmp,ytmp=create_steps(x=self.__R__[][:-1],y=self.__Rmoles__[:-1]) # self.output_params['Rmoles_radial']={'x':xtmp,'y':ytmp} # xtmp, ytmp = create_steps(x=self.__R__[:-1], y=self.__RzRatio__[:-1]) # self.output_params['RzRatio_radial'] = {'x': xtmp, 'y': ytmp} # xtmp, ytmp = create_steps(x=self.__R__[:-1], y=self.__density__[:-1]) # self.output_params['Density_radial'] = {'x': xtmp, 'y': ytmp} else: if self.SF is None: struct = np.ones_like(self.x) elif self.SF == 'Hard-Sphere': struct = hard_sphere_sf(self.x, D=self.D, phi=self.phi) else: struct = sticky_sphere_sf(self.x, D=self.D, phi=self.phi, U=self.U, delta=0.01) tsqf, eisqf, asqf, csqf = self.ellipsoid( tuple(self.x), tuple(self.__R__[self.__mkeys__[0]]), tuple(self.__RzRatio__[self.__mkeys__[0]]), self.Rsig, tuple(rho), tuple(eirho), tuple(adensity), dist=self.dist, Np=self.Np, Nalf=self.Nalf) sqf = self.norm * np.array( tsqf) * 6.022e20 * struct + self.sbkg # in cm^-1 # if not self.__fit__: #Generate all the quantities below while not fitting asqf = self.norm * np.array( asqf) * 6.022e20 * struct + self.abkg # in cm^-1 eisqf = self.norm * np.array( eisqf) * 6.022e20 * struct + self.sbkg # in cm^-1 csqf = self.norm * np.array( csqf) * 6.022e20 * struct + self.cbkg # in cm^-1 sqerr = np.sqrt(6.022e20 * self.norm * self.flux * tsqf * svol + self.sbkg) sqwerr = (6.022e20 * self.norm * tsqf * svol * self.flux + self.sbkg + 2 * (0.5 - np.random.rand(len(tsqf))) * sqerr) dr, rdist, totalR = self.calc_Rdist( tuple(self.__R__[self.__mkeys__[0]]), self.Rsig, self.dist, self.Np) self.output_params['Distribution'] = {'x': dr, 'y': rdist} self.output_params['simulated_total_w_err'] = { 'x': self.x, 'y': sqwerr, 'yerr': sqerr } self.output_params['Total'] = {'x': self.x, 'y': sqf} self.output_params['Resonant-term'] = {'x': self.x, 'y': asqf} self.output_params['SAXS-term'] = {'x': self.x, 'y': eisqf} self.output_params['Cross-term'] = {'x': self.x, 'y': csqf} xtmp, ytmp = create_steps(self.__R__[self.__mkeys__[0]], rho) self.output_params['rho_r'] = {'x': xtmp, 'y': ytmp} xtmp, ytmp = create_steps(self.__R__[self.__mkeys__[0]], eirho) self.output_params['eirho_r'] = {'x': xtmp, 'y': ytmp} xtmp, ytmp = create_steps(self.__R__[self.__mkeys__[0]], adensity) self.output_params['adensity_r'] = {'x': xtmp, 'y': ytmp} self.output_params['Structure_Factor'] = {'x': self.x, 'y': struct} # xtmp, ytmp = create_steps(x=self.__R__[:-1], y=self.__Rmoles__[:-1]) # self.output_params['Rmoles_radial'] = {'x':xtmp , 'y': ytmp} # sqf = self.output_params[self.term]['y'] # xtmp, ytmp = create_steps(x=self.__R__[:-1], y=self.__RzRatio__[:-1]) # self.output_params['RzRatio_radial'] = {'x': xtmp, 'y': ytmp} # xtmp, ytmp = create_steps(x=self.__R__[:-1], y=self.__density__[:-1]) # self.output_params['Density_radial'] = {'x': xtmp, 'y': ytmp} return sqf
def y(self): """ Define the function in terms of x to return some value """ svol = 1.5 * 0.0172**2 / 370**2 # scattering volume in cm^3 self.output_params = {'scaler_parameters': {}} self.update_params() mkey = 'Solvent' sol_density = tuple(np.ones_like(self.__Density__[mkey])) R = self.__R__[mkey] rho, eirho, adensity, rhor, eirhor, adensityr = calc_rho( R=tuple(R), material=tuple(self.__material__[mkey]), relement=self.relement, density=tuple(self.__Density__[mkey]), sol_density=sol_density, Energy=self.Energy, Rmoles=tuple(self.__Rmoles__[mkey]), NrDep=self.NrDep) for mkey in self.__mkeys__: if mkey != 'Solvent': trho, teirho, tadensity, trhor, teirhor, tadensityr = calc_rho( R=tuple(self.__R__[mkey]), material=tuple(self.__material__[mkey]), relement=self.relement, density=tuple(self.__Density__[mkey]), sol_density=sol_density, Energy=self.Energy, Rmoles=tuple(self.__Rmoles__[mkey]), NrDep=self.NrDep) vf = np.array(self.__VolFrac__[mkey]) rho = rho + vf * trho eirho = eirho + vf * teirho adensity = adensity + vf * tadensity if type(self.x) == dict: sqf = {} for key in self.x.keys(): sqf[key] = self.norm * 6.022e20 * self.new_sphere_dict( tuple(self.x[key]), tuple(self.__R__[self.__mkeys__[0]]), self.Rsig, tuple(rho), tuple(eirho), tuple(adensity), key=key, dist=self.dist, Np=self.Np) # in cm^-1 if self.SF is None: struct = np.ones_like( self.x[key] ) # hard_sphere_sf(self.x[key], D = self.D, phi = 0.0) elif self.SF == 'Hard-Sphere': struct = hard_sphere_sf(self.x[key], D=self.D, phi=self.phi) else: struct = sticky_sphere_sf(self.x[key], D=self.D, phi=self.phi, U=self.U, delta=0.01) if key == 'SAXS-term': sqf[key] = sqf[key] * struct + self.sbkg if key == 'Cross-term': sqf[key] = sqf[key] * struct + self.cbkg if key == 'Resonant-term': sqf[key] = sqf[key] * struct + self.abkg key1 = 'Total' total = self.norm * 6.022e20 * struct * self.new_sphere_dict( tuple(self.x[key]), tuple(self.__R__[self.__mkeys__[0]]), self.Rsig, tuple(rho), tuple(eirho), tuple(adensity), key=key1, dist=self.dist, Np=self.Np) + self.sbkg # in cm^-1 if not self.__fit__: dr, rdist, totalR = self.calc_Rdist( tuple(self.__R__[self.__mkeys__[0]]), self.Rsig, self.dist, self.Np) self.output_params['Distribution'] = {'x': dr, 'y': rdist} self.output_params['Simulated_total_wo_err'] = { 'x': self.x[key], 'y': total } self.output_params['Total'] = {'x': self.x[key], 'y': total} for key in self.x.keys(): self.output_params[key] = {'x': self.x[key], 'y': sqf[key]} self.output_params['rho_r'] = { 'x': rhor[:, 0], 'y': rhor[:, 1] } self.output_params['eirho_r'] = { 'x': eirhor[:, 0], 'y': eirhor[:, 1] } self.output_params['adensity_r'] = { 'x': adensityr[:, 0], 'y': adensityr[:, 1] } self.output_params['Structure_Factor'] = { 'x': self.x[key], 'y': struct } else: if self.SF is None: struct = np.ones_like(self.x) elif self.SF == 'Hard-Sphere': struct = hard_sphere_sf(self.x, D=self.D, phi=self.phi) else: struct = sticky_sphere_sf(self.x, D=self.D, phi=self.phi, U=self.U, delta=0.01) tsqf, eisqf, asqf, csqf = self.new_sphere( tuple(self.x), tuple(self.__R__[self.__mkeys__[0]]), self.Rsig, tuple(rho), tuple(eirho), tuple(adensity), dist=self.dist, Np=self.Np) sqf = self.norm * np.array( tsqf) * 6.022e20 * struct + self.sbkg # in cm^-1 # if not self.__fit__: #Generate all the quantities below while not fitting asqf = self.norm * np.array( asqf) * 6.022e20 * struct + self.abkg # in cm^-1 eisqf = self.norm * np.array( eisqf) * 6.022e20 * struct + self.sbkg # in cm^-1 csqf = self.norm * np.array( csqf) * 6.022e20 * struct + self.cbkg # in cm^-1 sqerr = np.sqrt(6.020e20 * self.flux * self.norm * tsqf * struct * svol + self.sbkg) sqwerr = (6.022e20 * tsqf * svol * self.flux * self.norm * struct + self.sbkg + 2 * (0.5 - np.random.rand(len(tsqf))) * sqerr) dr, rdist, totalR = self.calc_Rdist( tuple(self.__R__[self.__mkeys__[0]]), self.Rsig, self.dist, self.Np) self.output_params['Distribution'] = {'x': dr, 'y': rdist} self.output_params['simulated_total_w_err'] = { 'x': self.x, 'y': sqwerr, 'yerr': sqerr } self.output_params['Total'] = {'x': self.x, 'y': sqf} self.output_params['Resonant-term'] = {'x': self.x, 'y': asqf} self.output_params['SAXS-term'] = {'x': self.x, 'y': eisqf} self.output_params['Cross-term'] = {'x': self.x, 'y': csqf} self.output_params['rho_r'] = {'x': rhor[:, 0], 'y': rhor[:, 1]} self.output_params['eirho_r'] = { 'x': eirhor[:, 0], 'y': eirhor[:, 1] } self.output_params['adensity_r'] = { 'x': adensityr[:, 0], 'y': adensityr[:, 1] } self.output_params['Structure_Factor'] = {'x': self.x, 'y': struct} return sqf
def y(self): """ Define the function in terms of x to return some value """ self.update_params() svol = 1.5 * 0.0172**2 / 370**2 # scattering volume in cm^3 if type(self.x) == dict: sqf = {} for key in self.x.keys(): sq = [] term = key.split('_')[0] Energy = float(key.split('_')[1].split(':')[1]) rho, eirho, adensity, rhor, eirhor, adensityr = calc_rho( R=self.__R__, material=self.__material__, density=self.__density__, sol_density=self.__solDensity__, Energy=Energy, Rmoles=self.__Rmoles__, NrDep=self.NrDep) sqf[key] = self.norm * 6.022e20 * self.new_sphere_dict( tuple(self.x[key]), tuple(self.__R__), self.Rsig, tuple(rho), tuple(eirho), tuple(adensity), key=term, dist=self.dist, Np=self.Np) # in cm^-1 if self.SF is None: struct = np.ones_like( self.x[key] ) # hard_sphere_sf(self.x[key], D = self.D, phi = 0.0) elif self.SF == 'Hard-Sphere': struct = hard_sphere_sf(self.x[key], D=self.D, phi=self.phi) else: struct = sticky_sphere_sf(self.x[key], D=self.D, phi=self.phi, U=self.U, delta=0.01) sqf[key] = sqf[key] * struct + self.bkg if not self.__fit__: dr, rdist, totalR = self.calc_Rdist(tuple(self.__R__), self.Rsig, self.dist, self.Np) self.output_params['Distribution'] = {'x': dr, 'y': rdist} self.output_params['rho_r'] = { 'x': rhor[:, 0], 'y': rhor[:, 1] } self.output_params['eirho_r'] = { 'x': eirhor[:, 0], 'y': eirhor[:, 1] } self.output_params['adensity_r'] = { 'x': adensityr[:, 0], 'y': adensityr[:, 1] } self.output_params['Structure_Factor'] = { 'x': self.x[key], 'y': struct } for key in self.x.keys(): term = key.split('_')[0] Energy = key.split('_')[1].split(':')[1] sqerr = np.sqrt(self.flux * sqf[key] * svol) sqwerr = sqf[key] * svol * self.flux + 2 * ( 0.5 - np.random.rand(len(sqerr))) * sqerr self.output_params[term + '_w_E_' + Energy] = { 'x': self.x[key], 'y': sqwerr, 'yerr': sqerr } else: rho, eirho, adensity, rhor, eirhor, adensityr = calc_rho( R=self.__R__, material=self.__material__, density=self.__density__, sol_density=self.__solDensity__, Energy=self.Energy, Rmoles=self.__Rmoles__, NrDep=self.NrDep) if self.SF is None: struct = np.ones_like(self.x) elif self.SF == 'Hard-Sphere': struct = hard_sphere_sf(self.x, D=self.D, phi=self.phi) else: struct = sticky_sphere_sf(self.x, D=self.D, phi=self.phi, U=self.U, delta=0.01) tsqf, eisqf, asqf, csqf = self.new_sphere(tuple(self.x), tuple(self.__R__), self.Rsig, tuple(rho), tuple(eirho), tuple(adensity), dist=self.dist, Np=self.Np) self.output_params['Total'] = { 'x': self.x, 'y': self.norm * np.array(tsqf) * 6.022e20 * struct + self.bkg } self.output_params['SAXS-term'] = { 'x': self.x, 'y': self.norm * np.array(eisqf) * 6.022e20 * struct + self.bkg } self.output_params['Resonant-term'] = { 'x': self.x, 'y': self.norm * np.array(asqf) * 6.022e20 * struct + self.bkg } self.output_params['Cross-term'] = { 'x': self.x, 'y': self.norm * np.array(csqf) * 6.022e20 * struct + self.bkg } if not self.__fit__: dr, rdist, totalR = self.calc_Rdist(tuple(self.__R__), self.Rsig, self.dist, self.Np) self.output_params['Distribution'] = {'x': dr, 'y': rdist} sqerr = np.sqrt(6.020e20 * self.flux * self.norm * tsqf * struct * svol + self.bkg) sqwerr = ( 6.022e20 * tsqf * svol * self.flux * self.norm * struct + self.bkg + 2 * (0.5 - np.random.rand(len(tsqf))) * sqerr) self.output_params['simulated_total_w_err'] = { 'x': self.x, 'y': sqwerr, 'yerr': sqerr } self.output_params['Structure_Factor'] = { 'x': self.x, 'y': struct } self.output_params['rho_r'] = { 'x': rhor[:, 0], 'y': rhor[:, 1] } self.output_params['eirho_r'] = { 'x': eirhor[:, 0], 'y': eirhor[:, 1] } self.output_params['adensity_r'] = { 'x': adensityr[:, 0], 'y': adensityr[:, 1] } sqf = self.output_params[self.term]['y'] return sqf
def y(self): """ Define the function in terms of x to return some value """ scale = 1e27 / 6.022e23 self.update_params() tR = self.__R__[:-1] tnmaterial = self.__material__[:-1] tfmaterial = self.__material__[:-1] tndensity = self.__density__[:-1] tfdensity = self.__density__[:-1] tsoldensity = self.__sol_density__[:-1] tRmoles = self.__Rmoles__[:-1] Rp = (3 / (4 * np.pi * self.norm * 6.022e23))**(1.0 / 3.0) * 1e9 Rc = np.sum(tR) near, far = self.solrho(Rp=Rp, Rc=Rc, strho=self.stDensity, tst=self.stThickness, lrho=self.dbDensity * self.stDensity, lexp=self.dbLength * self.stThickness, rhosol=self.ionDensity, R=tuple(tR), material=tuple(tnmaterial), density=tuple(tndensity), sol_density=tuple(tsoldensity), relement=self.relement, Rmoles=tuple(tRmoles)) dbr = [self.stThickness] dbr = dbr + [(Rp - Rc - self.stThickness) / self.Ndb for i in range(self.Ndb)] nden = [self.stDensity] fden = [0.0] nden = np.append( nden, self.dbDensity * self.stDensity * np.exp(-np.cumsum(dbr[1:]) / self.dbLength / self.stThickness) + near) fden = np.append( fden, far * (1 - np.exp(-np.cumsum(dbr[1:]) / self.dbLength / self.stThickness))) self.output_params['scaler_parameters']['Rp'] = Rp nmf = self.__cf__.parse(self.nearIon) nmw = self.__cf__.molecular_weight() fmf = self.__cf__.parse(self.farIon) fmw = self.__cf__.molecular_weight() for i in range(len(dbr)): tR.append(dbr[i]) tsoldensity.append(self.__sol_density__[-1]) tndensity.append(2 * nden[i] * nmw / 1000) # converting int gms/cubic-cms tfdensity.append(2 * fden[i] * fmw / 1000) # converting int gms/cubic-cms tnmaterial.append('%s:%s' % (self.nearIon, self.__material__[-1])) tfmaterial.append('%s:%s' % (self.farIon, self.__material__[-1])) if self.relement in self.nearIon: tRmoles.append(1.0) elif self.relement in self.farIon: tRmoles.append(1.0) else: tRmoles.append(1.0) rhon, eirhon, adensityn, rhorn, eirhorn, adensityrn = calc_rho( R=tuple(tR), material=tuple(tnmaterial), relement=self.relement, density=tuple(tndensity), sol_density=tuple(tsoldensity), Energy=self.Energy, Rmoles=tuple(tRmoles), NrDep=self.NrDep) rhof, eirhof, adensityf, rhorf, eirhorf, adensityrf = calc_rho( R=tuple(tR), material=tuple(tfmaterial), relement=self.relement, density=tuple(tfdensity), sol_density=tuple(tsoldensity), Energy=self.Energy, Rmoles=tuple(tRmoles), NrDep=self.NrDep) rho, eirho, adensity = (rhon + rhof) / 2, (eirhon + eirhof) / 2, ( adensityn + adensityf) / 2 rhor, eirhor, adensityr = rhorn, eirhorn, adensityrn rhor[:, 1] = (rhor[:, 1] + rhorf[:, 1]) / 2 eirhor[:, 1] = (eirhor[:, 1] + eirhorf[:, 1]) / 2 adensityr[:, 1] = (adensityr[:, 1] + adensityrf[:, 1]) / 2 if type(self.x) == dict: sqf = {} for key in self.x.keys(): sqf[key] = self.norm * 6.022e20 * self.new_sphere_dict( tuple(self.x[key]), tuple(tR), self.Rsig, tuple(rho), tuple(eirho), tuple(adensity), key=key, dist=self.dist, Np=self.Np) # in cm^-1 if self.SF is None: struct = np.ones_like( self.x[key] ) # hard_sphere_sf(self.x[key], D = self.D, phi = 0.0) elif self.SF == 'Hard-Sphere': struct = hard_sphere_sf(self.x[key], D=self.D, phi=self.phi) else: struct = sticky_sphere_sf(self.x[key], D=self.D, phi=self.phi, U=self.U, delta=0.01) if key == 'SAXS-term': sqf[key] = sqf[key] * struct + self.sbkg if key == 'Cross-term': sqf[key] = sqf[key] * struct + self.cbkg if key == 'Resonant-term': sqf[key] = sqf[key] * struct + self.abkg key1 = 'Total' total = self.norm * 6.022e20 * struct * self.new_sphere_dict( tuple(self.x[key]), tuple(tR), self.Rsig, tuple(rho), tuple(eirho), tuple(adensity), key=key1, dist=self.dist, Np=self.Np) + self.sbkg # in cm^-1 if not self.__fit__: dr, rdist, totalR = self.calc_Rdist(tuple(self.__R__), self.Rsig, self.dist, self.Np) self.output_params['Distribution'] = {'x': dr, 'y': rdist} self.output_params['Simulated_total_wo_err'] = { 'x': self.x[key], 'y': total } self.output_params['Total'] = {'x': self.x[key], 'y': total} for key in self.x.keys(): self.output_params[key] = {'x': self.x[key], 'y': sqf[key]} self.output_params['rho_r'] = { 'x': rhor[:, 0], 'y': rhor[:, 1], 'names': ['r (Angs)', 'Electron Density (el/Angs^3)'] } self.output_params['eirho_r'] = { 'x': eirhor[:, 0], 'y': eirhor[:, 1], 'names': ['r (Angs)', 'Electron Density (el/Angs^3)'] } self.output_params['adensity_r'] = { 'x': adensityr[:, 0], 'y': adensityr[:, 1] * scale, 'names': ['r (Angs)', 'Density (Molar)'] } # in Molar self.output_params['rhon_r'] = { 'x': rhorn[:, 0], 'y': rhorn[:, 1], 'names': ['r (Angs)', 'Electron Density (el/Angs^3)'] } self.output_params['eirhon_r'] = { 'x': eirhorn[:, 0], 'y': eirhorn[:, 1], 'names': ['r (Angs)', 'Electron Density (el/Angs^3)'] } self.output_params['adensityn_r'] = { 'x': adensityrn[:, 0], 'y': adensityrn[:, 1] * scale, 'names': ['r (Angs)', 'Density (Molar)'] } # in Molar self.output_params['rhof_r'] = { 'x': rhorf[:, 0], 'y': rhorf[:, 1], 'names': ['r (Angs)', 'Electron Density (el/Angs^3)'] } self.output_params['eirhof_r'] = { 'x': eirhorf[:, 0], 'y': eirhorf[:, 1], 'names': ['r (Angs)', 'Electron Density (el/Angs^3)'] } self.output_params['adensityf_r'] = { 'x': adensityrf[:, 0], 'y': adensityrf[:, 1] * scale, 'names': ['r (Angs)', 'Density (Molar)'] } # in Molar self.output_params['Structure_Factor'] = { 'x': self.x[key], 'y': struct } else: if self.SF is None: struct = np.ones_like(self.x) elif self.SF == 'Hard-Sphere': struct = hard_sphere_sf(self.x, D=self.D, phi=self.phi) else: struct = sticky_sphere_sf(self.x, D=self.D, phi=self.phi, U=self.U, delta=0.01) tsqf, eisqf, asqf, csqf = self.new_sphere(tuple(self.x), tuple(tR), self.Rsig, tuple(rho), tuple(eirho), tuple(adensity), dist=self.dist, Np=self.Np) sqf = self.norm * np.array( tsqf) * 6.022e20 * struct + self.sbkg # in cm^-1 # if not self.__fit__: # Generate all the quantities below while not fitting dr, rdist, totalR = self.calc_Rdist(tuple(self.__R__), self.Rsig, self.dist, self.Np) self.output_params['Distribution'] = {'x': dr, 'y': rdist} asqf = self.norm * np.array( asqf) * 6.022e20 + self.abkg # in cm^-1 eisqf = self.norm * np.array( eisqf) * 6.022e20 + self.sbkg # in cm^-1 csqf = self.norm * np.array( csqf) * 6.022e20 + self.cbkg # in cm^-1 svol = 0.2**2 * 1.5 * 1e-3 # scattering volume in cm^3 sqerr = np.sqrt(self.flux * sqf * svol) sqwerr = (sqf * svol * self.flux + 2 * (0.5 - np.random.rand(len(sqf))) * sqerr) dr, rdist, totalR = self.calc_Rdist(tuple(self.__R__), self.Rsig, self.dist, self.Np) self.output_params['Distribution'] = {'x': dr, 'y': rdist} self.output_params['simulated_total_w_err'] = { 'x': self.x, 'y': sqwerr, 'yerr': sqerr } self.output_params['simulated_total_wo_err'] = { 'x': self.x, 'y': sqf * svol * self.flux } self.output_params['Total'] = {'x': self.x, 'y': sqf} self.output_params['SAXS-term'] = {'x': self.x, 'y': eisqf} self.output_params['Cross-term'] = {'x': self.x, 'y': csqf} self.output_params['Resontant-term'] = {'x': self.x, 'y': asqf} self.output_params['rho_r'] = { 'x': rhor[:, 0], 'y': rhor[:, 1], 'names': ['r (Angs)', 'Electron Density (el/Angs^3)'] } self.output_params['eirho_r'] = { 'x': eirhor[:, 0], 'y': eirhor[:, 1], 'names': ['r (Angs)', 'Electron Density (el/Angs^3)'] } self.output_params['adensity_r'] = { 'x': adensityr[:, 0], 'y': adensityr[:, 1] * scale, 'names': ['r (Angs)', 'Density (Molar)'] } # in Molar self.output_params['rhon_r'] = { 'x': rhorn[:, 0], 'y': rhorn[:, 1], 'names': ['r (Angs)', 'Electron Density (el/Angs^3)'] } self.output_params['eirhon_r'] = { 'x': eirhorn[:, 0], 'y': eirhorn[:, 1], 'names': ['r (Angs)', 'Electron Density (el/Angs^3)'] } self.output_params['adensityn_r'] = { 'x': adensityrn[:, 0], 'y': adensityrn[:, 1] * scale, 'names': ['r (Angs)', 'Density (Molar)'] } # in Molar self.output_params['rhof_r'] = { 'x': rhorf[:, 0], 'y': rhorf[:, 1], 'names': ['r (Angs)', 'Electron Density (el/Angs^3)'] } self.output_params['eirhof_r'] = { 'x': eirhorf[:, 0], 'y': eirhorf[:, 1], 'names': ['r(Angs)', 'Electron Density (el/Angs^3)'] } self.output_params['adensityf_r'] = { 'x': adensityrf[:, 0], 'y': adensityrf[:, 1] * scale, 'names': ['r (Angs)', 'Density (Molar)'] } # in Molar self.output_params['Structure_Factor'] = {'x': self.x, 'y': struct} sqf = self.output_params[self.term]['y'] return sqf
def train_ae_coea(net_params, layer_params_list, data_train, data_eval, n_layers=4, iters=4000, ind=None): """Trains the Autoencoder with the given parameters. Returns max Rho_MK and validation loss""" K.clear_session() # Required in order to have reproducible results from a specific random seed os.environ['PYTHONHASHSEED'] = '0' # Force tf to use a single thread (required for reproducibility) session_conf = tf.compat.v1.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1) sess = tf.compat.v1.Session(graph=tf.compat.v1.get_default_graph(), config=session_conf) tf.compat.v1.keras.backend.set_session(sess) # network parameters batch = 32 optim = net_params['optim'] learn_rate = net_params['learn_rate'] decay = net_params['decay'] mom = net_params['mom'] rand_seed = net_params['rand_seed'] np.random.seed(rand_seed) rn.seed(rand_seed) tf.compat.v1.set_random_seed(rand_seed) iter_per_epoch = int(np.ceil(len(data_train) / batch)) epochs = int(np.ceil(iters / iter_per_epoch)) if optim == 'adam': opt = optimizers.Adam(lr=learn_rate, beta_1=mom, decay=decay, clipvalue=0.3) elif optim == 'nadam': opt = optimizers.Nadam(lr=learn_rate, beta_1=mom, schedule_decay=decay, clipvalue=0.3) elif optim == 'rmsprop': opt = optimizers.RMSprop(lr=learn_rate, rho=mom, decay=decay, clipvalue=0.3) else: # adadelta opt = optimizers.Adadelta(lr=learn_rate, rho=mom, decay=decay, clipvalue=0.3) # pretraining input_data = data_train weights = [] ea = EarlyStopping(patience=int(epochs / 3)) cb = [ea] for i, layer_params in enumerate(layer_params_list): layer_params = layer_params_list[n_layers - 1 - i] input_layer = layers.Input(shape=(input_data.shape[1],)) hidden_layer = layers.Dense(layer_params['n_neuron'], activation=layer_params['act'], kernel_regularizer=regularizers.l2(layer_params['L2']), activity_regularizer=SparseActivityRegularizer (p=layer_params['SP'], sparsity_beta=layer_params['SR']), kernel_initializer=layer_params['init'])(input_layer) output_layer = layers.Dense(input_data.shape[1], activation=layer_params['act'], kernel_initializer=layer_params['init'])(hidden_layer) model = models.Model(input_layer, output_layer) model.compile(optimizer=opt, loss='mse') history = model.fit(x=input_data, y=input_data, batch_size=batch, epochs=epochs, callbacks=cb, validation_split=0.2, verbose=False) for loss in history.history['loss']: if np.isnan(loss): K.clear_session() return 0.01, 100 h_weights = model.get_weights() weights.insert(2 * i, h_weights[0]) weights.insert(2 * i + 1, h_weights[1]) weights.insert(len(weights) - 2 * i, h_weights[2]) weights.insert(len(weights) - 2 * i, h_weights[3]) model = models.Model(input_layer, hidden_layer) input_data = model.predict(input_data) # stacking the layers - fine tuning input_layer = layers.Input(shape=(data_train.shape[1],)) enc = layers.Dense(layer_params_list[-1]['n_neuron'], activation=layer_params_list[-1]['act'], kernel_initializer=layer_params_list[-1]['init'])(input_layer) for i in range(n_layers - 1): enc = layers.Dense(layer_params_list[-2 - i]['n_neuron'], activation=layer_params_list[-2 - i]['act'], kernel_initializer=layer_params_list[-2 - i]['init'])(enc) dec = layers.Dense(layer_params_list[1]['n_neuron'], activation=layer_params_list[1]['act'], kernel_initializer=layer_params_list[1]['init'])(enc) for i in range(n_layers - 2): dec = layers.Dense(layer_params_list[i + 2]['n_neuron'], activation=layer_params_list[i + 2]['act'], kernel_initializer=layer_params_list[i + 2]['init'])(dec) output_layer = layers.Dense(len(data_train.T), activation=layer_params_list[-1]['act'], kernel_initializer=layer_params_list[-1]['init'])(dec) # assumption: output layer has the same parameters as the final hidden layer model = models.Model(input_layer, output_layer) model.compile(optimizer=opt, loss='mse') model.set_weights(weights) history = model.fit(x=data_train, y=data_train, batch_size=batch, epochs=epochs, callbacks=cb, validation_data=(data_eval, data_eval), verbose=False) if ind: ind.final_weights = model.get_weights() for loss in history.history['loss']: if np.isnan(loss): K.clear_session() return 0.01, 100 val_loss = history.history['val_loss'][-1] model = models.Model(input_layer, enc) indicators = model.predict(data_eval) # MK test rho_mk = calc_rho(indicators) max_rho_mk = 0.01 if np.isnan(max(abs(rho_mk))) else max(abs(rho_mk)) loss = 100 if np.isnan(val_loss) else val_loss return max_rho_mk, loss
def y(self): """ Define the function in terms of x to return some value """ scale = 1e27 / 6.022e23 svol = 1.5 * 0.0172**2 / 370**2 # scattering volume in cm^3 self.output_params = {'scaler_parameters': {}} self.update_params() rho, eirho, adensity, rhor, eirhor, adensityr = calc_rho( R=tuple(self.__R__), material=tuple(self.__material__), relement=self.relement, density=tuple(self.__density__), sol_density=tuple(self.__sol_density__), Energy=self.Energy, Rmoles=tuple(self.__Rmoles__), NrDep=self.NrDep) if type(self.x) == dict: sqf = {} for key in self.x.keys(): sqf[key] = self.norm * 6.022e20 * self.new_sphere_dict( tuple(self.x[key]), tuple(self.__R__), self.Rsig, tuple(rho), tuple(eirho), tuple(adensity), key=key, dist=self.dist, Np=self.Np) # in cm^-1 if self.SF is None: struct = np.ones_like( self.x[key] ) # hard_sphere_sf(self.x[key], D = self.D, phi = 0.0) elif self.SF == 'Hard-Sphere': struct = hard_sphere_sf(self.x[key], D=self.D, phi=self.phi) else: struct = sticky_sphere_sf(self.x[key], D=self.D, phi=self.phi, U=self.U, delta=0.01) if key == 'SAXS-term': sqf[key] = sqf[key] * struct + self.sbkg if key == 'Cross-term': sqf[key] = sqf[key] * struct + self.cbkg if key == 'Resonant-term': sqf[key] = sqf[key] * struct + self.abkg key1 = 'Total' total = self.norm * 6.022e20 * struct * self.new_sphere_dict( tuple(self.x[key]), tuple(self.__R__), self.Rsig, tuple(rho), tuple(eirho), tuple(adensity), key=key1, dist=self.dist, Np=self.Np) + self.sbkg # in cm^-1 if not self.__fit__: dr, rdist, totalR = self.calc_Rdist(tuple(self.__R__), self.Rsig, self.dist, self.Np) self.output_params['Distribution'] = {'x': dr, 'y': rdist} self.output_params['Simulated_total_wo_err'] = { 'x': self.x[key], 'y': total } self.output_params['Total'] = {'x': self.x[key], 'y': total} for key in self.x.keys(): self.output_params[key] = {'x': self.x[key], 'y': sqf[key]} self.output_params['rho_r'] = { 'x': rhor[:, 0], 'y': rhor[:, 1], 'names': ['r (Angs)', 'Electron Density (el/Angs^3)'] } self.output_params['eirho_r'] = { 'x': eirhor[:, 0], 'y': eirhor[:, 1], 'names': ['r (Angs)', 'Electron Density (el/Angs^3)'] } self.output_params['adensity_r'] = { 'x': adensityr[:, 0], 'y': adensityr[:, 1] * scale, 'names': ['r (Angs)', 'Density (Molar)'] } # in Molar self.output_params['Structure_Factor'] = { 'x': self.x[key], 'y': struct } xtmp, ytmp = create_steps(x=self.__R__[:-1], y=self.__Rmoles__[:-1]) self.output_params['Rmoles_radial'] = {'x': xtmp, 'y': ytmp} xtmp, ytmp = create_steps(x=self.__R__[:-1], y=self.__density__[:-1]) self.output_params['Density_radial'] = {'x': xtmp, 'y': ytmp} else: if self.SF is None: struct = np.ones_like(self.x) elif self.SF == 'Hard-Sphere': struct = hard_sphere_sf(self.x, D=self.D, phi=self.phi) else: struct = sticky_sphere_sf(self.x, D=self.D, phi=self.phi, U=self.U, delta=0.01) tsqf, eisqf, asqf, csqf = self.new_sphere(tuple(self.x), tuple(self.__R__), self.Rsig, tuple(rho), tuple(eirho), tuple(adensity), dist=self.dist, Np=self.Np) sqf = self.norm * np.array( tsqf) * 6.022e20 * struct + self.sbkg # in cm^-1 # if not self.__fit__: #Generate all the quantities below while not fitting asqf = self.norm * np.array( asqf) * 6.022e20 * struct + self.abkg # in cm^-1 eisqf = self.norm * np.array( eisqf) * 6.022e20 * struct + self.sbkg # in cm^-1 csqf = self.norm * np.array( csqf) * 6.022e20 * struct + self.cbkg # in cm^-1 sqerr = np.sqrt(6.020e20 * self.flux * self.norm * tsqf * struct * svol + self.sbkg) sqwerr = (6.022e20 * tsqf * svol * self.flux * self.norm * struct + self.sbkg + 2 * (0.5 - np.random.rand(len(tsqf))) * sqerr) dr, rdist, totalR = self.calc_Rdist(tuple(self.__R__), self.Rsig, self.dist, self.Np) self.output_params['Distribution'] = {'x': dr, 'y': rdist} self.output_params['simulated_total_w_err'] = { 'x': self.x, 'y': sqwerr, 'yerr': sqerr } self.output_params['Total'] = {'x': self.x, 'y': sqf} self.output_params['Resonant-term'] = {'x': self.x, 'y': asqf} self.output_params['SAXS-term'] = {'x': self.x, 'y': eisqf} self.output_params['Cross-term'] = {'x': self.x, 'y': csqf} self.output_params['rho_r'] = { 'x': rhor[:, 0], 'y': rhor[:, 1], 'names': ['r (Angs)', 'Electron Density (el/Angs^3)'] } self.output_params['eirho_r'] = { 'x': eirhor[:, 0], 'y': eirhor[:, 1], 'names': ['r (Angs)', 'Electron Density (el/Angs^3)'] } self.output_params['adensity_r'] = { 'x': adensityr[:, 0], 'y': adensityr[:, 1] * scale, 'names': ['r (Angs)', 'Density (Molar)'] } # in Molar self.output_params['Structure_Factor'] = {'x': self.x, 'y': struct} xtmp, ytmp = create_steps(x=self.__R__[:-1], y=self.__Rmoles__[:-1]) self.output_params['Rmoles_radial'] = {'x': xtmp, 'y': ytmp} sqf = self.output_params[self.term]['y'] xtmp, ytmp = create_steps(x=self.__R__[:-1], y=self.__density__[:-1]) self.output_params['Density_radial'] = {'x': xtmp, 'y': ytmp} return sqf