Example #1
0
def save_excitation_force(result, filename):
    """
    Saves the excitation forces to a file in tec format
    Args:
        result: object, the hydrodynamic coefficients cases
        filename: The path to the file where to save
    """
    signature = __name__ + '.save_excitation_force(result, filename)'
    logger = logging.getLogger(__name__)
    utility.log_entrance(logger, signature,
                         {"result": str(result),
                          'filename': str(filename)})
    utility.mkdir_p(os.path.abspath(os.path.dirname(filename)))
    with open(filename, 'w') as inp:
        inp.write('VARIABLES="w (rad/s)"\n')

        for k in range(result.n_integration):
            s = '"abs(F\t' + str(result.idx_force[k, 1]) + '\t' + str(result.idx_force[k, 2])
            s += ')" "angle(F\t' + str(result.idx_force[k, 1]) + '\t' + str(result.idx_force[k, 2]) + ')"\n'
            inp.write(s)

        for j in range(result.n_beta):
            s = 'Zone t="Diffraction force - beta = ' + str(result.beta[j]*180./(4.*np.arctan(1.0)))
            s += ' deg",I=  ' + str(result.n_w) + ',F=POINT\n'
            inp.write(s)
            for i in range(result.n_w):
                s = str(result.w[i]) + '\t'
                for k in range(result.n_integration):
                    temp = result.diffraction_force[i, j, k] + result.froudkrylov_force[i, j, k]
                    s += str(np.abs(temp)) + '\t'
                    s += str(np.arctan2(np.imag(temp), np.real(temp))) + '\t'
                inp.write(s + '\n')

    utility.log_and_print(logger, utility.get_abs(filename) + ' contains the excitation forces in tec format.')
    utility.log_exit(logger, signature, [None])
Example #2
0
def test(args, testloader, enc, dec, disc_l, disc_v, epoch, use_cuda):
    global best_acc
    batch_time = AverageMeter()
    data_time = AverageMeter()
    top1 = AverageMeter()

    # switch to evaluate mode
    enc.eval()
    dec.eval()

    disc_l.eval()
    disc_v.eval()

    end = time.time()
    batch = args.batch
    isize = args.isize
    csize = args.cropsize
    exp = args.exp_name
    for batch_idx, (inputs, targets) in enumerate(testloader):

        # measure data loading time
        data_time.update(time.time() - end)

        if use_cuda:
            inputs, targets = inputs.cuda(), targets.cuda()
        with torch.no_grad():
            inputs, targets = torch.autograd.Variable(
                inputs), torch.autograd.Variable(targets)
            # compute output

            recon = dec(enc(inputs), args)
            scores = torch.mean(torch.pow((inputs - recon), 2), dim=[1, 2, 3])
            prec1 = roc_auc_score(targets.cpu().detach().numpy(),
                                  -scores.cpu().detach().numpy())

        top1.update(prec1, inputs.size(0))

        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()

    print(top1.avg)
    recon = recon.cpu().data
    recon = recon / 2 + 0.5
    inputs = inputs.cpu().data
    inputs = inputs / 2 + 0.5
    if not os.path.exists('./result'):
        os.mkdir('./result')
    if not os.path.isdir(os.path.join("result", exp, "test_real")):
        utility.mkdir_p(os.path.join("result", exp, "test_real"))
    if not os.path.isdir(os.path.join("result", exp, "test_fake")):
        utility.mkdir_p(os.path.join("result", exp, "test_fake"))
    t_fake_name = 'fake' + exp + '_' + str(epoch) + '.png'
    t_real_name = 'real' + exp + '_' + str(epoch) + '.png'
    save_image(recon, os.path.join(".", "result", exp, "test_fake",
                                   t_fake_name))
    save_image(inputs,
               os.path.join(".", "result", exp, "test_real", t_real_name))

    return top1.avg
Example #3
0
def save_radiation_coefficients(result, filename):
    """
    Saves the radiation coefficient to a file in tec format
    Args:
        result: object, the hydrodynamic coefficients cases
        filename: The path to the file where to save
    """
    utility.mkdir_p(os.path.abspath(os.path.dirname(filename)))
    with open(filename, 'w') as inp:

        inp.write('VARIABLES="w (rad/s)"\n')

        for k in range(result.n_integration):
            s = '"A\t' + str(result.idx_force[k, 1]) + '\t' + str(result.idx_force[k, 2])
            s = s + '" "B\t' + str(result.idx_force[k, 1]) + '\t' + str(result.idx_force[k, 2]) + '"\n'
            inp.write(s)

        for j in range(result.n_radiation):
            s = 'Zone t="Motion of body\t' + str(result.idx_radiation[j, 1])
            s += '\tin DoF\t' + str(result.idx_radiation[j, 2]) + '",I=\t' + str(result.n_w) + ',F=POINT\n'
            inp.write(s)
            for i in range(result.n_w):
                s = str(result.w[i]) + '\t'
                for k in range(result.n_integration):
                    s += str(result.added_mass[i, j, k]) + '\t' + str(result.radiation_damping[i, j, k]) + '\t'
                inp.write(s + '\n')
Example #4
0
def save_irf(irf, filename):
    """
    Saves the irf to a file in the tec format
    Args:
        irf: object, the irf
        filename: string, The path to the file where to save the irf
    """
    signature = __name__ + '.save_irf(irf, filename)'
    logger = logging.getLogger(__name__)
    utility.log_entrance(logger, signature,
                         {"irf": str(irf),
                          'filename': str(filename)})
    utility.mkdir_p(os.path.abspath(os.path.dirname(filename)))
    with open(filename, 'w') as inp:
        inp.write('VARIABLES="Time (s)"\n')
        for k in range(irf.n_integration):
            inp.write('"AddedMass '+ str(k+1) + '" "IRF ' + str(k+1) +'"\n')
        for j in range(irf.n_radiation):
            inp.write('Zone t="DoF ' + str(j+1) + '",I=\t' + str(irf.n_time) + ',F=POINT\n')
            for i in range(irf.n_time):
                inp.write(' ' + str(irf.time[i]) + ' ')
                s = ''
                for k in range(irf.n_integration):
                    s += ' ' + str(irf.added_mass[j, k]) + ' ' + ' ' + str(irf.k[i, j, k]) + ' '
                inp.write(s + '\n')

    utility.log_and_print(logger, utility.get_abs(filename) + ' contains the irf in tec format.')
    utility.log_exit(logger, signature, [None])
Example #5
0
def save_stifness(result, filename):
    """
    Saves the radiation coefficient to a file in tec format
    Args:
        result: object, the hydrodynamic coefficients cases
        filename: The path to the file where to save
    """
    signature = __name__ + '.save_stifness(result, filename)'
    logger = logging.getLogger(__name__)
    utility.log_entrance(logger, signature, {
        "result": str(result),
        'filename': str(filename)
    })

    utility.mkdir_p(os.path.abspath(os.path.dirname(filename)))

    with open(filename, 'w') as inp:
        for i in range(len(result[:, :])):
            for j in range(len(result[:, :])):
                if j == len(result[:, :]) - 1:
                    inp.write(" % E\n" % result[i, j])
                else:
                    inp.write(" % E " % result[i, j])

    utility.log_and_print(
        logger,
        utility.get_abs(filename) + ' contains the hydrostatic stifness.')
    utility.log_exit(logger, signature, [None])
Example #6
0
def save_irf(irf, filename):
    """
    Saves the irf to a file in the tec format
    Args:
        irf: object, the irf
        filename: string, The path to the file where to save the irf
    """
    signature = __name__ + '.save_irf(irf, filename)'
    logger = logging.getLogger(__name__)
    utility.log_entrance(logger, signature, {
        "irf": str(irf),
        'filename': str(filename)
    })
    utility.mkdir_p(os.path.abspath(os.path.dirname(filename)))
    with open(filename, 'w') as inp:
        inp.write('VARIABLES="Time (s)"\n')
        for k in range(irf.n_integration):
            inp.write('"AddedMass ' + str(k + 1) + '" "IRF ' + str(k + 1) +
                      '"\n')
        for j in range(irf.n_radiation):
            inp.write('Zone t="DoF ' + str(j + 1) + '",I=\t' +
                      str(irf.n_time) + ',F=POINT\n')
            for i in range(irf.n_time):
                inp.write(' ' + str(irf.time[i]) + ' ')
                s = ''
                for k in range(irf.n_integration):
                    s += ' ' + str(irf.added_mass[j, k]) + ' ' + ' ' + str(
                        irf.k[i, j, k]) + ' '
                inp.write(s + '\n')

    utility.log_and_print(
        logger,
        utility.get_abs(filename) + ' contains the irf in tec format.')
    utility.log_exit(logger, signature, [None])
Example #7
0
def save_excitation_force(result, filename):
    """
    Saves the excitation forces to a file in tec format
    Args:
        result: object, the hydrodynamic coefficients cases
        filename: The path to the file where to save
    """
    utility.mkdir_p(os.path.abspath(os.path.dirname(filename)))
    with open(filename, 'w') as inp:
        inp.write('VARIABLES="w (rad/s)"\n')

        for k in range(result.n_integration):
            s = '"abs(F\t' + str(result.idx_force[k, 1]) + '\t' + str(result.idx_force[k, 2])
            s += ')" "angle(F\t' + str(result.idx_force[k, 1]) + '\t' + str(result.idx_force[k, 2]) + ')"\n'
            inp.write(s)

        for j in range(result.n_beta):
            s = 'Zone t="Diffraction force - beta = ' + str(result.beta[j]*180./(4.*np.arctan(1.0)))
            s += ' deg",I=' + str(result.n_w) + ',F=POINT\n'
            inp.write(s)
            for i in range(result.n_w):
                s = str(result.w[i]) + '\t'
                for k in range(result.n_integration):
                    temp = result.diffraction_force[i, j, k] + result.froudkrylov_force[i, j, k]
                    s += str(np.abs(temp)) + '\t'
                    s += str(np.arctan2(np.imag(temp), np.real(temp))) + '\t'
                inp.write(s + '\n')
Example #8
0
def write_fk_force_tec(int_case, fk_force, w, beta, filename):
    """
    Writes the froude krylov forces to .tec format
    Args:
        int_case: 1D array, the integration cases
        fk_forces: 3D array, the froudkrylov forces
        w: 1D array, represents the wave frequencies omega
        beta: 1D array, represents the wave directions beta
        filename: string, the path to the file where to save the forces
    """
    utility.mkdir_p(os.path.abspath(os.path.dirname(filename)))
    n_integration = len(int_case)
    n_beta = len(beta)
    n_w = len(w)
    with open(filename, 'w') as inp:
        inp.write('VARIABLES="w (rad/s)"\n')
        for k in range(n_integration):
            s = '"abs(F\t' + str(int_case[k].body +
                                 1) + '\t' + str(k + 1) + ')" "angle(F\t'
            s += str(int_case[k].body + 1) + '\t' + str(k + 1) + ')"\n'
            inp.write(s)

        for c in range(n_beta):
            inp.write('Zone t="FKforce - beta =\t' +
                      str(beta[c] * 180. / np.pi) + '",I=\t' + str(n_w) +
                      ',F=POINT\n')
            for i in range(n_w):
                s = str(w[i]) + '\t'
                for k in range(n_integration):
                    val = str(
                        np.arctan2(np.imag(fk_force[i, c, k]),
                                   np.real(fk_force[i, c, k])))
                    s += str(np.abs(fk_force[i, c, k])) + '\t' + val + '\t'
                inp.write(s)
                inp.write('\n')
Example #9
0
def write_mesh_tec(mesh, mesh_tec_file):
    """
    Export the mesh to tec file
    Args:
        mesh: object, the mesh
        mesh_tec_file: string, the path to the mesh tec file to save
    """
    utility.mkdir_p(os.path.abspath(os.path.dirname(mesh_tec_file)))
    with open(mesh_tec_file, 'w') as inp:
        inp.write('VARIABLES="X" "Y" "Z" "NX" "NY" "NZ" "A"\n')
        inp.write('ZONE N=\t' + str(mesh.n_points) + '\t, E=\t' +
                  str(mesh.n_panels) + '\t, F=FEPOINT,ET=QUADRILATERAL\n')
        for i in range(mesh.n_points):
            s = str(mesh.x[0, i]) + '\t' + str(mesh.x[1, i]) + '\t' + str(
                mesh.x[2, i]) + '\t0.\t0.\t0.\t0.\n'
            inp.write(s)

        for i in range(mesh.n_panels):
            s = str(mesh.p[0, i] +
                    1) + '\t' + str(mesh.p[1, i] + 1) + '\t' + str(
                        mesh.p[2, i] + 1) + '\t' + str(mesh.p[3, i] + 1) + '\n'
            inp.write(s)

        inp.write('ZONE t="normales", F=POINT, I=\t' + str(mesh.n_panels) +
                  '\n')

        for i in range(mesh.n_panels):
            s = str(mesh.xm[0, i]) + '\t' + str(mesh.xm[1, i]) + '\t' + str(
                mesh.xm[2, i]) + '\t'
            s += str(mesh.n[0, i]) + '\t' + str(mesh.n[1, i]) + '\t' + str(
                mesh.n[2, i]) + '\t'
            s += str(mesh.a[i]) + '\n'
            inp.write(s)
Example #10
0
def write_fk_force_tec(int_case, fk_force, w, beta, filename):
    """
    Writes the froude krylov forces to .tec format
    Args:
        int_case: 1D array, the integration cases
        fk_forces: 3D array, the froudkrylov forces
        w: 1D array, represents the wave frequencies omega
        beta: 1D array, represents the wave directions beta
        filename: string, the path to the file where to save the forces
    """
    utility.mkdir_p(os.path.abspath(os.path.dirname(filename)))
    n_integration = len(int_case)
    n_beta = len(beta)
    n_w = len(w)
    with open(filename, 'w') as inp:
        inp.write('VARIABLES="w (rad/s)"\n')
        for k in range(n_integration):
            s = '"abs(F\t' + str(int_case[k].body + 1) + '\t' + str(k+1) + ')" "angle(F\t'
            s += str(int_case[k].body + 1) + '\t' + str(k+1) + ')"\n'
            inp.write(s)

        for c in range(n_beta):
            inp.write('Zone t="FKforce - beta =\t' + str(beta[c]*180./np.pi) + '",I=\t' + str(n_w) + ',F=POINT\n')
            for i in range(n_w):
                s = str(w[i]) + '\t'
                for k in range(n_integration):
                    val = str(np.arctan2(np.imag(fk_force[i, c, k]), np.real(fk_force[i, c, k])))
                    s += str(np.abs(fk_force[i, c, k])) + '\t' + val + '\t'
                inp.write(s)
                inp.write('\n')
Example #11
0
def write_mesh_tec(mesh, mesh_tec_file):
    """
    Export the mesh to tec file
    Args:
        mesh: object, the mesh
        mesh_tec_file: string, the path to the mesh tec file to save
    """
    utility.mkdir_p(os.path.abspath(os.path.dirname(mesh_tec_file)))
    with open(mesh_tec_file, 'w') as inp:
        inp.write('VARIABLES="X" "Y" "Z" "NX" "NY" "NZ" "A"\n')
        inp.write('ZONE N=\t' + str(mesh.n_points) + '\t, E=\t' + str(mesh.n_panels) + '\t, F=FEPOINT,ET=QUADRILATERAL\n')
        for i in range(mesh.n_points):
            s = str(mesh.x[0, i]) + '\t' + str(mesh.x[1, i]) + '\t' + str(mesh.x[2, i]) + '\t0.\t0.\t0.\t0.\n'
            inp.write(s)

        for i in range(mesh.n_panels):
            s = str(mesh.p[0, i] + 1) + '\t' + str(mesh.p[1, i] + 1) + '\t' + str(mesh.p[2, i] + 1) + '\t' + str(mesh.p[3, i] + 1) + '\n'
            inp.write(s)

        inp.write('ZONE t="normales", F=POINT, I=\t' + str(mesh.n_panels) + '\n')

        for i in range(mesh.n_panels):
            s = str(mesh.xm[0, i]) + '\t' + str(mesh.xm[1, i]) + '\t' + str(mesh.xm[2, i]) + '\t'
            s += str(mesh.n[0, i]) + '\t' + str(mesh.n[1, i]) + '\t' + str(mesh.n[2, i]) + '\t'
            s += str(mesh.a[i]) + '\n'
            inp.write(s)
Example #12
0
def save_radiation_coefficients(result, filename):
    """
    Saves the radiation coefficient to a file in tec format
    Args:
        result: object, the hydrodynamic coefficients cases
        filename: The path to the file where to save
    """
    signature = __name__ + '.save_radiation_coefficients(result, filename)'
    logger = logging.getLogger(__name__)
    utility.log_entrance(logger, signature,
                         {"result": str(result),
                          'filename': str(filename)})

    utility.mkdir_p(os.path.abspath(os.path.dirname(filename)))
    with open(filename, 'w') as inp:

        inp.write('VARIABLES="w (rad/s)"\n')

        for k in range(result.n_integration):
            s = '"A\t' + str(result.idx_force[k, 1]) + '\t' + str(result.idx_force[k, 2])
            s = s + '" "B\t' + str(result.idx_force[k, 1]) + '\t' + str(result.idx_force[k, 2]) + '"\n'
            inp.write(s)

        for j in range(result.n_radiation):
            s = 'Zone t="Motion of body\t' + str(result.idx_radiation[j, 1])
            s += '\tin DoF\t' + str(result.idx_radiation[j, 2]) + '",I=\t' + str(result.n_w) + ',F=POINT\n'
            inp.write(s)
            for i in range(result.n_w):
                s = str(result.w[i]) + '\t'
                for k in range(result.n_integration):
                    s += str(result.added_mass[i, j, k]) + '\t' + str(result.radiation_damping[i, j, k]) + '\t'
                inp.write(s + '\n')

    utility.log_and_print(logger, utility.get_abs(filename) + ' contains the radiation coefficients in tec format.')
    utility.log_exit(logger, signature, [None])
Example #13
0
 def _autosave(self, autosave_dir, sim_data):
     utility.mkdir_p(autosave_dir)
     #filename
     temp = ['%s',self.__class__.__name__]+list(map(lambda x: str(x), time.localtime()[0:6]))
     file_name = os.path.join(autosave_dir, "-".join(temp)+'.csv')
     
     #make and save dataframe
     str_formatted_data = sim_data.to_csv()#file_name%'all_sim_traces')
     #also save all model info to txt file
     with open(file_name%'simulation_autosave', "w") as f:
         with utility.stdout_redirected(f):
             self.printVarsHelper()
             print '--------------------'
             print str_formatted_data
Example #14
0
    def save_image(self):
        self.last_save_was_series = False
        mkdir_p(self.save_directory())

        write_image(self.filename() + '.tif',
                    self.image,
                    metadata=self.metadata)

        self.save.setChecked(False)
        if self.include_incrementing_image_num.isChecked():
            increment_textbox(self.include_incrementing_image_num)
        elif self.include_incrementing_dir_num.isChecked():
            increment_textbox(self.include_incrementing_dir_num)
            zero_textbox(self.include_incrementing_image_num)
Example #15
0
def save_wave_elevation(w, etai, etap, eta, x, y, filename):
    """
    Save the wave elevation to a file in tec format
    Args:
        w: float, the wave frequency
        etai: 2D array, a wave elevation variable
        eta: 2D array, a wave elevation variable
        etap: 2D array, a wave elevation variable
        x: 1D array, a wave elevation variable
        y: 1D array, a wave elevation variable
        filename: string, the path to the file where to save
    """
    signature = __name__ + '.save_wave_elevation(w, etai, etap, eta, x, y, filename)'
    logger = logging.getLogger(__name__)
    utility.log_entrance(logger, signature,
                         {"w": w,
                          "etai": etai,
                          "etap": etap,
                          "eta": eta,
                          "x": x,
                          "y": y,
                          "filename": filename})

    nx = len(x)
    ny = len(y)
    utility.mkdir_p(os.path.abspath(os.path.dirname(filename)))
    with open(filename, 'w') as inp:
        s = 'VARIABLES="X" "Y" "etaI_C" "etaI_S" "etaP_C" "etaC_S" '
        s += '"etaI_C+etaP_C" "etaI_S+etaI_P" "|etaP|" "|etaI+etaP|"\n'
        inp.write(s)

        s = 'ZONE t="Wave frequency - w = '
        s += str(w)+ '",N=\t'+ str(nx*ny) + ', E=\t' + str((nx-1)*(nx-1)) + '\t , F=FEPOINT,ET=QUADRILATERAL\n'
        inp.write(s)
        for i in range(nx):
            for j in range(ny):
                s = str(x[i]) + '\t' + str(y[j]) + '\t' + str(np.real(etai[i, j])) + '\t'
                s += str(np.imag(etai[i,j])) + '\t' + str(np.real(etap[i, j])) + '\t'
                s += str(np.imag(etap[i,j])) + '\t' + str(np.real(etai[i,j]+etap[i,j])) + '\t'
                s += str(np.imag(etai[i,j]+etap[i,j])) + '\t' + str(np.abs(etap[i,j])) + '\t'
                s += str(np.abs(eta[i,j])) + '\n'
                inp.write(s)

        for i in range(nx-1):
            for j in range(ny-1):
                s = str(j+1+i*ny) + '\t' + str(j+1+(i+1)*ny) + '\t' + str(j+2+ i*ny) + '\n'
                inp.write(s)

    utility.log_and_print(logger, utility.get_abs(filename) + ' contains the wave elevation in tec format.')
    utility.log_exit(logger, signature, [None])
Example #16
0
def train_options():
    parser=argparse.ArgumentParser()
    # Datasets
    parser.add_argument('--exp_name' , default='first_ex',type=str)
    parser.add_argument('--dataroot', default='./data', type=str)
    parser.add_argument('--normal_class', default='1', type=str)
    parser.add_argument('--abnormal_class', default='1', type=str)
    parser.add_argument('--datasource' , default='./data',type=str)
    parser.add_argument('--isize', default='28', type=int)
    parser.add_argument('--cropsize', default='28', type=int)
    parser.add_argument('--protocol', default ='1', type = int)
    parser.add_argument('-j', '--workers', default=4, type=int, metavar='N',
                        help='number of data loading workers (default: 4)')
    parser.add_argument('--latent', default = 16, type = int)
    # Optimization options
    parser.add_argument('--epochs', default=100, type=int, metavar='N',
                        help='number of total epochs to run')
    parser.add_argument('--start-epoch', default=0, type=int, metavar='N',
                        help='manual epoch number (useful on restarts)')
    parser.add_argument('--batch', default=128, type=int, metavar='N',
                        help='batchsize')
    parser.add_argument('--lr', '--learning-rate', default=0.001, type=float,
                        metavar='LR', help='initial learning rate')
    parser.add_argument('--momentum', default=0.9, type=float, metavar='M',
                        help='momentum')
    parser.add_argument('--weight-decay', '--wd', default=5e-4, type=float,
                        metavar='W', help='weight decay (default: 1e-4)')
    parser.add_argument('--manualSeed', type=int, help='manual seed')

    # Checkpoints
    parser.add_argument('-c', '--checkpoint', default='checkpoint', type=str, metavar='PATH',
                        help='path to save checkpoint (default: checkpoint)')
    parser.add_argument('--resume', default='', type=str, metavar='PATH',
                        help='path to latest checkpoint (default: none)')
    # Architecture

        # Miscs
    #Device options
    parser.add_argument('--gpu-id', default='0', type=str,
                        help='id(s) for CUDA_VISIBLE_DEVICES')

    args = parser.parse_args()
    if not os.path.isdir(args.checkpoint):
        utility.mkdir_p(args.checkpoint)

    with open(os.path.join(args.checkpoint,'configuration.json'), 'w') as f:
        json.dump(args.__dict__, f, indent=2)  

    return args
def plot(history_model):
    plt.plot(history_model.history['val_loss'],
             'g',
             label='Network 1 Val Loss')
    plt.plot(history_model.history['loss'], 'r', label='Network Loss')

    plt.plot(history_model.history['val_acc'], 'b-', label='Network 1 Val acc')
    plt.plot(history_model.history['acc'], 'y-', label='Network acc')

    plt.xlabel('Epochs')
    plt.ylabel('acc/loss')
    plt.legend()

    out_path = path.join(RESULT_PATH, 'figures')
    if not path.exists(out_path):
        util.mkdir_p(out_path)
    plt.savefig(path.join(out_path, 'comparison_of_networks_acc.png'))
Example #18
0
def preprocess(custom_config):
    """
    Configure and then run the preprocessor

    Args:
        custom_config, dict The custom configuration dictionary
    """

    if not custom_config:
        custom_config = {}

    hdf5_file = utility.get_setting(settings.HDF5_FILE, custom_config,
                                    'HDF5_FILE')
    nemoh_cal = utility.get_setting(settings.NEMOH_CALCULATIONS_FILE,
                                    custom_config, 'NEMOH_CALCULATIONS_FILE')
    input_file = utility.get_setting(settings.NEMOH_INPUT_FILE, custom_config,
                                     'NEMOH_INPUT_FILE')
    utility.validate_string(hdf5_file, 'HDF5_FILE')
    if not nemoh_cal and not input_file:
        utility.validate_file(hdf5_file, 'HDF5_FILE')

    utility.mkdir_p(os.path.abspath(os.path.dirname(hdf5_file)))

    with h5py.File(hdf5_file, "a") as hdf5_db:
        if nemoh_cal:
            utility.convert_calculations(nemoh_cal, hdf5_db)

        if input_file:
            utility.convert_input(input_file, hdf5_db)

        remove_irregular_frequencies = utility.get_setting(
            settings.REMOVE_IRREGULAR_FREQUENCIES, custom_config,
            'REMOVE_IRREGULAR_FREQUENCIES')
        if remove_irregular_frequencies is not None:
            dset = utility.require_dataset(
                hdf5_db,
                structure.H5_SOLVER_REMOVE_IRREGULAR_FREQUENCIES, (1, ),
                dtype='i')
            utility.set_hdf5_attributes(
                dset, structure.H5_SOLVER_REMOVE_IRREGULAR_FREQUENCIES_ATTR)
            dset[:] = int(remove_irregular_frequencies)
        else:
            settings.REMOVE_IRREGULAR_FREQUENCIES = hdf5_db.get(
                structure.H5_SOLVER_REMOVE_IRREGULAR_FREQUENCIES)[0]

        run(hdf5_db, custom_config)
Example #19
0
def plot(history_model):
    plt.figure()
    plt.plot(history_model.history['val_main_output_loss'],
             'g--',
             label='Val Main output Loss')
    plt.plot(history_model.history['main_output_loss'],
             'r',
             label='Main output Loss')
    plt.plot(history_model.history['val_aux_output_loss'],
             'b--',
             label='Val AUX output Loss')
    plt.plot(history_model.history['aux_output_loss'],
             'y',
             label='Aux Loss')

    plt.xlabel('Epochs')
    plt.ylabel('loss')
    plt.legend()

    out_path = path.join(RESULT_PATH, 'figures')
    if not path.exists(out_path):
        util.mkdir_p(out_path)

    plt.savefig(path.join(out_path, 'cnn_comparison_of_networks_loss.png'))

    plt.figure()
    plt.plot(history_model.history['val_main_output_acc'],
             'g--',
             label='Network 1 Val Main output acc')
    plt.plot(history_model.history['main_output_acc'],
             'r',
             label='Main output acc')

    plt.plot(history_model.history['val_aux_output_acc'],
             'b--',
             label='Val Aux acc')
    plt.plot(history_model.history['aux_output_acc'],
             'y',
             label='Aux acc')

    plt.xlabel('Epochs')
    plt.ylabel('acc')
    plt.legend()

    plt.savefig(path.join(out_path, 'cnn_comparison_of_networks_acc.png'))
Example #20
0
def save_radiation_coefficients(result, filename):
    """
    Saves the radiation coefficient to a file in tec format
    Args:
        result: object, the hydrodynamic coefficients cases
        filename: The path to the file where to save
    """
    signature = __name__ + '.save_radiation_coefficients(result, filename)'
    logger = logging.getLogger(__name__)
    utility.log_entrance(logger, signature, {
        "result": str(result),
        'filename': str(filename)
    })

    utility.mkdir_p(os.path.abspath(os.path.dirname(filename)))
    with open(filename, 'w') as inp:

        inp.write('VARIABLES="w (rad/s)"\n')

        for k in range(result.n_integration):
            s = '"A\t' + str(result.idx_force[k, 1]) + '\t' + str(
                result.idx_force[k, 2])
            s = s + '" "B\t' + str(result.idx_force[k, 1]) + '\t' + str(
                result.idx_force[k, 2]) + '"\n'
            inp.write(s)

        for j in range(result.n_radiation):
            s = 'Zone t="Motion of body\t' + str(result.idx_radiation[j, 1])
            s += '\tin DoF\t' + str(
                result.idx_radiation[j, 2]) + '",I=\t' + str(
                    result.n_w) + ',F=POINT\n'
            inp.write(s)
            for i in range(result.n_w):
                s = str(result.w[i]) + '\t'
                for k in range(result.n_integration):
                    s += str(result.added_mass[i, j, k]) + '\t' + str(
                        result.radiation_damping[i, j, k]) + '\t'
                inp.write(s + '\n')

    utility.log_and_print(
        logger,
        utility.get_abs(filename) +
        ' contains the radiation coefficients in tec format.')
    utility.log_exit(logger, signature, [None])
Example #21
0
def save_excitation_force(result, filename):
    """
    Saves the excitation forces to a file in tec format
    Args:
        result: object, the hydrodynamic coefficients cases
        filename: The path to the file where to save
    """
    signature = __name__ + '.save_excitation_force(result, filename)'
    logger = logging.getLogger(__name__)
    utility.log_entrance(logger, signature, {
        "result": str(result),
        'filename': str(filename)
    })
    utility.mkdir_p(os.path.abspath(os.path.dirname(filename)))
    with open(filename, 'w') as inp:
        inp.write('VARIABLES="w (rad/s)"\n')

        for k in range(result.n_integration):
            s = '"abs(F\t' + str(result.idx_force[k, 1]) + '\t' + str(
                result.idx_force[k, 2])
            s += ')" "angle(F\t' + str(result.idx_force[k, 1]) + '\t' + str(
                result.idx_force[k, 2]) + ')"\n'
            inp.write(s)

        for j in range(result.n_beta):
            s = 'Zone t="Diffraction force - beta = ' + str(
                result.beta[j] * 180. / (4. * np.arctan(1.0)))
            s += ' deg",I=' + str(result.n_w) + ',F=POINT\n'
            inp.write(s)
            for i in range(result.n_w):
                s = str(result.w[i]) + '\t'
                for k in range(result.n_integration):
                    temp = result.diffraction_force[
                        i, j, k] + result.froudkrylov_force[i, j, k]
                    s += str(np.abs(temp)) + '\t'
                    s += str(np.arctan2(np.imag(temp), np.real(temp))) + '\t'
                inp.write(s + '\n')

    utility.log_and_print(
        logger,
        utility.get_abs(filename) +
        ' contains the excitation forces in tec format.')
    utility.log_exit(logger, signature, [None])
Example #22
0
def save_irf(irf, filename):
    """
    Saves the irf to a file in the tec format
    Args:
        irf: object, the irf
        filename: string, The path to the file where to save the irf
    """
    utility.mkdir_p(os.path.abspath(os.path.dirname(filename)))
    with open(filename, 'w') as inp:
        inp.write('VARIABLES="Time (s)"\n')
        for k in range(irf.n_integration):
            inp.write('"AddedMass '+ str(k+1) + '" "IRF ' + str(k+1) +'"\n')
        for j in range(irf.n_radiation):
            inp.write('Zone t="DoF ' + str(j+1) + '",I=\t' + str(irf.n_time) + ',F=POINT\n')
            for i in range(irf.n_time):
                inp.write(' ' + str(irf.time[i]) + ' ')
                s = ''
                for k in range(irf.n_integration):
                    s += ' ' + str(irf.added_mass[j, k]) + ' ' + ' ' + str(irf.k[i, j, k]) + ' '
                inp.write(s + '\n')
Example #23
0
def save_hydroinfo(cg, cf, disVol, Warea, filename):
    """
    Saves the radiation coefficient to a file in tec format
    Args:
        result: object, the hydrodynamic coefficients cases
        filename: The path to the file where to save
    cb[0] = cf[0] + cg[0] 
    cb[1] = cf[1] + cg[1]
    cb[2] = cf[2] 
    """
    signature = __name__ + '.save_hydroinfo(result, filename)'
    logger = logging.getLogger(__name__)
    utility.log_entrance(
        logger, signature, {
            "cg": str(cg),
            "cf": str(cf),
            "disVol": str(disVol),
            "Warea": str(Warea),
            'filename': str(filename)
        })

    utility.mkdir_p(os.path.abspath(os.path.dirname(filename)))

    with open(filename, 'w') as inp:
        #         print "XF = %(cb) 7.3f - XG = %(cg) 7.3f" % {"cb":cb(1), "cg":cb(1)}
        inp.write("XB = %7.3f - XG =  %7.3f\n" % (cf[0] + cg[0], cg[0]))
        inp.write("YB = %7.3f - YG =  %7.3f\n" % (cf[1] + cg[1], cg[1]))
        inp.write("ZB = %7.3f - ZG =  %7.3f\n" % (cf[2], cg[2]))
        inp.write("Displacement = %E\n" % disVol)
        inp.write("Waterplane area = %E" % Warea)


#             inp.write(str(result[i,:])
#             tmpFmt = "%5.3f"*len(result[i,:])+"\n"
#             print(tmpFmt % result[i,:])

    utility.log_and_print(
        logger,
        utility.get_abs(filename) + ' contains the hydrostatic information.')
    utility.log_exit(logger, signature, [None])
Example #24
0
def preprocess(custom_config):
    """
    Configure and then run the preprocessor

    Args:
        custom_config, dict The custom configuration dictionary
    """

    if not custom_config:
        custom_config = {}

    hdf5_file = utility.get_setting(settings.HDF5_FILE, custom_config, 'HDF5_FILE')
    nemoh_cal = utility.get_setting(settings.NEMOH_CALCULATIONS_FILE, custom_config, 'NEMOH_CALCULATIONS_FILE')
    input_file = utility.get_setting(settings.NEMOH_INPUT_FILE, custom_config, 'NEMOH_INPUT_FILE')
    utility.validate_string(hdf5_file, 'HDF5_FILE')
    if not nemoh_cal and not input_file:
        utility.validate_file(hdf5_file, 'HDF5_FILE')

    utility.mkdir_p(os.path.abspath(os.path.dirname(hdf5_file)))

    with h5py.File(hdf5_file, "a") as hdf5_db:
        if nemoh_cal:
            utility.convert_calculations(nemoh_cal, hdf5_db)

        if input_file:
            utility.convert_input(input_file, hdf5_db)

        remove_irregular_frequencies = utility.get_setting(settings.REMOVE_IRREGULAR_FREQUENCIES, custom_config,
                                       'REMOVE_IRREGULAR_FREQUENCIES')
        if remove_irregular_frequencies is not None:
            dset = utility.require_dataset(hdf5_db, structure.H5_SOLVER_REMOVE_IRREGULAR_FREQUENCIES, (1, ), dtype='i')
            utility.set_hdf5_attributes(dset, structure.H5_SOLVER_REMOVE_IRREGULAR_FREQUENCIES_ATTR)
            dset[:] = int(remove_irregular_frequencies)
        else:
            settings.REMOVE_IRREGULAR_FREQUENCIES = hdf5_db.get(structure.H5_SOLVER_REMOVE_IRREGULAR_FREQUENCIES)[0]



        run(hdf5_db, custom_config)
Example #25
0
def save_wave_elevation(w, etai, etap, eta, x, y, filename):
    """
    Save the wave elevation to a file in tec format
    Args:
        w: float, the wave frequency
        etai: 2D array, a wave elevation variable
        eta: 2D array, a wave elevation variable
        etap: 2D array, a wave elevation variable
        x: 1D array, a wave elevation variable
        y: 1D array, a wave elevation variable
        filename: string, the path to the file where to save
    """
    nx = len(x)
    ny = len(y)
    utility.mkdir_p(os.path.abspath(os.path.dirname(filename)))
    with open(filename, 'w') as inp:
        s = 'VARIABLES="X" "Y" "etaI_C" "etaI_S" "etaP_C" "etaC_S" '
        s += '"etaI_C+etaP_C" "etaI_S+etaI_P" "|etaP|" "|etaI+etaP|"\n'
        inp.write(s)

        s = 'ZONE t="Wave frequency - w = '
        s += str(w)+ '",N=\t'+ str(nx*ny) + ', E=\t' + str((nx-1)*(nx-1)) + '\t , F=FEPOINT,ET=QUADRILATERAL\n'
        inp.write(s)
        for i in range(nx):
            for j in range(ny):
                s = str(x[i]) + '\t' + str(y[j]) + '\t' + str(np.real(etai[i, j])) + '\t'
                s += str(np.imag(etai[i,j])) + '\t' + str(np.real(etap[i, j])) + '\t'
                s += str(np.imag(etap[i,j])) + '\t' + str(np.real(etai[i,j]+etap[i,j])) + '\t'
                s += str(np.imag(etai[i,j]+etap[i,j])) + '\t' + str(np.abs(etap[i,j])) + '\t'
                s += str(np.abs(eta[i,j])) + '\n'
                inp.write(s)

        for i in range(nx-1):
            for j in range(ny-1):
                s = str(j+1+i*ny) + '\t' + str(j+1+(i+1)*ny) + '\t' + str(j+2+ i*ny) + '\n'
                inp.write(s)
Example #26
0
def train(args, trainloader, enc, dec, disc_l, disc_v, optimizer_en,
          optimizer_de, optimizer_dl, optimizer_dv, criterion_ae, criterion_ce,
          Tensor, epoch, use_cuda):
    enc.train()
    dec.train()

    disc_l.train()
    disc_v.train()
    batch_time = AverageMeter()
    data_time = AverageMeter()
    losses = AverageMeter()
    isize = args.isize
    csize = args.cropsize
    batch = args.batch
    latent = args.latent
    exp = args.exp_name
    end = time.time()
    bat = 0
    for batch_idx, (inputs, targets) in tqdm(enumerate(trainloader)):

        data_time.update(time.time() - end)
        if use_cuda:
            inputs, targets = inputs.cuda(), targets.cuda()
        inputs, targets = torch.autograd.Variable(
            inputs), torch.autograd.Variable(targets)

        u = np.random.uniform(-1, 1, (batch, latent, 1, 1))
        l2 = torch.from_numpy(u).float().cuda()
        l2 = torch.tanh(l2)
        dec_l2 = dec(l2, args)

        n = torch.normal(0, 0.2, size=(batch, 3, csize, csize)).cuda()
        inputs_noise = inputs + n

        l1 = enc(inputs_noise)

        #Discriminator update

        logits_Dl_l1 = disc_l(l1)
        logits_Dl_l2 = disc_l(l2)

        dl_logits_DL_l1 = Variable(Tensor(logits_Dl_l1.shape[0], 1).fill_(0.0),
                                   requires_grad=False)
        dl_logits_DL_l2 = Variable(Tensor(logits_Dl_l2.shape[0], 1).fill_(1.0),
                                   requires_grad=False)

        loss_dl_1 = criterion_ce(logits_Dl_l1, dl_logits_DL_l1)
        loss_dl_2 = criterion_ce(logits_Dl_l2, dl_logits_DL_l2)
        loss_dl = loss_dl_1 + loss_dl_2

        # visual Discriminator l1 = real l2 = random
        logits_Dv_X = disc_v(inputs)
        logits_Dv_l2 = disc_v(dec(l2, args))

        dv_logits_Dv_X = Variable(Tensor(logits_Dv_X.shape[0], 1).fill_(1.0),
                                  requires_grad=False)
        dv_logits_Dv_l2 = Variable(Tensor(logits_Dv_l2.shape[0], 1).fill_(0.0),
                                   requires_grad=False)

        optimizer_dl.zero_grad()
        optimizer_dv.zero_grad()
        loss_dv_1 = criterion_ce(logits_Dv_X, dv_logits_Dv_X)
        loss_dv_2 = criterion_ce(logits_Dv_l2, dv_logits_Dv_l2)
        loss_dv = loss_dv_1 + loss_dv_2

        loss_second = loss_dv + loss_dl
        loss_second.backward(retain_graph=True)

        optimizer_dv.step()
        optimizer_dl.step()

        ######  update ae
        Xh = dec(l1, args)

        loss_mse = criterion_ae(Xh, inputs)  #errR

        logits_Dl_l1 = disc_l(l1)
        #logits_Dl_l2=disc_l(l2)
        ones_logits_Dl_l1 = Variable(Tensor(logits_Dl_l1.shape[0],
                                            1).fill_(1.0),
                                     requires_grad=False)
        # zeros_logits_Dl_l2 = Variable(Tensor(logits_Dl_l2.shape[0], 1).fill_(0.0), requires_grad=False)
        loss_AE_l = criterion_ce(logits_Dl_l1, ones_logits_Dl_l1)

        logits_Dv_l1_mine = disc_v(dec(l2, args))
        # logits_Dv_l2_mine = disc_v(inputs)
        ones_logits_Dv_l1_mine = Variable(Tensor(logits_Dv_l1_mine.shape[0],
                                                 1).fill_(1.0),
                                          requires_grad=False)
        # zeros_logits_Dv_l2_mine = Variable(Tensor(logits_Dv_l2_mine.shape[0], 1).fill_(0.0), requires_grad=False)
        loss_ae_v = criterion_ce(logits_Dv_l1_mine, ones_logits_Dv_l1_mine)

        optimizer_en.zero_grad()
        optimizer_de.zero_grad()

        loss_ae_all = 10.0 * loss_mse + loss_ae_v + loss_AE_l

        loss_ae_all.backward()

        optimizer_en.step()
        optimizer_de.step()

        losses.update(loss_ae_all.item(), inputs.size(0))

        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()
        bat += batch_idx

    if epoch % 5 == 0:
        recon = dec(enc(inputs), args)
        recon = recon.cpu().data
        recon = recon / 2 + 0.5
        inputs_noise = inputs_noise.cpu().data
        inputs_noise = inputs_noise / 2 + 0.5
        inputs = inputs.cpu().data
        inputs = inputs / 2 + 0.5
        if not os.path.exists('./result'):
            os.mkdir('./result')
        if not os.path.isdir(os.path.join("result", exp, "real")):
            utility.mkdir_p(os.path.join("result", exp, "real"))
        if not os.path.isdir(os.path.join("result", exp, "fake")):
            utility.mkdir_p(os.path.join("result", exp, "fake"))
        if not os.path.isdir(os.path.join("result", exp, "real_noise")):
            utility.mkdir_p(os.path.join("result", exp, "real_noise"))

        fake_name = 'fake' + exp + '_' + str(epoch) + '.png'
        real_name = 'real' + exp + '_' + str(epoch) + '.png'
        real_noise_name = 'real' + exp + '_' + str(epoch) + '.png'
        save_image(recon, os.path.join(".", "result", exp, "fake", fake_name))
        save_image(inputs, os.path.join(".", "result", exp, "real", real_name))
        save_image(
            inputs_noise,
            os.path.join(".", "result", exp, "real_noise", real_noise_name))

    return losses.avg
Example #27
0
 def save_metadata(self):
     mkdir_p(self.save_directory())
     open(self.metadata_filename, 'w').write(yaml.dump(self.metadata))
Example #28
0
    def timerEvent(self, event):
        #obtain most recent image
        frame_number = self.camera.get_frame_number()
        self.image = self.camera.get_image()

        self.roi_shape = np.shape(self.image)

        #show most recent image
        self.showImage()

        #show info about this image
        def set_imageinfo():
            maxval = self.image.max()
            height = len(self.image)
            width = np.size(self.image) / height
            portion = round(
                np.sum(self.image == maxval) / (1.0 * np.size(self.image)), 3)
            if self.divide_background.isChecked():
                is_autoscaled = ", Contrast Autoscaled"
            else:
                is_autoscaled = ""
            self.imageinfo.setText(
                'Camera Controller Version 0.0.1, Image Size: {}x{}, Max pixel value: {}, Fraction at max: {}, Frame number in buffer: {}{}'
                .format(width, height, maxval, portion, frame_number,
                        is_autoscaled))

        set_imageinfo()

        if self.timeseries_slow.isChecked():
            if (textbox_int(self.include_incrementing_image_num) *
                    textbox_float(self.interval) *
                    60) <= time.time() - self.slowseries_start:
                self.save_image()
            if textbox_int(self.include_incrementing_image_num) >= textbox_int(
                    self.numOfFrames2):
                self.timeseries_slow.setChecked(False)
                self.next_directory()

        #check if a time series to save has been finished collecting:
        if self.timeseries.isChecked():
            if self.camera.finished_live_sequence():
                time.sleep(0.1)
                set_imageinfo()
                self.freeze.toggle()
                mkdir_p(self.save_directory())

                write_timeseries(self.filename(),
                                 range(1, 1 + textbox_int(self.numOfFrames)),
                                 self.metadata, self)

                increment_textbox(self.include_incrementing_image_num)
                self.next_directory()
                self.timeseries.setChecked(False)
                self.livebutton.setChecked(True)
                self.live()

        #for back-saving the contents of the rolling buffer
        if self.save_buffer.isChecked():
            self.freeze.setChecked(True)
            self.live()  #this processes that the freeze button was checked
            time.sleep(0.1)
            set_imageinfo()
            mkdir_p(self.save_directory())

            lastframe = self.camera.get_frame_number()

            imagenums = []
            for i in range(lastframe + 2,
                           textbox_int(self.buffersize) + 1) + range(
                               1, lastframe + 2):
                #chonological
                imagenums.append(i)

            write_timeseries(self.filename(), imagenums, self.metadata, self)

            self.next_directory()
            self.save_buffer.setChecked(False)
            self.livebutton.setChecked(True)
            self.live()
