Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
G4Alfo
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Alfonso Pisapia
G4Alfo
Commits
66c74b6c
Commit
66c74b6c
authored
8 months ago
by
Alfonso
Browse files
Options
Downloads
Patches
Plain Diff
file .arf e plot
parent
2a10a1b5
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
python/aeFF.py
+49
-55
49 additions, 55 deletions
python/aeFF.py
python/arf.py
+53
-45
53 additions, 45 deletions
python/arf.py
python/plot.py
+15
-24
15 additions, 24 deletions
python/plot.py
with
117 additions
and
124 deletions
python/aeFF.py
+
49
−
55
View file @
66c74b6c
# va bene fare la media per ogni combinazione theta phi di energia?
# come faccio a sapere se il mio codice è robusto
# dato che divido per gli eventi detected va bene cosi o devo dividere per il totale
# dei generati e moltiplicare per l'area della sorgente?
import
numpy
as
np
import
os
from
data
import
data_estraction
import
astropy.io.fits
as
fits
import
pandas
as
pd
from
multiprocessing
import
Pool
from
plot
import
plot_area
from
plot
import
plot_area
from
arf
import
arf_file
def
main
():
# 1 Estract data from fits file
file_dat
=
"
areaEfficace.dat
"
data_estraction
(
file_dat
)
#2 Filter data
data
=
np
.
loadtxt
(
file_dat
)
energy
=
data
[:,
0
]
ratio_X
=
data
[:,
1
]
ratio_S
=
data
[:,
2
]
theta
=
data
[:,
3
]
phi
=
data
[:,
4
]
# Ottieni tutte le combinazioni uniche di theta e phi
angle_combinations
=
np
.
unique
(
np
.
column_stack
((
theta
,
phi
)),
axis
=
0
)
# Inizializza due liste per tracciare i dati per conti X e conti S
plot_data_X
=
[]
plot_data_S
=
[]
# Cicla attraverso tutte le combinazioni uniche di theta e phi
for
combination
in
angle_combinations
:
# estrae dall'array due valori e gli da il nome
theta_val
=
combination
[
0
]
phi_val
=
combination
[
1
]
# Maschera booleana per filtrare i dati per la combinazione corrente di theta e phi
# confronta i dati del file con i dati dell'array creato
mask
=
(
theta
==
theta_val
)
&
(
phi
==
phi_val
)
energy_filtered
=
energy
[
mask
]
ratio_X_filtered
=
ratio_X
[
mask
]
ratio_S_filtered
=
ratio_S
[
mask
]
# Salva i dati per il plot
plot_data_X
.
append
((
energy_filtered
,
ratio_X_filtered
,
theta_val
,
phi_val
))
plot_data_S
.
append
((
energy_filtered
,
ratio_S_filtered
,
theta_val
,
phi_val
))
# 3 Arf
arf_file
(
energy_filtered
,
ratio_X_filtered
,
ratio_S_filtered
,
theta_val
,
phi_val
)
# 4 Plot
plot_area
(
plot_data_X
,
'
X
'
,
file_name
=
"
XGIS_AeffX.png
"
)
plot_area
(
plot_data_S
,
'
S
'
,
file_name
=
"
XGIS_AeffS.png
"
)
# Funzione che prende in input un singolo file FITS da processare.
def
get_data
(
file_path
):
with
fits
.
open
(
file_path
)
as
hdul
:
data
=
hdul
[
1
].
data
tot_events
=
len
(
data
)
scint_id_col
=
data
[
'
Scint_ID
'
]
energy
=
data
[
'
En_Primary
'
][
0
]
eventi_X
=
(
scint_id_col
==
-
1000
).
sum
()
eventi_S
=
tot_events
-
eventi_X
ratio_X
=
eventi_X
/
tot_events
ratio_S
=
eventi_S
/
tot_events
return
energy
,
ratio_X
,
ratio_S
def
write_raw_results
(
output
,
results
):
with
open
(
output
,
"
a
"
)
as
f
:
for
energy
,
ratio_X
,
ratio_S
in
results
:
f
.
write
(
f
"
{
energy
:
.
1
f
}
\t
{
ratio_X
:
.
3
f
}
\t
{
ratio_S
:
.
3
f
}
\n
"
)
# se vuoi i dati rough commenta questa funzione
#def write_results(output_file):
# # Questa riga legge il file .dat e crea un DataFrame
# data = pd.read_csv(output_file, delim_whitespace=True, header=None, names=['energy', 'ratio_X', 'ratio_S'])
# # Calcola la media per ogni valore unico di energia
# result = data.groupby('energy').mean().reset_index()
# # Approssima i risultati alla terza cifra decimale
# result = result.round(3)
# # Scrive il risultato nel file di output
# result.to_csv(output_file, sep='\t', index=False, header=False)
# Funzione che processa tutti i file .fits presenti nella folder
def
process_folder_parallel
(
folder
,
output
):
# Crea una lista di file FITS trovati nella folder. Sorted per leggerli in ordine
files
=
sorted
([
os
.
path
.
join
(
folder
,
file
)
for
file
in
os
.
listdir
(
folder
)
if
file
.
endswith
(
"
.fits
"
)],
key
=
os
.
path
.
getmtime
)
# Crea un pool di processi che permette di eseguire funzioni in parallelo.
# Usa il metodo map() del pool per eseguire la funzione get_data() su ciascun file FITS in parallelo.
# I risultati vengono raccolti in una lista chiamata results.
with
Pool
()
as
pool
:
results
=
pool
.
map
(
get_data
,
files
)
# qui files è l'argomento che passi a get_data
write_raw_results
(
output
,
results
)
#write_results(output_file)
if
__name__
==
"
__main__
"
:
if
__name__
==
"
__main__
"
:
folder
=
"
/home/alfonso/Scrivania/THESEUS/xgis_m7-main/fits
"
main
()
output
=
"
areaEfficace.dat
"
# Inizializza il file di output
open
(
output
,
"
w
"
).
close
()
process_folder_parallel
(
folder
,
output
)
plot_area
(
output
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
python/arf.py
+
53
−
45
View file @
66c74b6c
...
@@ -2,49 +2,57 @@ import astropy.io.fits as pyfits
...
@@ -2,49 +2,57 @@ import astropy.io.fits as pyfits
import
os
import
os
import
numpy
as
np
import
numpy
as
np
try
:
def
create_header
(
extension
):
extension
.
header
[
'
EXTNAME
'
]
=
'
SPECRESP
'
extension
.
header
[
'
TELESCOP
'
]
=
'
THESEUS
'
extension
.
header
[
'
INSTRUME
'
]
=
'
XGIS
'
extension
.
header
[
'
FILTER
'
]
=
'
NONE
'
extension
.
header
[
'
HDUCLASS
'
]
=
'
OGIP
'
extension
.
header
[
'
HDUCLAS1
'
]
=
'
RESPONSE
'
extension
.
header
[
'
HDUCLAS2
'
]
=
'
SPECRESP
'
extension
.
header
[
'
HDUVERS
'
]
=
'
1.1.0
'
# obsolete (for old software)
extension
.
header
[
'
ARFVERSN
'
]
=
'
1992A
'
extension
.
header
[
'
HDUVERS1
'
]
=
'
1.0.0
'
extension
.
header
[
'
HDUVERS2
'
]
=
'
1.1.0
'
def
arf_file
(
energy_filt
,
ratio_X_filt
,
ratio_S_filt
,
theta
,
phi
):
try
:
# Creazione di "Null" primary array
# Creazione di "Null" primary array
primary_hdu
=
pyfits
.
PrimaryHDU
()
primary_hdu
=
pyfits
.
PrimaryHDU
()
# Extension
# Creazione di BinTableHDU per EVENTI X
data
=
np
.
loadtxt
(
"
areaEfficace.dat
"
,
comments
=
'
#
'
)
bin_tableX_hdu
=
pyfits
.
BinTableHDU
.
from_columns
([
energy
=
data
[:,
0
]
pyfits
.
Column
(
name
=
'
ENERG_LO
'
,
format
=
'
1E
'
,
unit
=
'
keV
'
,
array
=
energy_filt
[:
-
1
]),
ratio_X
=
data
[:,
1
]
pyfits
.
Column
(
name
=
'
ENERG_HI
'
,
format
=
'
1E
'
,
unit
=
'
keV
'
,
array
=
energy_filt
[
1
:]),
ratio_S
=
data
[:,
2
]
pyfits
.
Column
(
name
=
'
SPECRESP
'
,
format
=
'
1E
'
,
unit
=
'
cm**2
'
,
array
=
ratio_X_filt
),
# Creazione di BinTableHDU
bin_table_hdu
=
pyfits
.
BinTableHDU
.
from_columns
([
pyfits
.
Column
(
name
=
'
ENERG_LO
'
,
format
=
'
1E
'
,
unit
=
'
keV
'
,
array
=
energy
[:
-
1
]),
# [inizio:fine:passo]
pyfits
.
Column
(
name
=
'
ENERG_HI
'
,
format
=
'
1E
'
,
unit
=
'
keV
'
,
array
=
energy
[
1
:]),
# 0 primo, 1 secondo, -1 ultimo escluso
pyfits
.
Column
(
name
=
'
SPECRESP
'
,
format
=
'
1E
'
,
unit
=
'
cm**2
'
,
array
=
ratio_X
),
])
])
create_header
(
bin_tableX_hdu
)
# Aggiunta header comune per gli eventi X
# Modifica dell'header. Mandatory keywords
# Creazione di BinTableHDU per EVENTI S
bin_table_hdu
.
header
[
'
EXTNAME
'
]
=
'
SPECRESP
'
bin_tableS_hdu
=
pyfits
.
BinTableHDU
.
from_columns
([
bin_table_hdu
.
header
[
'
TELESCOP
'
]
=
'
THESEUS
'
pyfits
.
Column
(
name
=
'
ENERG_LO
'
,
format
=
'
1E
'
,
unit
=
'
keV
'
,
array
=
energy_filt
[:
-
1
]),
bin_table_hdu
.
header
[
'
INSTRUME
'
]
=
'
XGIS
'
pyfits
.
Column
(
name
=
'
ENERG_HI
'
,
format
=
'
1E
'
,
unit
=
'
keV
'
,
array
=
energy_filt
[
1
:]),
bin_table_hdu
.
header
[
'
FILTER
'
]
=
'
NONE
'
pyfits
.
Column
(
name
=
'
SPECRESP
'
,
format
=
'
1E
'
,
unit
=
'
cm**2
'
,
array
=
ratio_S_filt
),
bin_table_hdu
.
header
[
'
HDUCLASS
'
]
=
'
OGIP
'
])
bin_table_hdu
.
header
[
'
HDUCLAS1
'
]
=
'
RESPONSE
'
create_header
(
bin_tableS_hdu
)
# Aggiunta header comune per gli eventi S
bin_table_hdu
.
header
[
'
HDUCLAS2
'
]
=
'
SPECRESP
'
bin_table_hdu
.
header
[
'
HDUVERS
'
]
=
'
1.1.0
'
# obsolete (for old software)
bin_table_hdu
.
header
[
'
ARFVERSN
'
]
=
'
1992A
'
bin_table_hdu
.
header
[
'
HDUVERS1
'
]
=
'
1.0.0
'
bin_table_hdu
.
header
[
'
HDUVERS2
'
]
=
'
1.1.0
'
# optional
# bin_table_hdu.header['PHAFILE'] =
# Creazione di HDUList e scrittura del file FITS
# Creazione di HDUList e scrittura del file FITS
hdul
=
pyfits
.
HDUList
([
primary_hdu
,
bin_table_hdu
])
hdulX
=
pyfits
.
HDUList
([
primary_hdu
,
bin_tableX_hdu
])
hdulS
=
pyfits
.
HDUList
([
primary_hdu
,
bin_tableS_hdu
])
output_dir
=
"
/home/alfonso/Scrivania/THESEUS/xgis_m7-main/arf
"
output_dir
=
"
/home/alfonso/Scrivania/THESEUS/xgis_m7-main/python
"
if
not
os
.
path
.
exists
(
output_dir
):
#output_file = os.path.join(output_dir, input_file.split(".")[0] + ".arf")
os
.
makedirs
(
output_dir
)
output_file
=
os
.
path
.
join
(
output_dir
,
"
areaEfficace.arf
"
)
hdul
.
writeto
(
output_file
,
overwrite
=
True
)
output_X
=
os
.
path
.
join
(
output_dir
,
f
"
XaeFF_
{
theta
}
_
{
phi
}
.arf
"
)
output_S
=
os
.
path
.
join
(
output_dir
,
f
"
SaeFF_
{
theta
}
_
{
phi
}
.arf
"
)
print
(
"
File .arf creato correttamente
"
)
hdulX
.
writeto
(
output_X
,
overwrite
=
True
)
hdulS
.
writeto
(
output_S
,
overwrite
=
True
)
print
(
"
File .arf creati correttamente.
"
)
except
Exception
as
e
:
except
Exception
as
e
:
print
(
"
Errore durante la creazione del file .arf
"
)
print
(
f
"
Errore durante la creazione del file .arf:
{
e
}
"
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
python/plot.py
+
15
−
24
View file @
66c74b6c
...
@@ -2,40 +2,31 @@ import numpy as np
...
@@ -2,40 +2,31 @@ import numpy as np
import
matplotlib.pyplot
as
plt
import
matplotlib.pyplot
as
plt
import
os
import
os
def
plot_area
(
output
):
def
plot_area
(
plot_data
,
type
,
file_name
):
# Legge il file ignorando le righe che iniziano con #
data
=
np
.
loadtxt
(
output
,
comments
=
'
#
'
)
# Separa le due colonne
for
data
in
plot_data
:
energy
=
data
[:,
0
]
# Prima colonna: Energy [keV]
energy
,
ratio
,
theta_val
,
phi_val
=
data
ratio_X
=
data
[:,
1
]
label
=
f
'
Theta=
{
theta_val
}
, Phi=
{
phi_val
}
'
ratio_S
=
data
[:,
2
]
# Crea il grafico
plt
.
plot
(
energy
,
ratio
,
marker
=
'
o
'
,
linestyle
=
'
-
'
,
label
=
label
)
plt
.
figure
(
figsize
=
(
8
,
6
))
plt
.
plot
(
energy
,
ratio_X
,
marker
=
'
o
'
,
linestyle
=
'
-
'
,
color
=
'
b
'
,
label
=
'
sdd
'
)
plt
.
plot
(
energy
,
ratio_S
,
marker
=
'
x
'
,
linestyle
=
'
--
'
,
color
=
'
r
'
,
label
=
'
scint
'
)
# Aggiungi etichette e titolo
# Aggiungi etichette e titolo
plt
.
xlabel
(
'
Energy [keV]
'
)
plt
.
xlabel
(
'
Energy [keV]
'
)
plt
.
ylabel
(
'
Aeff
'
)
plt
.
ylabel
(
f
'
Aeff
_
{
type
}
'
)
plt
.
title
(
'
XGIS
'
)
plt
.
title
(
f
'
XGIS
- Eventi_
{
type
}
'
)
plt
.
grid
(
True
)
plt
.
grid
(
True
)
# Mostra la legenda
# Mostra la legenda
plt
.
legend
()
plt
.
legend
(
bbox_to_anchor
=
(
1.05
,
1
),
loc
=
'
upper left
'
)
# Salva il grafico
# Salva il grafico
# Specifica il percorso completo dove salvare il grafico
directory
=
'
/home/alfonso/Scrivania/THESEUS/xgis_m7-main/python/figure
'
directory
=
'
/home/alfonso/Scrivania/THESEUS/xgis_m7-main/python/figure
'
if
not
os
.
path
.
exists
(
directory
):
if
not
os
.
path
.
exists
(
directory
):
os
.
makedirs
(
directory
)
os
.
makedirs
(
directory
)
file_path
=
os
.
path
.
join
(
directory
,
output
.
split
(
"
.
"
)[
0
]
+
"
.png
"
)
#.pdf
file_path
=
os
.
path
.
join
(
directory
,
file_name
)
plt
.
savefig
(
file_path
,
bbox_inches
=
'
tight
'
)
# Salva il grafico
plt
.
savefig
(
file_path
)
# Mostra il grafico
plt
.
show
()
# Pulire la figura per i plot successivi
plt
.
clf
()
#plt.show()
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment