# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # There is a hint for each section, after the answer field
#
# ## first think then use the hint!
#
# ### Good luck

# ## First and foremost import the-as-usual libraries (hamoon hamishegi!) needed to data analysis



# **HINT:**
#
# import numpy as np 
#
# import matplotl...
#
# import pan....
#
# # %matplotli...

# ### read the `metadata.csv` in sdss folder using pandas
# #### change directory if it is needed



# **HINT:**
#
# df = pd...('ADDRESS TO THE FILE')

# ### check the file









# #### create a function that uses columns `'#plate'` and `'mjd'` and `'fiberid'` and create the file names in the SDSS folder
#
# #### then add a new column named `'filename'` in your DataFrame in the previous cell with this filename
#
# * you should use pandas apply method to apply the function on the columns
#     for example for adding c1 and c2 columns
#     
#     def add(x):
#         return x['c1'] + x['c2']
#     
#     x.apply(add, axis=1)
#     
#     



# **HINT:**
#
# def name_maker(x):
#
#     return 'spec-{:04d.....}'.format(x['#plat....)
#
# df[.....]....apply(name_maker....1)

# ### check the output!





# ### you need a the list of fits files
# #### Dang it! you can't use `` m=`ls *.fits` `` here, or can you?
# #### don't worry! use import glob funtion from glob library!



# **HINT:**
#
# from glob ...

# ### now give the address of fits files using *!
#
# ### for example `glob('ADDRESS_TO_THE_FITS_FILES/*.fits')`



# **HINT:**
#
# spec_list = glob('adress to the fileSSSSSSS (file ha)')

# ### Check out the type of the output! it is a list!
# you can use the indecies!





# ## you now need to read fits file!
# fits file are complex file format you can put various data type in them from data to picture
#
# import `fits` from `astropy.io` library



# **HINT:**
#
# from astropy.io ....



# hdul = fits.open(THIS IS FITS FILE) 

# ### About fits file format
#
# **fits data are complex data format**
#
# **there are some things like folder inside them**
#
# **use this commend to take a look**
#
# **`ESM_OBJECT.info()`**







# **HINT:**
#
# data = hdul[1].data  # assume the first extension is a table

# ### You need a tabular data 
# use `Table` function from  `astropy.table`



# **HINT:**
#
# from ...... import Tab....





# ### now you leant how to read file! use a for loop to read all file but notice
#
# *) use list notion to keep the track od each file
#     fitfile = []
#     for i in range(len(list of fits)):
#         hdul = fits.open(spec_list[i])
#         
#         
# *) redshift is at the end of filename use split 
#     my_str = '<EMAIL>'
#     splited_add = my_str.split(@)
#     splitted_add[0] = majid
#     splitted_add[1] = majid.com
#     
# *) The next part is your homework



# **HINT**
#
# X_train = []
#
#
# lam_max = 3500
#
# for i in range(len(spec_list)):
#     
#     hdul = fits.open(spec_list[i])  # open a FITS file
#    
#
#     ....
#     
#     zclass = int(10*(z-1))
#     
#     zhotv = np.zeros(10)
#     
#     zhotv[zclass] = 1
#     
#     if i<len(spec_list)//2:
#         
#         X_train.append(xx)
#     ....
#     else:
#         X_test.append(xx)
#        ....
#     # tdata

# ### Create the test and training set for parameter and target
# ### Then print the shape of each 



#

# ### Now it is time to create the Neural Network
# #### first import the needed library



from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout, Conv1D, MaxPooling1D, Flatten

# ### Create the NN using Keras



# **HINT:** 
#
# model = Sequential()
#
# model.add(Conv1D(.....))
#
# model.add(MaxPooling1D(......))
#
# model.add(Conv1D(......
#
# model.add(MaxPooling1D(.....
#
# model.add(Conv1D(10, 10, .....
#
# model.add(MaxPooling1D(....
#
# model.add(Flatte......
#
# model.add(Dense(32, ....
#
# model.add(Dense(16, ....
#
# model.add(Dropout(.....
#
# model.add(Dense(10, activation='si.....



# ### compile the model



model.compile(loss='categorical_cro.....

# ### Fit the model



# +
# model.fit(X_t...
# -



# ### Evaluate the model



model.evaluate(X_t.....



# ### Do prediction



# **HINT:**
#
# y_pred = model.pr.......
#
# y_true = y_test.a.....
#
# y_pred = y_pred.....



# ### Confusion Matrix plot
# ### just use the Matrix below (the code is prepared! - just use it!)

# +
from sklearn.metrics import confusion_matrix

def plot_confusion_matrix(y_true, y_pred, classes,
                          normalize=False,
                          title=None,
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    if not title:
        if normalize:
            title = 'Normalized confusion matrix'
        else:
            title = 'Confusion matrix, without normalization'

    # Compute confusion matrix
    cm = confusion_matrix(y_true, y_pred)
    # Only use the labels that appear in the data
#     classes = classes[unique_labels(y_true, y_pred)]
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
#         print("Normalized confusion matrix")
#     else:
#         print('Confusion matrix, without normalization')

#     print(cm)
    
    fig, ax = plt.subplots(figsize=(10,10))
    im = ax.imshow(cm, interpolation='nearest', cmap=cmap)
    # We want to show all ticks...
    ax.set(xticks=np.arange(cm.shape[1]),
           yticks=np.arange(cm.shape[0]),
           # ... and label them with the respective list entries
           xticklabels=classes, yticklabels=classes,
           title=title,
           ylabel='True label',
           xlabel='Predicted label')

    # Rotate the tick labels and set their alignment.
    plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
             rotation_mode="anchor")

    # Loop over data dimensions and create text annotations.
    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i in range(cm.shape[0]):
        for j in range(cm.shape[1]):
            ax.text(j, i, format(cm[i, j], fmt),
                    ha="center", va="center",
                    color="white" if cm[i, j] > thresh else "black")
    fig.tight_layout()
    return ax



# -

# ### The Output should be something like this:

plot_confusion_matrix(y_true, y_pred,
                      ['{:4.2f}'.format(i+0.05) for i in np.linspace(0,1,10,endpoint=0)],
                      normalize=1,title='CM')




StarcoderdataJupyterScriptsDedupFiltered