Example #29
0
def train(args, trainloader,enc,dec,optimizer_en, optimizer_de,          
           criterion_ae, criterion_ce, Tensor, epoch, use_cuda):
    enc.train()
    dec.train()
    batch_time = AverageMeter()
    data_time = AverageMeter()
    losses = AverageMeter()
    isize=args.isize
    csize=args.cropsize
    batch = args.batch
    latent = args.latent
    exp = args.exp_name
    end=time.time()
    bat=0
    for batch_idx, (inputs, targets) in tqdm(enumerate(trainloader)):
        
        data_time.update(time.time()-end)
        if use_cuda:
            inputs, targets = inputs.cuda(), targets.cuda()
        inputs, targets = torch.autograd.Variable(inputs), torch.autograd.Variable(targets)
        



        
        u = np.random.uniform(-1,1, (batch, latent,1,1))
        l2 = torch.from_numpy(u).float().cuda()
        l2 = torch.tanh(l2)
        dec_l2 = dec(l2,args)
 
        n= torch.normal(0,0.2,size=(batch, 3, csize , csize)).cuda()
        inputs_noise = inputs + n
        l1 = enc(inputs_noise)

        ######  update ae 
        Xh = dec(l1,args)

        loss_mse = criterion_ae(Xh,inputs) #errR
        
        optimizer_en.zero_grad()
        optimizer_de.zero_grad()

        loss_ae_all = loss_mse
        loss_ae_all.backward()

        optimizer_en.step()
        optimizer_de.step()

        losses.update(loss_ae_all.item(), inputs.size(0))



       
        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()
        bat+=batch_idx



    if epoch % 5 ==0:
        recon=dec(enc(inputs),args)
        recon=recon.cpu().data
        recon=recon/2 +0.5
        inputs_noise=inputs_noise.cpu().data
        inputs_noise=inputs_noise/2+0.5
        inputs=inputs.cpu().data
        inputs=inputs/2+0.5
        if not os.path.exists('./result'):
            os.mkdir('./result')
        if not os.path.isdir(os.path.join("result",exp,"real")):
            utility.mkdir_p(os.path.join("result",exp,"real"))
        if not os.path.isdir(os.path.join("result",exp,"fake")):
            utility.mkdir_p(os.path.join("result",exp,"fake"))
        if not os.path.isdir(os.path.join("result",exp,"real_noise")):
            utility.mkdir_p(os.path.join("result",exp,"real_noise"))

        fake_name = 'fake'+exp+'_'+str(epoch)+'.png'
        real_name = 'real'+exp+'_'+str(epoch)+'.png'
        real_noise_name = 'real'+exp+'_'+str(epoch)+'.png'
        save_image(recon, os.path.join(".", "result",exp,"fake",fake_name))
        save_image(inputs, os.path.join(".", "result",exp,"real",real_name))
        save_image(inputs_noise, os.path.join(".", "result",exp,"real_noise",real_noise_name))

    return losses.avg
    def saveImageOrYaml(self, img, series = False, slowseries = False):
        '''
        For saving images and metadata to locations on hard disk. Autogenerates a suggested file name
        based on the user's input in the "Saving" tab. User may also type in a
        different name at this point. User will be asked to change name if the file
        already exists.

        Is not allowed to overwrite any files.
        '''

        # img is true if saving an image, false if saving a yaml file

        if img and series and self.numberincrement.isChecked(): #include number
            self.keynumber.setText(str(0).zfill(4))
            self.createFilename()

        #Write yaml file
        if img and self.saveMetaYes.checkState():
            self.saveImageOrYaml(False)

        savingcheck = QtGui.QInputDialog()

        #get correct filename for yamls
        if img:
            fname = self.filename
        else:
            tempname = self.filename
            filetype = tempname.split('.')[-1]
            n_filetype = filetype.__len__()
            fname = tempname[:-n_filetype] +'yaml'

        #grab image for saving
        selectedFrame = self.im

        usersfilename = fname

        if series == False:
            numOfFrames = 1
        else:
            #TODO: implement numOfFrames2
            numOfFrames = int(str(self.numOfFrames.text()))

        for i in range(1,numOfFrames+1):
            if series == True:
                #get each image
                selectedFrame = frameToArray(i)
                selectedFrame = toimage(selectedFrame) #PIL image
                usersfilename = self.filename

            #TODO: move overwriting check elsewhere
            '''#don't overwrite files
            if os.path.isfile(str(usersfilename)):
                directorywarning = QtGui.QMessageBox()
                directorywarning.setWindowTitle('Filename Error')
                directorywarning.setText('Overwriting data not allowed. Choose a different name or delete the item with the same name.')
                ret = directorywarning.exec_()
                self.saveImageOrYaml(img) #cycle back to try again

            else:'''
            #check if directory exists, and make it, save, or warn
            directory, filename = os.path.split(str(usersfilename))

            mkdir_p(directory)

            if os.path.isdir(directory):
                if img:#save image
                    if usersfilename[-4:]== '.tif':
                        selectedFrame.save(str(usersfilename), autoscale=False)
                    else:
                        selectedFrame.save(str(usersfilename), autoscale=False)
                else:#write yaml
                    data = self.createYaml()
                    with open(str(usersfilename), 'w') as outfile:
                        outfile.write( yaml.dump(data, default_flow_style=True) )

                #saving was successful, advance the incrementers if they are being used
                #numbered file, increment if checked and if saving an image
                if img and self.numberincrement.isChecked(): #include number
                    int(self.keynumber.text()) + 1
                    self.keynumber.setText(str((int(self.keynumber.text())+ 1)).zfill(4))
                self.createFilename()

                if not slowseries:
                    if series == False or i == numOfFrames:
                        #only increment the directory if done with this series
                        #numbered directory, increment if checked
                        if img and self.numberincrementdir.isChecked():
                            int(self.keynumberdir.text())+1
                            #automatically sets first image number back to zero
                            self.keynumber.setText(str(int(0)).zfill(4))
                            self.keynumberdir.setText(str((int(self.keynumberdir.text())+ 1)).zfill(2))
                        self.createFilename()
                if slowseries:
                    if self.lastimageflag == True:
                        self.livebutton.toggle()
                        #only increment the directory if done with the series
                        if img and self.numberincrementdir.isChecked():
                            int(self.keynumberdir.text())+1
                            #automatically sets first image number back to zero
                            self.keynumber.setText(str(int(0)).zfill(4))
                            self.keynumberdir.setText(str((int(self.keynumberdir.text())+ 1)).zfill(2))
                        self.createFilename()
            else:
                directorywarning = QtGui.QMessageBox()
                directorywarning.setWindowTitle('Filename Error')
                directorywarning.setText('The directory you chose does not exist. This software will only create the directory for you if the path exists all the way up to the final subdirectory. Create the necessary path structure on your computer or save to an already existing location. Image NOT saved.')
                ret = directorywarning.exec_()
                self.saveImageOrYaml(img)
        self.save.setChecked(False)
Example #31
0
def save_wave_elevation(w, etai, etap, eta, x, y, filename):
    """
    Save the wave elevation to a file in tec format
    Args:
        w: float, the wave frequency
        etai: 2D array, a wave elevation variable
        eta: 2D array, a wave elevation variable
        etap: 2D array, a wave elevation variable
        x: 1D array, a wave elevation variable
        y: 1D array, a wave elevation variable
        filename: string, the path to the file where to save
    """
    signature = __name__ + '.save_wave_elevation(w, etai, etap, eta, x, y, filename)'
    logger = logging.getLogger(__name__)
    utility.log_entrance(
        logger, signature, {
            "w": w,
            "etai": etai,
            "etap": etap,
            "eta": eta,
            "x": x,
            "y": y,
            "filename": filename
        })

    nx = len(x)
    ny = len(y)
    utility.mkdir_p(os.path.abspath(os.path.dirname(filename)))
    with open(filename, 'w') as inp:
        s = 'VARIABLES="X" "Y" "etaI_C" "etaI_S" "etaP_C" "etaC_S" '
        s += '"etaI_C+etaP_C" "etaI_S+etaI_P" "|etaP|" "|etaI+etaP|"\n'
        inp.write(s)

        s = 'ZONE t="Wave frequency - w = '
        s += str(w) + '",N=\t' + str(nx * ny) + ', E=\t' + str(
            (nx - 1) * (nx - 1)) + '\t , F=FEPOINT,ET=QUADRILATERAL\n'
        inp.write(s)
        for i in range(nx):
            for j in range(ny):
                s = str(x[i]) + '\t' + str(y[j]) + '\t' + str(
                    np.real(etai[i, j])) + '\t'
                s += str(np.imag(etai[i, j])) + '\t' + str(np.real(
                    etap[i, j])) + '\t'
                s += str(np.imag(etap[i, j])) + '\t' + str(
                    np.real(etai[i, j] + etap[i, j])) + '\t'
                s += str(np.imag(etai[i, j] + etap[i, j])) + '\t' + str(
                    np.abs(etap[i, j])) + '\t'
                s += str(np.abs(eta[i, j])) + '\n'
                inp.write(s)

        for i in range(nx - 1):
            for j in range(ny - 1):
                s = str(j + 1 + i * ny) + '\t' + str(
                    j + 1 + (i + 1) * ny) + '\t' + str(j + 2 + i * ny) + '\n'
                inp.write(s)

    utility.log_and_print(
        logger,
        utility.get_abs(filename) +
        ' contains the wave elevation in tec format.')
    utility.log_exit(logger, signature, [None])
