source: fact/tools/pyscripts/examples/result_collection/area_vs_size_with_can.py

Last change on this file was 13113, checked in by neise, 13 years ago
initial commit of first thoughts about how to collect anaysis results
  • Property svn:executable set to *
File size: 4.6 KB
Line 
1#!/usr/bin/python -i
2#
3# Dominik Neise
4#
5# copy of the /tools/cleaning/avea_vs_size.py script which was originally meant
6# as an example for the AmplitudeCleaner.
7#
8# This script is an example how one can use the python dict, for
9# book keeping.
10# Note, this script is not yet runnable, since one needs to change
11# quite some things in the classes...
12# this is just meant to be entertaining
13
14from pyfact_rename import *
15import os.path
16import matplotlib.pyplot as plt
17import numpy as np
18from fir_filter import *
19from extractor import *
20from drs_spikes import *
21from plotters import *
22import time as t
23from cleaners import AmplitudeCleaner
24confirm_next_step = False# this is for user interaction
25
26import sys
27import pickle
28
29# this little can will hold
30# * all the settings,
31# * all results and intermediate results
32# * as well as this script itself
33# and then it will be written, to an output file
34can = {} # the FACT can
35can['settings'] = {} # a subspace for holdind all settings
36can['results'] = {} # a subspace for holding all results... or similar stuff
37can['logbook'] = [] # a list of statements ... human readable logbook
38can['src'] = [] # this script and other sources
39
40# store this scipt in the list of sources.
41can['src'].append( open(sys.argv[0], 'r').read() )
42
43# declare filenames to work with:
44can['setting']['data_file_names'] = ['/media/daten_platte/FACT/data/20120229_144.fits.gz']
45can['setting']['calib_file_names'] = ['/media/daten_platte/FACT/data/20120229_132.drs.fits.gz']
46# we even tell the can, where we are going to store it
47can['setting']['outfile_name'] = ['./can.pkl']
48
49# FileChecker checks if all files exist, and appends this action to the logbook
50# if one File does not exist, it will cause this script to end. (...well ... maybe)
51FileChecker(can)
52
53# put the settings for all the extractors and similar data processors
54# into the settings
55can['settings']['SlidingAverage'] = [8]
56can['settings']['DRSSpikes'] = []
57can['settings']['GlobalMaxFinder'] = [40,200]
58can['settings']['AmplitudeCleaner'] = [45,18,'return_bool_mask=False']
59# then call the __init__s of the processors and give them the can...
60# they will see if they can find their own init parameters ... if not they take
61# the default
62# in addition, they will add some lines to the logbook telling the reader
63# that they were created, and what parameters they found/used.
64event = RawDataFeeder(can)
65despike = DRSSpikes(can)
66smooth = SlidingAverage(can)
67extract = GlobalMaxFinder(can)
68cleaner = AmplitudeCleaner(can)
69
70# prepare some space in the can, for the results of this script
71can['results']['areas'] = []
72can['results']['sizes'] = []
73# and make some shortcuts
74areas = can['results']['areas']
75sizes = can['results']['sizes']
76
77# this loop will loop over all events, out of all data files in the can,
78# so the analysis loop doesn't even know, there are more files involved.
79# of course in the logbook the opening and closing will be stored
80for data,startcell,tt in event:
81 if tt==4:
82 data = despike(data)
83 data = smooth(data)
84 amplitude, time_of_max = extract(data)
85 survivors = cleaner(amplitude)
86
87 size = 0
88 for pixel in survivors:
89 size += amplitude[pixel]
90
91 if len(survivors) > 0:
92 areas.append( len(survivors) )
93 sizes.append( size )
94 # we suddenly realize, that we would like to store
95 # the event IDs of events, which are have no survivors
96 # we should scroll up ... before the loop and declare an empty list
97 # like: can['results']['no_survivor_ids'] = []
98 # but imagine we are writing a function, which is beeing called inside the loop
99 # and this function has no possibility to declare that ...
100 # well then this function can still put new things inside the can!
101 else:
102 if 'no_survivor_ids' not in can['results']:
103 can['results']['no_survivor_ids'] = [event.event_id.value]
104 else:
105 can['results']['no_survivor_ids'].append(event.event_id.value)
106
107 if confirm_next_step:
108 user_input = raw_input("'q'-quit, 'r'-run, anything else goes one step")
109 number=None
110 try:
111 number=int(user_input)
112 except:
113 number=None
114 if user_input.find('q') != -1:
115 sys.exit(0)
116 elif user_input.find('r') != -1:
117 confirm_next_step = False
118 elif number!=None:
119 run += number
120
121# Now our analysis is done and we save the can
122output = open(can['settings']['outfile_name'], 'wb')
123pickle.dump(can, output)
124output.close()
Note: See TracBrowser for help on using the repository browser.