def fit_to_weibull(sim_file,beta_0,tau_0,event_range=None,magnitude_filter=None,section_filter=None): from scipy import optimize from pyvc import VCEvents,VCSimData fitfunc = lambda p,x: weibull_one(x,p[0],p[1]) errfunc = lambda p,x,y: fitfunc(p,x) - y p0 = [beta_0,tau_0] with VCSimData() as sim_data: # open the simulation data file sim_data.open_file(sim_file) # instantiate the vc classes passing in an instance of the VCSimData # class events = VCEvents(sim_data) event_data = events.get_event_data(['event_number', 'event_year', 'event_magnitude', 'event_range_duration'], event_range=event_range, magnitude_filter=magnitude_filter, section_filter=section_filter) intervals = np.array([ x - event_data['event_year'][n-1] for n,x in enumerate(event_data['event_year']) if n != 0 ]) cumulative = {} cumulative['x'] = np.sort(intervals) cumulative['y'] = np.arange(float(intervals.size))/float(intervals.size) p1,success = optimize.leastsq(errfunc,p0[:],args=(cumulative['x'],cumulative['y'])) print "\nBETA: {}".format(p1[0]) print "TAU : {}\n".format(p1[1])
def plot_number_area_data(sim_file, output_file=None, event_range=None): from pyvc import * from pyvc import vcplotutils #Can't handle plot label if no event_range given with VCSimData() as sim_data: # open the simulation data file sim_data.open_file(sim_file) # instantiate the vc events class passing in an instance of the # VCSimData class events = VCEvents(sim_data) # get the data event_data = events.get_event_data(['event_number','event_area'], event_range=event_range) start,end = event_range['filter'] duration = round(end-start) #--------------------------------------------------------------------------- # Prepare the plot and do it. #--------------------------------------------------------------------------- # initilize a dict to store the event counts and get the total number # of events. cum_num = {} total_events = len(event_data['event_number']) for num in range(total_events): area = float(sorted(event_data['event_area'])[num])*float(pow(10,-6)) cum_num[area] = total_events - (num + 1) sys.stdout.write("\nnumber of events : {}\n".format(total_events)) # dump the counts into x and y arrays for plotting. also, divide the count # by the number of years so we get count per year. x = [] y = [] for area in sorted(iter(cum_num)): x.append(float(area)) y.append(float(cum_num[area])) # create the line for b = 1 x_b1 = np.linspace(min(x),max(x),num=1000) y_b1 = y[0]*x_b1[0]*(np.array(x_b1)**-1) #sys.stdout.write(str(y_b1)) plt.title('Duration: {} years Total events: {}'.format(duration,total_events)) plt.plot(x_b1,y_b1,label='b=1',ls='-',lw=2,c='r') plt.plot(x,y,'.',c='k') plt.legend(loc='upper right') plt.ylabel(r'$N (\geq A_r)$') plt.xlabel(r'$A_r [km^2]$') plt.xlim(-500,5500) plt.ylim(-20,750) plt.savefig(output_file,dpi=200) # do the standard plot """vcplotutils.standard_plot(output_file, x, y,