Example #32
0
def train(args, trainloader, enc, dec, cl, disc_l, disc_v, l2_int,
          optimizer_en, optimizer_de, optimizer_c, optimizer_dl, optimizer_dv,
          optimizer_l2, criterion_ae, criterion_ce, Tensor, epoch, use_cuda):
    enc.train()
    dec.train()
    cl.train()
    disc_l.train()
    disc_v.train()
    batch_time = AverageMeter()
    data_time = AverageMeter()
    losses = AverageMeter()
    isize = args.isize
    csize = args.cropsize
    batch = args.batch
    latent = args.latent
    exp = args.exp_name
    end = time.time()
    bat = 0
    for batch_idx, (inputs, targets) in tqdm(enumerate(trainloader)):

        data_time.update(time.time() - end)
        if use_cuda:
            inputs, targets = inputs.cuda(), targets.cuda()
        inputs, targets = torch.autograd.Variable(
            inputs), torch.autograd.Variable(targets)

        #무작위 latent space 생성
        #l2 = 무작위 latent vector l1 = 노이즈 추가된 이미지의 latent vector

        u = np.random.uniform(-1, 1, (batch, latent, 1, 1))
        l2 = torch.from_numpy(u).float().cuda()
        l2 = torch.tanh(l2)
        dec_l2 = dec(l2, args)
        #가우시안 분포로 노이즈 만들기
        n = torch.normal(0, 0.2, size=(batch, 3, csize, csize)).cuda()
        inputs_noise = inputs + n
        #노이즈가 추가된 이미지 생성
        l1 = enc(inputs)

        #latent discriminator optimizer 초기화 후

        # classifier update
        #노이즈가 추가된 이미지 classifier
        logits_C_l1 = cl(dec(l1, args))
        #무작위로 생성된 이미지 classifier
        logits_C_l2 = cl(dec(l2, args))

        #classifier에 대한 label 생성 l1 = 1, l2 = 0
        valid_logits_C_l1 = Variable(Tensor(logits_C_l1.shape[0],
                                            1).fill_(1.0),
                                     requires_grad=False)
        fake_logits_C_l2 = Variable(Tensor(logits_C_l2.shape[0], 1).fill_(0.0),
                                    requires_grad=False)

        #바이너리 크로스 엔트로피 로스 계산 classifier
        loss_cl_l1 = criterion_ce(logits_C_l1, valid_logits_C_l1)
        loss_cl_l2 = criterion_ce(logits_C_l2, fake_logits_C_l2)

        loss_cl = loss_cl_l1 + loss_cl_l2
        #classifier optimizer 초기화 후 역전파, 업데이트

        optimizer_c.zero_grad()
        loss_cl.backward(retain_graph=True)
        optimizer_c.step()

        #Discriminator update
        #latent discriminator 통과
        logits_Dl_l1 = disc_l(l1)
        logits_Dl_l2 = disc_l(l2)

        # latent D 에 대한 label 생성 l1 = 0, l2 = 1
        dl_logits_DL_l1 = Variable(Tensor(logits_Dl_l1.shape[0], 1).fill_(0.0),
                                   requires_grad=False)
        dl_logits_DL_l2 = Variable(Tensor(logits_Dl_l2.shape[0], 1).fill_(1.0),
                                   requires_grad=False)

        # 정답지에 대한 결과
        loss_dl_1 = criterion_ce(logits_Dl_l1, dl_logits_DL_l1)
        loss_dl_2 = criterion_ce(logits_Dl_l2, dl_logits_DL_l2)
        loss_dl = loss_dl_1 + loss_dl_2

        #역전파 및 업데이트

        # visual Discriminator l1 = real l2 = random
        logits_Dv_X = disc_v(inputs)
        logits_Dv_l2 = disc_v(dec(l2, args))
        # visual Discriminator 에 대한 label
        dv_logits_Dv_X = Variable(Tensor(logits_Dv_X.shape[0], 1).fill_(1.0),
                                  requires_grad=False)
        dv_logits_Dv_l2 = Variable(Tensor(logits_Dv_l2.shape[0], 1).fill_(0.0),
                                   requires_grad=False)

        optimizer_dl.zero_grad()
        optimizer_dv.zero_grad()

        # loss값 계산
        loss_dv_1 = criterion_ce(logits_Dv_X, dv_logits_Dv_X)
        loss_dv_2 = criterion_ce(logits_Dv_l2, dv_logits_Dv_l2)
        loss_dv = loss_dv_1 + loss_dv_2

        #초기화, 역전파, 업데이트
        loss_second = loss_dv + loss_dl
        loss_second.backward(retain_graph=True)

        optimizer_dv.step()
        optimizer_dl.step()

        for i in range(5):
            logits_C_l2_mine = cl(dec(l2, args))
            zeros_logits_C_l2_mine = Variable(Tensor(logits_C_l2_mine.shape[0],
                                                     1).fill_(1.0),
                                              requires_grad=False)
            loss_C_l2_mine = criterion_ce(logits_C_l2_mine,
                                          zeros_logits_C_l2_mine)
            optimizer_l2.zero_grad()
            loss_C_l2_mine.backward()
            optimizer_l2.step()
            u = u - 2.0 * batch / l2.shape[0] * l2_int.numpy()
            l2 = torch.from_numpy(u).float().cuda()
            l2 = torch.tanh(l2)

        ######  update ae

        # 노이즈 추가된 재생성된 이미지
        Xh = dec(l1, args)
        # 위 값 vs 입력된 이미지 reconstruction error계산
        loss_mse = criterion_ae(Xh, inputs)  #errR

        #l1에 대해 label생성 및 latent loss계산

        logits_Dl_l1 = disc_l(l1)
        #logits_Dl_l2=disc_l(l2)
        ones_logits_Dl_l1 = Variable(Tensor(logits_Dl_l1.shape[0],
                                            1).fill_(1.0),
                                     requires_grad=False)
        # zeros_logits_Dl_l2 = Variable(Tensor(logits_Dl_l2.shape[0], 1).fill_(0.0), requires_grad=False)
        loss_AE_l = criterion_ce(logits_Dl_l1, ones_logits_Dl_l1)

        logits_Dv_l1_mine = disc_v(dec(l2, args))
        # logits_Dv_l2_mine = disc_v(inputs)
        ones_logits_Dv_l1_mine = Variable(Tensor(logits_Dv_l1_mine.shape[0],
                                                 1).fill_(1.0),
                                          requires_grad=False)
        # zeros_logits_Dv_l2_mine = Variable(Tensor(logits_Dv_l2_mine.shape[0], 1).fill_(0.0), requires_grad=False)
        loss_ae_v = criterion_ce(logits_Dv_l1_mine, ones_logits_Dv_l1_mine)

        optimizer_en.zero_grad()
        optimizer_de.zero_grad()

        loss_ae_all = 10.0 * loss_mse + loss_ae_v + loss_AE_l

        loss_ae_all.backward()

        optimizer_en.step()
        optimizer_de.step()

        losses.update(loss_ae_all.item(), inputs.size(0))

        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()
        bat += batch_idx

    #그림 저장
    if epoch % 5 == 0:
        recon = dec(enc(inputs), args)
        recon = recon.cpu().data
        recon = recon / 2 + 0.5
        inputs_noise = inputs_noise.cpu().data
        inputs_noise = inputs_noise / 2 + 0.5
        inputs = inputs.cpu().data
        inputs = inputs / 2 + 0.5
        if not os.path.exists('./result'):
            os.mkdir('./result')
        if not os.path.isdir(os.path.join("result", exp, "real")):
            utility.mkdir_p(os.path.join("result", exp, "real"))
        if not os.path.isdir(os.path.join("result", exp, "fake")):
            utility.mkdir_p(os.path.join("result", exp, "fake"))
        if not os.path.isdir(os.path.join("result", exp, "real_noise")):
            utility.mkdir_p(os.path.join("result", exp, "real_noise"))

        fake_name = 'fake' + exp + '_' + str(epoch) + '.png'
        real_name = 'real' + exp + '_' + str(epoch) + '.png'
        real_noise_name = 'real' + exp + '_' + str(epoch) + '.png'
        save_image(recon, os.path.join(".", "result", exp, "fake", fake_name))
        save_image(inputs, os.path.join(".", "result", exp, "real", real_name))
        save_image(
            inputs_noise,
            os.path.join(".", "result", exp, "real_noise", real_noise_name))

    return losses.avg